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
47*0Sstevel@tonic-gate #include "config.h"
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gate #ifndef lint
50*0Sstevel@tonic-gate static const char sccsid[] = "@(#)hash.c 10.63 (Sleepycat) 12/11/98";
51*0Sstevel@tonic-gate #endif /* not lint */
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gate #ifndef NO_SYSTEM_INCLUDES
54*0Sstevel@tonic-gate #include <sys/types.h>
55*0Sstevel@tonic-gate
56*0Sstevel@tonic-gate #include <errno.h>
57*0Sstevel@tonic-gate #include <stdlib.h>
58*0Sstevel@tonic-gate #include <string.h>
59*0Sstevel@tonic-gate #endif
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gate #include "db_int.h"
62*0Sstevel@tonic-gate #include "shqueue.h"
63*0Sstevel@tonic-gate #include "db_page.h"
64*0Sstevel@tonic-gate #include "db_am.h"
65*0Sstevel@tonic-gate #include "db_ext.h"
66*0Sstevel@tonic-gate #include "hash.h"
67*0Sstevel@tonic-gate #include "btree.h"
68*0Sstevel@tonic-gate #include "log.h"
69*0Sstevel@tonic-gate #include "db_shash.h"
70*0Sstevel@tonic-gate #include "lock.h"
71*0Sstevel@tonic-gate #include "lock_ext.h"
72*0Sstevel@tonic-gate
73*0Sstevel@tonic-gate static int __ham_c_close __P((DBC *));
74*0Sstevel@tonic-gate static int __ham_c_del __P((DBC *, u_int32_t));
75*0Sstevel@tonic-gate static int __ham_c_destroy __P((DBC *));
76*0Sstevel@tonic-gate static int __ham_c_get __P((DBC *, DBT *, DBT *, u_int32_t));
77*0Sstevel@tonic-gate static int __ham_c_put __P((DBC *, DBT *, DBT *, u_int32_t));
78*0Sstevel@tonic-gate static int __ham_delete __P((DB *, DB_TXN *, DBT *, u_int32_t));
79*0Sstevel@tonic-gate static int __ham_dup_return __P((DBC *, DBT *, u_int32_t));
80*0Sstevel@tonic-gate static int __ham_expand_table __P((DBC *));
81*0Sstevel@tonic-gate static void __ham_init_htab __P((DBC *, u_int32_t, u_int32_t));
82*0Sstevel@tonic-gate static int __ham_lookup __P((DBC *, const DBT *, u_int32_t, db_lockmode_t));
83*0Sstevel@tonic-gate static int __ham_overwrite __P((DBC *, DBT *));
84*0Sstevel@tonic-gate
85*0Sstevel@tonic-gate /************************** INTERFACE ROUTINES ***************************/
86*0Sstevel@tonic-gate /* OPEN/CLOSE */
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gate /*
89*0Sstevel@tonic-gate * __ham_open --
90*0Sstevel@tonic-gate *
91*0Sstevel@tonic-gate * PUBLIC: int __ham_open __P((DB *, DB_INFO *));
92*0Sstevel@tonic-gate */
93*0Sstevel@tonic-gate int
__ham_open(dbp,dbinfo)94*0Sstevel@tonic-gate __ham_open(dbp, dbinfo)
95*0Sstevel@tonic-gate DB *dbp;
96*0Sstevel@tonic-gate DB_INFO *dbinfo;
97*0Sstevel@tonic-gate {
98*0Sstevel@tonic-gate DB_ENV *dbenv;
99*0Sstevel@tonic-gate DBC *dbc;
100*0Sstevel@tonic-gate HASH_CURSOR *hcp;
101*0Sstevel@tonic-gate int file_existed, ret;
102*0Sstevel@tonic-gate
103*0Sstevel@tonic-gate dbc = NULL;
104*0Sstevel@tonic-gate dbenv = dbp->dbenv;
105*0Sstevel@tonic-gate
106*0Sstevel@tonic-gate /* Set the hash function if specified by the user. */
107*0Sstevel@tonic-gate if (dbinfo != NULL && dbinfo->h_hash != NULL)
108*0Sstevel@tonic-gate dbp->h_hash = dbinfo->h_hash;
109*0Sstevel@tonic-gate
110*0Sstevel@tonic-gate /*
111*0Sstevel@tonic-gate * Initialize the remaining fields of the dbp. The only function
112*0Sstevel@tonic-gate * that differs from the default set is __ham_stat().
113*0Sstevel@tonic-gate */
114*0Sstevel@tonic-gate dbp->internal = NULL;
115*0Sstevel@tonic-gate dbp->am_close = __ham_close;
116*0Sstevel@tonic-gate dbp->del = __ham_delete;
117*0Sstevel@tonic-gate dbp->stat = __ham_stat;
118*0Sstevel@tonic-gate
119*0Sstevel@tonic-gate /* Get a cursor we can use for the rest of this function. */
120*0Sstevel@tonic-gate if ((ret = dbp->cursor(dbp, NULL, &dbc, 0)) != 0)
121*0Sstevel@tonic-gate goto out;
122*0Sstevel@tonic-gate
123*0Sstevel@tonic-gate hcp = (HASH_CURSOR *)dbc->internal;
124*0Sstevel@tonic-gate GET_META(dbp, hcp, ret);
125*0Sstevel@tonic-gate if (ret != 0)
126*0Sstevel@tonic-gate goto out;
127*0Sstevel@tonic-gate
128*0Sstevel@tonic-gate /*
129*0Sstevel@tonic-gate * If this is a new file, initialize it, and put it back dirty.
130*0Sstevel@tonic-gate */
131*0Sstevel@tonic-gate
132*0Sstevel@tonic-gate /* Initialize the hdr structure */
133*0Sstevel@tonic-gate if (hcp->hdr->magic == DB_HASHMAGIC) {
134*0Sstevel@tonic-gate file_existed = 1;
135*0Sstevel@tonic-gate /* File exists, verify the data in the header. */
136*0Sstevel@tonic-gate if (dbp->h_hash == NULL)
137*0Sstevel@tonic-gate dbp->h_hash =
138*0Sstevel@tonic-gate hcp->hdr->version < 5 ? __ham_func4 : __ham_func5;
139*0Sstevel@tonic-gate if (dbp->h_hash(CHARKEY, sizeof(CHARKEY)) !=
140*0Sstevel@tonic-gate hcp->hdr->h_charkey) {
141*0Sstevel@tonic-gate __db_err(dbp->dbenv, "hash: incompatible hash function");
142*0Sstevel@tonic-gate ret = EINVAL;
143*0Sstevel@tonic-gate goto out;
144*0Sstevel@tonic-gate }
145*0Sstevel@tonic-gate if (F_ISSET(hcp->hdr, DB_HASH_DUP))
146*0Sstevel@tonic-gate F_SET(dbp, DB_AM_DUP);
147*0Sstevel@tonic-gate } else {
148*0Sstevel@tonic-gate /*
149*0Sstevel@tonic-gate * File does not exist, we must initialize the header. If
150*0Sstevel@tonic-gate * locking is enabled that means getting a write lock first.
151*0Sstevel@tonic-gate */
152*0Sstevel@tonic-gate file_existed = 0;
153*0Sstevel@tonic-gate if (F_ISSET(dbp, DB_AM_LOCKING) &&
154*0Sstevel@tonic-gate ((ret = lock_put(dbenv->lk_info, hcp->hlock)) != 0 ||
155*0Sstevel@tonic-gate (ret = lock_get(dbenv->lk_info, dbc->locker, 0,
156*0Sstevel@tonic-gate &dbc->lock_dbt, DB_LOCK_WRITE, &hcp->hlock)) != 0)) {
157*0Sstevel@tonic-gate if (ret < 0)
158*0Sstevel@tonic-gate ret = EAGAIN;
159*0Sstevel@tonic-gate goto out;
160*0Sstevel@tonic-gate }
161*0Sstevel@tonic-gate
162*0Sstevel@tonic-gate __ham_init_htab(dbc, dbinfo != NULL ? dbinfo->h_nelem : 0,
163*0Sstevel@tonic-gate dbinfo != NULL ? dbinfo->h_ffactor : 0);
164*0Sstevel@tonic-gate if (F_ISSET(dbp, DB_AM_DUP))
165*0Sstevel@tonic-gate F_SET(hcp->hdr, DB_HASH_DUP);
166*0Sstevel@tonic-gate if ((ret = __ham_dirty_page(dbp, (PAGE *)hcp->hdr)) != 0)
167*0Sstevel@tonic-gate goto out;
168*0Sstevel@tonic-gate }
169*0Sstevel@tonic-gate
170*0Sstevel@tonic-gate /* Release the meta data page */
171*0Sstevel@tonic-gate RELEASE_META(dbp, hcp);
172*0Sstevel@tonic-gate if ((ret = dbc->c_close(dbc)) != 0)
173*0Sstevel@tonic-gate goto out;
174*0Sstevel@tonic-gate
175*0Sstevel@tonic-gate /* Sync the file so that we know that the meta data goes to disk. */
176*0Sstevel@tonic-gate if (!file_existed && (ret = dbp->sync(dbp, 0)) != 0)
177*0Sstevel@tonic-gate goto out;
178*0Sstevel@tonic-gate return (0);
179*0Sstevel@tonic-gate
180*0Sstevel@tonic-gate out: (void)__ham_close(dbp);
181*0Sstevel@tonic-gate return (ret);
182*0Sstevel@tonic-gate }
183*0Sstevel@tonic-gate
184*0Sstevel@tonic-gate /*
185*0Sstevel@tonic-gate * PUBLIC: int __ham_close __P((DB *));
186*0Sstevel@tonic-gate */
187*0Sstevel@tonic-gate int
__ham_close(dbp)188*0Sstevel@tonic-gate __ham_close(dbp)
189*0Sstevel@tonic-gate DB *dbp;
190*0Sstevel@tonic-gate {
191*0Sstevel@tonic-gate COMPQUIET(dbp, NULL);
192*0Sstevel@tonic-gate return (0);
193*0Sstevel@tonic-gate }
194*0Sstevel@tonic-gate
195*0Sstevel@tonic-gate /************************** LOCAL CREATION ROUTINES **********************/
196*0Sstevel@tonic-gate /*
197*0Sstevel@tonic-gate * Returns 0 on No Error
198*0Sstevel@tonic-gate */
199*0Sstevel@tonic-gate static void
__ham_init_htab(dbc,nelem,ffactor)200*0Sstevel@tonic-gate __ham_init_htab(dbc, nelem, ffactor)
201*0Sstevel@tonic-gate DBC *dbc;
202*0Sstevel@tonic-gate u_int32_t nelem, ffactor;
203*0Sstevel@tonic-gate {
204*0Sstevel@tonic-gate DB *dbp;
205*0Sstevel@tonic-gate HASH_CURSOR *hcp;
206*0Sstevel@tonic-gate int32_t l2, nbuckets;
207*0Sstevel@tonic-gate
208*0Sstevel@tonic-gate hcp = (HASH_CURSOR *)dbc->internal;
209*0Sstevel@tonic-gate dbp = dbc->dbp;
210*0Sstevel@tonic-gate memset(hcp->hdr, 0, sizeof(HASHHDR));
211*0Sstevel@tonic-gate hcp->hdr->ffactor = ffactor;
212*0Sstevel@tonic-gate hcp->hdr->pagesize = dbp->pgsize;
213*0Sstevel@tonic-gate ZERO_LSN(hcp->hdr->lsn);
214*0Sstevel@tonic-gate hcp->hdr->magic = DB_HASHMAGIC;
215*0Sstevel@tonic-gate hcp->hdr->version = DB_HASHVERSION;
216*0Sstevel@tonic-gate
217*0Sstevel@tonic-gate if (dbp->h_hash == NULL)
218*0Sstevel@tonic-gate dbp->h_hash = hcp->hdr->version < 5 ? __ham_func4 : __ham_func5;
219*0Sstevel@tonic-gate hcp->hdr->h_charkey = dbp->h_hash(CHARKEY, sizeof(CHARKEY));
220*0Sstevel@tonic-gate if (nelem != 0 && hcp->hdr->ffactor != 0) {
221*0Sstevel@tonic-gate nelem = (nelem - 1) / hcp->hdr->ffactor + 1;
222*0Sstevel@tonic-gate l2 = __db_log2(nelem > 2 ? nelem : 2);
223*0Sstevel@tonic-gate } else
224*0Sstevel@tonic-gate l2 = 2;
225*0Sstevel@tonic-gate
226*0Sstevel@tonic-gate nbuckets = 1 << l2;
227*0Sstevel@tonic-gate
228*0Sstevel@tonic-gate hcp->hdr->ovfl_point = l2;
229*0Sstevel@tonic-gate hcp->hdr->last_freed = PGNO_INVALID;
230*0Sstevel@tonic-gate
231*0Sstevel@tonic-gate hcp->hdr->max_bucket = hcp->hdr->high_mask = nbuckets - 1;
232*0Sstevel@tonic-gate hcp->hdr->low_mask = (nbuckets >> 1) - 1;
233*0Sstevel@tonic-gate memcpy(hcp->hdr->uid, dbp->fileid, DB_FILE_ID_LEN);
234*0Sstevel@tonic-gate }
235*0Sstevel@tonic-gate
236*0Sstevel@tonic-gate static int
__ham_delete(dbp,txn,key,flags)237*0Sstevel@tonic-gate __ham_delete(dbp, txn, key, flags)
238*0Sstevel@tonic-gate DB *dbp;
239*0Sstevel@tonic-gate DB_TXN *txn;
240*0Sstevel@tonic-gate DBT *key;
241*0Sstevel@tonic-gate u_int32_t flags;
242*0Sstevel@tonic-gate {
243*0Sstevel@tonic-gate DBC *dbc;
244*0Sstevel@tonic-gate HASH_CURSOR *hcp;
245*0Sstevel@tonic-gate int ret, tret;
246*0Sstevel@tonic-gate
247*0Sstevel@tonic-gate DB_PANIC_CHECK(dbp);
248*0Sstevel@tonic-gate
249*0Sstevel@tonic-gate if ((ret =
250*0Sstevel@tonic-gate __db_delchk(dbp, key, flags, F_ISSET(dbp, DB_AM_RDONLY))) != 0)
251*0Sstevel@tonic-gate return (ret);
252*0Sstevel@tonic-gate
253*0Sstevel@tonic-gate if ((ret = dbp->cursor(dbp, txn, &dbc, DB_WRITELOCK)) != 0)
254*0Sstevel@tonic-gate return (ret);
255*0Sstevel@tonic-gate
256*0Sstevel@tonic-gate DEBUG_LWRITE(dbc, txn, "ham_delete", key, NULL, flags);
257*0Sstevel@tonic-gate
258*0Sstevel@tonic-gate hcp = (HASH_CURSOR *)dbc->internal;
259*0Sstevel@tonic-gate GET_META(dbp, hcp, ret);
260*0Sstevel@tonic-gate if (ret != 0)
261*0Sstevel@tonic-gate goto out;
262*0Sstevel@tonic-gate
263*0Sstevel@tonic-gate hcp->stats.hash_deleted++;
264*0Sstevel@tonic-gate if ((ret = __ham_lookup(dbc, key, 0, DB_LOCK_WRITE)) == 0)
265*0Sstevel@tonic-gate if (F_ISSET(hcp, H_OK))
266*0Sstevel@tonic-gate ret = __ham_del_pair(dbc, 1);
267*0Sstevel@tonic-gate else
268*0Sstevel@tonic-gate ret = DB_NOTFOUND;
269*0Sstevel@tonic-gate
270*0Sstevel@tonic-gate RELEASE_META(dbp, hcp);
271*0Sstevel@tonic-gate out: if ((tret = dbc->c_close(dbc)) != 0 && ret == 0)
272*0Sstevel@tonic-gate ret = tret;
273*0Sstevel@tonic-gate return (ret);
274*0Sstevel@tonic-gate }
275*0Sstevel@tonic-gate
276*0Sstevel@tonic-gate /* ****************** CURSORS ********************************** */
277*0Sstevel@tonic-gate /*
278*0Sstevel@tonic-gate * __ham_c_init --
279*0Sstevel@tonic-gate * Initialize the hash-specific portion of a cursor.
280*0Sstevel@tonic-gate *
281*0Sstevel@tonic-gate * PUBLIC: int __ham_c_init __P((DBC *));
282*0Sstevel@tonic-gate */
283*0Sstevel@tonic-gate int
__ham_c_init(dbc)284*0Sstevel@tonic-gate __ham_c_init(dbc)
285*0Sstevel@tonic-gate DBC *dbc;
286*0Sstevel@tonic-gate {
287*0Sstevel@tonic-gate HASH_CURSOR *new_curs;
288*0Sstevel@tonic-gate int ret;
289*0Sstevel@tonic-gate
290*0Sstevel@tonic-gate if ((ret = __os_calloc(1, sizeof(struct cursor_t), &new_curs)) != 0)
291*0Sstevel@tonic-gate return (ret);
292*0Sstevel@tonic-gate if ((ret =
293*0Sstevel@tonic-gate __os_malloc(dbc->dbp->pgsize, NULL, &new_curs->split_buf)) != 0) {
294*0Sstevel@tonic-gate __os_free(new_curs, sizeof(*new_curs));
295*0Sstevel@tonic-gate return (ret);
296*0Sstevel@tonic-gate }
297*0Sstevel@tonic-gate
298*0Sstevel@tonic-gate new_curs->dbc = dbc;
299*0Sstevel@tonic-gate
300*0Sstevel@tonic-gate dbc->internal = new_curs;
301*0Sstevel@tonic-gate dbc->c_am_close = __ham_c_close;
302*0Sstevel@tonic-gate dbc->c_am_destroy = __ham_c_destroy;
303*0Sstevel@tonic-gate dbc->c_del = __ham_c_del;
304*0Sstevel@tonic-gate dbc->c_get = __ham_c_get;
305*0Sstevel@tonic-gate dbc->c_put = __ham_c_put;
306*0Sstevel@tonic-gate
307*0Sstevel@tonic-gate __ham_item_init(new_curs);
308*0Sstevel@tonic-gate
309*0Sstevel@tonic-gate return (0);
310*0Sstevel@tonic-gate }
311*0Sstevel@tonic-gate
312*0Sstevel@tonic-gate /*
313*0Sstevel@tonic-gate * __ham_c_close --
314*0Sstevel@tonic-gate * Close down the cursor from a single use.
315*0Sstevel@tonic-gate */
316*0Sstevel@tonic-gate static int
__ham_c_close(dbc)317*0Sstevel@tonic-gate __ham_c_close(dbc)
318*0Sstevel@tonic-gate DBC *dbc;
319*0Sstevel@tonic-gate {
320*0Sstevel@tonic-gate int ret;
321*0Sstevel@tonic-gate
322*0Sstevel@tonic-gate if ((ret = __ham_item_done(dbc, 0)) != 0)
323*0Sstevel@tonic-gate return (ret);
324*0Sstevel@tonic-gate
325*0Sstevel@tonic-gate __ham_item_init((HASH_CURSOR *)dbc->internal);
326*0Sstevel@tonic-gate return (0);
327*0Sstevel@tonic-gate }
328*0Sstevel@tonic-gate
329*0Sstevel@tonic-gate /*
330*0Sstevel@tonic-gate * __ham_c_destroy --
331*0Sstevel@tonic-gate * Cleanup the access method private part of a cursor.
332*0Sstevel@tonic-gate */
333*0Sstevel@tonic-gate static int
__ham_c_destroy(dbc)334*0Sstevel@tonic-gate __ham_c_destroy(dbc)
335*0Sstevel@tonic-gate DBC *dbc;
336*0Sstevel@tonic-gate {
337*0Sstevel@tonic-gate HASH_CURSOR *hcp;
338*0Sstevel@tonic-gate
339*0Sstevel@tonic-gate hcp = (HASH_CURSOR *)dbc->internal;
340*0Sstevel@tonic-gate if (hcp->split_buf != NULL)
341*0Sstevel@tonic-gate __os_free(hcp->split_buf, dbc->dbp->pgsize);
342*0Sstevel@tonic-gate __os_free(hcp, sizeof(HASH_CURSOR));
343*0Sstevel@tonic-gate
344*0Sstevel@tonic-gate return (0);
345*0Sstevel@tonic-gate }
346*0Sstevel@tonic-gate
347*0Sstevel@tonic-gate static int
__ham_c_del(dbc,flags)348*0Sstevel@tonic-gate __ham_c_del(dbc, flags)
349*0Sstevel@tonic-gate DBC *dbc;
350*0Sstevel@tonic-gate u_int32_t flags;
351*0Sstevel@tonic-gate {
352*0Sstevel@tonic-gate DB *dbp;
353*0Sstevel@tonic-gate DBT repldbt;
354*0Sstevel@tonic-gate HASH_CURSOR *hcp;
355*0Sstevel@tonic-gate HASH_CURSOR save_curs;
356*0Sstevel@tonic-gate db_pgno_t ppgno, chg_pgno;
357*0Sstevel@tonic-gate int ret, t_ret;
358*0Sstevel@tonic-gate
359*0Sstevel@tonic-gate DEBUG_LWRITE(dbc, dbc->txn, "ham_c_del", NULL, NULL, flags);
360*0Sstevel@tonic-gate dbp = dbc->dbp;
361*0Sstevel@tonic-gate DB_PANIC_CHECK(dbp);
362*0Sstevel@tonic-gate hcp = (HASH_CURSOR *)dbc->internal;
363*0Sstevel@tonic-gate
364*0Sstevel@tonic-gate if ((ret = __db_cdelchk(dbc->dbp, flags,
365*0Sstevel@tonic-gate F_ISSET(dbc->dbp, DB_AM_RDONLY), IS_VALID(hcp))) != 0)
366*0Sstevel@tonic-gate return (ret);
367*0Sstevel@tonic-gate
368*0Sstevel@tonic-gate if (F_ISSET(hcp, H_DELETED))
369*0Sstevel@tonic-gate return (DB_NOTFOUND);
370*0Sstevel@tonic-gate
371*0Sstevel@tonic-gate /*
372*0Sstevel@tonic-gate * If we are in the concurrent DB product and this cursor
373*0Sstevel@tonic-gate * is not a write cursor, then this request is invalid.
374*0Sstevel@tonic-gate * If it is a simple write cursor, then we need to upgrade its
375*0Sstevel@tonic-gate * lock.
376*0Sstevel@tonic-gate */
377*0Sstevel@tonic-gate if (F_ISSET(dbp, DB_AM_CDB)) {
378*0Sstevel@tonic-gate /* Make sure it's a valid update cursor. */
379*0Sstevel@tonic-gate if (!F_ISSET(dbc, DBC_RMW | DBC_WRITER))
380*0Sstevel@tonic-gate return (EINVAL);
381*0Sstevel@tonic-gate
382*0Sstevel@tonic-gate if (F_ISSET(dbc, DBC_RMW) &&
383*0Sstevel@tonic-gate (ret = lock_get(dbp->dbenv->lk_info, dbc->locker,
384*0Sstevel@tonic-gate DB_LOCK_UPGRADE, &dbc->lock_dbt, DB_LOCK_WRITE,
385*0Sstevel@tonic-gate &dbc->mylock)) != 0)
386*0Sstevel@tonic-gate return (EAGAIN);
387*0Sstevel@tonic-gate }
388*0Sstevel@tonic-gate
389*0Sstevel@tonic-gate GET_META(dbp, hcp, ret);
390*0Sstevel@tonic-gate if (ret != 0)
391*0Sstevel@tonic-gate return (ret);
392*0Sstevel@tonic-gate
393*0Sstevel@tonic-gate SAVE_CURSOR(hcp, &save_curs);
394*0Sstevel@tonic-gate hcp->stats.hash_deleted++;
395*0Sstevel@tonic-gate
396*0Sstevel@tonic-gate if ((ret = __ham_get_cpage(dbc, DB_LOCK_WRITE)) != 0)
397*0Sstevel@tonic-gate goto out;
398*0Sstevel@tonic-gate if (F_ISSET(hcp, H_ISDUP) && hcp->dpgno != PGNO_INVALID) {
399*0Sstevel@tonic-gate /*
400*0Sstevel@tonic-gate * We are about to remove a duplicate from offpage.
401*0Sstevel@tonic-gate *
402*0Sstevel@tonic-gate * There are 4 cases.
403*0Sstevel@tonic-gate * 1. We will remove an item on a page, but there are more
404*0Sstevel@tonic-gate * items on that page.
405*0Sstevel@tonic-gate * 2. We will remove the last item on a page, but there is a
406*0Sstevel@tonic-gate * following page of duplicates.
407*0Sstevel@tonic-gate * 3. We will remove the last item on a page, this page was the
408*0Sstevel@tonic-gate * last page in a duplicate set, but there were dups before
409*0Sstevel@tonic-gate * it.
410*0Sstevel@tonic-gate * 4. We will remove the last item on a page, removing the last
411*0Sstevel@tonic-gate * duplicate.
412*0Sstevel@tonic-gate * In case 1 hcp->dpagep is unchanged.
413*0Sstevel@tonic-gate * In case 2 hcp->dpagep comes back pointing to the next dup
414*0Sstevel@tonic-gate * page.
415*0Sstevel@tonic-gate * In case 3 hcp->dpagep comes back NULL.
416*0Sstevel@tonic-gate * In case 4 hcp->dpagep comes back NULL.
417*0Sstevel@tonic-gate *
418*0Sstevel@tonic-gate * Case 4 results in deleting the pair off the master page.
419*0Sstevel@tonic-gate * The normal code for doing this knows how to delete the
420*0Sstevel@tonic-gate * duplicates, so we will handle this case in the normal code.
421*0Sstevel@tonic-gate */
422*0Sstevel@tonic-gate ppgno = PREV_PGNO(hcp->dpagep);
423*0Sstevel@tonic-gate if (ppgno == PGNO_INVALID &&
424*0Sstevel@tonic-gate NEXT_PGNO(hcp->dpagep) == PGNO_INVALID &&
425*0Sstevel@tonic-gate NUM_ENT(hcp->dpagep) == 1)
426*0Sstevel@tonic-gate goto normal;
427*0Sstevel@tonic-gate
428*0Sstevel@tonic-gate /* Remove item from duplicate page. */
429*0Sstevel@tonic-gate chg_pgno = hcp->dpgno;
430*0Sstevel@tonic-gate if ((ret = __db_drem(dbc,
431*0Sstevel@tonic-gate &hcp->dpagep, hcp->dndx, __ham_del_page)) != 0)
432*0Sstevel@tonic-gate goto out;
433*0Sstevel@tonic-gate
434*0Sstevel@tonic-gate if (hcp->dpagep == NULL) {
435*0Sstevel@tonic-gate if (ppgno != PGNO_INVALID) { /* Case 3 */
436*0Sstevel@tonic-gate hcp->dpgno = ppgno;
437*0Sstevel@tonic-gate if ((ret = __ham_get_cpage(dbc,
438*0Sstevel@tonic-gate DB_LOCK_READ)) != 0)
439*0Sstevel@tonic-gate goto out;
440*0Sstevel@tonic-gate hcp->dndx = NUM_ENT(hcp->dpagep);
441*0Sstevel@tonic-gate F_SET(hcp, H_DELETED);
442*0Sstevel@tonic-gate } else { /* Case 4 */
443*0Sstevel@tonic-gate ret = __ham_del_pair(dbc, 1);
444*0Sstevel@tonic-gate hcp->dpgno = PGNO_INVALID;
445*0Sstevel@tonic-gate /*
446*0Sstevel@tonic-gate * Delpair updated the cursor queue, so we
447*0Sstevel@tonic-gate * don't have to do that here.
448*0Sstevel@tonic-gate */
449*0Sstevel@tonic-gate chg_pgno = PGNO_INVALID;
450*0Sstevel@tonic-gate }
451*0Sstevel@tonic-gate } else if (PGNO(hcp->dpagep) != hcp->dpgno) {
452*0Sstevel@tonic-gate hcp->dndx = 0; /* Case 2 */
453*0Sstevel@tonic-gate hcp->dpgno = PGNO(hcp->dpagep);
454*0Sstevel@tonic-gate if (ppgno == PGNO_INVALID)
455*0Sstevel@tonic-gate memcpy(HOFFDUP_PGNO(P_ENTRY(hcp->pagep,
456*0Sstevel@tonic-gate H_DATAINDEX(hcp->bndx))),
457*0Sstevel@tonic-gate &hcp->dpgno, sizeof(db_pgno_t));
458*0Sstevel@tonic-gate /*
459*0Sstevel@tonic-gate * We need to put the master page here, because
460*0Sstevel@tonic-gate * although we have a duplicate page, the master
461*0Sstevel@tonic-gate * page is dirty, and ham_item_done assumes that
462*0Sstevel@tonic-gate * if you have a duplicate page, it's the only one
463*0Sstevel@tonic-gate * that can be dirty.
464*0Sstevel@tonic-gate */
465*0Sstevel@tonic-gate ret = __ham_put_page(dbp, hcp->pagep, 1);
466*0Sstevel@tonic-gate hcp->pagep = NULL;
467*0Sstevel@tonic-gate F_SET(hcp, H_DELETED);
468*0Sstevel@tonic-gate } else /* Case 1 */
469*0Sstevel@tonic-gate F_SET(hcp, H_DELETED);
470*0Sstevel@tonic-gate if (chg_pgno != PGNO_INVALID)
471*0Sstevel@tonic-gate __ham_c_update(hcp, chg_pgno, 0, 0, 1);
472*0Sstevel@tonic-gate } else if (F_ISSET(hcp, H_ISDUP)) { /* on page */
473*0Sstevel@tonic-gate if (hcp->dup_off == 0 && DUP_SIZE(hcp->dup_len) ==
474*0Sstevel@tonic-gate LEN_HDATA(hcp->pagep, hcp->hdr->pagesize, hcp->bndx))
475*0Sstevel@tonic-gate ret = __ham_del_pair(dbc, 1);
476*0Sstevel@tonic-gate else {
477*0Sstevel@tonic-gate repldbt.flags = 0;
478*0Sstevel@tonic-gate F_SET(&repldbt, DB_DBT_PARTIAL);
479*0Sstevel@tonic-gate repldbt.doff = hcp->dup_off;
480*0Sstevel@tonic-gate repldbt.dlen = DUP_SIZE(hcp->dup_len);
481*0Sstevel@tonic-gate repldbt.size = 0;
482*0Sstevel@tonic-gate repldbt.data =
483*0Sstevel@tonic-gate HKEYDATA_DATA(H_PAIRDATA(hcp->pagep, hcp->bndx));
484*0Sstevel@tonic-gate ret = __ham_replpair(dbc, &repldbt, 0);
485*0Sstevel@tonic-gate hcp->dup_tlen -= DUP_SIZE(hcp->dup_len);
486*0Sstevel@tonic-gate F_SET(hcp, H_DELETED);
487*0Sstevel@tonic-gate __ham_c_update(hcp, hcp->pgno,
488*0Sstevel@tonic-gate DUP_SIZE(hcp->dup_len), 0, 1);
489*0Sstevel@tonic-gate }
490*0Sstevel@tonic-gate
491*0Sstevel@tonic-gate } else
492*0Sstevel@tonic-gate /* Not a duplicate */
493*0Sstevel@tonic-gate normal: ret = __ham_del_pair(dbc, 1);
494*0Sstevel@tonic-gate
495*0Sstevel@tonic-gate out: if ((t_ret = __ham_item_done(dbc, ret == 0)) != 0 && ret == 0)
496*0Sstevel@tonic-gate ret = t_ret;
497*0Sstevel@tonic-gate RELEASE_META(dbp, hcp);
498*0Sstevel@tonic-gate RESTORE_CURSOR(dbp, hcp, &save_curs, ret);
499*0Sstevel@tonic-gate if (F_ISSET(dbp, DB_AM_CDB) && F_ISSET(dbc, DBC_RMW))
500*0Sstevel@tonic-gate (void)__lock_downgrade(dbp->dbenv->lk_info, dbc->mylock,
501*0Sstevel@tonic-gate DB_LOCK_IWRITE, 0);
502*0Sstevel@tonic-gate return (ret);
503*0Sstevel@tonic-gate }
504*0Sstevel@tonic-gate
505*0Sstevel@tonic-gate static int
__ham_c_get(dbc,key,data,flags)506*0Sstevel@tonic-gate __ham_c_get(dbc, key, data, flags)
507*0Sstevel@tonic-gate DBC *dbc;
508*0Sstevel@tonic-gate DBT *key;
509*0Sstevel@tonic-gate DBT *data;
510*0Sstevel@tonic-gate u_int32_t flags;
511*0Sstevel@tonic-gate {
512*0Sstevel@tonic-gate DB *dbp;
513*0Sstevel@tonic-gate HASH_CURSOR *hcp, save_curs;
514*0Sstevel@tonic-gate db_lockmode_t lock_type;
515*0Sstevel@tonic-gate int get_key, ret, t_ret;
516*0Sstevel@tonic-gate
517*0Sstevel@tonic-gate DEBUG_LREAD(dbc, dbc->txn, "ham_c_get",
518*0Sstevel@tonic-gate flags == DB_SET || flags == DB_SET_RANGE ? key : NULL,
519*0Sstevel@tonic-gate NULL, flags);
520*0Sstevel@tonic-gate
521*0Sstevel@tonic-gate hcp = (HASH_CURSOR *)dbc->internal;
522*0Sstevel@tonic-gate dbp = dbc->dbp;
523*0Sstevel@tonic-gate DB_PANIC_CHECK(dbp);
524*0Sstevel@tonic-gate SAVE_CURSOR(hcp, &save_curs);
525*0Sstevel@tonic-gate if ((ret =
526*0Sstevel@tonic-gate __db_cgetchk(dbp, key, data, flags, IS_VALID(hcp))) != 0)
527*0Sstevel@tonic-gate return (ret);
528*0Sstevel@tonic-gate
529*0Sstevel@tonic-gate /* Clear OR'd in additional bits so we can check for flag equality. */
530*0Sstevel@tonic-gate if (LF_ISSET(DB_RMW)) {
531*0Sstevel@tonic-gate lock_type = DB_LOCK_WRITE;
532*0Sstevel@tonic-gate LF_CLR(DB_RMW);
533*0Sstevel@tonic-gate } else
534*0Sstevel@tonic-gate lock_type = DB_LOCK_READ;
535*0Sstevel@tonic-gate
536*0Sstevel@tonic-gate GET_META(dbp, hcp, ret);
537*0Sstevel@tonic-gate if (ret != 0)
538*0Sstevel@tonic-gate return (ret);
539*0Sstevel@tonic-gate hcp->stats.hash_get++;
540*0Sstevel@tonic-gate hcp->seek_size = 0;
541*0Sstevel@tonic-gate
542*0Sstevel@tonic-gate ret = 0;
543*0Sstevel@tonic-gate get_key = 1;
544*0Sstevel@tonic-gate switch (flags) {
545*0Sstevel@tonic-gate case DB_PREV:
546*0Sstevel@tonic-gate if (hcp->bucket != BUCKET_INVALID) {
547*0Sstevel@tonic-gate ret = __ham_item_prev(dbc, lock_type);
548*0Sstevel@tonic-gate break;
549*0Sstevel@tonic-gate }
550*0Sstevel@tonic-gate /* FALLTHROUGH */
551*0Sstevel@tonic-gate case DB_LAST:
552*0Sstevel@tonic-gate ret = __ham_item_last(dbc, lock_type);
553*0Sstevel@tonic-gate break;
554*0Sstevel@tonic-gate case DB_FIRST:
555*0Sstevel@tonic-gate ret = __ham_item_first(dbc, lock_type);
556*0Sstevel@tonic-gate break;
557*0Sstevel@tonic-gate case DB_NEXT_DUP:
558*0Sstevel@tonic-gate if (hcp->bucket == BUCKET_INVALID)
559*0Sstevel@tonic-gate ret = EINVAL;
560*0Sstevel@tonic-gate else {
561*0Sstevel@tonic-gate F_SET(hcp, H_DUPONLY);
562*0Sstevel@tonic-gate ret = __ham_item_next(dbc, lock_type);
563*0Sstevel@tonic-gate }
564*0Sstevel@tonic-gate break;
565*0Sstevel@tonic-gate case DB_NEXT:
566*0Sstevel@tonic-gate if (hcp->bucket == BUCKET_INVALID)
567*0Sstevel@tonic-gate hcp->bucket = 0;
568*0Sstevel@tonic-gate ret = __ham_item_next(dbc, lock_type);
569*0Sstevel@tonic-gate break;
570*0Sstevel@tonic-gate case DB_SET:
571*0Sstevel@tonic-gate case DB_SET_RANGE:
572*0Sstevel@tonic-gate case DB_GET_BOTH:
573*0Sstevel@tonic-gate if (F_ISSET(dbc, DBC_CONTINUE)) {
574*0Sstevel@tonic-gate F_SET(hcp, H_DUPONLY);
575*0Sstevel@tonic-gate ret = __ham_item_next(dbc, lock_type);
576*0Sstevel@tonic-gate } else if (F_ISSET(dbc, DBC_KEYSET))
577*0Sstevel@tonic-gate ret = __ham_item(dbc, lock_type);
578*0Sstevel@tonic-gate else
579*0Sstevel@tonic-gate ret = __ham_lookup(dbc, key, 0, lock_type);
580*0Sstevel@tonic-gate get_key = 0;
581*0Sstevel@tonic-gate break;
582*0Sstevel@tonic-gate case DB_CURRENT:
583*0Sstevel@tonic-gate if (F_ISSET(hcp, H_DELETED)) {
584*0Sstevel@tonic-gate ret = DB_KEYEMPTY;
585*0Sstevel@tonic-gate goto out;
586*0Sstevel@tonic-gate }
587*0Sstevel@tonic-gate
588*0Sstevel@tonic-gate ret = __ham_item(dbc, lock_type);
589*0Sstevel@tonic-gate break;
590*0Sstevel@tonic-gate }
591*0Sstevel@tonic-gate
592*0Sstevel@tonic-gate /*
593*0Sstevel@tonic-gate * Must always enter this loop to do error handling and
594*0Sstevel@tonic-gate * check for big key/data pair.
595*0Sstevel@tonic-gate */
596*0Sstevel@tonic-gate while (1) {
597*0Sstevel@tonic-gate if (ret != 0 && ret != DB_NOTFOUND)
598*0Sstevel@tonic-gate goto out1;
599*0Sstevel@tonic-gate else if (F_ISSET(hcp, H_OK)) {
600*0Sstevel@tonic-gate /* Get the key. */
601*0Sstevel@tonic-gate if (get_key && (ret = __db_ret(dbp, hcp->pagep,
602*0Sstevel@tonic-gate H_KEYINDEX(hcp->bndx), key, &dbc->rkey.data,
603*0Sstevel@tonic-gate &dbc->rkey.size)) != 0)
604*0Sstevel@tonic-gate goto out1;
605*0Sstevel@tonic-gate
606*0Sstevel@tonic-gate ret = __ham_dup_return(dbc, data, flags);
607*0Sstevel@tonic-gate break;
608*0Sstevel@tonic-gate } else if (!F_ISSET(hcp, H_NOMORE)) {
609*0Sstevel@tonic-gate abort();
610*0Sstevel@tonic-gate break;
611*0Sstevel@tonic-gate }
612*0Sstevel@tonic-gate
613*0Sstevel@tonic-gate /*
614*0Sstevel@tonic-gate * Ran out of entries in a bucket; change buckets.
615*0Sstevel@tonic-gate */
616*0Sstevel@tonic-gate switch (flags) {
617*0Sstevel@tonic-gate case DB_LAST:
618*0Sstevel@tonic-gate case DB_PREV:
619*0Sstevel@tonic-gate ret = __ham_item_done(dbc, 0);
620*0Sstevel@tonic-gate if (hcp->bucket == 0) {
621*0Sstevel@tonic-gate ret = DB_NOTFOUND;
622*0Sstevel@tonic-gate goto out1;
623*0Sstevel@tonic-gate }
624*0Sstevel@tonic-gate hcp->bucket--;
625*0Sstevel@tonic-gate hcp->bndx = NDX_INVALID;
626*0Sstevel@tonic-gate if (ret == 0)
627*0Sstevel@tonic-gate ret = __ham_item_prev(dbc, lock_type);
628*0Sstevel@tonic-gate break;
629*0Sstevel@tonic-gate case DB_FIRST:
630*0Sstevel@tonic-gate case DB_NEXT:
631*0Sstevel@tonic-gate ret = __ham_item_done(dbc, 0);
632*0Sstevel@tonic-gate hcp->bndx = NDX_INVALID;
633*0Sstevel@tonic-gate hcp->bucket++;
634*0Sstevel@tonic-gate hcp->pgno = PGNO_INVALID;
635*0Sstevel@tonic-gate hcp->pagep = NULL;
636*0Sstevel@tonic-gate if (hcp->bucket > hcp->hdr->max_bucket) {
637*0Sstevel@tonic-gate ret = DB_NOTFOUND;
638*0Sstevel@tonic-gate goto out1;
639*0Sstevel@tonic-gate }
640*0Sstevel@tonic-gate if (ret == 0)
641*0Sstevel@tonic-gate ret = __ham_item_next(dbc, lock_type);
642*0Sstevel@tonic-gate break;
643*0Sstevel@tonic-gate case DB_GET_BOTH:
644*0Sstevel@tonic-gate case DB_NEXT_DUP:
645*0Sstevel@tonic-gate case DB_SET:
646*0Sstevel@tonic-gate case DB_SET_RANGE:
647*0Sstevel@tonic-gate /* Key not found. */
648*0Sstevel@tonic-gate ret = DB_NOTFOUND;
649*0Sstevel@tonic-gate goto out1;
650*0Sstevel@tonic-gate }
651*0Sstevel@tonic-gate }
652*0Sstevel@tonic-gate out1: if ((t_ret = __ham_item_done(dbc, 0)) != 0 && ret == 0)
653*0Sstevel@tonic-gate ret = t_ret;
654*0Sstevel@tonic-gate out: RELEASE_META(dbp, hcp);
655*0Sstevel@tonic-gate RESTORE_CURSOR(dbp, hcp, &save_curs, ret);
656*0Sstevel@tonic-gate return (ret);
657*0Sstevel@tonic-gate }
658*0Sstevel@tonic-gate
659*0Sstevel@tonic-gate static int
__ham_c_put(dbc,key,data,flags)660*0Sstevel@tonic-gate __ham_c_put(dbc, key, data, flags)
661*0Sstevel@tonic-gate DBC *dbc;
662*0Sstevel@tonic-gate DBT *key;
663*0Sstevel@tonic-gate DBT *data;
664*0Sstevel@tonic-gate u_int32_t flags;
665*0Sstevel@tonic-gate {
666*0Sstevel@tonic-gate DB *dbp;
667*0Sstevel@tonic-gate DBT tmp_val, *myval;
668*0Sstevel@tonic-gate HASH_CURSOR *hcp, save_curs;
669*0Sstevel@tonic-gate u_int32_t nbytes;
670*0Sstevel@tonic-gate int ret, t_ret;
671*0Sstevel@tonic-gate
672*0Sstevel@tonic-gate dbp = dbc->dbp;
673*0Sstevel@tonic-gate DB_PANIC_CHECK(dbp);
674*0Sstevel@tonic-gate DEBUG_LWRITE(dbc, dbc->txn, "ham_c_put",
675*0Sstevel@tonic-gate flags == DB_KEYFIRST || flags == DB_KEYLAST ? key : NULL,
676*0Sstevel@tonic-gate data, flags);
677*0Sstevel@tonic-gate hcp = (HASH_CURSOR *)dbc->internal;
678*0Sstevel@tonic-gate
679*0Sstevel@tonic-gate if ((ret = __db_cputchk(dbp, key, data, flags,
680*0Sstevel@tonic-gate F_ISSET(dbp, DB_AM_RDONLY), IS_VALID(hcp))) != 0)
681*0Sstevel@tonic-gate return (ret);
682*0Sstevel@tonic-gate
683*0Sstevel@tonic-gate if (F_ISSET(hcp, H_DELETED) &&
684*0Sstevel@tonic-gate flags != DB_KEYFIRST && flags != DB_KEYLAST)
685*0Sstevel@tonic-gate return (DB_NOTFOUND);
686*0Sstevel@tonic-gate
687*0Sstevel@tonic-gate /*
688*0Sstevel@tonic-gate * If we are in the concurrent DB product and this cursor
689*0Sstevel@tonic-gate * is not a write cursor, then this request is invalid.
690*0Sstevel@tonic-gate * If it is a simple write cursor, then we need to upgrade its
691*0Sstevel@tonic-gate * lock.
692*0Sstevel@tonic-gate */
693*0Sstevel@tonic-gate if (F_ISSET(dbp, DB_AM_CDB)) {
694*0Sstevel@tonic-gate /* Make sure it's a valid update cursor. */
695*0Sstevel@tonic-gate if (!F_ISSET(dbc, DBC_RMW | DBC_WRITER))
696*0Sstevel@tonic-gate return (EINVAL);
697*0Sstevel@tonic-gate
698*0Sstevel@tonic-gate if (F_ISSET(dbc, DBC_RMW) &&
699*0Sstevel@tonic-gate (ret = lock_get(dbp->dbenv->lk_info, dbc->locker,
700*0Sstevel@tonic-gate DB_LOCK_UPGRADE, &dbc->lock_dbt, DB_LOCK_WRITE,
701*0Sstevel@tonic-gate &dbc->mylock)) != 0)
702*0Sstevel@tonic-gate return (EAGAIN);
703*0Sstevel@tonic-gate }
704*0Sstevel@tonic-gate
705*0Sstevel@tonic-gate GET_META(dbp, hcp, ret);
706*0Sstevel@tonic-gate if (ret != 0)
707*0Sstevel@tonic-gate return (ret);
708*0Sstevel@tonic-gate
709*0Sstevel@tonic-gate SAVE_CURSOR(hcp, &save_curs);
710*0Sstevel@tonic-gate hcp->stats.hash_put++;
711*0Sstevel@tonic-gate
712*0Sstevel@tonic-gate switch (flags) {
713*0Sstevel@tonic-gate case DB_KEYLAST:
714*0Sstevel@tonic-gate case DB_KEYFIRST:
715*0Sstevel@tonic-gate nbytes = (ISBIG(hcp, key->size) ? HOFFPAGE_PSIZE :
716*0Sstevel@tonic-gate HKEYDATA_PSIZE(key->size)) +
717*0Sstevel@tonic-gate (ISBIG(hcp, data->size) ? HOFFPAGE_PSIZE :
718*0Sstevel@tonic-gate HKEYDATA_PSIZE(data->size));
719*0Sstevel@tonic-gate if ((ret = __ham_lookup(dbc,
720*0Sstevel@tonic-gate key, nbytes, DB_LOCK_WRITE)) == DB_NOTFOUND) {
721*0Sstevel@tonic-gate ret = 0;
722*0Sstevel@tonic-gate if (hcp->seek_found_page != PGNO_INVALID &&
723*0Sstevel@tonic-gate hcp->seek_found_page != hcp->pgno) {
724*0Sstevel@tonic-gate if ((ret = __ham_item_done(dbc, 0)) != 0)
725*0Sstevel@tonic-gate goto out;
726*0Sstevel@tonic-gate hcp->pgno = hcp->seek_found_page;
727*0Sstevel@tonic-gate hcp->bndx = NDX_INVALID;
728*0Sstevel@tonic-gate }
729*0Sstevel@tonic-gate
730*0Sstevel@tonic-gate if (F_ISSET(data, DB_DBT_PARTIAL) && data->doff != 0) {
731*0Sstevel@tonic-gate /*
732*0Sstevel@tonic-gate * A partial put, but the key does not exist
733*0Sstevel@tonic-gate * and we are not beginning the write at 0.
734*0Sstevel@tonic-gate * We must create a data item padded up to doff
735*0Sstevel@tonic-gate * and then write the new bytes represented by
736*0Sstevel@tonic-gate * val.
737*0Sstevel@tonic-gate */
738*0Sstevel@tonic-gate if ((ret = __ham_init_dbt(&tmp_val,
739*0Sstevel@tonic-gate data->size + data->doff,
740*0Sstevel@tonic-gate &dbc->rdata.data, &dbc->rdata.size)) == 0) {
741*0Sstevel@tonic-gate memset(tmp_val.data, 0, data->doff);
742*0Sstevel@tonic-gate memcpy((u_int8_t *)tmp_val.data +
743*0Sstevel@tonic-gate data->doff, data->data, data->size);
744*0Sstevel@tonic-gate myval = &tmp_val;
745*0Sstevel@tonic-gate }
746*0Sstevel@tonic-gate } else
747*0Sstevel@tonic-gate myval = (DBT *)data;
748*0Sstevel@tonic-gate
749*0Sstevel@tonic-gate if (ret == 0)
750*0Sstevel@tonic-gate ret = __ham_add_el(dbc, key, myval, H_KEYDATA);
751*0Sstevel@tonic-gate goto done;
752*0Sstevel@tonic-gate }
753*0Sstevel@tonic-gate break;
754*0Sstevel@tonic-gate case DB_BEFORE:
755*0Sstevel@tonic-gate case DB_AFTER:
756*0Sstevel@tonic-gate case DB_CURRENT:
757*0Sstevel@tonic-gate ret = __ham_item(dbc, DB_LOCK_WRITE);
758*0Sstevel@tonic-gate break;
759*0Sstevel@tonic-gate }
760*0Sstevel@tonic-gate
761*0Sstevel@tonic-gate if (ret == 0) {
762*0Sstevel@tonic-gate if ((flags == DB_CURRENT && !F_ISSET(hcp, H_ISDUP)) ||
763*0Sstevel@tonic-gate ((flags == DB_KEYFIRST || flags == DB_KEYLAST) &&
764*0Sstevel@tonic-gate !F_ISSET(dbp, DB_AM_DUP)))
765*0Sstevel@tonic-gate ret = __ham_overwrite(dbc, data);
766*0Sstevel@tonic-gate else
767*0Sstevel@tonic-gate ret = __ham_add_dup(dbc, data, flags);
768*0Sstevel@tonic-gate }
769*0Sstevel@tonic-gate
770*0Sstevel@tonic-gate done: if (ret == 0 && F_ISSET(hcp, H_EXPAND)) {
771*0Sstevel@tonic-gate ret = __ham_expand_table(dbc);
772*0Sstevel@tonic-gate F_CLR(hcp, H_EXPAND);
773*0Sstevel@tonic-gate }
774*0Sstevel@tonic-gate
775*0Sstevel@tonic-gate if ((t_ret = __ham_item_done(dbc, ret == 0)) != 0 && ret == 0)
776*0Sstevel@tonic-gate ret = t_ret;
777*0Sstevel@tonic-gate
778*0Sstevel@tonic-gate out: RELEASE_META(dbp, hcp);
779*0Sstevel@tonic-gate RESTORE_CURSOR(dbp, hcp, &save_curs, ret);
780*0Sstevel@tonic-gate if (F_ISSET(dbp, DB_AM_CDB) && F_ISSET(dbc, DBC_RMW))
781*0Sstevel@tonic-gate (void)__lock_downgrade(dbp->dbenv->lk_info, dbc->mylock,
782*0Sstevel@tonic-gate DB_LOCK_IWRITE, 0);
783*0Sstevel@tonic-gate return (ret);
784*0Sstevel@tonic-gate }
785*0Sstevel@tonic-gate
786*0Sstevel@tonic-gate /********************************* UTILITIES ************************/
787*0Sstevel@tonic-gate
788*0Sstevel@tonic-gate /*
789*0Sstevel@tonic-gate * __ham_expand_table --
790*0Sstevel@tonic-gate */
791*0Sstevel@tonic-gate static int
__ham_expand_table(dbc)792*0Sstevel@tonic-gate __ham_expand_table(dbc)
793*0Sstevel@tonic-gate DBC *dbc;
794*0Sstevel@tonic-gate {
795*0Sstevel@tonic-gate DB *dbp;
796*0Sstevel@tonic-gate HASH_CURSOR *hcp;
797*0Sstevel@tonic-gate DB_LSN new_lsn;
798*0Sstevel@tonic-gate u_int32_t old_bucket, new_bucket, spare_ndx;
799*0Sstevel@tonic-gate int ret;
800*0Sstevel@tonic-gate
801*0Sstevel@tonic-gate dbp = dbc->dbp;
802*0Sstevel@tonic-gate hcp = (HASH_CURSOR *)dbc->internal;
803*0Sstevel@tonic-gate ret = 0;
804*0Sstevel@tonic-gate DIRTY_META(dbp, hcp, ret);
805*0Sstevel@tonic-gate if (ret)
806*0Sstevel@tonic-gate return (ret);
807*0Sstevel@tonic-gate
808*0Sstevel@tonic-gate /*
809*0Sstevel@tonic-gate * If the split point is about to increase, make sure that we
810*0Sstevel@tonic-gate * have enough extra pages. The calculation here is weird.
811*0Sstevel@tonic-gate * We'd like to do this after we've upped max_bucket, but it's
812*0Sstevel@tonic-gate * too late then because we've logged the meta-data split. What
813*0Sstevel@tonic-gate * we'll do between then and now is increment max bucket and then
814*0Sstevel@tonic-gate * see what the log of one greater than that is; here we have to
815*0Sstevel@tonic-gate * look at the log of max + 2. VERY NASTY STUFF.
816*0Sstevel@tonic-gate */
817*0Sstevel@tonic-gate if (__db_log2(hcp->hdr->max_bucket + 2) > hcp->hdr->ovfl_point) {
818*0Sstevel@tonic-gate /*
819*0Sstevel@tonic-gate * We are about to shift the split point. Make sure that
820*0Sstevel@tonic-gate * if the next doubling is going to be big (more than 8
821*0Sstevel@tonic-gate * pages), we have some extra pages around.
822*0Sstevel@tonic-gate */
823*0Sstevel@tonic-gate if (hcp->hdr->max_bucket + 1 >= 8 &&
824*0Sstevel@tonic-gate hcp->hdr->spares[hcp->hdr->ovfl_point] <
825*0Sstevel@tonic-gate hcp->hdr->spares[hcp->hdr->ovfl_point - 1] +
826*0Sstevel@tonic-gate hcp->hdr->ovfl_point + 1)
827*0Sstevel@tonic-gate __ham_init_ovflpages(dbc);
828*0Sstevel@tonic-gate }
829*0Sstevel@tonic-gate
830*0Sstevel@tonic-gate /* Now we can log the meta-data split. */
831*0Sstevel@tonic-gate if (DB_LOGGING(dbc)) {
832*0Sstevel@tonic-gate if ((ret = __ham_splitmeta_log(dbp->dbenv->lg_info,
833*0Sstevel@tonic-gate dbc->txn, &new_lsn, 0, dbp->log_fileid,
834*0Sstevel@tonic-gate hcp->hdr->max_bucket, hcp->hdr->ovfl_point,
835*0Sstevel@tonic-gate hcp->hdr->spares[hcp->hdr->ovfl_point],
836*0Sstevel@tonic-gate &hcp->hdr->lsn)) != 0)
837*0Sstevel@tonic-gate return (ret);
838*0Sstevel@tonic-gate
839*0Sstevel@tonic-gate hcp->hdr->lsn = new_lsn;
840*0Sstevel@tonic-gate }
841*0Sstevel@tonic-gate
842*0Sstevel@tonic-gate hcp->stats.hash_expansions++;
843*0Sstevel@tonic-gate new_bucket = ++hcp->hdr->max_bucket;
844*0Sstevel@tonic-gate old_bucket = (hcp->hdr->max_bucket & hcp->hdr->low_mask);
845*0Sstevel@tonic-gate
846*0Sstevel@tonic-gate /*
847*0Sstevel@tonic-gate * If the split point is increasing, copy the current contents
848*0Sstevel@tonic-gate * of the spare split bucket to the next bucket.
849*0Sstevel@tonic-gate */
850*0Sstevel@tonic-gate spare_ndx = __db_log2(hcp->hdr->max_bucket + 1);
851*0Sstevel@tonic-gate if (spare_ndx > hcp->hdr->ovfl_point) {
852*0Sstevel@tonic-gate hcp->hdr->spares[spare_ndx] =
853*0Sstevel@tonic-gate hcp->hdr->spares[hcp->hdr->ovfl_point];
854*0Sstevel@tonic-gate hcp->hdr->ovfl_point = spare_ndx;
855*0Sstevel@tonic-gate }
856*0Sstevel@tonic-gate
857*0Sstevel@tonic-gate if (new_bucket > hcp->hdr->high_mask) {
858*0Sstevel@tonic-gate /* Starting a new doubling */
859*0Sstevel@tonic-gate hcp->hdr->low_mask = hcp->hdr->high_mask;
860*0Sstevel@tonic-gate hcp->hdr->high_mask = new_bucket | hcp->hdr->low_mask;
861*0Sstevel@tonic-gate }
862*0Sstevel@tonic-gate
863*0Sstevel@tonic-gate if (BUCKET_TO_PAGE(hcp, new_bucket) > MAX_PAGES(hcp)) {
864*0Sstevel@tonic-gate __db_err(dbp->dbenv,
865*0Sstevel@tonic-gate "hash: Cannot allocate new bucket. Pages exhausted.");
866*0Sstevel@tonic-gate return (ENOSPC);
867*0Sstevel@tonic-gate }
868*0Sstevel@tonic-gate
869*0Sstevel@tonic-gate /* Relocate records to the new bucket */
870*0Sstevel@tonic-gate return (__ham_split_page(dbc, old_bucket, new_bucket));
871*0Sstevel@tonic-gate }
872*0Sstevel@tonic-gate
873*0Sstevel@tonic-gate /*
874*0Sstevel@tonic-gate * PUBLIC: u_int32_t __ham_call_hash __P((HASH_CURSOR *, u_int8_t *, int32_t));
875*0Sstevel@tonic-gate */
876*0Sstevel@tonic-gate u_int32_t
__ham_call_hash(hcp,k,len)877*0Sstevel@tonic-gate __ham_call_hash(hcp, k, len)
878*0Sstevel@tonic-gate HASH_CURSOR *hcp;
879*0Sstevel@tonic-gate u_int8_t *k;
880*0Sstevel@tonic-gate int32_t len;
881*0Sstevel@tonic-gate {
882*0Sstevel@tonic-gate u_int32_t n, bucket;
883*0Sstevel@tonic-gate
884*0Sstevel@tonic-gate n = (u_int32_t)(hcp->dbc->dbp->h_hash(k, len));
885*0Sstevel@tonic-gate
886*0Sstevel@tonic-gate bucket = n & hcp->hdr->high_mask;
887*0Sstevel@tonic-gate if (bucket > hcp->hdr->max_bucket)
888*0Sstevel@tonic-gate bucket = bucket & hcp->hdr->low_mask;
889*0Sstevel@tonic-gate return (bucket);
890*0Sstevel@tonic-gate }
891*0Sstevel@tonic-gate
892*0Sstevel@tonic-gate /*
893*0Sstevel@tonic-gate * Check for duplicates, and call __db_ret appropriately. Release
894*0Sstevel@tonic-gate * everything held by the cursor.
895*0Sstevel@tonic-gate */
896*0Sstevel@tonic-gate static int
__ham_dup_return(dbc,val,flags)897*0Sstevel@tonic-gate __ham_dup_return(dbc, val, flags)
898*0Sstevel@tonic-gate DBC *dbc;
899*0Sstevel@tonic-gate DBT *val;
900*0Sstevel@tonic-gate u_int32_t flags;
901*0Sstevel@tonic-gate {
902*0Sstevel@tonic-gate DB *dbp;
903*0Sstevel@tonic-gate HASH_CURSOR *hcp;
904*0Sstevel@tonic-gate PAGE *pp;
905*0Sstevel@tonic-gate DBT *myval, tmp_val;
906*0Sstevel@tonic-gate db_indx_t ndx;
907*0Sstevel@tonic-gate db_pgno_t pgno;
908*0Sstevel@tonic-gate u_int32_t off, tlen;
909*0Sstevel@tonic-gate u_int8_t *hk, type;
910*0Sstevel@tonic-gate int cmp, ret;
911*0Sstevel@tonic-gate db_indx_t len;
912*0Sstevel@tonic-gate
913*0Sstevel@tonic-gate /* Check for duplicate and return the first one. */
914*0Sstevel@tonic-gate dbp = dbc->dbp;
915*0Sstevel@tonic-gate hcp = (HASH_CURSOR *)dbc->internal;
916*0Sstevel@tonic-gate ndx = H_DATAINDEX(hcp->bndx);
917*0Sstevel@tonic-gate type = HPAGE_TYPE(hcp->pagep, ndx);
918*0Sstevel@tonic-gate pp = hcp->pagep;
919*0Sstevel@tonic-gate myval = val;
920*0Sstevel@tonic-gate
921*0Sstevel@tonic-gate /*
922*0Sstevel@tonic-gate * There are 4 cases:
923*0Sstevel@tonic-gate * 1. We are not in duplicate, simply call db_ret.
924*0Sstevel@tonic-gate * 2. We are looking at keys and stumbled onto a duplicate.
925*0Sstevel@tonic-gate * 3. We are in the middle of a duplicate set. (ISDUP set)
926*0Sstevel@tonic-gate * 4. This is a duplicate and we need to return a specific item.
927*0Sstevel@tonic-gate */
928*0Sstevel@tonic-gate
929*0Sstevel@tonic-gate /*
930*0Sstevel@tonic-gate * Here we check for the case where we just stumbled onto a
931*0Sstevel@tonic-gate * duplicate. In this case, we do initialization and then
932*0Sstevel@tonic-gate * let the normal duplicate code handle it.
933*0Sstevel@tonic-gate */
934*0Sstevel@tonic-gate if (!F_ISSET(hcp, H_ISDUP))
935*0Sstevel@tonic-gate if (type == H_DUPLICATE) {
936*0Sstevel@tonic-gate F_SET(hcp, H_ISDUP);
937*0Sstevel@tonic-gate hcp->dup_tlen = LEN_HDATA(hcp->pagep,
938*0Sstevel@tonic-gate hcp->hdr->pagesize, hcp->bndx);
939*0Sstevel@tonic-gate hk = H_PAIRDATA(hcp->pagep, hcp->bndx);
940*0Sstevel@tonic-gate if (flags == DB_LAST || flags == DB_PREV) {
941*0Sstevel@tonic-gate hcp->dndx = 0;
942*0Sstevel@tonic-gate hcp->dup_off = 0;
943*0Sstevel@tonic-gate do {
944*0Sstevel@tonic-gate memcpy(&len,
945*0Sstevel@tonic-gate HKEYDATA_DATA(hk) + hcp->dup_off,
946*0Sstevel@tonic-gate sizeof(db_indx_t));
947*0Sstevel@tonic-gate hcp->dup_off += DUP_SIZE(len);
948*0Sstevel@tonic-gate hcp->dndx++;
949*0Sstevel@tonic-gate } while (hcp->dup_off < hcp->dup_tlen);
950*0Sstevel@tonic-gate hcp->dup_off -= DUP_SIZE(len);
951*0Sstevel@tonic-gate hcp->dndx--;
952*0Sstevel@tonic-gate } else {
953*0Sstevel@tonic-gate memcpy(&len,
954*0Sstevel@tonic-gate HKEYDATA_DATA(hk), sizeof(db_indx_t));
955*0Sstevel@tonic-gate hcp->dup_off = 0;
956*0Sstevel@tonic-gate hcp->dndx = 0;
957*0Sstevel@tonic-gate }
958*0Sstevel@tonic-gate hcp->dup_len = len;
959*0Sstevel@tonic-gate } else if (type == H_OFFDUP) {
960*0Sstevel@tonic-gate F_SET(hcp, H_ISDUP);
961*0Sstevel@tonic-gate memcpy(&pgno, HOFFDUP_PGNO(P_ENTRY(hcp->pagep, ndx)),
962*0Sstevel@tonic-gate sizeof(db_pgno_t));
963*0Sstevel@tonic-gate if (flags == DB_LAST || flags == DB_PREV) {
964*0Sstevel@tonic-gate if ((ret = __db_dend(dbc,
965*0Sstevel@tonic-gate pgno, &hcp->dpagep)) != 0)
966*0Sstevel@tonic-gate return (ret);
967*0Sstevel@tonic-gate hcp->dpgno = PGNO(hcp->dpagep);
968*0Sstevel@tonic-gate hcp->dndx = NUM_ENT(hcp->dpagep) - 1;
969*0Sstevel@tonic-gate } else if ((ret = __ham_next_cpage(dbc,
970*0Sstevel@tonic-gate pgno, 0, H_ISDUP)) != 0)
971*0Sstevel@tonic-gate return (ret);
972*0Sstevel@tonic-gate }
973*0Sstevel@tonic-gate
974*0Sstevel@tonic-gate
975*0Sstevel@tonic-gate /*
976*0Sstevel@tonic-gate * If we are retrieving a specific key/data pair, then we
977*0Sstevel@tonic-gate * may need to adjust the cursor before returning data.
978*0Sstevel@tonic-gate */
979*0Sstevel@tonic-gate if (flags == DB_GET_BOTH) {
980*0Sstevel@tonic-gate if (F_ISSET(hcp, H_ISDUP)) {
981*0Sstevel@tonic-gate if (hcp->dpgno != PGNO_INVALID) {
982*0Sstevel@tonic-gate if ((ret = __db_dsearch(dbc, 0, val,
983*0Sstevel@tonic-gate hcp->dpgno, &hcp->dndx, &hcp->dpagep, &cmp))
984*0Sstevel@tonic-gate != 0)
985*0Sstevel@tonic-gate return (ret);
986*0Sstevel@tonic-gate if (cmp == 0)
987*0Sstevel@tonic-gate hcp->dpgno = PGNO(hcp->dpagep);
988*0Sstevel@tonic-gate } else {
989*0Sstevel@tonic-gate __ham_dsearch(dbc, val, &off, &cmp);
990*0Sstevel@tonic-gate hcp->dup_off = off;
991*0Sstevel@tonic-gate }
992*0Sstevel@tonic-gate } else {
993*0Sstevel@tonic-gate hk = H_PAIRDATA(hcp->pagep, hcp->bndx);
994*0Sstevel@tonic-gate if (((HKEYDATA *)hk)->type == H_OFFPAGE) {
995*0Sstevel@tonic-gate memcpy(&tlen,
996*0Sstevel@tonic-gate HOFFPAGE_TLEN(hk), sizeof(u_int32_t));
997*0Sstevel@tonic-gate memcpy(&pgno,
998*0Sstevel@tonic-gate HOFFPAGE_PGNO(hk), sizeof(db_pgno_t));
999*0Sstevel@tonic-gate if ((ret = __db_moff(dbp, val,
1000*0Sstevel@tonic-gate pgno, tlen, dbp->dup_compare, &cmp)) != 0)
1001*0Sstevel@tonic-gate return (ret);
1002*0Sstevel@tonic-gate } else {
1003*0Sstevel@tonic-gate /*
1004*0Sstevel@tonic-gate * We do not zero tmp_val since the comparison
1005*0Sstevel@tonic-gate * routines may only look at data and size.
1006*0Sstevel@tonic-gate */
1007*0Sstevel@tonic-gate tmp_val.data = HKEYDATA_DATA(hk);
1008*0Sstevel@tonic-gate tmp_val.size = LEN_HDATA(hcp->pagep,
1009*0Sstevel@tonic-gate dbp->pgsize, hcp->bndx);
1010*0Sstevel@tonic-gate cmp = dbp->dup_compare == NULL ?
1011*0Sstevel@tonic-gate __bam_defcmp(&tmp_val, val) :
1012*0Sstevel@tonic-gate dbp->dup_compare(&tmp_val, val);
1013*0Sstevel@tonic-gate }
1014*0Sstevel@tonic-gate }
1015*0Sstevel@tonic-gate
1016*0Sstevel@tonic-gate if (cmp != 0)
1017*0Sstevel@tonic-gate return (DB_NOTFOUND);
1018*0Sstevel@tonic-gate }
1019*0Sstevel@tonic-gate
1020*0Sstevel@tonic-gate /*
1021*0Sstevel@tonic-gate * Now, everything is initialized, grab a duplicate if
1022*0Sstevel@tonic-gate * necessary.
1023*0Sstevel@tonic-gate */
1024*0Sstevel@tonic-gate if (F_ISSET(hcp, H_ISDUP))
1025*0Sstevel@tonic-gate if (hcp->dpgno != PGNO_INVALID) {
1026*0Sstevel@tonic-gate pp = hcp->dpagep;
1027*0Sstevel@tonic-gate ndx = hcp->dndx;
1028*0Sstevel@tonic-gate } else {
1029*0Sstevel@tonic-gate /*
1030*0Sstevel@tonic-gate * Copy the DBT in case we are retrieving into user
1031*0Sstevel@tonic-gate * memory and we need the parameters for it. If the
1032*0Sstevel@tonic-gate * user requested a partial, then we need to adjust
1033*0Sstevel@tonic-gate * the user's parameters to get the partial of the
1034*0Sstevel@tonic-gate * duplicate which is itself a partial.
1035*0Sstevel@tonic-gate */
1036*0Sstevel@tonic-gate memcpy(&tmp_val, val, sizeof(*val));
1037*0Sstevel@tonic-gate if (F_ISSET(&tmp_val, DB_DBT_PARTIAL)) {
1038*0Sstevel@tonic-gate /*
1039*0Sstevel@tonic-gate * Take the user's length unless it would go
1040*0Sstevel@tonic-gate * beyond the end of the duplicate.
1041*0Sstevel@tonic-gate */
1042*0Sstevel@tonic-gate if (tmp_val.doff + hcp->dup_off > hcp->dup_len)
1043*0Sstevel@tonic-gate tmp_val.dlen = 0;
1044*0Sstevel@tonic-gate else if (tmp_val.dlen + tmp_val.doff >
1045*0Sstevel@tonic-gate hcp->dup_len)
1046*0Sstevel@tonic-gate tmp_val.dlen =
1047*0Sstevel@tonic-gate hcp->dup_len - tmp_val.doff;
1048*0Sstevel@tonic-gate
1049*0Sstevel@tonic-gate /*
1050*0Sstevel@tonic-gate * Calculate the new offset.
1051*0Sstevel@tonic-gate */
1052*0Sstevel@tonic-gate tmp_val.doff += hcp->dup_off;
1053*0Sstevel@tonic-gate } else {
1054*0Sstevel@tonic-gate F_SET(&tmp_val, DB_DBT_PARTIAL);
1055*0Sstevel@tonic-gate tmp_val.dlen = hcp->dup_len;
1056*0Sstevel@tonic-gate tmp_val.doff = hcp->dup_off + sizeof(db_indx_t);
1057*0Sstevel@tonic-gate }
1058*0Sstevel@tonic-gate myval = &tmp_val;
1059*0Sstevel@tonic-gate }
1060*0Sstevel@tonic-gate
1061*0Sstevel@tonic-gate
1062*0Sstevel@tonic-gate /*
1063*0Sstevel@tonic-gate * Finally, if we had a duplicate, pp, ndx, and myval should be
1064*0Sstevel@tonic-gate * set appropriately.
1065*0Sstevel@tonic-gate */
1066*0Sstevel@tonic-gate if ((ret = __db_ret(dbp, pp, ndx, myval, &dbc->rdata.data,
1067*0Sstevel@tonic-gate &dbc->rdata.size)) != 0)
1068*0Sstevel@tonic-gate return (ret);
1069*0Sstevel@tonic-gate
1070*0Sstevel@tonic-gate /*
1071*0Sstevel@tonic-gate * In case we sent a temporary off to db_ret, set the real
1072*0Sstevel@tonic-gate * return values.
1073*0Sstevel@tonic-gate */
1074*0Sstevel@tonic-gate val->data = myval->data;
1075*0Sstevel@tonic-gate val->size = myval->size;
1076*0Sstevel@tonic-gate
1077*0Sstevel@tonic-gate return (0);
1078*0Sstevel@tonic-gate }
1079*0Sstevel@tonic-gate
1080*0Sstevel@tonic-gate static int
__ham_overwrite(dbc,nval)1081*0Sstevel@tonic-gate __ham_overwrite(dbc, nval)
1082*0Sstevel@tonic-gate DBC *dbc;
1083*0Sstevel@tonic-gate DBT *nval;
1084*0Sstevel@tonic-gate {
1085*0Sstevel@tonic-gate HASH_CURSOR *hcp;
1086*0Sstevel@tonic-gate DBT *myval, tmp_val;
1087*0Sstevel@tonic-gate u_int8_t *hk;
1088*0Sstevel@tonic-gate
1089*0Sstevel@tonic-gate hcp = (HASH_CURSOR *)dbc->internal;
1090*0Sstevel@tonic-gate if (F_ISSET(dbc->dbp, DB_AM_DUP))
1091*0Sstevel@tonic-gate return (__ham_add_dup(dbc, nval, DB_KEYLAST));
1092*0Sstevel@tonic-gate else if (!F_ISSET(nval, DB_DBT_PARTIAL)) {
1093*0Sstevel@tonic-gate /* Put/overwrite */
1094*0Sstevel@tonic-gate memcpy(&tmp_val, nval, sizeof(*nval));
1095*0Sstevel@tonic-gate F_SET(&tmp_val, DB_DBT_PARTIAL);
1096*0Sstevel@tonic-gate tmp_val.doff = 0;
1097*0Sstevel@tonic-gate hk = H_PAIRDATA(hcp->pagep, hcp->bndx);
1098*0Sstevel@tonic-gate if (HPAGE_PTYPE(hk) == H_OFFPAGE)
1099*0Sstevel@tonic-gate memcpy(&tmp_val.dlen,
1100*0Sstevel@tonic-gate HOFFPAGE_TLEN(hk), sizeof(u_int32_t));
1101*0Sstevel@tonic-gate else
1102*0Sstevel@tonic-gate tmp_val.dlen = LEN_HDATA(hcp->pagep,
1103*0Sstevel@tonic-gate hcp->hdr->pagesize,hcp->bndx);
1104*0Sstevel@tonic-gate myval = &tmp_val;
1105*0Sstevel@tonic-gate } else /* Regular partial put */
1106*0Sstevel@tonic-gate myval = nval;
1107*0Sstevel@tonic-gate
1108*0Sstevel@tonic-gate return (__ham_replpair(dbc, myval, 0));
1109*0Sstevel@tonic-gate }
1110*0Sstevel@tonic-gate
1111*0Sstevel@tonic-gate /*
1112*0Sstevel@tonic-gate * Given a key and a cursor, sets the cursor to the page/ndx on which
1113*0Sstevel@tonic-gate * the key resides. If the key is found, the cursor H_OK flag is set
1114*0Sstevel@tonic-gate * and the pagep, bndx, pgno (dpagep, dndx, dpgno) fields are set.
1115*0Sstevel@tonic-gate * If the key is not found, the H_OK flag is not set. If the sought
1116*0Sstevel@tonic-gate * field is non-0, the pagep, bndx, pgno (dpagep, dndx, dpgno) fields
1117*0Sstevel@tonic-gate * are set indicating where an add might take place. If it is 0,
1118*0Sstevel@tonic-gate * non of the cursor pointer field are valid.
1119*0Sstevel@tonic-gate */
1120*0Sstevel@tonic-gate static int
__ham_lookup(dbc,key,sought,mode)1121*0Sstevel@tonic-gate __ham_lookup(dbc, key, sought, mode)
1122*0Sstevel@tonic-gate DBC *dbc;
1123*0Sstevel@tonic-gate const DBT *key;
1124*0Sstevel@tonic-gate u_int32_t sought;
1125*0Sstevel@tonic-gate db_lockmode_t mode;
1126*0Sstevel@tonic-gate {
1127*0Sstevel@tonic-gate DB *dbp;
1128*0Sstevel@tonic-gate HASH_CURSOR *hcp;
1129*0Sstevel@tonic-gate db_pgno_t pgno;
1130*0Sstevel@tonic-gate u_int32_t tlen;
1131*0Sstevel@tonic-gate int match, ret, t_ret;
1132*0Sstevel@tonic-gate u_int8_t *hk;
1133*0Sstevel@tonic-gate
1134*0Sstevel@tonic-gate dbp = dbc->dbp;
1135*0Sstevel@tonic-gate hcp = (HASH_CURSOR *)dbc->internal;
1136*0Sstevel@tonic-gate /*
1137*0Sstevel@tonic-gate * Set up cursor so that we're looking for space to add an item
1138*0Sstevel@tonic-gate * as we cycle through the pages looking for the key.
1139*0Sstevel@tonic-gate */
1140*0Sstevel@tonic-gate if ((ret = __ham_item_reset(dbc)) != 0)
1141*0Sstevel@tonic-gate return (ret);
1142*0Sstevel@tonic-gate hcp->seek_size = sought;
1143*0Sstevel@tonic-gate
1144*0Sstevel@tonic-gate hcp->bucket = __ham_call_hash(hcp, (u_int8_t *)key->data, key->size);
1145*0Sstevel@tonic-gate while (1) {
1146*0Sstevel@tonic-gate if ((ret = __ham_item_next(dbc, mode)) != 0)
1147*0Sstevel@tonic-gate return (ret);
1148*0Sstevel@tonic-gate
1149*0Sstevel@tonic-gate if (F_ISSET(hcp, H_NOMORE))
1150*0Sstevel@tonic-gate break;
1151*0Sstevel@tonic-gate
1152*0Sstevel@tonic-gate hk = H_PAIRKEY(hcp->pagep, hcp->bndx);
1153*0Sstevel@tonic-gate switch (HPAGE_PTYPE(hk)) {
1154*0Sstevel@tonic-gate case H_OFFPAGE:
1155*0Sstevel@tonic-gate memcpy(&tlen, HOFFPAGE_TLEN(hk), sizeof(u_int32_t));
1156*0Sstevel@tonic-gate if (tlen == key->size) {
1157*0Sstevel@tonic-gate memcpy(&pgno,
1158*0Sstevel@tonic-gate HOFFPAGE_PGNO(hk), sizeof(db_pgno_t));
1159*0Sstevel@tonic-gate if ((ret = __db_moff(dbp,
1160*0Sstevel@tonic-gate key, pgno, tlen, NULL, &match)) != 0)
1161*0Sstevel@tonic-gate return (ret);
1162*0Sstevel@tonic-gate if (match == 0) {
1163*0Sstevel@tonic-gate F_SET(hcp, H_OK);
1164*0Sstevel@tonic-gate return (0);
1165*0Sstevel@tonic-gate }
1166*0Sstevel@tonic-gate }
1167*0Sstevel@tonic-gate break;
1168*0Sstevel@tonic-gate case H_KEYDATA:
1169*0Sstevel@tonic-gate if (key->size == LEN_HKEY(hcp->pagep,
1170*0Sstevel@tonic-gate hcp->hdr->pagesize, hcp->bndx) &&
1171*0Sstevel@tonic-gate memcmp(key->data,
1172*0Sstevel@tonic-gate HKEYDATA_DATA(hk), key->size) == 0) {
1173*0Sstevel@tonic-gate F_SET(hcp, H_OK);
1174*0Sstevel@tonic-gate return (0);
1175*0Sstevel@tonic-gate }
1176*0Sstevel@tonic-gate break;
1177*0Sstevel@tonic-gate case H_DUPLICATE:
1178*0Sstevel@tonic-gate case H_OFFDUP:
1179*0Sstevel@tonic-gate /*
1180*0Sstevel@tonic-gate * These are errors because keys are never
1181*0Sstevel@tonic-gate * duplicated, only data items are.
1182*0Sstevel@tonic-gate */
1183*0Sstevel@tonic-gate return (__db_pgfmt(dbp, PGNO(hcp->pagep)));
1184*0Sstevel@tonic-gate }
1185*0Sstevel@tonic-gate hcp->stats.hash_collisions++;
1186*0Sstevel@tonic-gate }
1187*0Sstevel@tonic-gate
1188*0Sstevel@tonic-gate /*
1189*0Sstevel@tonic-gate * Item was not found, adjust cursor properly.
1190*0Sstevel@tonic-gate */
1191*0Sstevel@tonic-gate
1192*0Sstevel@tonic-gate if (sought != 0)
1193*0Sstevel@tonic-gate return (ret);
1194*0Sstevel@tonic-gate
1195*0Sstevel@tonic-gate if ((t_ret = __ham_item_done(dbc, 0)) != 0 && ret == 0)
1196*0Sstevel@tonic-gate ret = t_ret;
1197*0Sstevel@tonic-gate return (ret);
1198*0Sstevel@tonic-gate }
1199*0Sstevel@tonic-gate
1200*0Sstevel@tonic-gate /*
1201*0Sstevel@tonic-gate * Initialize a dbt using some possibly already allocated storage
1202*0Sstevel@tonic-gate * for items.
1203*0Sstevel@tonic-gate * PUBLIC: int __ham_init_dbt __P((DBT *, u_int32_t, void **, u_int32_t *));
1204*0Sstevel@tonic-gate */
1205*0Sstevel@tonic-gate int
__ham_init_dbt(dbt,size,bufp,sizep)1206*0Sstevel@tonic-gate __ham_init_dbt(dbt, size, bufp, sizep)
1207*0Sstevel@tonic-gate DBT *dbt;
1208*0Sstevel@tonic-gate u_int32_t size;
1209*0Sstevel@tonic-gate void **bufp;
1210*0Sstevel@tonic-gate u_int32_t *sizep;
1211*0Sstevel@tonic-gate {
1212*0Sstevel@tonic-gate int ret;
1213*0Sstevel@tonic-gate
1214*0Sstevel@tonic-gate memset(dbt, 0, sizeof(*dbt));
1215*0Sstevel@tonic-gate if (*sizep < size) {
1216*0Sstevel@tonic-gate if ((ret = __os_realloc(bufp, size)) != 0) {
1217*0Sstevel@tonic-gate *sizep = 0;
1218*0Sstevel@tonic-gate return (ret);
1219*0Sstevel@tonic-gate }
1220*0Sstevel@tonic-gate *sizep = size;
1221*0Sstevel@tonic-gate }
1222*0Sstevel@tonic-gate dbt->data = *bufp;
1223*0Sstevel@tonic-gate dbt->size = size;
1224*0Sstevel@tonic-gate return (0);
1225*0Sstevel@tonic-gate }
1226*0Sstevel@tonic-gate
1227*0Sstevel@tonic-gate /*
1228*0Sstevel@tonic-gate * Adjust the cursor after an insert or delete. The cursor passed is
1229*0Sstevel@tonic-gate * the one that was operated upon; we just need to check any of the
1230*0Sstevel@tonic-gate * others.
1231*0Sstevel@tonic-gate *
1232*0Sstevel@tonic-gate * len indicates the length of the item added/deleted
1233*0Sstevel@tonic-gate * add indicates if the item indicated by the cursor has just been
1234*0Sstevel@tonic-gate * added (add == 1) or deleted (add == 0).
1235*0Sstevel@tonic-gate * dup indicates if the addition occurred into a duplicate set.
1236*0Sstevel@tonic-gate *
1237*0Sstevel@tonic-gate * PUBLIC: void __ham_c_update
1238*0Sstevel@tonic-gate * PUBLIC: __P((HASH_CURSOR *, db_pgno_t, u_int32_t, int, int));
1239*0Sstevel@tonic-gate */
1240*0Sstevel@tonic-gate void
__ham_c_update(hcp,chg_pgno,len,add,is_dup)1241*0Sstevel@tonic-gate __ham_c_update(hcp, chg_pgno, len, add, is_dup)
1242*0Sstevel@tonic-gate HASH_CURSOR *hcp;
1243*0Sstevel@tonic-gate db_pgno_t chg_pgno;
1244*0Sstevel@tonic-gate u_int32_t len;
1245*0Sstevel@tonic-gate int add, is_dup;
1246*0Sstevel@tonic-gate {
1247*0Sstevel@tonic-gate DB *dbp;
1248*0Sstevel@tonic-gate DBC *cp;
1249*0Sstevel@tonic-gate HASH_CURSOR *lcp;
1250*0Sstevel@tonic-gate int page_deleted;
1251*0Sstevel@tonic-gate
1252*0Sstevel@tonic-gate /*
1253*0Sstevel@tonic-gate * Regular adds are always at the end of a given page, so we never
1254*0Sstevel@tonic-gate * have to adjust anyone's cursor after a regular add.
1255*0Sstevel@tonic-gate */
1256*0Sstevel@tonic-gate if (!is_dup && add)
1257*0Sstevel@tonic-gate return;
1258*0Sstevel@tonic-gate
1259*0Sstevel@tonic-gate /*
1260*0Sstevel@tonic-gate * Determine if a page was deleted. If this is a regular update
1261*0Sstevel@tonic-gate * (i.e., not is_dup) then the deleted page's number will be that in
1262*0Sstevel@tonic-gate * chg_pgno, and the pgno in the cursor will be different. If this
1263*0Sstevel@tonic-gate * was an onpage-duplicate, then the same conditions apply. If this
1264*0Sstevel@tonic-gate * was an off-page duplicate, then we need to verify if hcp->dpgno
1265*0Sstevel@tonic-gate * is the same (no delete) or different (delete) than chg_pgno.
1266*0Sstevel@tonic-gate */
1267*0Sstevel@tonic-gate if (!is_dup || hcp->dpgno == PGNO_INVALID)
1268*0Sstevel@tonic-gate page_deleted =
1269*0Sstevel@tonic-gate chg_pgno != PGNO_INVALID && chg_pgno != hcp->pgno;
1270*0Sstevel@tonic-gate else
1271*0Sstevel@tonic-gate page_deleted =
1272*0Sstevel@tonic-gate chg_pgno != PGNO_INVALID && chg_pgno != hcp->dpgno;
1273*0Sstevel@tonic-gate
1274*0Sstevel@tonic-gate dbp = hcp->dbc->dbp;
1275*0Sstevel@tonic-gate DB_THREAD_LOCK(dbp);
1276*0Sstevel@tonic-gate
1277*0Sstevel@tonic-gate for (cp = TAILQ_FIRST(&dbp->active_queue); cp != NULL;
1278*0Sstevel@tonic-gate cp = TAILQ_NEXT(cp, links)) {
1279*0Sstevel@tonic-gate if (cp->internal == hcp)
1280*0Sstevel@tonic-gate continue;
1281*0Sstevel@tonic-gate
1282*0Sstevel@tonic-gate lcp = (HASH_CURSOR *)cp->internal;
1283*0Sstevel@tonic-gate
1284*0Sstevel@tonic-gate if (!is_dup && lcp->pgno != chg_pgno)
1285*0Sstevel@tonic-gate continue;
1286*0Sstevel@tonic-gate
1287*0Sstevel@tonic-gate if (is_dup) {
1288*0Sstevel@tonic-gate if (F_ISSET(hcp, H_DELETED) && lcp->pgno != chg_pgno)
1289*0Sstevel@tonic-gate continue;
1290*0Sstevel@tonic-gate if (!F_ISSET(hcp, H_DELETED) && lcp->dpgno != chg_pgno)
1291*0Sstevel@tonic-gate continue;
1292*0Sstevel@tonic-gate }
1293*0Sstevel@tonic-gate
1294*0Sstevel@tonic-gate if (page_deleted) {
1295*0Sstevel@tonic-gate if (is_dup) {
1296*0Sstevel@tonic-gate lcp->dpgno = hcp->dpgno;
1297*0Sstevel@tonic-gate lcp->dndx = hcp->dndx;
1298*0Sstevel@tonic-gate } else {
1299*0Sstevel@tonic-gate lcp->pgno = hcp->pgno;
1300*0Sstevel@tonic-gate lcp->bndx = hcp->bndx;
1301*0Sstevel@tonic-gate lcp->bucket = hcp->bucket;
1302*0Sstevel@tonic-gate }
1303*0Sstevel@tonic-gate F_CLR(lcp, H_ISDUP);
1304*0Sstevel@tonic-gate continue;
1305*0Sstevel@tonic-gate }
1306*0Sstevel@tonic-gate
1307*0Sstevel@tonic-gate if (!is_dup && lcp->bndx > hcp->bndx)
1308*0Sstevel@tonic-gate lcp->bndx--;
1309*0Sstevel@tonic-gate else if (!is_dup && lcp->bndx == hcp->bndx)
1310*0Sstevel@tonic-gate F_SET(lcp, H_DELETED);
1311*0Sstevel@tonic-gate else if (is_dup && lcp->bndx == hcp->bndx) {
1312*0Sstevel@tonic-gate /* Assign dpgno in case there was page conversion. */
1313*0Sstevel@tonic-gate lcp->dpgno = hcp->dpgno;
1314*0Sstevel@tonic-gate if (add && lcp->dndx >= hcp->dndx )
1315*0Sstevel@tonic-gate lcp->dndx++;
1316*0Sstevel@tonic-gate else if (!add && lcp->dndx > hcp->dndx)
1317*0Sstevel@tonic-gate lcp->dndx--;
1318*0Sstevel@tonic-gate else if (!add && lcp->dndx == hcp->dndx)
1319*0Sstevel@tonic-gate F_SET(lcp, H_DELETED);
1320*0Sstevel@tonic-gate
1321*0Sstevel@tonic-gate /* Now adjust on-page information. */
1322*0Sstevel@tonic-gate if (lcp->dpgno == PGNO_INVALID)
1323*0Sstevel@tonic-gate if (add) {
1324*0Sstevel@tonic-gate lcp->dup_tlen += len;
1325*0Sstevel@tonic-gate if (lcp->dndx > hcp->dndx)
1326*0Sstevel@tonic-gate lcp->dup_off += len;
1327*0Sstevel@tonic-gate } else {
1328*0Sstevel@tonic-gate lcp->dup_tlen -= len;
1329*0Sstevel@tonic-gate if (lcp->dndx > hcp->dndx)
1330*0Sstevel@tonic-gate lcp->dup_off -= len;
1331*0Sstevel@tonic-gate }
1332*0Sstevel@tonic-gate }
1333*0Sstevel@tonic-gate }
1334*0Sstevel@tonic-gate DB_THREAD_UNLOCK(dbp);
1335*0Sstevel@tonic-gate }
1336*0Sstevel@tonic-gate
1337