1*0Sstevel@tonic-gate /*- 2*0Sstevel@tonic-gate * See the file LICENSE for redistribution information. 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * Copyright (c) 1996, 1997, 1998 5*0Sstevel@tonic-gate * Sleepycat Software. All rights reserved. 6*0Sstevel@tonic-gate */ 7*0Sstevel@tonic-gate /* 8*0Sstevel@tonic-gate * Copyright (c) 1990, 1993, 1994 9*0Sstevel@tonic-gate * Margo Seltzer. All rights reserved. 10*0Sstevel@tonic-gate */ 11*0Sstevel@tonic-gate /* 12*0Sstevel@tonic-gate * Copyright (c) 1990, 1993, 1994 13*0Sstevel@tonic-gate * The Regents of the University of California. All rights reserved. 14*0Sstevel@tonic-gate * 15*0Sstevel@tonic-gate * This code is derived from software contributed to Berkeley by 16*0Sstevel@tonic-gate * Margo Seltzer. 17*0Sstevel@tonic-gate * 18*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 19*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions 20*0Sstevel@tonic-gate * are met: 21*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 22*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 23*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 24*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 25*0Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 26*0Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software 27*0Sstevel@tonic-gate * must display the following acknowledgement: 28*0Sstevel@tonic-gate * This product includes software developed by the University of 29*0Sstevel@tonic-gate * California, Berkeley and its contributors. 30*0Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors 31*0Sstevel@tonic-gate * may be used to endorse or promote products derived from this software 32*0Sstevel@tonic-gate * without specific prior written permission. 33*0Sstevel@tonic-gate * 34*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 35*0Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 36*0Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 37*0Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 38*0Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 39*0Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 40*0Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 41*0Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 42*0Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 43*0Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 44*0Sstevel@tonic-gate * SUCH DAMAGE. 45*0Sstevel@tonic-gate * 46*0Sstevel@tonic-gate * @(#)hash.h 10.14 (Sleepycat) 10/4/98 47*0Sstevel@tonic-gate */ 48*0Sstevel@tonic-gate 49*0Sstevel@tonic-gate /* Cursor structure definitions. */ 50*0Sstevel@tonic-gate typedef struct cursor_t { 51*0Sstevel@tonic-gate DBC *dbc; 52*0Sstevel@tonic-gate 53*0Sstevel@tonic-gate /* Per-thread information */ 54*0Sstevel@tonic-gate DB_LOCK hlock; /* Metadata page lock. */ 55*0Sstevel@tonic-gate HASHHDR *hdr; /* Pointer to meta-data page. */ 56*0Sstevel@tonic-gate PAGE *split_buf; /* Temporary buffer for splits. */ 57*0Sstevel@tonic-gate struct __db_h_stat stats; /* Hash statistics. */ 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate /* Hash cursor information */ 60*0Sstevel@tonic-gate db_pgno_t bucket; /* Bucket we are traversing. */ 61*0Sstevel@tonic-gate db_pgno_t lbucket; /* Bucket for which we are locked. */ 62*0Sstevel@tonic-gate DB_LOCK lock; /* Lock held on the current bucket. */ 63*0Sstevel@tonic-gate PAGE *pagep; /* The current page. */ 64*0Sstevel@tonic-gate db_pgno_t pgno; /* Current page number. */ 65*0Sstevel@tonic-gate db_indx_t bndx; /* Index within the current page. */ 66*0Sstevel@tonic-gate PAGE *dpagep; /* Duplicate page pointer. */ 67*0Sstevel@tonic-gate db_pgno_t dpgno; /* Duplicate page number. */ 68*0Sstevel@tonic-gate db_indx_t dndx; /* Index within a duplicate set. */ 69*0Sstevel@tonic-gate db_indx_t dup_off; /* Offset within a duplicate set. */ 70*0Sstevel@tonic-gate db_indx_t dup_len; /* Length of current duplicate. */ 71*0Sstevel@tonic-gate db_indx_t dup_tlen; /* Total length of duplicate entry. */ 72*0Sstevel@tonic-gate u_int32_t seek_size; /* Number of bytes we need for add. */ 73*0Sstevel@tonic-gate db_pgno_t seek_found_page;/* Page on which we can insert. */ 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gate #define H_DELETED 0x0001 /* Cursor item is deleted. */ 76*0Sstevel@tonic-gate #define H_DUPONLY 0x0002 /* Dups only; do not change key. */ 77*0Sstevel@tonic-gate #define H_EXPAND 0x0004 /* Table expanded. */ 78*0Sstevel@tonic-gate #define H_ISDUP 0x0008 /* Cursor is within duplicate set. */ 79*0Sstevel@tonic-gate #define H_NOMORE 0x0010 /* No more entries in bucket. */ 80*0Sstevel@tonic-gate #define H_OK 0x0020 /* Request succeeded. */ 81*0Sstevel@tonic-gate #define H_DIRTY 0x0040 /* Meta-data page needs to be written */ 82*0Sstevel@tonic-gate #define H_ORIGINAL 0x0080 /* Bucket lock existed on entry. */ 83*0Sstevel@tonic-gate u_int32_t flags; 84*0Sstevel@tonic-gate } HASH_CURSOR; 85*0Sstevel@tonic-gate 86*0Sstevel@tonic-gate #define IS_VALID(C) ((C)->bucket != BUCKET_INVALID) 87*0Sstevel@tonic-gate 88*0Sstevel@tonic-gate #define SAVE_CURSOR(ORIG, COPY) { \ 89*0Sstevel@tonic-gate F_SET((ORIG), H_ORIGINAL); \ 90*0Sstevel@tonic-gate *(COPY) = *(ORIG); \ 91*0Sstevel@tonic-gate } 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate #define RESTORE_CURSOR(D, ORIG, COPY, RET) { \ 94*0Sstevel@tonic-gate if ((RET) == 0) { \ 95*0Sstevel@tonic-gate if ((ORIG)->dbc->txn == NULL && \ 96*0Sstevel@tonic-gate (COPY)->lock != 0 && (ORIG)->lock != (COPY)->lock) \ 97*0Sstevel@tonic-gate (void)lock_put((D)->dbenv->lk_info, (COPY)->lock); \ 98*0Sstevel@tonic-gate } else { \ 99*0Sstevel@tonic-gate if ((ORIG)->dbc->txn == NULL && \ 100*0Sstevel@tonic-gate (ORIG)->lock != 0 && (ORIG)->lock != (COPY)->lock) \ 101*0Sstevel@tonic-gate (void)lock_put((D)->dbenv->lk_info, (ORIG)->lock); \ 102*0Sstevel@tonic-gate *ORIG = *COPY; \ 103*0Sstevel@tonic-gate } \ 104*0Sstevel@tonic-gate } 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate /* 107*0Sstevel@tonic-gate * More interface macros used to get/release the meta data page. 108*0Sstevel@tonic-gate */ 109*0Sstevel@tonic-gate #define GET_META(D, I, R) { \ 110*0Sstevel@tonic-gate if (F_ISSET(D, DB_AM_LOCKING) && \ 111*0Sstevel@tonic-gate !F_ISSET((I)->dbc, DBC_RECOVER)) { \ 112*0Sstevel@tonic-gate (I)->dbc->lock.pgno = BUCKET_INVALID; \ 113*0Sstevel@tonic-gate (R) = lock_get((D)->dbenv->lk_info, (I)->dbc->locker, \ 114*0Sstevel@tonic-gate 0, &(I)->dbc->lock_dbt, DB_LOCK_READ, &(I)->hlock); \ 115*0Sstevel@tonic-gate (R) = (R) < 0 ? EAGAIN : (R); \ 116*0Sstevel@tonic-gate } \ 117*0Sstevel@tonic-gate if ((R) == 0 && \ 118*0Sstevel@tonic-gate ((R) = __ham_get_page(D, 0, (PAGE **)&((I)->hdr))) != 0 && \ 119*0Sstevel@tonic-gate (I)->hlock != LOCK_INVALID) { \ 120*0Sstevel@tonic-gate (void)lock_put((D)->dbenv->lk_info, (I)->hlock); \ 121*0Sstevel@tonic-gate (I)->hlock = LOCK_INVALID; \ 122*0Sstevel@tonic-gate } \ 123*0Sstevel@tonic-gate } 124*0Sstevel@tonic-gate 125*0Sstevel@tonic-gate #define RELEASE_META(D, I) { \ 126*0Sstevel@tonic-gate if ((I)->hdr) \ 127*0Sstevel@tonic-gate (void)__ham_put_page(D, (PAGE *)(I)->hdr, \ 128*0Sstevel@tonic-gate F_ISSET(I, H_DIRTY) ? 1 : 0); \ 129*0Sstevel@tonic-gate (I)->hdr = NULL; \ 130*0Sstevel@tonic-gate if (!F_ISSET((I)->dbc, DBC_RECOVER) && \ 131*0Sstevel@tonic-gate (I)->dbc->txn == NULL && (I)->hlock) \ 132*0Sstevel@tonic-gate (void)lock_put((D)->dbenv->lk_info, (I)->hlock); \ 133*0Sstevel@tonic-gate (I)->hlock = LOCK_INVALID; \ 134*0Sstevel@tonic-gate F_CLR(I, H_DIRTY); \ 135*0Sstevel@tonic-gate } 136*0Sstevel@tonic-gate 137*0Sstevel@tonic-gate #define DIRTY_META(D, I, R) { \ 138*0Sstevel@tonic-gate if (F_ISSET(D, DB_AM_LOCKING) && \ 139*0Sstevel@tonic-gate !F_ISSET((I)->dbc, DBC_RECOVER)) { \ 140*0Sstevel@tonic-gate DB_LOCK _tmp; \ 141*0Sstevel@tonic-gate (I)->dbc->lock.pgno = BUCKET_INVALID; \ 142*0Sstevel@tonic-gate if (((R) = lock_get((D)->dbenv->lk_info, \ 143*0Sstevel@tonic-gate (I)->dbc->locker, 0, &(I)->dbc->lock_dbt, \ 144*0Sstevel@tonic-gate DB_LOCK_WRITE, &_tmp)) == 0) \ 145*0Sstevel@tonic-gate (R) = lock_put((D)->dbenv->lk_info, (I)->hlock);\ 146*0Sstevel@tonic-gate else if ((R) < 0) \ 147*0Sstevel@tonic-gate (R) = EAGAIN; \ 148*0Sstevel@tonic-gate (I)->hlock = _tmp; \ 149*0Sstevel@tonic-gate } \ 150*0Sstevel@tonic-gate F_SET((I), H_DIRTY); \ 151*0Sstevel@tonic-gate } 152*0Sstevel@tonic-gate 153*0Sstevel@tonic-gate /* Test string. */ 154*0Sstevel@tonic-gate #define CHARKEY "%$sniglet^&" 155*0Sstevel@tonic-gate 156*0Sstevel@tonic-gate /* Overflow management */ 157*0Sstevel@tonic-gate /* 158*0Sstevel@tonic-gate * Overflow page numbers are allocated per split point. At each doubling of 159*0Sstevel@tonic-gate * the table, we can allocate extra pages. We keep track of how many pages 160*0Sstevel@tonic-gate * we've allocated at each point to calculate bucket to page number mapping. 161*0Sstevel@tonic-gate */ 162*0Sstevel@tonic-gate #define BUCKET_TO_PAGE(I, B) \ 163*0Sstevel@tonic-gate ((B) + 1 + ((B) ? (I)->hdr->spares[__db_log2((B)+1)-1] : 0)) 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate #define PGNO_OF(I, S, O) (BUCKET_TO_PAGE((I), (1 << (S)) - 1) + (O)) 166*0Sstevel@tonic-gate 167*0Sstevel@tonic-gate /* Constraints about number of pages and how much data goes on a page. */ 168*0Sstevel@tonic-gate 169*0Sstevel@tonic-gate #define MAX_PAGES(H) UINT32_T_MAX 170*0Sstevel@tonic-gate #define MINFILL 4 171*0Sstevel@tonic-gate #define ISBIG(I, N) (((N) > ((I)->hdr->pagesize / MINFILL)) ? 1 : 0) 172*0Sstevel@tonic-gate 173*0Sstevel@tonic-gate /* Shorthands for accessing structure */ 174*0Sstevel@tonic-gate #define NDX_INVALID 0xFFFF 175*0Sstevel@tonic-gate #define BUCKET_INVALID 0xFFFFFFFF 176*0Sstevel@tonic-gate 177*0Sstevel@tonic-gate /* On page duplicates are stored as a string of size-data-size triples. */ 178*0Sstevel@tonic-gate #define DUP_SIZE(len) ((len) + 2 * sizeof(db_indx_t)) 179*0Sstevel@tonic-gate 180*0Sstevel@tonic-gate /* Log messages types (these are subtypes within a record type) */ 181*0Sstevel@tonic-gate #define PAIR_KEYMASK 0x1 182*0Sstevel@tonic-gate #define PAIR_DATAMASK 0x2 183*0Sstevel@tonic-gate #define PAIR_ISKEYBIG(N) (N & PAIR_KEYMASK) 184*0Sstevel@tonic-gate #define PAIR_ISDATABIG(N) (N & PAIR_DATAMASK) 185*0Sstevel@tonic-gate #define OPCODE_OF(N) (N & ~(PAIR_KEYMASK | PAIR_DATAMASK)) 186*0Sstevel@tonic-gate 187*0Sstevel@tonic-gate #define PUTPAIR 0x20 188*0Sstevel@tonic-gate #define DELPAIR 0x30 189*0Sstevel@tonic-gate #define PUTOVFL 0x40 190*0Sstevel@tonic-gate #define DELOVFL 0x50 191*0Sstevel@tonic-gate #define ALLOCPGNO 0x60 192*0Sstevel@tonic-gate #define DELPGNO 0x70 193*0Sstevel@tonic-gate #define SPLITOLD 0x80 194*0Sstevel@tonic-gate #define SPLITNEW 0x90 195*0Sstevel@tonic-gate 196*0Sstevel@tonic-gate #include "hash_auto.h" 197*0Sstevel@tonic-gate #include "hash_ext.h" 198*0Sstevel@tonic-gate #include "db_am.h" 199*0Sstevel@tonic-gate #include "common_ext.h" 200