146366Sbostic /*- 246366Sbostic * Copyright (c) 1990 The Regents of the University of California. 346366Sbostic * All rights reserved. 446366Sbostic * 546366Sbostic * This code is derived from software contributed to Berkeley by 646366Sbostic * Margo Seltzer. 746366Sbostic * 846366Sbostic * %sccs.include.redist.c% 946366Sbostic */ 1046366Sbostic 1146366Sbostic #if defined(LIBC_SCCS) && !defined(lint) 12*52001Sbostic static char sccsid[] = "@(#)hash.c 5.22 (Berkeley) 12/18/91"; 1346366Sbostic #endif /* LIBC_SCCS and not lint */ 1446366Sbostic 1546366Sbostic #include <sys/param.h> 1646366Sbostic #include <sys/stat.h> 1746562Sbostic #include <fcntl.h> 1846366Sbostic #include <errno.h> 1950997Sbostic #ifdef DEBUG 2046366Sbostic #include <assert.h> 2150997Sbostic #endif 2246366Sbostic #include <db.h> 2346562Sbostic #include <stdio.h> 2446562Sbostic #include <stdlib.h> 2546500Sbostic #include <unistd.h> 2646562Sbostic #include <string.h> 2746366Sbostic #include "hash.h" 2850997Sbostic #include "page.h" 2950997Sbostic #include "extern.h" 3046366Sbostic 3150997Sbostic static int alloc_segs __P((int)); 3250997Sbostic static int flush_meta __P((void)); 3350997Sbostic static int hash_access __P((ACTION, DBT *, DBT *)); 3450997Sbostic static int hash_close __P((DB *)); 3550997Sbostic static int hash_delete __P((const DB *, const DBT *, u_int)); 3651057Sbostic static int hash_get __P((const DB *, const DBT *, DBT *, u_int)); 3750997Sbostic static int hash_put __P((const DB *, const DBT *, const DBT *, u_int)); 3850997Sbostic static void *hash_realloc __P((SEGMENT **, int, int)); 3950997Sbostic static int hash_seq __P((const DB *, DBT *, DBT *, u_int)); 4050997Sbostic static int hash_sync __P((const DB *)); 4150997Sbostic static int hdestroy __P((void)); 4250997Sbostic static HTAB *init_hash __P((HASHINFO *)); 4350997Sbostic static int init_htab __P((int)); 4450997Sbostic #if BYTE_ORDER == LITTLE_ENDIAN 4550997Sbostic static void swap_header __P((void)); 4650997Sbostic static void swap_header_copy __P((HASHHDR *, HASHHDR *)); 4750997Sbostic #endif 4850997Sbostic 4946366Sbostic /* Fast arithmetic, relying on powers of 2, */ 5050997Sbostic #define MOD(x, y) ((x) & ((y) - 1)) 5146366Sbostic 5250997Sbostic #define RETURN_ERROR(ERR, LOC) { save_errno = ERR; goto LOC; } 5346366Sbostic 5446366Sbostic /* Return values */ 5550997Sbostic #define SUCCESS (0) 5650997Sbostic #define ERROR (-1) 5750997Sbostic #define ABNORMAL (1) 5846366Sbostic 5946366Sbostic /* Local data */ 6046366Sbostic HTAB *hashp = NULL; 6146366Sbostic 6246366Sbostic #ifdef HASH_STATISTICS 6346366Sbostic long hash_accesses, hash_collisions, hash_expansions, hash_overflows; 6446366Sbostic #endif 6546366Sbostic 6650997Sbostic /************************** INTERFACE ROUTINES ***************************/ 6746366Sbostic /* OPEN/CLOSE */ 6846366Sbostic 6950997Sbostic extern DB * 7051072Sbostic __hash_open(file, flags, mode, info) 7150997Sbostic const char *file; 7250997Sbostic int flags, mode; 7350997Sbostic const HASHINFO *info; /* Special directives for create */ 7446366Sbostic { 7550997Sbostic struct stat statbuf; 7650997Sbostic DB *dbp; 7750997Sbostic int bpages, hdrsize, new_table, nsegs, save_errno; 7846366Sbostic 7950997Sbostic if (!(hashp = calloc(1, sizeof(HTAB)))) 8050997Sbostic return (NULL); 8150997Sbostic hashp->fp = -1; 8250997Sbostic /* 8350997Sbostic * Select flags relevant to us. Even if user wants write only, we need 8450997Sbostic * to be able to read the actual file, so we need to open it read/write. 8550997Sbostic * But, the field in the hashp structure needs to be accurate so that 8650997Sbostic * we can check accesses. 8750997Sbostic */ 8850997Sbostic hashp->flags = flags = 8950997Sbostic flags & (O_CREAT | O_EXCL | O_RDONLY | O_RDWR | O_TRUNC | O_WRONLY); 9050997Sbostic if (flags & O_WRONLY) 9150997Sbostic flags = (flags & ~O_WRONLY) | O_RDWR; 9246366Sbostic 9350997Sbostic new_table = 0; 9450997Sbostic if (!file || (flags & O_TRUNC) || 9550997Sbostic (stat(file, &statbuf) && (errno == ENOENT))) { 9650997Sbostic if (errno == ENOENT) 9750997Sbostic errno = 0; /* Just in case someone looks at errno */ 9850997Sbostic new_table = 1; 9946485Sbostic } 10050997Sbostic if (file) { 10150997Sbostic if ((hashp->fp = open(file, flags, mode)) == -1) 10250997Sbostic RETURN_ERROR(errno, error0); 10350997Sbostic (void)fcntl(hashp->fp, F_SETFD, 1); 10446366Sbostic } 10550997Sbostic if (new_table) { 10650997Sbostic if (!(hashp = init_hash((HASHINFO *)info))) 10750997Sbostic RETURN_ERROR(errno, error1); 10850997Sbostic } else { 10950997Sbostic /* Table already exists */ 11050997Sbostic if (info && info->hash) 11150997Sbostic hashp->hash = info->hash; 11250997Sbostic else 11350997Sbostic hashp->hash = default_hash; 11446366Sbostic 11550997Sbostic hdrsize = read(hashp->fp, &hashp->hdr, sizeof(HASHHDR)); 11646366Sbostic #if BYTE_ORDER == LITTLE_ENDIAN 11750997Sbostic swap_header(); 11846366Sbostic #endif 11950997Sbostic if (hdrsize == -1) 12050997Sbostic RETURN_ERROR(errno, error1); 12150997Sbostic if (hdrsize != sizeof(HASHHDR)) 12250997Sbostic RETURN_ERROR(EFTYPE, error1); 12350997Sbostic /* Verify file type, versions and hash function */ 12450997Sbostic if (hashp->MAGIC != HASHMAGIC) 12550997Sbostic RETURN_ERROR(EFTYPE, error1); 12651061Sbostic if (hashp->VERSION != HASHVERSION) 12750997Sbostic RETURN_ERROR(EFTYPE, error1); 12850997Sbostic if (hashp->hash(CHARKEY, sizeof(CHARKEY)) != hashp->H_CHARKEY) 12950997Sbostic RETURN_ERROR(EFTYPE, error1); 13051057Sbostic /* 13151057Sbostic * Figure out how many segments we need. Max_Bucket is the 13251057Sbostic * maximum bucket number, so the number of buckets is 13351057Sbostic * max_bucket + 1. 13451057Sbostic */ 13551057Sbostic nsegs = (hashp->MAX_BUCKET + 1 + hashp->SGSIZE - 1) / 13650997Sbostic hashp->SGSIZE; 13750997Sbostic hashp->nsegs = 0; 13850997Sbostic if (alloc_segs(nsegs)) 13950997Sbostic /* 14050997Sbostic * If alloc_segs fails, table will have been destroyed 14150997Sbostic * and errno will have been set. 14250997Sbostic */ 14350997Sbostic return (NULL); 14450997Sbostic /* Read in bitmaps */ 14551061Sbostic bpages = (hashp->SPARES[hashp->OVFL_POINT] + 14650997Sbostic (hashp->BSIZE << BYTE_SHIFT) - 1) >> 14750997Sbostic (hashp->BSHIFT + BYTE_SHIFT); 14846366Sbostic 14950997Sbostic hashp->nmaps = bpages; 15051057Sbostic (void)memset(&hashp->mapp[0], 0, bpages * sizeof(u_long *)); 15146366Sbostic } 15246366Sbostic 15350997Sbostic /* Initialize Buffer Manager */ 15450997Sbostic if (info && info->cachesize) 15550997Sbostic __buf_init(info->cachesize); 15650997Sbostic else 15750997Sbostic __buf_init(DEF_BUFSIZE); 15850997Sbostic 15950997Sbostic hashp->new_file = new_table; 16050997Sbostic hashp->save_file = file && (hashp->flags & (O_WRONLY | O_RDWR)); 16150997Sbostic hashp->cbucket = -1; 16250997Sbostic if (!(dbp = malloc(sizeof(DB)))) { 16350997Sbostic save_errno = errno; 16450997Sbostic hdestroy(); 16550997Sbostic errno = save_errno; 16650997Sbostic return (NULL); 16746366Sbostic } 16850997Sbostic dbp->internal = (char *)hashp; 16950997Sbostic dbp->close = hash_close; 17050997Sbostic dbp->del = hash_delete; 17150997Sbostic dbp->get = hash_get; 17250997Sbostic dbp->put = hash_put; 17350997Sbostic dbp->seq = hash_seq; 17450997Sbostic dbp->sync = hash_sync; 17550997Sbostic dbp->type = DB_HASH; 17646366Sbostic 17746366Sbostic #ifdef DEBUG 17850997Sbostic (void)fprintf(stderr, 17951061Sbostic "%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", 18050997Sbostic "init_htab:", 18150997Sbostic "TABLE POINTER ", hashp, 18250997Sbostic "BUCKET SIZE ", hashp->BSIZE, 18350997Sbostic "BUCKET SHIFT ", hashp->BSHIFT, 18450997Sbostic "DIRECTORY SIZE ", hashp->DSIZE, 18550997Sbostic "SEGMENT SIZE ", hashp->SGSIZE, 18650997Sbostic "SEGMENT SHIFT ", hashp->SSHIFT, 18750997Sbostic "FILL FACTOR ", hashp->FFACTOR, 18850997Sbostic "MAX BUCKET ", hashp->MAX_BUCKET, 18951061Sbostic "OVFL POINT ", hashp->OVFL_POINT, 19051061Sbostic "LAST FREED ", hashp->LAST_FREED, 19150997Sbostic "HIGH MASK ", hashp->HIGH_MASK, 19250997Sbostic "LOW MASK ", hashp->LOW_MASK, 19350997Sbostic "NSEGS ", hashp->nsegs, 19450997Sbostic "NKEYS ", hashp->NKEYS); 19546366Sbostic #endif 19646366Sbostic #ifdef HASH_STATISTICS 19746366Sbostic hash_overflows = hash_accesses = hash_collisions = hash_expansions = 0; 19846366Sbostic #endif 19950997Sbostic return (dbp); 20046366Sbostic 20146366Sbostic error1: 20251192Sbostic if (hashp != NULL) 20351192Sbostic (void)close(hashp->fp); 20446366Sbostic 20546366Sbostic error0: 20650997Sbostic free(hashp); 20750997Sbostic errno = save_errno; 20850997Sbostic return (NULL); 20946366Sbostic } 21046366Sbostic 21146366Sbostic static int 21250997Sbostic hash_close(dbp) 21350997Sbostic DB *dbp; 21446366Sbostic { 21550997Sbostic int retval; 21646457Sbostic 21750997Sbostic if (!dbp) 21850997Sbostic return (ERROR); 21946366Sbostic hashp = (HTAB *)dbp->internal; 22046457Sbostic retval = hdestroy(); 22150997Sbostic free(dbp); 22250997Sbostic return (retval); 22346366Sbostic } 22446366Sbostic 22546366Sbostic /************************** LOCAL CREATION ROUTINES **********************/ 22650997Sbostic static HTAB * 22750997Sbostic init_hash(info) 22850997Sbostic HASHINFO *info; 22946366Sbostic { 23050997Sbostic int nelem; 23146366Sbostic 23246366Sbostic nelem = 1; 23351061Sbostic hashp->NKEYS = 0; 23450997Sbostic hashp->LORDER = BYTE_ORDER; 23550997Sbostic hashp->BSIZE = DEF_BUCKET_SIZE; 23650997Sbostic hashp->BSHIFT = DEF_BUCKET_SHIFT; 23750997Sbostic hashp->SGSIZE = DEF_SEGSIZE; 23850997Sbostic hashp->SSHIFT = DEF_SEGSIZE_SHIFT; 23950997Sbostic hashp->DSIZE = DEF_DIRSIZE; 24050997Sbostic hashp->FFACTOR = DEF_FFACTOR; 24150997Sbostic hashp->hash = default_hash; 24250997Sbostic bzero(hashp->SPARES, sizeof(hashp->SPARES)); 24351061Sbostic bzero (hashp->BITMAPS, sizeof (hashp->BITMAPS)); 24446366Sbostic 24550997Sbostic if (info) { 24650997Sbostic if (info->bsize) { 24750997Sbostic /* Round pagesize up to power of 2 */ 24850997Sbostic hashp->BSHIFT = __log2(info->bsize); 24950997Sbostic hashp->BSIZE = 1 << hashp->BSHIFT; 25050997Sbostic if (hashp->BSIZE > MAX_BSIZE) { 25150997Sbostic errno = EINVAL; 25250997Sbostic return (NULL); 25350997Sbostic } 25446366Sbostic } 25550997Sbostic if (info->ffactor) 25650997Sbostic hashp->FFACTOR = info->ffactor; 25750997Sbostic if (info->hash) 25850997Sbostic hashp->hash = info->hash; 25950997Sbostic if (info->nelem) 26050997Sbostic nelem = info->nelem; 26150997Sbostic if (info->lorder) { 26250997Sbostic if (info->lorder != BIG_ENDIAN && 26350997Sbostic info->lorder != LITTLE_ENDIAN) { 26450997Sbostic errno = EINVAL; 26550997Sbostic return (NULL); 26650997Sbostic } 26750997Sbostic hashp->LORDER = info->lorder; 26846366Sbostic } 26946366Sbostic } 27046366Sbostic /* init_htab should destroy the table and set errno if it fails */ 27150997Sbostic if (init_htab(nelem)) 27250997Sbostic return (NULL); 27350997Sbostic else 27450997Sbostic return (hashp); 27546366Sbostic } 27646366Sbostic /* 27750997Sbostic * This calls alloc_segs which may run out of memory. Alloc_segs will destroy 27850997Sbostic * the table and set errno, so we just pass the error information along. 27950997Sbostic * 28050997Sbostic * Returns 0 on No Error 28150997Sbostic */ 28246366Sbostic static int 28350997Sbostic init_htab(nelem) 28450997Sbostic int nelem; 28546366Sbostic { 28650997Sbostic register int nbuckets, nsegs; 28750997Sbostic int l2; 28846366Sbostic 28950997Sbostic /* 29050997Sbostic * Divide number of elements by the fill factor and determine a 29150997Sbostic * desired number of buckets. Allocate space for the next greater 29250997Sbostic * power of two number of buckets. 29350997Sbostic */ 29446366Sbostic nelem = (nelem - 1) / hashp->FFACTOR + 1; 29546366Sbostic 296*52001Sbostic l2 = __log2(MAX(nelem, 2)); 29746366Sbostic nbuckets = 1 << l2; 29846366Sbostic 29946366Sbostic hashp->SPARES[l2] = l2 + 1; 30050997Sbostic hashp->SPARES[l2 + 1] = l2 + 1; 30151061Sbostic hashp->OVFL_POINT = l2; 30251061Sbostic hashp->LAST_FREED = 2; 30351061Sbostic 30450997Sbostic /* First bitmap page is at: splitpoint l2 page offset 1 */ 30551057Sbostic if (__init_bitmap(OADDR_OF(l2, 1), l2 + 1, 0)) 30651057Sbostic return (-1); 30746366Sbostic 30846366Sbostic hashp->MAX_BUCKET = hashp->LOW_MASK = nbuckets - 1; 30946366Sbostic hashp->HIGH_MASK = (nbuckets << 1) - 1; 31050997Sbostic hashp->HDRPAGES = ((MAX(sizeof(HASHHDR), MINHDRSIZE) - 1) >> 31150997Sbostic hashp->BSHIFT) + 1; 31246366Sbostic 31346366Sbostic nsegs = (nbuckets - 1) / hashp->SGSIZE + 1; 31446366Sbostic nsegs = 1 << __log2(nsegs); 31546366Sbostic 31650997Sbostic if (nsegs > hashp->DSIZE) 31750997Sbostic hashp->DSIZE = nsegs; 31850997Sbostic return (alloc_segs(nsegs)); 31946366Sbostic } 32046366Sbostic 32146366Sbostic /********************** DESTROY/CLOSE ROUTINES ************************/ 32246366Sbostic 32346366Sbostic /* 32450997Sbostic * Flushes any changes to the file if necessary and destroys the hashp 32550997Sbostic * structure, freeing all allocated space. 32650997Sbostic */ 32746366Sbostic static int 32846366Sbostic hdestroy() 32946366Sbostic { 33050997Sbostic int i, save_errno; 33146366Sbostic 33246366Sbostic save_errno = 0; 33346366Sbostic 33446366Sbostic if (hashp != NULL) { 33546366Sbostic #ifdef HASH_STATISTICS 33650997Sbostic (void)fprintf(stderr, "hdestroy: accesses %ld collisions %ld\n", 33750997Sbostic hash_accesses, hash_collisions); 33850997Sbostic (void)fprintf(stderr, "hdestroy: expansions %ld\n", 33950997Sbostic hash_expansions); 34050997Sbostic (void)fprintf(stderr, "hdestroy: overflows %ld\n", 34150997Sbostic hash_overflows); 34250997Sbostic (void)fprintf(stderr, "keys %ld maxp %d segmentcount %d\n", 34350997Sbostic hashp->NKEYS, hashp->MAX_BUCKET, hashp->nsegs); 34446366Sbostic 34550997Sbostic for (i = 0; i < NCACHED; i++) 34650997Sbostic (void)fprintf(stderr, 34750997Sbostic "spares[%d] = %d\n", i, hashp->SPARES[i]); 34846366Sbostic #endif 34950997Sbostic /* 35050997Sbostic * Call on buffer manager to free buffers, and if required, 35150997Sbostic * write them to disk. 35250997Sbostic */ 35350997Sbostic if (__buf_free(1, hashp->save_file)) 35450997Sbostic save_errno = errno; 35550997Sbostic if (hashp->dir) { 35650997Sbostic free(*hashp->dir); /* Free initial segments */ 35750997Sbostic /* Free extra segments */ 35850997Sbostic while (hashp->exsegs--) 35950997Sbostic free(hashp->dir[--hashp->nsegs]); 36050997Sbostic free(hashp->dir); 36146366Sbostic } 36250997Sbostic if (flush_meta() && !save_errno) 36350997Sbostic save_errno = errno; 36447251Sbostic /* Free Bigmaps */ 36550997Sbostic for (i = 0; i < hashp->nmaps; i++) 36650997Sbostic if (hashp->mapp[i]) 36750997Sbostic free(hashp->mapp[i]); 36846503Sbostic 36950997Sbostic if (hashp->fp != -1) 37050997Sbostic (void)close(hashp->fp); 37150997Sbostic free(hashp); 37246366Sbostic hashp = NULL; 37346366Sbostic } 37450997Sbostic if (save_errno) { 37550997Sbostic errno = save_errno; 37650997Sbostic return (ERROR); 37746366Sbostic } 37850997Sbostic return (SUCCESS); 37946366Sbostic } 38050997Sbostic /* 38150997Sbostic * Write modified pages to disk 38250997Sbostic * 38350997Sbostic * Returns: 38450997Sbostic * 0 == OK 38550997Sbostic * -1 ERROR 38650997Sbostic */ 38746366Sbostic static int 38846366Sbostic hash_sync(dbp) 38950997Sbostic const DB *dbp; 39046366Sbostic { 39150997Sbostic if (!dbp) 39250997Sbostic return (ERROR); 39346366Sbostic hashp = (HTAB *)dbp->internal; 39446366Sbostic 39550997Sbostic if (!hashp->save_file) 39650997Sbostic return (0); 39750997Sbostic if (__buf_free(0, 1) || flush_meta()) 39850997Sbostic return (ERROR); 39946366Sbostic hashp->new_file = 0; 40050997Sbostic return (0); 40146366Sbostic } 40246366Sbostic 40346366Sbostic /* 40450997Sbostic * Returns: 40550997Sbostic * 0 == OK 40650997Sbostic * -1 indicates that errno should be set 40750997Sbostic */ 40846366Sbostic static int 40946366Sbostic flush_meta() 41046366Sbostic { 41151814Smarc HASHHDR *whdrp; 41251814Smarc #if BYTE_ORDER == LITTLE_ENDIAN 41351814Smarc HASHHDR whdr; 41451814Smarc #endif 41550997Sbostic int fp, i, wsize; 41646366Sbostic 41750997Sbostic if (!hashp->save_file) 41850997Sbostic return (0); 41946366Sbostic hashp->MAGIC = HASHMAGIC; 42051061Sbostic hashp->VERSION = HASHVERSION; 42150997Sbostic hashp->H_CHARKEY = hashp->hash(CHARKEY, sizeof(CHARKEY)); 42246366Sbostic 42346366Sbostic fp = hashp->fp; 42446366Sbostic whdrp = &hashp->hdr; 42546366Sbostic #if BYTE_ORDER == LITTLE_ENDIAN 42646366Sbostic whdrp = &whdr; 42750997Sbostic swap_header_copy(&hashp->hdr, whdrp); 42846366Sbostic #endif 42950997Sbostic if ((lseek(fp, 0, SEEK_SET) == -1) || 43050997Sbostic ((wsize = write(fp, whdrp, sizeof(HASHHDR))) == -1)) 43150997Sbostic return (-1); 43250997Sbostic else 43350997Sbostic if (wsize != sizeof(HASHHDR)) { 43450997Sbostic errno = EFTYPE; 43550997Sbostic hashp->errno = errno; 43650997Sbostic return (-1); 43746366Sbostic } 43850997Sbostic for (i = 0; i < NCACHED; i++) 43950997Sbostic if (hashp->mapp[i]) 44051072Sbostic if (__put_page((char *)hashp->mapp[i], 44150997Sbostic hashp->BITMAPS[i], 0, 1)) 44250997Sbostic return (-1); 44350997Sbostic return (0); 44446366Sbostic } 44550997Sbostic 44646366Sbostic /*******************************SEARCH ROUTINES *****************************/ 44746366Sbostic /* 44850997Sbostic * All the access routines return 44950997Sbostic * 45050997Sbostic * Returns: 45150997Sbostic * 0 on SUCCESS 45250997Sbostic * 1 to indicate an external ERROR (i.e. key not found, etc) 45350997Sbostic * -1 to indicate an internal ERROR (i.e. out of memory, etc) 45450997Sbostic */ 45546366Sbostic static int 45650997Sbostic hash_get(dbp, key, data, flag) 45750997Sbostic const DB *dbp; 45851057Sbostic const DBT *key; 45951057Sbostic DBT *data; 46050997Sbostic u_int flag; 46146366Sbostic { 46250997Sbostic if (flag) { 46350997Sbostic hashp->errno = errno = EINVAL; 46450997Sbostic return (ERROR); 46550997Sbostic } 46650997Sbostic hashp = (HTAB *)dbp->internal; 46750997Sbostic if (hashp->flags & O_WRONLY) { 46850997Sbostic hashp->errno = errno = EPERM; 46950997Sbostic return (ERROR); 47050997Sbostic } 47151057Sbostic return (hash_access(HASH_GET, (DBT *)key, data)); 47246366Sbostic } 47346366Sbostic 47446366Sbostic static int 47550997Sbostic hash_put(dbp, key, data, flag) 47650997Sbostic const DB *dbp; 47750997Sbostic const DBT *key, *data; 47850997Sbostic u_int flag; 47946366Sbostic { 48050997Sbostic if (flag && flag != R_NOOVERWRITE) { 48150997Sbostic hashp->errno = errno = EINVAL; 48250997Sbostic return (ERROR); 48350997Sbostic } 48450997Sbostic hashp = (HTAB *)dbp->internal; 48550997Sbostic if ((hashp->flags & O_ACCMODE) == O_RDONLY) { 48650997Sbostic hashp->errno = errno = EPERM; 48750997Sbostic return (ERROR); 48850997Sbostic } 48951057Sbostic return (hash_access(flag == R_NOOVERWRITE ? 49050997Sbostic HASH_PUTNEW : HASH_PUT, (DBT *)key, (DBT *)data)); 49146366Sbostic } 49246366Sbostic 49346366Sbostic static int 49450997Sbostic hash_delete(dbp, key, flag) 49550997Sbostic const DB *dbp; 49650997Sbostic const DBT *key; 49750997Sbostic u_int flag; /* Ignored */ 49846366Sbostic { 49950997Sbostic if (flag && flag != R_CURSOR) { 50050997Sbostic hashp->errno = errno = EINVAL; 50150997Sbostic return (ERROR); 50250997Sbostic } 50350997Sbostic hashp = (HTAB *)dbp->internal; 50450997Sbostic if ((hashp->flags & O_ACCMODE) == O_RDONLY) { 50550997Sbostic hashp->errno = errno = EPERM; 50650997Sbostic return (ERROR); 50750997Sbostic } 50850997Sbostic return (hash_access(HASH_DELETE, (DBT *)key, NULL)); 50946366Sbostic } 51046366Sbostic 51146366Sbostic /* 51250997Sbostic * Assume that hashp has been set in wrapper routine. 51350997Sbostic */ 51446366Sbostic static int 51546366Sbostic hash_access(action, key, val) 51650997Sbostic ACTION action; 51750997Sbostic DBT *key, *val; 51846366Sbostic { 51950997Sbostic register BUFHEAD *rbufp; 52050997Sbostic BUFHEAD *bufp, *save_bufp; 52150997Sbostic register u_short *bp; 52250997Sbostic register int n, ndx, off, size; 52350997Sbostic register char *kp; 52450997Sbostic u_short pageno; 52546366Sbostic 52646366Sbostic #ifdef HASH_STATISTICS 52746366Sbostic hash_accesses++; 52846366Sbostic #endif 52946366Sbostic 53050997Sbostic off = hashp->BSIZE; 53146366Sbostic size = key->size; 53246950Sbostic kp = (char *)key->data; 53350997Sbostic rbufp = __get_buf(__call_hash(kp, size), NULL, 0); 53450997Sbostic if (!rbufp) 53550997Sbostic return (ERROR); 53646457Sbostic save_bufp = rbufp; 53746366Sbostic 53846457Sbostic /* Pin the bucket chain */ 53946457Sbostic rbufp->flags |= BUF_PIN; 54050997Sbostic for (bp = (u_short *)rbufp->page, n = *bp++, ndx = 1; ndx < n;) 54150997Sbostic if (bp[1] >= REAL_KEY) { 54250997Sbostic /* Real key/data pair */ 54350997Sbostic if (size == off - *bp && 54450997Sbostic bcmp(kp, rbufp->page + *bp, size) == 0) 54550997Sbostic goto found; 54650997Sbostic off = bp[1]; 54746366Sbostic #ifdef HASH_STATISTICS 54850997Sbostic hash_collisions++; 54946366Sbostic #endif 55050997Sbostic bp += 2; 55150997Sbostic ndx += 2; 55250997Sbostic } else if (bp[1] == OVFLPAGE) { 55350997Sbostic rbufp = __get_buf(*bp, rbufp, 0); 55450997Sbostic if (!rbufp) { 55550997Sbostic save_bufp->flags &= ~BUF_PIN; 55650997Sbostic return (ERROR); 55750997Sbostic } 55850997Sbostic /* FOR LOOP INIT */ 55950997Sbostic bp = (u_short *)rbufp->page; 56050997Sbostic n = *bp++; 56150997Sbostic ndx = 1; 56250997Sbostic off = hashp->BSIZE; 56350997Sbostic } else if (bp[1] < REAL_KEY) { 56450997Sbostic if ((ndx = __find_bigpair(rbufp, ndx, kp, size)) > 0) 56550997Sbostic goto found; 56650997Sbostic if (ndx == -2) { 56750997Sbostic bufp = rbufp; 56850997Sbostic if (!(pageno = __find_last_page(&bufp))) { 56950997Sbostic ndx = 0; 57050997Sbostic rbufp = bufp; 57150997Sbostic break; /* FOR */ 57250997Sbostic } 57350997Sbostic rbufp = __get_buf(pageno, bufp, 0); 57450997Sbostic if (!rbufp) { 57550997Sbostic save_bufp->flags &= ~BUF_PIN; 57650997Sbostic return (ERROR); 57750997Sbostic } 57850997Sbostic /* FOR LOOP INIT */ 57950997Sbostic bp = (u_short *)rbufp->page; 58050997Sbostic n = *bp++; 58150997Sbostic ndx = 1; 58250997Sbostic off = hashp->BSIZE; 58350997Sbostic } else { 58450997Sbostic save_bufp->flags &= ~BUF_PIN; 58550997Sbostic return (ERROR); 58650997Sbostic } 58746457Sbostic } 58846366Sbostic 58946366Sbostic /* Not found */ 59050997Sbostic switch (action) { 59150997Sbostic case HASH_PUT: 59250997Sbostic case HASH_PUTNEW: 59346366Sbostic if (__addel(rbufp, key, val)) { 59450997Sbostic save_bufp->flags &= ~BUF_PIN; 59550997Sbostic return (ERROR); 59646366Sbostic } else { 59750997Sbostic save_bufp->flags &= ~BUF_PIN; 59850997Sbostic return (SUCCESS); 59946366Sbostic } 60050997Sbostic case HASH_GET: 60150997Sbostic case HASH_DELETE: 60250997Sbostic default: 60346457Sbostic save_bufp->flags &= ~BUF_PIN; 60450997Sbostic return (ABNORMAL); 60546366Sbostic } 60646366Sbostic 60746366Sbostic found: 60846366Sbostic switch (action) { 60950997Sbostic case HASH_PUTNEW: 61046457Sbostic save_bufp->flags &= ~BUF_PIN; 61150997Sbostic return (ABNORMAL); 61250997Sbostic case HASH_GET: 61346366Sbostic bp = (u_short *)rbufp->page; 61451072Sbostic if (bp[ndx + 1] < REAL_KEY) { 61551057Sbostic if (__big_return(rbufp, ndx, val, 0)) 61651057Sbostic return (ERROR); 61751072Sbostic } else { 61850997Sbostic val->data = (u_char *)rbufp->page + (int)bp[ndx + 1]; 61950997Sbostic val->size = bp[ndx] - bp[ndx + 1]; 62046366Sbostic } 62146366Sbostic break; 62250997Sbostic case HASH_PUT: 62350997Sbostic if ((__delpair(rbufp, ndx)) || (__addel(rbufp, key, val))) { 62450997Sbostic save_bufp->flags &= ~BUF_PIN; 62550997Sbostic return (ERROR); 62646457Sbostic } 62746366Sbostic break; 62850997Sbostic case HASH_DELETE: 62950997Sbostic if (__delpair(rbufp, ndx)) 63050997Sbostic return (ERROR); 63146366Sbostic break; 63246366Sbostic } 63346457Sbostic save_bufp->flags &= ~BUF_PIN; 63446366Sbostic return (SUCCESS); 63546366Sbostic } 63646366Sbostic 63746366Sbostic static int 63846366Sbostic hash_seq(dbp, key, data, flag) 63950997Sbostic const DB *dbp; 64050997Sbostic DBT *key, *data; 64150997Sbostic u_int flag; 64246366Sbostic { 64350997Sbostic register u_int bucket; 64450997Sbostic register BUFHEAD *bufp; 64550997Sbostic u_short *bp, ndx; 64646366Sbostic 64750997Sbostic if (flag && flag != R_FIRST && flag != R_NEXT) { 64850997Sbostic hashp->errno = errno = EINVAL; 64950997Sbostic return (ERROR); 65046366Sbostic } 65146366Sbostic hashp = (HTAB *)dbp->internal; 65250997Sbostic if (hashp->flags & O_WRONLY) { 65350997Sbostic hashp->errno = errno = EPERM; 65450997Sbostic return (ERROR); 65546366Sbostic } 65646366Sbostic #ifdef HASH_STATISTICS 65746366Sbostic hash_accesses++; 65846366Sbostic #endif 65950997Sbostic if ((hashp->cbucket < 0) || (flag == R_FIRST)) { 66050997Sbostic hashp->cbucket = 0; 66150997Sbostic hashp->cndx = 1; 66250997Sbostic hashp->cpage = NULL; 66346366Sbostic } 66450997Sbostic if (!(bufp = hashp->cpage)) { 66550997Sbostic for (bucket = hashp->cbucket; bucket <= hashp->MAX_BUCKET; 66650997Sbostic bucket++, hashp->cndx = 1) { 66746366Sbostic 66850997Sbostic bufp = __get_buf(bucket, NULL, 0); 66950997Sbostic if (!bufp) 67050997Sbostic return (ERROR); 67150997Sbostic hashp->cpage = bufp; 67250997Sbostic bp = (u_short *)bufp->page; 67350997Sbostic if (bp[0]) 67450997Sbostic break; 67550997Sbostic } 67650997Sbostic hashp->cbucket = bucket; 67750997Sbostic if (hashp->cbucket > hashp->MAX_BUCKET) { 67850997Sbostic hashp->cbucket = -1; 67950997Sbostic return (ABNORMAL); 68050997Sbostic } 68150997Sbostic } else 68250997Sbostic bp = (u_short *)hashp->cpage->page; 68346366Sbostic 68446366Sbostic #ifdef DEBUG 68550997Sbostic assert(bp); 68650997Sbostic assert(bufp); 68746366Sbostic #endif 68850997Sbostic while (bp[hashp->cndx + 1] == OVFLPAGE) { 68950997Sbostic bufp = hashp->cpage = __get_buf(bp[hashp->cndx], bufp, 0); 69050997Sbostic if (!bufp) 69150997Sbostic return (ERROR); 69250997Sbostic bp = (u_short *)(bufp->page); 69350997Sbostic hashp->cndx = 1; 69446366Sbostic } 69546366Sbostic ndx = hashp->cndx; 69650997Sbostic if (bp[ndx + 1] < REAL_KEY) { 69751057Sbostic if (__big_keydata(bufp, key, data, 1)) 69850997Sbostic return (ERROR); 69946366Sbostic } else { 70050997Sbostic key->data = (u_char *)hashp->cpage->page + bp[ndx]; 70150997Sbostic key->size = (ndx > 1 ? bp[ndx - 1] : hashp->BSIZE) - bp[ndx]; 70250997Sbostic data->data = (u_char *)hashp->cpage->page + bp[ndx + 1]; 70350997Sbostic data->size = bp[ndx] - bp[ndx + 1]; 70450997Sbostic ndx += 2; 70550997Sbostic if (ndx > bp[0]) { 70650997Sbostic hashp->cpage = NULL; 70750997Sbostic hashp->cbucket++; 70850997Sbostic hashp->cndx = 1; 70950997Sbostic } else 71050997Sbostic hashp->cndx = ndx; 71146366Sbostic } 71246366Sbostic return (SUCCESS); 71346366Sbostic } 71446366Sbostic 71546366Sbostic /********************************* UTILITIES ************************/ 71650997Sbostic 71746366Sbostic /* 71850997Sbostic * Returns: 71950997Sbostic * 0 ==> OK 72050997Sbostic * -1 ==> Error 72150997Sbostic */ 72246366Sbostic extern int 72346366Sbostic __expand_table() 72446366Sbostic { 72550997Sbostic u_int old_bucket, new_bucket; 72650997Sbostic int dirsize, new_segnum, spare_ndx; 72750997Sbostic 72846366Sbostic #ifdef HASH_STATISTICS 72946366Sbostic hash_expansions++; 73046366Sbostic #endif 73146366Sbostic new_bucket = ++hashp->MAX_BUCKET; 73246366Sbostic old_bucket = (hashp->MAX_BUCKET & hashp->LOW_MASK); 73346366Sbostic 73446366Sbostic new_segnum = new_bucket >> hashp->SSHIFT; 73546366Sbostic 73646366Sbostic /* Check if we need a new segment */ 73750997Sbostic if (new_segnum >= hashp->nsegs) { 73850997Sbostic /* Check if we need to expand directory */ 73950997Sbostic if (new_segnum >= hashp->DSIZE) { 74050997Sbostic /* Reallocate directory */ 74150997Sbostic dirsize = hashp->DSIZE * sizeof(SEGMENT *); 74250997Sbostic if (!hash_realloc(&hashp->dir, dirsize, dirsize << 1)) 74350997Sbostic return (-1); 74450997Sbostic hashp->DSIZE = dirsize << 1; 74546366Sbostic } 74650997Sbostic if (!(hashp->dir[new_segnum] = 74750997Sbostic calloc(hashp->SGSIZE, sizeof(SEGMENT)))) 74850997Sbostic return (-1); 74950997Sbostic hashp->exsegs++; 75050997Sbostic hashp->nsegs++; 75146366Sbostic } 75246366Sbostic /* 75350997Sbostic * If the split point is increasing (MAX_BUCKET's log base 2 75450997Sbostic * * increases), we need to copy the current contents of the spare 75550997Sbostic * split bucket to the next bucket. 75650997Sbostic */ 757*52001Sbostic spare_ndx = __log2(hashp->MAX_BUCKET + 1); 758*52001Sbostic if (spare_ndx > hashp->OVFL_POINT) { 75951061Sbostic hashp->SPARES[spare_ndx] = hashp->SPARES[hashp->OVFL_POINT]; 76051061Sbostic hashp->OVFL_POINT = spare_ndx; 76151061Sbostic } 76251061Sbostic 76350997Sbostic if (new_bucket > hashp->HIGH_MASK) { 76450997Sbostic /* Starting a new doubling */ 76550997Sbostic hashp->LOW_MASK = hashp->HIGH_MASK; 76650997Sbostic hashp->HIGH_MASK = new_bucket | hashp->LOW_MASK; 76746366Sbostic } 76850997Sbostic /* Relocate records to the new bucket */ 76950997Sbostic return (__split_page(old_bucket, new_bucket)); 77050997Sbostic } 77146366Sbostic 77246366Sbostic /* 77350997Sbostic * If realloc guarantees that the pointer is not destroyed if the realloc 77450997Sbostic * fails, then this routine can go away. 77550997Sbostic */ 77650997Sbostic static void * 77750997Sbostic hash_realloc(p_ptr, oldsize, newsize) 77850997Sbostic SEGMENT **p_ptr; 77950997Sbostic int oldsize, newsize; 78046366Sbostic { 78150997Sbostic register void *p; 78246366Sbostic 78350997Sbostic if (p = malloc(newsize)) { 78450997Sbostic bcopy(*p_ptr, p, oldsize); 78550997Sbostic bzero(*p_ptr + oldsize, newsize - oldsize); 78646366Sbostic free(*p_ptr); 78746366Sbostic *p_ptr = p; 78846366Sbostic } 78946366Sbostic return (p); 79046366Sbostic } 79146366Sbostic 79247251Sbostic extern u_int 79350997Sbostic __call_hash(k, len) 79450997Sbostic char *k; 79550997Sbostic int len; 79646366Sbostic { 79750997Sbostic int n, bucket; 79850997Sbostic 79946366Sbostic n = hashp->hash(k, len); 80046366Sbostic bucket = n & hashp->HIGH_MASK; 80150997Sbostic if (bucket > hashp->MAX_BUCKET) 80250997Sbostic bucket = bucket & hashp->LOW_MASK; 80350997Sbostic return (bucket); 80446366Sbostic } 80546366Sbostic 80646366Sbostic /* 80750997Sbostic * Allocate segment table. On error, destroy the table and set errno. 80850997Sbostic * 80950997Sbostic * Returns 0 on success 81050997Sbostic */ 81146366Sbostic static int 81250997Sbostic alloc_segs(nsegs) 81350997Sbostic int nsegs; 81446366Sbostic { 81550997Sbostic register int i; 81650997Sbostic register SEGMENT store; 81746366Sbostic 81850997Sbostic int save_errno; 81946366Sbostic 82050997Sbostic if (!(hashp->dir = calloc(hashp->DSIZE, sizeof(SEGMENT *)))) { 82150997Sbostic save_errno = errno; 82250997Sbostic (void)hdestroy(); 82350997Sbostic errno = save_errno; 82450997Sbostic return (-1); 82550997Sbostic } 82650997Sbostic /* Allocate segments */ 82750997Sbostic store = calloc(nsegs << hashp->SSHIFT, sizeof(SEGMENT)); 82850997Sbostic if (!store) { 82950997Sbostic save_errno = errno; 83050997Sbostic (void)hdestroy(); 83150997Sbostic errno = save_errno; 83250997Sbostic return (-1); 83350997Sbostic } 83450997Sbostic for (i = 0; i < nsegs; i++, hashp->nsegs++) 83550997Sbostic hashp->dir[i] = &store[i << hashp->SSHIFT]; 83650997Sbostic return (0); 83746366Sbostic } 83846366Sbostic 83950997Sbostic #if BYTE_ORDER == LITTLE_ENDIAN 84046366Sbostic /* 84150997Sbostic * Hashp->hdr needs to be byteswapped. 84250997Sbostic */ 84346366Sbostic static void 84450997Sbostic swap_header_copy(srcp, destp) 84550997Sbostic HASHHDR *srcp, *destp; 84646366Sbostic { 84750997Sbostic int i; 84846366Sbostic 84950997Sbostic BLSWAP_COPY(srcp->magic, destp->magic); 85050997Sbostic BLSWAP_COPY(srcp->version, destp->version); 85150997Sbostic BLSWAP_COPY(srcp->lorder, destp->lorder); 85250997Sbostic BLSWAP_COPY(srcp->bsize, destp->bsize); 85350997Sbostic BLSWAP_COPY(srcp->bshift, destp->bshift); 85450997Sbostic BLSWAP_COPY(srcp->dsize, destp->dsize); 85550997Sbostic BLSWAP_COPY(srcp->ssize, destp->ssize); 85650997Sbostic BLSWAP_COPY(srcp->sshift, destp->sshift); 85751061Sbostic BLSWAP_COPY(srcp->ovfl_point, destp->ovfl_point); 85851061Sbostic BLSWAP_COPY(srcp->last_freed, destp->last_freed); 85950997Sbostic BLSWAP_COPY(srcp->max_bucket, destp->max_bucket); 86050997Sbostic BLSWAP_COPY(srcp->high_mask, destp->high_mask); 86150997Sbostic BLSWAP_COPY(srcp->low_mask, destp->low_mask); 86250997Sbostic BLSWAP_COPY(srcp->ffactor, destp->ffactor); 86350997Sbostic BLSWAP_COPY(srcp->nkeys, destp->nkeys); 86450997Sbostic BLSWAP_COPY(srcp->hdrpages, destp->hdrpages); 86550997Sbostic BLSWAP_COPY(srcp->h_charkey, destp->h_charkey); 86650997Sbostic for (i = 0; i < NCACHED; i++) { 86750997Sbostic BLSWAP_COPY(srcp->spares[i], destp->spares[i]); 86850997Sbostic BSSWAP_COPY(srcp->bitmaps[i], destp->bitmaps[i]); 86950997Sbostic } 87046366Sbostic } 87146366Sbostic 87246366Sbostic static void 87350997Sbostic swap_header() 87446366Sbostic { 87550997Sbostic HASHHDR *hdrp; 87650997Sbostic int i; 87746366Sbostic 87850997Sbostic hdrp = &hashp->hdr; 87946366Sbostic 88050997Sbostic BLSWAP(hdrp->magic); 88150997Sbostic BLSWAP(hdrp->version); 88250997Sbostic BLSWAP(hdrp->lorder); 88350997Sbostic BLSWAP(hdrp->bsize); 88450997Sbostic BLSWAP(hdrp->bshift); 88550997Sbostic BLSWAP(hdrp->dsize); 88650997Sbostic BLSWAP(hdrp->ssize); 88750997Sbostic BLSWAP(hdrp->sshift); 88851061Sbostic BLSWAP(hdrp->ovfl_point); 88951061Sbostic BLSWAP(hdrp->last_freed); 89050997Sbostic BLSWAP(hdrp->max_bucket); 89150997Sbostic BLSWAP(hdrp->high_mask); 89250997Sbostic BLSWAP(hdrp->low_mask); 89350997Sbostic BLSWAP(hdrp->ffactor); 89450997Sbostic BLSWAP(hdrp->nkeys); 89550997Sbostic BLSWAP(hdrp->hdrpages); 89650997Sbostic BLSWAP(hdrp->h_charkey); 89750997Sbostic for (i = 0; i < NCACHED; i++) { 89850997Sbostic BLSWAP(hdrp->spares[i]); 89950997Sbostic BSSWAP(hdrp->bitmaps[i]); 90050997Sbostic } 90146366Sbostic } 90250997Sbostic #endif 903