* Copyright (c) 1990, 1993, 1994
* The Regents of the University of California. All rights reserved.
* This code is derived from software contributed to Berkeley by
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid
[] = "@(#)hash.c 8.9 (Berkeley) 6/16/94";
#endif /* LIBC_SCCS and not lint */
static int alloc_segs
__P((HTAB
*, int));
static int flush_meta
__P((HTAB
*));
static int hash_access
__P((HTAB
*, ACTION
, DBT
*, DBT
*));
static int hash_close
__P((DB
*));
static int hash_delete
__P((const DB
*, const DBT
*, u_int32_t
));
static int hash_fd
__P((const DB
*));
static int hash_get
__P((const DB
*, const DBT
*, DBT
*, u_int32_t
));
static int hash_put
__P((const DB
*, DBT
*, const DBT
*, u_int32_t
));
static void *hash_realloc
__P((SEGMENT
**, int, int));
static int hash_seq
__P((const DB
*, DBT
*, DBT
*, u_int32_t
));
static int hash_sync
__P((const DB
*, u_int32_t
));
static int hdestroy
__P((HTAB
*));
static HTAB
*init_hash
__P((HTAB
*, const char *, HASHINFO
*));
static int init_htab
__P((HTAB
*, int));
#if BYTE_ORDER == LITTLE_ENDIAN
static void swap_header
__P((HTAB
*));
static void swap_header_copy
__P((HASHHDR
*, HASHHDR
*));
/* Fast arithmetic, relying on powers of 2, */
#define MOD(x, y) ((x) & ((y) - 1))
#define RETURN_ERROR(ERR, LOC) { save_errno = ERR; goto LOC; }
int hash_accesses
, hash_collisions
, hash_expansions
, hash_overflows
;
/************************** INTERFACE ROUTINES ***************************/
__hash_open(file
, flags
, mode
, info
, dflags
)
const HASHINFO
*info
; /* Special directives for create */
int bpages
, hdrsize
, new_table
, nsegs
, save_errno
;
if ((flags
& O_ACCMODE
) == O_WRONLY
) {
if (!(hashp
= (HTAB
*)calloc(1, sizeof(HTAB
))))
* Even if user wants write only, we need to be able to read
* the actual file, so we need to open it read/write. But, the
* field in the hashp structure needs to be accurate so that
if (!file
|| (flags
& O_TRUNC
) ||
(stat(file
, &statbuf
) && (errno
== ENOENT
))) {
errno
= 0; /* Just in case someone looks at errno */
if ((hashp
->fp
= open(file
, flags
, mode
)) == -1)
RETURN_ERROR(errno
, error0
);
(void)fcntl(hashp
->fp
, F_SETFD
, 1);
if (!(hashp
= init_hash(hashp
, file
, (HASHINFO
*)info
)))
RETURN_ERROR(errno
, error1
);
/* Table already exists */
hashp
->hash
= info
->hash
;
hashp
->hash
= __default_hash
;
hdrsize
= read(hashp
->fp
, &hashp
->hdr
, sizeof(HASHHDR
));
#if BYTE_ORDER == LITTLE_ENDIAN
RETURN_ERROR(errno
, error1
);
if (hdrsize
!= sizeof(HASHHDR
))
RETURN_ERROR(EFTYPE
, error1
);
/* Verify file type, versions and hash function */
if (hashp
->MAGIC
!= HASHMAGIC
)
RETURN_ERROR(EFTYPE
, error1
);
if (hashp
->VERSION
!= HASHVERSION
&&
hashp
->VERSION
!= OLDHASHVERSION
)
RETURN_ERROR(EFTYPE
, error1
);
if (hashp
->hash(CHARKEY
, sizeof(CHARKEY
)) != hashp
->H_CHARKEY
)
RETURN_ERROR(EFTYPE
, error1
);
* Figure out how many segments we need. Max_Bucket is the
* maximum bucket number, so the number of buckets is
nsegs
= (hashp
->MAX_BUCKET
+ 1 + hashp
->SGSIZE
- 1) /
if (alloc_segs(hashp
, nsegs
))
* If alloc_segs fails, table will have been destroyed
* and errno will have been set.
bpages
= (hashp
->SPARES
[hashp
->OVFL_POINT
] +
(hashp
->BSIZE
<< BYTE_SHIFT
) - 1) >>
(hashp
->BSHIFT
+ BYTE_SHIFT
);
(void)memset(&hashp
->mapp
[0], 0, bpages
* sizeof(u_int32_t
*));
/* Initialize Buffer Manager */
if (info
&& info
->cachesize
)
__buf_init(hashp
, info
->cachesize
);
__buf_init(hashp
, DEF_BUFSIZE
);
hashp
->new_file
= new_table
;
hashp
->save_file
= file
&& (hashp
->flags
& O_RDWR
);
if (!(dbp
= (DB
*)malloc(sizeof(DB
)))) {
"%s\n%s%x\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%x\n%s%x\n%s%d\n%s%d\n",
"BUCKET SIZE ", hashp
->BSIZE
,
"BUCKET SHIFT ", hashp
->BSHIFT
,
"DIRECTORY SIZE ", hashp
->DSIZE
,
"SEGMENT SIZE ", hashp
->SGSIZE
,
"SEGMENT SHIFT ", hashp
->SSHIFT
,
"FILL FACTOR ", hashp
->FFACTOR
,
"MAX BUCKET ", hashp
->MAX_BUCKET
,
"OVFL POINT ", hashp
->OVFL_POINT
,
"LAST FREED ", hashp
->LAST_FREED
,
"HIGH MASK ", hashp
->HIGH_MASK
,
"LOW MASK ", hashp
->LOW_MASK
,
hash_overflows
= hash_accesses
= hash_collisions
= hash_expansions
= 0;
hashp
= (HTAB
*)dbp
->internal
;
retval
= hdestroy(hashp
);
hashp
= (HTAB
*)dbp
->internal
;
/************************** LOCAL CREATION ROUTINES **********************/
init_hash(hashp
, file
, info
)
hashp
->LORDER
= BYTE_ORDER
;
hashp
->BSIZE
= DEF_BUCKET_SIZE
;
hashp
->BSHIFT
= DEF_BUCKET_SHIFT
;
hashp
->SGSIZE
= DEF_SEGSIZE
;
hashp
->SSHIFT
= DEF_SEGSIZE_SHIFT
;
hashp
->DSIZE
= DEF_DIRSIZE
;
hashp
->FFACTOR
= DEF_FFACTOR
;
hashp
->hash
= __default_hash
;
memset(hashp
->SPARES
, 0, sizeof(hashp
->SPARES
));
memset(hashp
->BITMAPS
, 0, sizeof (hashp
->BITMAPS
));
/* Fix bucket size to be optimal for file system */
if (stat(file
, &statbuf
))
#ifdef STBLKSIZE_NOT_AVAILABLE
hashp
->BSIZE
= statbuf
.st_blksize
;
hashp
->BSHIFT
= __log2(hashp
->BSIZE
);
/* Round pagesize up to power of 2 */
hashp
->BSHIFT
= __log2(info
->bsize
);
hashp
->BSIZE
= 1 << hashp
->BSHIFT
;
if (hashp
->BSIZE
> MAX_BSIZE
) {
hashp
->FFACTOR
= info
->ffactor
;
hashp
->hash
= info
->hash
;
if (info
->lorder
!= BIG_ENDIAN
&&
info
->lorder
!= LITTLE_ENDIAN
) {
hashp
->LORDER
= info
->lorder
;
/* init_htab should destroy the table and set errno if it fails */
if (init_htab(hashp
, nelem
))
* This calls alloc_segs which may run out of memory. Alloc_segs will destroy
* the table and set errno, so we just pass the error information along.
register int nbuckets
, nsegs
;
* Divide number of elements by the fill factor and determine a
* desired number of buckets. Allocate space for the next greater
* power of two number of buckets.
nelem
= (nelem
- 1) / hashp
->FFACTOR
+ 1;
l2
= __log2(MAX(nelem
, 2));
hashp
->SPARES
[l2
] = l2
+ 1;
hashp
->SPARES
[l2
+ 1] = l2
+ 1;
/* First bitmap page is at: splitpoint l2 page offset 1 */
if (__ibitmap(hashp
, OADDR_OF(l2
, 1), l2
+ 1, 0))
hashp
->MAX_BUCKET
= hashp
->LOW_MASK
= nbuckets
- 1;
hashp
->HIGH_MASK
= (nbuckets
<< 1) - 1;
hashp
->HDRPAGES
= ((MAX(sizeof(HASHHDR
), MINHDRSIZE
) - 1) >>
nsegs
= (nbuckets
- 1) / hashp
->SGSIZE
+ 1;
nsegs
= 1 << __log2(nsegs
);
if (nsegs
> hashp
->DSIZE
)
return (alloc_segs(hashp
, nsegs
));
/********************** DESTROY/CLOSE ROUTINES ************************/
* Flushes any changes to the file if necessary and destroys the hashp
* structure, freeing all allocated space.
(void)fprintf(stderr
, "hdestroy: accesses %ld collisions %ld\n",
hash_accesses
, hash_collisions
);
(void)fprintf(stderr
, "hdestroy: expansions %ld\n",
(void)fprintf(stderr
, "hdestroy: overflows %ld\n",
(void)fprintf(stderr
, "keys %ld maxp %d segmentcount %d\n",
hashp
->NKEYS
, hashp
->MAX_BUCKET
, hashp
->nsegs
);
for (i
= 0; i
< NCACHED
; i
++)
"spares[%d] = %d\n", i
, hashp
->SPARES
[i
]);
* Call on buffer manager to free buffers, and if required,
if (__buf_free(hashp
, 1, hashp
->save_file
))
free(*hashp
->dir
); /* Free initial segments */
/* Free extra segments */
free(hashp
->dir
[--hashp
->nsegs
]);
if (flush_meta(hashp
) && !save_errno
)
for (i
= 0; i
< hashp
->nmaps
; i
++)
* Write modified pages to disk
hashp
= (HTAB
*)dbp
->internal
;
if (__buf_free(hashp
, 0, 1) || flush_meta(hashp
))
* -1 indicates that errno should be set
#if BYTE_ORDER == LITTLE_ENDIAN
hashp
->MAGIC
= HASHMAGIC
;
hashp
->VERSION
= HASHVERSION
;
hashp
->H_CHARKEY
= hashp
->hash(CHARKEY
, sizeof(CHARKEY
));
#if BYTE_ORDER == LITTLE_ENDIAN
swap_header_copy(&hashp
->hdr
, whdrp
);
if ((lseek(fp
, (off_t
)0, SEEK_SET
) == -1) ||
((wsize
= write(fp
, whdrp
, sizeof(HASHHDR
))) == -1))
if (wsize
!= sizeof(HASHHDR
)) {
for (i
= 0; i
< NCACHED
; i
++)
if (__put_page(hashp
, (char *)hashp
->mapp
[i
],
hashp
->BITMAPS
[i
], 0, 1))
/*******************************SEARCH ROUTINES *****************************/
* All the access routines return
* 1 to indicate an external ERROR (i.e. key not found, etc)
* -1 to indicate an internal ERROR (i.e. out of memory, etc)
hash_get(dbp
, key
, data
, flag
)
hashp
= (HTAB
*)dbp
->internal
;
hashp
->errno
= errno
= EINVAL
;
return (hash_access(hashp
, HASH_GET
, (DBT
*)key
, data
));
hash_put(dbp
, key
, data
, flag
)
hashp
= (HTAB
*)dbp
->internal
;
if (flag
&& flag
!= R_NOOVERWRITE
) {
hashp
->errno
= errno
= EINVAL
;
if ((hashp
->flags
& O_ACCMODE
) == O_RDONLY
) {
hashp
->errno
= errno
= EPERM
;
return (hash_access(hashp
, flag
== R_NOOVERWRITE
?
HASH_PUTNEW
: HASH_PUT
, (DBT
*)key
, (DBT
*)data
));
hash_delete(dbp
, key
, flag
)
u_int32_t flag
; /* Ignored */
hashp
= (HTAB
*)dbp
->internal
;
if (flag
&& flag
!= R_CURSOR
) {
hashp
->errno
= errno
= EINVAL
;
if ((hashp
->flags
& O_ACCMODE
) == O_RDONLY
) {
hashp
->errno
= errno
= EPERM
;
return (hash_access(hashp
, HASH_DELETE
, (DBT
*)key
, NULL
));
* Assume that hashp has been set in wrapper routine.
hash_access(hashp
, action
, key
, val
)
BUFHEAD
*bufp
, *save_bufp
;
register int n
, ndx
, off
, size
;
rbufp
= __get_buf(hashp
, __call_hash(hashp
, kp
, size
), NULL
, 0);
/* Pin the bucket chain */
for (bp
= (u_int16_t
*)rbufp
->page
, n
= *bp
++, ndx
= 1; ndx
< n
;)
memcmp(kp
, rbufp
->page
+ *bp
, size
) == 0)
} else if (bp
[1] == OVFLPAGE
) {
rbufp
= __get_buf(hashp
, *bp
, rbufp
, 0);
save_bufp
->flags
&= ~BUF_PIN
;
bp
= (u_int16_t
*)rbufp
->page
;
} else if (bp
[1] < REAL_KEY
) {
__find_bigpair(hashp
, rbufp
, ndx
, kp
, size
)) > 0)
__find_last_page(hashp
, &bufp
))) {
rbufp
= __get_buf(hashp
, pageno
, bufp
, 0);
save_bufp
->flags
&= ~BUF_PIN
;
bp
= (u_int16_t
*)rbufp
->page
;
save_bufp
->flags
&= ~BUF_PIN
;
if (__addel(hashp
, rbufp
, key
, val
)) {
save_bufp
->flags
&= ~BUF_PIN
;
save_bufp
->flags
&= ~BUF_PIN
;
save_bufp
->flags
&= ~BUF_PIN
;
save_bufp
->flags
&= ~BUF_PIN
;
bp
= (u_int16_t
*)rbufp
->page
;
if (bp
[ndx
+ 1] < REAL_KEY
) {
if (__big_return(hashp
, rbufp
, ndx
, val
, 0))
val
->data
= (u_char
*)rbufp
->page
+ (int)bp
[ndx
+ 1];
val
->size
= bp
[ndx
] - bp
[ndx
+ 1];
if ((__delpair(hashp
, rbufp
, ndx
)) ||
(__addel(hashp
, rbufp
, key
, val
))) {
save_bufp
->flags
&= ~BUF_PIN
;
if (__delpair(hashp
, rbufp
, ndx
))
save_bufp
->flags
&= ~BUF_PIN
;
hash_seq(dbp
, key
, data
, flag
)
register u_int32_t bucket
;
hashp
= (HTAB
*)dbp
->internal
;
if (flag
&& flag
!= R_FIRST
&& flag
!= R_NEXT
) {
hashp
->errno
= errno
= EINVAL
;
if ((hashp
->cbucket
< 0) || (flag
== R_FIRST
)) {
for (bp
= NULL
; !bp
|| !bp
[0]; ) {
if (!(bufp
= hashp
->cpage
)) {
for (bucket
= hashp
->cbucket
;
bucket
<= hashp
->MAX_BUCKET
;
bucket
++, hashp
->cndx
= 1) {
bufp
= __get_buf(hashp
, bucket
, NULL
, 0);
bp
= (u_int16_t
*)bufp
->page
;
if (hashp
->cbucket
> hashp
->MAX_BUCKET
) {
bp
= (u_int16_t
*)hashp
->cpage
->page
;
while (bp
[hashp
->cndx
+ 1] == OVFLPAGE
) {
__get_buf(hashp
, bp
[hashp
->cndx
], bufp
, 0);
bp
= (u_int16_t
*)(bufp
->page
);
if (bp
[ndx
+ 1] < REAL_KEY
) {
if (__big_keydata(hashp
, bufp
, key
, data
, 1))
key
->data
= (u_char
*)hashp
->cpage
->page
+ bp
[ndx
];
key
->size
= (ndx
> 1 ? bp
[ndx
- 1] : hashp
->BSIZE
) - bp
[ndx
];
data
->data
= (u_char
*)hashp
->cpage
->page
+ bp
[ndx
+ 1];
data
->size
= bp
[ndx
] - bp
[ndx
+ 1];
/********************************* UTILITIES ************************/
u_int32_t old_bucket
, new_bucket
;
int dirsize
, new_segnum
, spare_ndx
;
new_bucket
= ++hashp
->MAX_BUCKET
;
old_bucket
= (hashp
->MAX_BUCKET
& hashp
->LOW_MASK
);
new_segnum
= new_bucket
>> hashp
->SSHIFT
;
/* Check if we need a new segment */
if (new_segnum
>= hashp
->nsegs
) {
/* Check if we need to expand directory */
if (new_segnum
>= hashp
->DSIZE
) {
/* Reallocate directory */
dirsize
= hashp
->DSIZE
* sizeof(SEGMENT
*);
if (!hash_realloc(&hashp
->dir
, dirsize
, dirsize
<< 1))
hashp
->DSIZE
= dirsize
<< 1;
if ((hashp
->dir
[new_segnum
] =
(SEGMENT
)calloc(hashp
->SGSIZE
, sizeof(SEGMENT
))) == NULL
)
* If the split point is increasing (MAX_BUCKET's log base 2
* * increases), we need to copy the current contents of the spare
* split bucket to the next bucket.
spare_ndx
= __log2(hashp
->MAX_BUCKET
+ 1);
if (spare_ndx
> hashp
->OVFL_POINT
) {
hashp
->SPARES
[spare_ndx
] = hashp
->SPARES
[hashp
->OVFL_POINT
];
hashp
->OVFL_POINT
= spare_ndx
;
if (new_bucket
> hashp
->HIGH_MASK
) {
/* Starting a new doubling */
hashp
->LOW_MASK
= hashp
->HIGH_MASK
;
hashp
->HIGH_MASK
= new_bucket
| hashp
->LOW_MASK
;
/* Relocate records to the new bucket */
return (__split_page(hashp
, old_bucket
, new_bucket
));
* If realloc guarantees that the pointer is not destroyed if the realloc
* fails, then this routine can go away.
hash_realloc(p_ptr
, oldsize
, newsize
)
if (p
= malloc(newsize
)) {
memmove(p
, *p_ptr
, oldsize
);
memset((char *)p
+ oldsize
, 0, newsize
- oldsize
);
__call_hash(hashp
, k
, len
)
bucket
= n
& hashp
->HIGH_MASK
;
if (bucket
> hashp
->MAX_BUCKET
)
bucket
= bucket
& hashp
->LOW_MASK
;
* Allocate segment table. On error, destroy the table and set errno.
(SEGMENT
*)calloc(hashp
->DSIZE
, sizeof(SEGMENT
*))) == NULL
) {
(SEGMENT
)calloc(nsegs
<< hashp
->SSHIFT
, sizeof(SEGMENT
))) == NULL
) {
for (i
= 0; i
< nsegs
; i
++, hashp
->nsegs
++)
hashp
->dir
[i
] = &store
[i
<< hashp
->SSHIFT
];
#if BYTE_ORDER == LITTLE_ENDIAN
* Hashp->hdr needs to be byteswapped.
swap_header_copy(srcp
, destp
)
P_32_COPY(srcp
->magic
, destp
->magic
);
P_32_COPY(srcp
->version
, destp
->version
);
P_32_COPY(srcp
->lorder
, destp
->lorder
);
P_32_COPY(srcp
->bsize
, destp
->bsize
);
P_32_COPY(srcp
->bshift
, destp
->bshift
);
P_32_COPY(srcp
->dsize
, destp
->dsize
);
P_32_COPY(srcp
->ssize
, destp
->ssize
);
P_32_COPY(srcp
->sshift
, destp
->sshift
);
P_32_COPY(srcp
->ovfl_point
, destp
->ovfl_point
);
P_32_COPY(srcp
->last_freed
, destp
->last_freed
);
P_32_COPY(srcp
->max_bucket
, destp
->max_bucket
);
P_32_COPY(srcp
->high_mask
, destp
->high_mask
);
P_32_COPY(srcp
->low_mask
, destp
->low_mask
);
P_32_COPY(srcp
->ffactor
, destp
->ffactor
);
P_32_COPY(srcp
->nkeys
, destp
->nkeys
);
P_32_COPY(srcp
->hdrpages
, destp
->hdrpages
);
P_32_COPY(srcp
->h_charkey
, destp
->h_charkey
);
for (i
= 0; i
< NCACHED
; i
++) {
P_32_COPY(srcp
->spares
[i
], destp
->spares
[i
]);
P_16_COPY(srcp
->bitmaps
[i
], destp
->bitmaps
[i
]);
M_32_SWAP(hdrp
->version
);
M_32_SWAP(hdrp
->ovfl_point
);
M_32_SWAP(hdrp
->last_freed
);
M_32_SWAP(hdrp
->max_bucket
);
M_32_SWAP(hdrp
->high_mask
);
M_32_SWAP(hdrp
->low_mask
);
M_32_SWAP(hdrp
->ffactor
);
M_32_SWAP(hdrp
->hdrpages
);
M_32_SWAP(hdrp
->h_charkey
);
for (i
= 0; i
< NCACHED
; i
++) {
M_32_SWAP(hdrp
->spares
[i
]);
M_16_SWAP(hdrp
->bitmaps
[i
]);