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