andre@3: /*- andre@3: * Copyright (c) 1990, 1993, 1994 andre@3: * The Regents of the University of California. All rights reserved. andre@3: * andre@3: * This code is derived from software contributed to Berkeley by andre@3: * Margo Seltzer. andre@3: * andre@3: * Redistribution and use in source and binary forms, with or without andre@3: * modification, are permitted provided that the following conditions andre@3: * are met: andre@3: * 1. Redistributions of source code must retain the above copyright andre@3: * notice, this list of conditions and the following disclaimer. andre@3: * 2. Redistributions in binary form must reproduce the above copyright andre@3: * notice, this list of conditions and the following disclaimer in the andre@3: * documentation and/or other materials provided with the distribution. andre@3: * 3. ***REMOVED*** - see andre@3: * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change andre@3: * 4. Neither the name of the University nor the names of its contributors andre@3: * may be used to endorse or promote products derived from this software andre@3: * without specific prior written permission. andre@3: * andre@3: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND andre@3: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE andre@3: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE andre@3: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE andre@3: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL andre@3: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS andre@3: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) andre@3: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT andre@3: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY andre@3: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF andre@3: * SUCH DAMAGE. andre@3: * andre@3: * @(#)hash.h 8.3 (Berkeley) 5/31/94 andre@3: */ andre@3: andre@3: /* Operations */ andre@3: andre@3: #include andre@4: #include "cdefs.h" andre@3: #include "mcom_db.h" andre@3: typedef enum { andre@3: HASH_GET, HASH_PUT, HASH_PUTNEW, HASH_DELETE, HASH_FIRST, HASH_NEXT andre@3: } ACTION; andre@3: andre@3: /* Buffer Management structures */ andre@3: typedef struct _bufhead BUFHEAD; andre@3: andre@3: struct _bufhead { andre@3: BUFHEAD *prev; /* LRU links */ andre@3: BUFHEAD *next; /* LRU links */ andre@3: BUFHEAD *ovfl; /* Overflow page buffer header */ andre@3: uint32 addr; /* Address of this page */ andre@3: char *page; /* Actual page data */ andre@3: char is_disk; andre@3: char flags; andre@3: #define BUF_MOD 0x0001 andre@3: #define BUF_DISK 0x0002 andre@3: #define BUF_BUCKET 0x0004 andre@3: #define BUF_PIN 0x0008 andre@3: }; andre@3: andre@3: #define IS_BUCKET(X) ((X) & BUF_BUCKET) andre@3: andre@3: typedef BUFHEAD **SEGMENT; andre@3: andre@3: typedef int DBFILE_PTR; andre@3: #define NO_FILE -1 andre@3: #ifdef macintosh andre@3: #define DBFILE_OPEN(path, flag,mode) open((path), flag) andre@3: #define EXISTS(path) andre@3: #else andre@3: #define DBFILE_OPEN(path, flag,mode) open((path), (flag), (mode)) andre@3: #endif andre@3: /* Hash Table Information */ andre@3: typedef struct hashhdr { /* Disk resident portion */ andre@3: int32 magic; /* Magic NO for hash tables */ andre@3: int32 version; /* Version ID */ andre@3: uint32 lorder; /* Byte Order */ andre@3: int32 bsize; /* Bucket/Page Size */ andre@3: int32 bshift; /* Bucket shift */ andre@3: int32 dsize; /* Directory Size */ andre@3: int32 ssize; /* Segment Size */ andre@3: int32 sshift; /* Segment shift */ andre@3: int32 ovfl_point; /* Where overflow pages are being andre@3: * allocated */ andre@3: int32 last_freed; /* Last overflow page freed */ andre@3: int32 max_bucket; /* ID of Maximum bucket in use */ andre@3: int32 high_mask; /* Mask to modulo into entire table */ andre@3: int32 low_mask; /* Mask to modulo into lower half of andre@3: * table */ andre@3: int32 ffactor; /* Fill factor */ andre@3: int32 nkeys; /* Number of keys in hash table */ andre@3: int32 hdrpages; /* Size of table header */ andre@3: uint32 h_charkey; /* value of hash(CHARKEY) */ andre@3: #define NCACHED 32 /* number of bit maps and spare andre@3: * points */ andre@3: int32 spares[NCACHED];/* spare pages for overflow */ andre@3: uint16 bitmaps[NCACHED]; /* address of overflow page andre@3: * bitmaps */ andre@3: } HASHHDR; andre@3: andre@3: typedef struct htab { /* Memory resident data structure */ andre@3: HASHHDR hdr; /* Header */ andre@3: int nsegs; /* Number of allocated segments */ andre@3: int exsegs; /* Number of extra allocated andre@3: * segments */ andre@3: uint32 /* Hash function */ andre@3: (*hash)(const void *, size_t); andre@3: int flags; /* Flag values */ andre@3: DBFILE_PTR fp; /* File pointer */ andre@3: char *filename; andre@3: char *tmp_buf; /* Temporary Buffer for BIG data */ andre@3: char *tmp_key; /* Temporary Buffer for BIG keys */ andre@3: BUFHEAD *cpage; /* Current page */ andre@3: int cbucket; /* Current bucket */ andre@3: int cndx; /* Index of next item on cpage */ andre@3: int dbmerrno; /* Error Number -- for DBM andre@3: * compatability */ andre@3: int new_file; /* Indicates if fd is backing store andre@3: * or no */ andre@3: int save_file; /* Indicates whether we need to flush andre@3: * file at andre@3: * exit */ andre@3: uint32 *mapp[NCACHED]; /* Pointers to page maps */ andre@3: int nmaps; /* Initial number of bitmaps */ andre@3: int nbufs; /* Number of buffers left to andre@3: * allocate */ andre@3: BUFHEAD bufhead; /* Header of buffer lru list */ andre@3: SEGMENT *dir; /* Hash Bucket directory */ andre@3: off_t file_size; /* in bytes */ andre@3: char is_temp; /* unlink file on close */ andre@3: char updateEOF; /* force EOF update on flush */ andre@3: } HTAB; andre@3: andre@3: /* andre@3: * Constants andre@3: */ andre@3: #define DATABASE_CORRUPTED_ERROR -999 /* big ugly abort, delete database */ andre@3: #define OLD_MAX_BSIZE 65536 /* 2^16 */ andre@3: #define MAX_BSIZE 32l*1024l /* 2^15 */ andre@3: #define MIN_BUFFERS 6 andre@3: #define MINHDRSIZE 512 andre@3: #define DEF_BUFSIZE 65536l /* 64 K */ andre@3: #define DEF_BUCKET_SIZE 4096 andre@3: #define DEF_BUCKET_SHIFT 12 /* log2(BUCKET) */ andre@3: #define DEF_SEGSIZE 256 andre@3: #define DEF_SEGSIZE_SHIFT 8 /* log2(SEGSIZE) */ andre@3: #define DEF_DIRSIZE 256 andre@3: #define DEF_FFACTOR 65536l andre@3: #define MIN_FFACTOR 4 andre@3: #define SPLTMAX 8 andre@3: #define CHARKEY "%$sniglet^&" andre@3: #define NUMKEY 1038583l andre@3: #define BYTE_SHIFT 3 andre@3: #define INT_TO_BYTE 2 andre@3: #define INT_BYTE_SHIFT 5 andre@3: #define ALL_SET ((uint32)0xFFFFFFFF) andre@3: #define ALL_CLEAR 0 andre@3: andre@3: #define PTROF(X) ((ptrdiff_t)(X) == BUF_DISK ? 0 : (X)) andre@3: #define ISDISK(X) ((X) ? ((ptrdiff_t)(X) == BUF_DISK ? BUF_DISK \ andre@3: : (X)->is_disk) : 0) andre@3: andre@3: #define BITS_PER_MAP 32 andre@3: andre@3: /* Given the address of the beginning of a big map, clear/set the nth bit */ andre@3: #define CLRBIT(A, N) ((A)[(N)/BITS_PER_MAP] &= ~(1<<((N)%BITS_PER_MAP))) andre@3: #define SETBIT(A, N) ((A)[(N)/BITS_PER_MAP] |= (1<<((N)%BITS_PER_MAP))) andre@3: #define ISSET(A, N) ((A)[(N)/BITS_PER_MAP] & (1<<((N)%BITS_PER_MAP))) andre@3: andre@3: /* Overflow management */ andre@3: /* andre@3: * Overflow page numbers are allocated per split point. At each doubling of andre@3: * the table, we can allocate extra pages. So, an overflow page number has andre@3: * the top 5 bits indicate which split point and the lower 11 bits indicate andre@3: * which page at that split point is indicated (pages within split points are andre@3: * numberered starting with 1). andre@3: */ andre@3: andre@3: #define SPLITSHIFT 11 andre@3: #define SPLITMASK 0x7FF andre@3: #define SPLITNUM(N) (((uint32)(N)) >> SPLITSHIFT) andre@3: #define OPAGENUM(N) ((N) & SPLITMASK) andre@3: #define OADDR_OF(S,O) ((uint32)((uint32)(S) << SPLITSHIFT) + (O)) andre@3: andre@3: #define BUCKET_TO_PAGE(B) \ andre@3: (B) + hashp->HDRPAGES + ((B) ? hashp->SPARES[__log2((uint32)((B)+1))-1] : 0) andre@3: #define OADDR_TO_PAGE(B) \ andre@3: BUCKET_TO_PAGE ( (1 << SPLITNUM((B))) -1 ) + OPAGENUM((B)); andre@3: andre@3: /* andre@3: * page.h contains a detailed description of the page format. andre@3: * andre@3: * Normally, keys and data are accessed from offset tables in the top of andre@3: * each page which point to the beginning of the key and data. There are andre@3: * four flag values which may be stored in these offset tables which indicate andre@3: * the following: andre@3: * andre@3: * andre@3: * OVFLPAGE Rather than a key data pair, this pair contains andre@3: * the address of an overflow page. The format of andre@3: * the pair is: andre@3: * OVERFLOW_PAGE_NUMBER OVFLPAGE andre@3: * andre@3: * PARTIAL_KEY This must be the first key/data pair on a page andre@3: * and implies that page contains only a partial key. andre@3: * That is, the key is too big to fit on a single page andre@3: * so it starts on this page and continues on the next. andre@3: * The format of the page is: andre@3: * KEY_OFF PARTIAL_KEY OVFL_PAGENO OVFLPAGE andre@3: * andre@3: * KEY_OFF -- offset of the beginning of the key andre@3: * PARTIAL_KEY -- 1 andre@3: * OVFL_PAGENO - page number of the next overflow page andre@3: * OVFLPAGE -- 0 andre@3: * andre@3: * FULL_KEY This must be the first key/data pair on the page. It andre@3: * is used in two cases. andre@3: * andre@3: * Case 1: andre@3: * There is a complete key on the page but no data andre@3: * (because it wouldn't fit). The next page contains andre@3: * the data. andre@3: * andre@3: * Page format it: andre@3: * KEY_OFF FULL_KEY OVFL_PAGENO OVFL_PAGE andre@3: * andre@3: * KEY_OFF -- offset of the beginning of the key andre@3: * FULL_KEY -- 2 andre@3: * OVFL_PAGENO - page number of the next overflow page andre@3: * OVFLPAGE -- 0 andre@3: * andre@3: * Case 2: andre@3: * This page contains no key, but part of a large andre@3: * data field, which is continued on the next page. andre@3: * andre@3: * Page format it: andre@3: * DATA_OFF FULL_KEY OVFL_PAGENO OVFL_PAGE andre@3: * andre@3: * KEY_OFF -- offset of the beginning of the data on andre@3: * this page andre@3: * FULL_KEY -- 2 andre@3: * OVFL_PAGENO - page number of the next overflow page andre@3: * OVFLPAGE -- 0 andre@3: * andre@3: * FULL_KEY_DATA andre@3: * This must be the first key/data pair on the page. andre@3: * There are two cases: andre@3: * andre@3: * Case 1: andre@3: * This page contains a key and the beginning of the andre@3: * data field, but the data field is continued on the andre@3: * next page. andre@3: * andre@3: * Page format is: andre@3: * KEY_OFF FULL_KEY_DATA OVFL_PAGENO DATA_OFF andre@3: * andre@3: * KEY_OFF -- offset of the beginning of the key andre@3: * FULL_KEY_DATA -- 3 andre@3: * OVFL_PAGENO - page number of the next overflow page andre@3: * DATA_OFF -- offset of the beginning of the data andre@3: * andre@3: * Case 2: andre@3: * This page contains the last page of a big data pair. andre@3: * There is no key, only the tail end of the data andre@3: * on this page. andre@3: * andre@3: * Page format is: andre@3: * DATA_OFF FULL_KEY_DATA andre@3: * andre@3: * DATA_OFF -- offset of the beginning of the data on andre@3: * this page andre@3: * FULL_KEY_DATA -- 3 andre@3: * OVFL_PAGENO - page number of the next overflow page andre@3: * OVFLPAGE -- 0 andre@3: * andre@3: * OVFL_PAGENO and OVFLPAGE are optional (they are andre@3: * not present if there is no next page). andre@3: */ andre@3: andre@3: #define OVFLPAGE 0 andre@3: #define PARTIAL_KEY 1 andre@3: #define FULL_KEY 2 andre@3: #define FULL_KEY_DATA 3 andre@3: #define REAL_KEY 4 andre@3: andre@3: /* Short hands for accessing structure */ andre@3: #undef BSIZE andre@3: #define BSIZE hdr.bsize andre@3: #undef BSHIFT andre@3: #define BSHIFT hdr.bshift andre@3: #define DSIZE hdr.dsize andre@3: #define SGSIZE hdr.ssize andre@3: #define SSHIFT hdr.sshift andre@3: #define LORDER hdr.lorder andre@3: #define OVFL_POINT hdr.ovfl_point andre@3: #define LAST_FREED hdr.last_freed andre@3: #define MAX_BUCKET hdr.max_bucket andre@3: #define FFACTOR hdr.ffactor andre@3: #define HIGH_MASK hdr.high_mask andre@3: #define LOW_MASK hdr.low_mask andre@3: #define NKEYS hdr.nkeys andre@3: #define HDRPAGES hdr.hdrpages andre@3: #define SPARES hdr.spares andre@3: #define BITMAPS hdr.bitmaps andre@3: #define VERSION hdr.version andre@3: #define MAGIC hdr.magic andre@3: #define NEXT_FREE hdr.next_free andre@3: #define H_CHARKEY hdr.h_charkey andre@3: andre@3: extern uint32 (*__default_hash) (const void *, size_t); andre@3: void __buf_init(HTAB *hashp, int32 nbytes); andre@3: int __big_delete(HTAB *hashp, BUFHEAD *bufp); andre@3: BUFHEAD * __get_buf(HTAB *hashp, uint32 addr, BUFHEAD *prev_bp, int newpage); andre@3: uint32 __call_hash(HTAB *hashp, char *k, size_t len); andre@3: #include "page.h" andre@3: extern int __big_split(HTAB *hashp, BUFHEAD *op,BUFHEAD *np, andre@3: BUFHEAD *big_keyp,uint32 addr,uint32 obucket, SPLIT_RETURN *ret); andre@3: void __free_ovflpage(HTAB *hashp, BUFHEAD *obufp); andre@3: BUFHEAD * __add_ovflpage(HTAB *hashp, BUFHEAD *bufp); andre@3: int __big_insert(HTAB *hashp, BUFHEAD *bufp, const DBT *key, const DBT *val); andre@3: int __expand_table(HTAB *hashp); andre@3: uint32 __log2(uint32 num); andre@3: void __reclaim_buf(HTAB *hashp, BUFHEAD *bp); andre@3: int __get_page(HTAB *hashp, char * p, uint32 bucket, int is_bucket, int is_disk, int is_bitmap); andre@3: int __put_page(HTAB *hashp, char *p, uint32 bucket, int is_bucket, int is_bitmap); andre@3: int __ibitmap(HTAB *hashp, int pnum, int nbits, int ndx); andre@3: int __buf_free(HTAB *hashp, int do_free, int to_disk); andre@3: int __find_bigpair(HTAB *hashp, BUFHEAD *bufp, int ndx, char *key, int size); andre@3: uint16 __find_last_page(HTAB *hashp, BUFHEAD **bpp); andre@3: int __addel(HTAB *hashp, BUFHEAD *bufp, const DBT *key, const DBT * val); andre@3: int __big_return(HTAB *hashp, BUFHEAD *bufp, int ndx, DBT *val, int set_current); andre@3: int __delpair(HTAB *hashp, BUFHEAD *bufp, int ndx); andre@3: int __big_keydata(HTAB *hashp, BUFHEAD *bufp, DBT *key, DBT *val, int set); andre@3: int __split_page(HTAB *hashp, uint32 obucket, uint32 nbucket);