* Copyright (c) 1990 The Regents of the University of California.
* This code is derived from software contributed to Berkeley by
* %sccs.include.redist.c%
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid
[] = "@(#)hash_bigkey.c 5.2 (Berkeley) %G%";
#endif /* LIBC_SCCS and not lint */
/******************************************************************************
Big key/data handling for the hashing package.
******************************************************************************/
extern BUFHEAD
*__get_buf();
extern BUFHEAD
*__add_ovflpage();
extern int __big_keydata();
extern int __big_split();
extern int __big_insert();
extern int __big_return();
extern int __big_delete();
extern u_short
__find_last_page();
extern int __find_bigpair();
static int collect_key();
static int collect_data();
extern long hash_accesses
, hash_collisions
, hash_expansions
, hash_overflows
;
You need to do an insert and the key/data pair is too big
__big_insert ( bufp
, key
, val
)
char *cp
= bufp
->page
; /* Character pointer of p */
register u_short
*p
= (u_short
*)cp
;
char *key_data
, *val_data
;
u_short space
, move_bytes
, off
;
for ( space
= FREESPACE(p
) - BIGOVERHEAD
;
space
= FREESPACE(p
) - BIGOVERHEAD
) {
move_bytes
= MIN(space
, key_size
);
off
= OFFSET(p
) - move_bytes
;
bcopy (key_data
, cp
+off
, move_bytes
);
FREESPACE(p
) = off
- PAGE_META(n
);
bufp
= __add_ovflpage(bufp
);
move_bytes
= MIN (FREESPACE(p
), val_size
);
off
= OFFSET(p
) - move_bytes
;
bcopy ( val_data
, cp
+ off
, move_bytes
);
FREESPACE(p
) = FREESPACE(p
) - move_bytes
;
p
= (u_short
*)bufp
->page
;
for ( space
= FREESPACE(p
) - BIGOVERHEAD
;
space
= FREESPACE(p
) - BIGOVERHEAD
) {
move_bytes
= MIN(space
, val_size
);
Here's the hack to make sure that if the data ends
on the same page as the key ends, FREESPACE is
if ( space
== val_size
&& val_size
== val
->size
) {
off
= OFFSET(p
) - move_bytes
;
bcopy (val_data
, cp
+off
, move_bytes
);
FREESPACE(p
) = off
- PAGE_META(n
);
bufp
= __add_ovflpage (bufp
);
Called when bufp's page contains a partial key (index should be 1)
All pages in the big key/data pair except bufp are freed. We cannot
free bufp because the page pointing to it is lost and we can't
register BUFHEAD
*rbufp
= bufp
;
register BUFHEAD
*last_bfp
= NULL
;
u_short
*bp
= (u_short
*)bufp
->page
;
while (!key_done
|| (bp
[2] != FULL_KEY_DATA
)) {
if ( bp
[2] == FULL_KEY
|| bp
[2] == FULL_KEY_DATA
) key_done
= 1;
If there is freespace left on a FULL_KEY_DATA page,
then the data is short and fits entirely on this
page, and this is the last page.
if ( bp
[2] == FULL_KEY_DATA
&& FREESPACE(bp
) ) break;
rbufp
= __get_buf ( pageno
, rbufp
, 0 );
if ( last_bfp
) __free_ovflpage(last_bfp
);
if ( !rbufp
) return(-1); /* Error */
bp
= (u_short
*)rbufp
->page
;
If we get here then rbufp points to the last page of
the big key/data pair. Bufp points to the first
one -- it should now be empty pointing to the next
page after this pair. Can't free it because we don't
have the page pointing to it.
/* This is information from the last page of the pair */
/* Now, bp is the first page of the pair */
bp
= (u_short
*)bufp
->page
;
/* There is an overflow page */
bufp
->ovfl
= rbufp
->ovfl
;
/* This is the last page */
FREESPACE(bp
) = hashp
->BSIZE
- PAGE_META(n
);
OFFSET(bp
) = hashp
->BSIZE
- 1;
if ( rbufp
) __free_ovflpage(rbufp
);
if ( last_bfp
!= rbufp
) __free_ovflpage(last_bfp
);
-1 = get next overflow page
-2 means key not found and this is big key/data
__find_bigpair(bufp
, ndx
, key
, size
)
register u_short
*bp
= (u_short
*)bufp
->page
;
register char *p
= bufp
->page
;
for ( bytes
= hashp
->BSIZE
- bp
[ndx
];
bytes
<= size
&& bp
[ndx
+1] == PARTIAL_KEY
;
bytes
= hashp
->BSIZE
- bp
[ndx
] ) {
if ( bcmp ( p
+bp
[ndx
], kkey
, bytes
))return(-2);
bufp
= __get_buf ( bp
[ndx
+2], bufp
, 0 );
if ( (bytes
!= ksize
) || bcmp ( p
+bp
[ndx
], kkey
, bytes
)) {
Given the buffer pointer of the first overflow page of a big pair,
find the end of the big pair
This will set bpp to the buffer header of the last page of the big pair.
It will return the pageno of the overflow page following the last page of
the pair; 0 if there isn't any (i.e. big pair is the last key in the
u_short
*bp
= (u_short
*)bufp
->page
;
This is the last page if:
the tag is FULL_KEY_DATA and either
OVFLPAGE marker is explicit
there is freespace on the page
if ( bp
[2] == FULL_KEY_DATA
&&
((n
== 2) || (bp
[n
] == OVFLPAGE
) || (FREESPACE(bp
)) ) ) break;
bufp
= __get_buf ( pageno
, bufp
, 0 );
if ( !bufp
) return (0); /* Need to indicate an error! */
bp
= (u_short
*)bufp
->page
;
if ( bp
[0] > 2 ) return ( bp
[3] );
Return the data for the key/data pair
that begins on this page at this index
(index should always be 1)
__big_return ( bufp
, ndx
, val
, set_current
)
u_short
*bp
= (u_short
*)bufp
->page
;
while ( bp
[ndx
+1] == PARTIAL_KEY
) {
bufp
= __get_buf ( bp
[bp
[0]-1], bufp
, 0 );
bp
= (u_short
*)bufp
->page
;
if ( bp
[ndx
+1] == FULL_KEY
) {
bufp
= __get_buf ( bp
[bp
[0]-1], bufp
, 0 );
bp
= (u_short
*)bufp
->page
;
save_addr
= save_p
->addr
;
} else if (!FREESPACE(bp
)) {
This is a hack. We can't distinguish between
FULL_KEY_DATA that contains complete data or
incomplete data, so we require that if the
data is complete, there is at least 1 byte
bufp
= __get_buf ( bp
[bp
[0]-1], bufp
, 0 );
bp
= (u_short
*)bufp
->page
;
/* The data is all on one page */
if ( bp
[0] == 2 ) { /* No more buckets in chain */
hashp
->cpage
= __get_buf ( bp
[bp
[0]-1], bufp
, 0 );
if ( !hashp
->cpage
)return(-1);
if ( !((u_short
*)hashp
->cpage
->page
)[0] ) {
val
->size
= collect_data ( bufp
, len
, set_current
);
if ( save_p
->addr
!= save_addr
) {
/* We are pretty short on buffers */
errno
= EINVAL
; /* OUT OF BUFFERS */
bcopy ( (save_p
->page
)+off
, hashp
->tmp_buf
, len
);
val
->data
= hashp
->tmp_buf
;
Count how big the total datasize is by
recursing through the pages. Then allocate
a buffer and copy the data as you recurse up.
collect_data ( bufp
, len
, set
)
register char *p
= bufp
->page
;
register u_short
*bp
= (u_short
*)p
;
mylen
= hashp
->BSIZE
- bp
[1];
if ( bp
[2] == FULL_KEY_DATA
) { /* End of Data */
if ( hashp
->tmp_buf
) free (hashp
->tmp_buf
);
hashp
->tmp_buf
= (char *)malloc ( totlen
);
if ( bp
[0] == 2 ) { /* No more buckets in chain */
hashp
->cpage
= __get_buf ( bp
[bp
[0]-1], bufp
, 0 );
} else if ( !((u_short
*)hashp
->cpage
->page
)[0] ) {
xbp
= __get_buf ( bp
[bp
[0]-1], bufp
, 0 );
if ( !xbp
|| ((totlen
= collect_data ( xbp
, len
+ mylen
, set
)) < 1) ) {
if ( bufp
->addr
!= save_addr
) {
errno
= EINVAL
; /* Out of buffers */
bcopy ( (bufp
->page
) + bp
[1], &hashp
->tmp_buf
[len
], mylen
);
__big_keydata ( bufp
, ndx
, key
, val
, set
)
key
->size
= collect_key ( bufp
, 0, val
, set
);
key
->data
= hashp
->tmp_key
;
Count how big the total key size is by
recursing through the pages. Then collect
the data, allocate a buffer and copy the key as
collect_key ( bufp
, len
, val
, set
)
u_short
*bp
= (u_short
*)p
;
mylen
= hashp
->BSIZE
- bp
[1];
if ( bp
[2] == FULL_KEY
|| bp
[2] == FULL_KEY_DATA
) {/* End of Key */
if ( hashp
->tmp_key
) free (hashp
->tmp_key
);
hashp
->tmp_key
= (char *)malloc ( totlen
);
__big_return ( bufp
, 1, val
, set
);
xbp
= __get_buf (bp
[bp
[0]-1], bufp
, 0);
if ( !xbp
|| ((totlen
= collect_key (xbp
, totlen
, val
, set
)) < 1 ) ) {
if ( bufp
->addr
!= save_addr
) {
errno
= EINVAL
; /* MIS -- OUT OF BUFFERS */
bcopy ( (bufp
->page
) + bp
[1], &hashp
->tmp_key
[len
], mylen
);
__big_split ( op
, np
, big_keyp
, addr
, obucket
, ret
)
BUFHEAD
*op
; /* Pointer to where to put keys that go in old bucket */
BUFHEAD
*np
; /* Pointer to new bucket page */
BUFHEAD
*big_keyp
; /* Pointer to first page containing the big key/data */
u_short addr
; /* Address of big_keyp */
int obucket
; /* Old Bucket */
register u_short
*prev_pagep
;
/* Now figure out where the big key/data goes */
if (__big_keydata ( big_keyp
, 1, &key
, &val
, 0 )) {
change
= (__call_hash ( key
.data
, key
.size
) != obucket
);
if ( ret
->next_addr
= __find_last_page ( &big_keyp
) ) {
if (!(ret
->nextp
= __get_buf ( ret
->next_addr
, big_keyp
, 0 ))) {
/* Now make one of np/op point to the big key/data pair */
assert(np
->ovfl
== NULL
);
fprintf ( stderr
, "BIG_SPLIT: %d->ovfl was %d is now %d\n", tmpp
->addr
,
(tmpp
->ovfl
?tmpp
->ovfl
->addr
:0),
tmpp
->ovfl
= bp
; /* one of op/np point to big_keyp */
tp
= (u_short
*)tmpp
->page
;
assert ( FREESPACE(tp
) >= OVFLSIZE
);
free_space
= FREESPACE(tp
);
FREESPACE(tp
) = free_space
- OVFLSIZE
;
Finally, set the new and old return values.
BIG_KEYP contains a pointer to the last page of the big key_data pair.
Make sure that big_keyp has no following page (2 elements) or create
tp
= (u_short
*)big_keyp
->page
;
big_keyp
->flags
|= BUF_MOD
;
There may be either one or two offsets on this page
If there is one, then the overflow page is linked on
normally and tp[4] is OVFLPAGE. If there are two, tp[4]
contains the second offset and needs to get stuffed in
after the next overflow page is added
free_space
= FREESPACE(tp
);
FREESPACE(tp
) = free_space
+ OVFLSIZE
;
tmpp
= __add_ovflpage ( big_keyp
);
if ( change
) ret
->newp
= tmpp
;