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