Deleted Added
full compact
hash.c (190486) hash.c (190489)
1/*-
2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Margo Seltzer.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#if defined(LIBC_SCCS) && !defined(lint)
34static char sccsid[] = "@(#)hash.c 8.9 (Berkeley) 6/16/94";
35#endif /* LIBC_SCCS and not lint */
36#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Margo Seltzer.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#if defined(LIBC_SCCS) && !defined(lint)
34static char sccsid[] = "@(#)hash.c 8.9 (Berkeley) 6/16/94";
35#endif /* LIBC_SCCS and not lint */
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD: head/lib/libc/db/hash/hash.c 190486 2009-03-28 06:05:53Z delphij $");
37__FBSDID("$FreeBSD: head/lib/libc/db/hash/hash.c 190489 2009-03-28 06:23:10Z delphij $");
38
39#include "namespace.h"
40#include <sys/param.h>
41#include <sys/stat.h>
42
43#include <errno.h>
44#include <fcntl.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include <unistd.h>
49#ifdef DEBUG
50#include <assert.h>
51#endif
52#include "un-namespace.h"
53
54#include <db.h>
55#include "hash.h"
56#include "page.h"
57#include "extern.h"
58
59static int alloc_segs(HTAB *, int);
60static int flush_meta(HTAB *);
61static int hash_access(HTAB *, ACTION, DBT *, DBT *);
62static int hash_close(DB *);
63static int hash_delete(const DB *, const DBT *, u_int32_t);
64static int hash_fd(const DB *);
65static int hash_get(const DB *, const DBT *, DBT *, u_int32_t);
66static int hash_put(const DB *, DBT *, const DBT *, u_int32_t);
67static void *hash_realloc(SEGMENT **, int, int);
68static int hash_seq(const DB *, DBT *, DBT *, u_int32_t);
69static int hash_sync(const DB *, u_int32_t);
70static int hdestroy(HTAB *);
71static HTAB *init_hash(HTAB *, const char *, const HASHINFO *);
72static int init_htab(HTAB *, int);
73#if BYTE_ORDER == LITTLE_ENDIAN
74static void swap_header(HTAB *);
75static void swap_header_copy(HASHHDR *, HASHHDR *);
76#endif
77
78/* Fast arithmetic, relying on powers of 2, */
79#define MOD(x, y) ((x) & ((y) - 1))
80
81#define RETURN_ERROR(ERR, LOC) { save_errno = ERR; goto LOC; }
82
83/* Return values */
84#define SUCCESS (0)
85#define ERROR (-1)
86#define ABNORMAL (1)
87
88#ifdef HASH_STATISTICS
89int hash_accesses, hash_collisions, hash_expansions, hash_overflows;
90#endif
91
92/************************** INTERFACE ROUTINES ***************************/
93/* OPEN/CLOSE */
94
95/* ARGSUSED */
96DB *
97__hash_open(const char *file, int flags, int mode,
98 const HASHINFO *info, /* Special directives for create */
99 int dflags)
100{
101 HTAB *hashp;
102 struct stat statbuf;
103 DB *dbp;
104 int bpages, hdrsize, new_table, nsegs, save_errno;
105
106 if ((flags & O_ACCMODE) == O_WRONLY) {
107 errno = EINVAL;
108 return (NULL);
109 }
110
111 if (!(hashp = (HTAB *)calloc(1, sizeof(HTAB))))
112 return (NULL);
113 hashp->fp = -1;
114
115 /*
116 * Even if user wants write only, we need to be able to read
117 * the actual file, so we need to open it read/write. But, the
118 * field in the hashp structure needs to be accurate so that
119 * we can check accesses.
120 */
121 hashp->flags = flags;
122
123 new_table = 0;
124 if (!file || (flags & O_TRUNC) ||
125 (stat(file, &statbuf) && (errno == ENOENT))) {
126 if (errno == ENOENT)
127 errno = 0; /* Just in case someone looks at errno */
128 new_table = 1;
129 }
130 if (file) {
131 if ((hashp->fp = _open(file, flags, mode)) == -1)
132 RETURN_ERROR(errno, error0);
133
134 /* if the .db file is empty, and we had permission to create
135 a new .db file, then reinitialize the database */
136 if ((flags & O_CREAT) &&
137 _fstat(hashp->fp, &statbuf) == 0 && statbuf.st_size == 0)
138 new_table = 1;
139
140 (void)_fcntl(hashp->fp, F_SETFD, 1);
141 }
142 if (new_table) {
143 if (!(hashp = init_hash(hashp, file, info)))
144 RETURN_ERROR(errno, error1);
145 } else {
146 /* Table already exists */
147 if (info && info->hash)
148 hashp->hash = info->hash;
149 else
150 hashp->hash = __default_hash;
151
152 hdrsize = _read(hashp->fp, &hashp->hdr, sizeof(HASHHDR));
153#if BYTE_ORDER == LITTLE_ENDIAN
154 swap_header(hashp);
155#endif
156 if (hdrsize == -1)
157 RETURN_ERROR(errno, error1);
158 if (hdrsize != sizeof(HASHHDR))
159 RETURN_ERROR(EFTYPE, error1);
160 /* Verify file type, versions and hash function */
161 if (hashp->MAGIC != HASHMAGIC)
162 RETURN_ERROR(EFTYPE, error1);
163#define OLDHASHVERSION 1
164 if (hashp->VERSION != HASHVERSION &&
165 hashp->VERSION != OLDHASHVERSION)
166 RETURN_ERROR(EFTYPE, error1);
167 if ((int32_t)hashp->hash(CHARKEY, sizeof(CHARKEY)) != hashp->H_CHARKEY)
168 RETURN_ERROR(EFTYPE, error1);
169 /*
170 * Figure out how many segments we need. Max_Bucket is the
171 * maximum bucket number, so the number of buckets is
172 * max_bucket + 1.
173 */
174 nsegs = (hashp->MAX_BUCKET + 1 + hashp->SGSIZE - 1) /
175 hashp->SGSIZE;
176 hashp->nsegs = 0;
177 if (alloc_segs(hashp, nsegs))
178 /*
179 * If alloc_segs fails, table will have been destroyed
180 * and errno will have been set.
181 */
182 return (NULL);
183 /* Read in bitmaps */
184 bpages = (hashp->SPARES[hashp->OVFL_POINT] +
185 (hashp->BSIZE << BYTE_SHIFT) - 1) >>
186 (hashp->BSHIFT + BYTE_SHIFT);
187
188 hashp->nmaps = bpages;
189 (void)memset(&hashp->mapp[0], 0, bpages * sizeof(u_int32_t *));
190 }
191
192 /* Initialize Buffer Manager */
193 if (info && info->cachesize)
194 __buf_init(hashp, info->cachesize);
195 else
196 __buf_init(hashp, DEF_BUFSIZE);
197
198 hashp->new_file = new_table;
199 hashp->save_file = file && (hashp->flags & O_RDWR);
200 hashp->cbucket = -1;
201 if (!(dbp = (DB *)malloc(sizeof(DB)))) {
202 save_errno = errno;
203 hdestroy(hashp);
204 errno = save_errno;
205 return (NULL);
206 }
207 dbp->internal = hashp;
208 dbp->close = hash_close;
209 dbp->del = hash_delete;
210 dbp->fd = hash_fd;
211 dbp->get = hash_get;
212 dbp->put = hash_put;
213 dbp->seq = hash_seq;
214 dbp->sync = hash_sync;
215 dbp->type = DB_HASH;
216
217#ifdef DEBUG
218 (void)fprintf(stderr,
219"%s\n%s%p\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%x\n%s%x\n%s%d\n%s%d\n",
220 "init_htab:",
221 "TABLE POINTER ", hashp,
222 "BUCKET SIZE ", hashp->BSIZE,
223 "BUCKET SHIFT ", hashp->BSHIFT,
224 "DIRECTORY SIZE ", hashp->DSIZE,
225 "SEGMENT SIZE ", hashp->SGSIZE,
226 "SEGMENT SHIFT ", hashp->SSHIFT,
227 "FILL FACTOR ", hashp->FFACTOR,
228 "MAX BUCKET ", hashp->MAX_BUCKET,
229 "OVFL POINT ", hashp->OVFL_POINT,
230 "LAST FREED ", hashp->LAST_FREED,
231 "HIGH MASK ", hashp->HIGH_MASK,
232 "LOW MASK ", hashp->LOW_MASK,
233 "NSEGS ", hashp->nsegs,
234 "NKEYS ", hashp->NKEYS);
235#endif
236#ifdef HASH_STATISTICS
237 hash_overflows = hash_accesses = hash_collisions = hash_expansions = 0;
238#endif
239 return (dbp);
240
241error1:
242 if (hashp != NULL)
243 (void)_close(hashp->fp);
244
245error0:
246 free(hashp);
247 errno = save_errno;
248 return (NULL);
249}
250
251static int
252hash_close(DB *dbp)
253{
254 HTAB *hashp;
255 int retval;
256
257 if (!dbp)
258 return (ERROR);
259
260 hashp = (HTAB *)dbp->internal;
261 retval = hdestroy(hashp);
262 free(dbp);
263 return (retval);
264}
265
266static int
267hash_fd(const DB *dbp)
268{
269 HTAB *hashp;
270
271 if (!dbp)
272 return (ERROR);
273
274 hashp = (HTAB *)dbp->internal;
275 if (hashp->fp == -1) {
276 errno = ENOENT;
277 return (-1);
278 }
279 return (hashp->fp);
280}
281
282/************************** LOCAL CREATION ROUTINES **********************/
283static HTAB *
284init_hash(HTAB *hashp, const char *file, const HASHINFO *info)
285{
286 struct stat statbuf;
287 int nelem;
288
289 nelem = 1;
290 hashp->NKEYS = 0;
291 hashp->LORDER = BYTE_ORDER;
292 hashp->BSIZE = DEF_BUCKET_SIZE;
293 hashp->BSHIFT = DEF_BUCKET_SHIFT;
294 hashp->SGSIZE = DEF_SEGSIZE;
295 hashp->SSHIFT = DEF_SEGSIZE_SHIFT;
296 hashp->DSIZE = DEF_DIRSIZE;
297 hashp->FFACTOR = DEF_FFACTOR;
298 hashp->hash = __default_hash;
299 memset(hashp->SPARES, 0, sizeof(hashp->SPARES));
300 memset(hashp->BITMAPS, 0, sizeof (hashp->BITMAPS));
301
302 /* Fix bucket size to be optimal for file system */
303 if (file != NULL) {
304 if (stat(file, &statbuf))
305 return (NULL);
306 hashp->BSIZE = statbuf.st_blksize;
307 hashp->BSHIFT = __log2(hashp->BSIZE);
308 }
309
310 if (info) {
311 if (info->bsize) {
312 /* Round pagesize up to power of 2 */
313 hashp->BSHIFT = __log2(info->bsize);
314 hashp->BSIZE = 1 << hashp->BSHIFT;
315 if (hashp->BSIZE > MAX_BSIZE) {
316 errno = EINVAL;
317 return (NULL);
318 }
319 }
320 if (info->ffactor)
321 hashp->FFACTOR = info->ffactor;
322 if (info->hash)
323 hashp->hash = info->hash;
324 if (info->nelem)
325 nelem = info->nelem;
326 if (info->lorder) {
327 if (info->lorder != BIG_ENDIAN &&
328 info->lorder != LITTLE_ENDIAN) {
329 errno = EINVAL;
330 return (NULL);
331 }
332 hashp->LORDER = info->lorder;
333 }
334 }
335 /* init_htab should destroy the table and set errno if it fails */
336 if (init_htab(hashp, nelem))
337 return (NULL);
338 else
339 return (hashp);
340}
341/*
342 * This calls alloc_segs which may run out of memory. Alloc_segs will destroy
343 * the table and set errno, so we just pass the error information along.
344 *
345 * Returns 0 on No Error
346 */
347static int
348init_htab(HTAB *hashp, int nelem)
349{
38
39#include "namespace.h"
40#include <sys/param.h>
41#include <sys/stat.h>
42
43#include <errno.h>
44#include <fcntl.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include <unistd.h>
49#ifdef DEBUG
50#include <assert.h>
51#endif
52#include "un-namespace.h"
53
54#include <db.h>
55#include "hash.h"
56#include "page.h"
57#include "extern.h"
58
59static int alloc_segs(HTAB *, int);
60static int flush_meta(HTAB *);
61static int hash_access(HTAB *, ACTION, DBT *, DBT *);
62static int hash_close(DB *);
63static int hash_delete(const DB *, const DBT *, u_int32_t);
64static int hash_fd(const DB *);
65static int hash_get(const DB *, const DBT *, DBT *, u_int32_t);
66static int hash_put(const DB *, DBT *, const DBT *, u_int32_t);
67static void *hash_realloc(SEGMENT **, int, int);
68static int hash_seq(const DB *, DBT *, DBT *, u_int32_t);
69static int hash_sync(const DB *, u_int32_t);
70static int hdestroy(HTAB *);
71static HTAB *init_hash(HTAB *, const char *, const HASHINFO *);
72static int init_htab(HTAB *, int);
73#if BYTE_ORDER == LITTLE_ENDIAN
74static void swap_header(HTAB *);
75static void swap_header_copy(HASHHDR *, HASHHDR *);
76#endif
77
78/* Fast arithmetic, relying on powers of 2, */
79#define MOD(x, y) ((x) & ((y) - 1))
80
81#define RETURN_ERROR(ERR, LOC) { save_errno = ERR; goto LOC; }
82
83/* Return values */
84#define SUCCESS (0)
85#define ERROR (-1)
86#define ABNORMAL (1)
87
88#ifdef HASH_STATISTICS
89int hash_accesses, hash_collisions, hash_expansions, hash_overflows;
90#endif
91
92/************************** INTERFACE ROUTINES ***************************/
93/* OPEN/CLOSE */
94
95/* ARGSUSED */
96DB *
97__hash_open(const char *file, int flags, int mode,
98 const HASHINFO *info, /* Special directives for create */
99 int dflags)
100{
101 HTAB *hashp;
102 struct stat statbuf;
103 DB *dbp;
104 int bpages, hdrsize, new_table, nsegs, save_errno;
105
106 if ((flags & O_ACCMODE) == O_WRONLY) {
107 errno = EINVAL;
108 return (NULL);
109 }
110
111 if (!(hashp = (HTAB *)calloc(1, sizeof(HTAB))))
112 return (NULL);
113 hashp->fp = -1;
114
115 /*
116 * Even if user wants write only, we need to be able to read
117 * the actual file, so we need to open it read/write. But, the
118 * field in the hashp structure needs to be accurate so that
119 * we can check accesses.
120 */
121 hashp->flags = flags;
122
123 new_table = 0;
124 if (!file || (flags & O_TRUNC) ||
125 (stat(file, &statbuf) && (errno == ENOENT))) {
126 if (errno == ENOENT)
127 errno = 0; /* Just in case someone looks at errno */
128 new_table = 1;
129 }
130 if (file) {
131 if ((hashp->fp = _open(file, flags, mode)) == -1)
132 RETURN_ERROR(errno, error0);
133
134 /* if the .db file is empty, and we had permission to create
135 a new .db file, then reinitialize the database */
136 if ((flags & O_CREAT) &&
137 _fstat(hashp->fp, &statbuf) == 0 && statbuf.st_size == 0)
138 new_table = 1;
139
140 (void)_fcntl(hashp->fp, F_SETFD, 1);
141 }
142 if (new_table) {
143 if (!(hashp = init_hash(hashp, file, info)))
144 RETURN_ERROR(errno, error1);
145 } else {
146 /* Table already exists */
147 if (info && info->hash)
148 hashp->hash = info->hash;
149 else
150 hashp->hash = __default_hash;
151
152 hdrsize = _read(hashp->fp, &hashp->hdr, sizeof(HASHHDR));
153#if BYTE_ORDER == LITTLE_ENDIAN
154 swap_header(hashp);
155#endif
156 if (hdrsize == -1)
157 RETURN_ERROR(errno, error1);
158 if (hdrsize != sizeof(HASHHDR))
159 RETURN_ERROR(EFTYPE, error1);
160 /* Verify file type, versions and hash function */
161 if (hashp->MAGIC != HASHMAGIC)
162 RETURN_ERROR(EFTYPE, error1);
163#define OLDHASHVERSION 1
164 if (hashp->VERSION != HASHVERSION &&
165 hashp->VERSION != OLDHASHVERSION)
166 RETURN_ERROR(EFTYPE, error1);
167 if ((int32_t)hashp->hash(CHARKEY, sizeof(CHARKEY)) != hashp->H_CHARKEY)
168 RETURN_ERROR(EFTYPE, error1);
169 /*
170 * Figure out how many segments we need. Max_Bucket is the
171 * maximum bucket number, so the number of buckets is
172 * max_bucket + 1.
173 */
174 nsegs = (hashp->MAX_BUCKET + 1 + hashp->SGSIZE - 1) /
175 hashp->SGSIZE;
176 hashp->nsegs = 0;
177 if (alloc_segs(hashp, nsegs))
178 /*
179 * If alloc_segs fails, table will have been destroyed
180 * and errno will have been set.
181 */
182 return (NULL);
183 /* Read in bitmaps */
184 bpages = (hashp->SPARES[hashp->OVFL_POINT] +
185 (hashp->BSIZE << BYTE_SHIFT) - 1) >>
186 (hashp->BSHIFT + BYTE_SHIFT);
187
188 hashp->nmaps = bpages;
189 (void)memset(&hashp->mapp[0], 0, bpages * sizeof(u_int32_t *));
190 }
191
192 /* Initialize Buffer Manager */
193 if (info && info->cachesize)
194 __buf_init(hashp, info->cachesize);
195 else
196 __buf_init(hashp, DEF_BUFSIZE);
197
198 hashp->new_file = new_table;
199 hashp->save_file = file && (hashp->flags & O_RDWR);
200 hashp->cbucket = -1;
201 if (!(dbp = (DB *)malloc(sizeof(DB)))) {
202 save_errno = errno;
203 hdestroy(hashp);
204 errno = save_errno;
205 return (NULL);
206 }
207 dbp->internal = hashp;
208 dbp->close = hash_close;
209 dbp->del = hash_delete;
210 dbp->fd = hash_fd;
211 dbp->get = hash_get;
212 dbp->put = hash_put;
213 dbp->seq = hash_seq;
214 dbp->sync = hash_sync;
215 dbp->type = DB_HASH;
216
217#ifdef DEBUG
218 (void)fprintf(stderr,
219"%s\n%s%p\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%x\n%s%x\n%s%d\n%s%d\n",
220 "init_htab:",
221 "TABLE POINTER ", hashp,
222 "BUCKET SIZE ", hashp->BSIZE,
223 "BUCKET SHIFT ", hashp->BSHIFT,
224 "DIRECTORY SIZE ", hashp->DSIZE,
225 "SEGMENT SIZE ", hashp->SGSIZE,
226 "SEGMENT SHIFT ", hashp->SSHIFT,
227 "FILL FACTOR ", hashp->FFACTOR,
228 "MAX BUCKET ", hashp->MAX_BUCKET,
229 "OVFL POINT ", hashp->OVFL_POINT,
230 "LAST FREED ", hashp->LAST_FREED,
231 "HIGH MASK ", hashp->HIGH_MASK,
232 "LOW MASK ", hashp->LOW_MASK,
233 "NSEGS ", hashp->nsegs,
234 "NKEYS ", hashp->NKEYS);
235#endif
236#ifdef HASH_STATISTICS
237 hash_overflows = hash_accesses = hash_collisions = hash_expansions = 0;
238#endif
239 return (dbp);
240
241error1:
242 if (hashp != NULL)
243 (void)_close(hashp->fp);
244
245error0:
246 free(hashp);
247 errno = save_errno;
248 return (NULL);
249}
250
251static int
252hash_close(DB *dbp)
253{
254 HTAB *hashp;
255 int retval;
256
257 if (!dbp)
258 return (ERROR);
259
260 hashp = (HTAB *)dbp->internal;
261 retval = hdestroy(hashp);
262 free(dbp);
263 return (retval);
264}
265
266static int
267hash_fd(const DB *dbp)
268{
269 HTAB *hashp;
270
271 if (!dbp)
272 return (ERROR);
273
274 hashp = (HTAB *)dbp->internal;
275 if (hashp->fp == -1) {
276 errno = ENOENT;
277 return (-1);
278 }
279 return (hashp->fp);
280}
281
282/************************** LOCAL CREATION ROUTINES **********************/
283static HTAB *
284init_hash(HTAB *hashp, const char *file, const HASHINFO *info)
285{
286 struct stat statbuf;
287 int nelem;
288
289 nelem = 1;
290 hashp->NKEYS = 0;
291 hashp->LORDER = BYTE_ORDER;
292 hashp->BSIZE = DEF_BUCKET_SIZE;
293 hashp->BSHIFT = DEF_BUCKET_SHIFT;
294 hashp->SGSIZE = DEF_SEGSIZE;
295 hashp->SSHIFT = DEF_SEGSIZE_SHIFT;
296 hashp->DSIZE = DEF_DIRSIZE;
297 hashp->FFACTOR = DEF_FFACTOR;
298 hashp->hash = __default_hash;
299 memset(hashp->SPARES, 0, sizeof(hashp->SPARES));
300 memset(hashp->BITMAPS, 0, sizeof (hashp->BITMAPS));
301
302 /* Fix bucket size to be optimal for file system */
303 if (file != NULL) {
304 if (stat(file, &statbuf))
305 return (NULL);
306 hashp->BSIZE = statbuf.st_blksize;
307 hashp->BSHIFT = __log2(hashp->BSIZE);
308 }
309
310 if (info) {
311 if (info->bsize) {
312 /* Round pagesize up to power of 2 */
313 hashp->BSHIFT = __log2(info->bsize);
314 hashp->BSIZE = 1 << hashp->BSHIFT;
315 if (hashp->BSIZE > MAX_BSIZE) {
316 errno = EINVAL;
317 return (NULL);
318 }
319 }
320 if (info->ffactor)
321 hashp->FFACTOR = info->ffactor;
322 if (info->hash)
323 hashp->hash = info->hash;
324 if (info->nelem)
325 nelem = info->nelem;
326 if (info->lorder) {
327 if (info->lorder != BIG_ENDIAN &&
328 info->lorder != LITTLE_ENDIAN) {
329 errno = EINVAL;
330 return (NULL);
331 }
332 hashp->LORDER = info->lorder;
333 }
334 }
335 /* init_htab should destroy the table and set errno if it fails */
336 if (init_htab(hashp, nelem))
337 return (NULL);
338 else
339 return (hashp);
340}
341/*
342 * This calls alloc_segs which may run out of memory. Alloc_segs will destroy
343 * the table and set errno, so we just pass the error information along.
344 *
345 * Returns 0 on No Error
346 */
347static int
348init_htab(HTAB *hashp, int nelem)
349{
350 int nbuckets, nsegs;
351 int l2;
350 int nbuckets, nsegs, l2;
352
353 /*
354 * Divide number of elements by the fill factor and determine a
355 * desired number of buckets. Allocate space for the next greater
356 * power of two number of buckets.
357 */
358 nelem = (nelem - 1) / hashp->FFACTOR + 1;
359
360 l2 = __log2(MAX(nelem, 2));
361 nbuckets = 1 << l2;
362
363 hashp->SPARES[l2] = l2 + 1;
364 hashp->SPARES[l2 + 1] = l2 + 1;
365 hashp->OVFL_POINT = l2;
366 hashp->LAST_FREED = 2;
367
368 /* First bitmap page is at: splitpoint l2 page offset 1 */
369 if (__ibitmap(hashp, OADDR_OF(l2, 1), l2 + 1, 0))
370 return (-1);
371
372 hashp->MAX_BUCKET = hashp->LOW_MASK = nbuckets - 1;
373 hashp->HIGH_MASK = (nbuckets << 1) - 1;
374 hashp->HDRPAGES = ((MAX(sizeof(HASHHDR), MINHDRSIZE) - 1) >>
375 hashp->BSHIFT) + 1;
376
377 nsegs = (nbuckets - 1) / hashp->SGSIZE + 1;
378 nsegs = 1 << __log2(nsegs);
379
380 if (nsegs > hashp->DSIZE)
381 hashp->DSIZE = nsegs;
382 return (alloc_segs(hashp, nsegs));
383}
384
385/********************** DESTROY/CLOSE ROUTINES ************************/
386
387/*
388 * Flushes any changes to the file if necessary and destroys the hashp
389 * structure, freeing all allocated space.
390 */
391static int
392hdestroy(HTAB *hashp)
393{
394 int i, save_errno;
395
396 save_errno = 0;
397
398#ifdef HASH_STATISTICS
399 (void)fprintf(stderr, "hdestroy: accesses %ld collisions %ld\n",
400 hash_accesses, hash_collisions);
401 (void)fprintf(stderr, "hdestroy: expansions %ld\n",
402 hash_expansions);
403 (void)fprintf(stderr, "hdestroy: overflows %ld\n",
404 hash_overflows);
405 (void)fprintf(stderr, "keys %ld maxp %d segmentcount %d\n",
406 hashp->NKEYS, hashp->MAX_BUCKET, hashp->nsegs);
407
408 for (i = 0; i < NCACHED; i++)
409 (void)fprintf(stderr,
410 "spares[%d] = %d\n", i, hashp->SPARES[i]);
411#endif
412 /*
413 * Call on buffer manager to free buffers, and if required,
414 * write them to disk.
415 */
416 if (__buf_free(hashp, 1, hashp->save_file))
417 save_errno = errno;
418 if (hashp->dir) {
419 free(*hashp->dir); /* Free initial segments */
420 /* Free extra segments */
421 while (hashp->exsegs--)
422 free(hashp->dir[--hashp->nsegs]);
423 free(hashp->dir);
424 }
425 if (flush_meta(hashp) && !save_errno)
426 save_errno = errno;
427 /* Free Bigmaps */
428 for (i = 0; i < hashp->nmaps; i++)
429 if (hashp->mapp[i])
430 free(hashp->mapp[i]);
431
432 if (hashp->fp != -1)
433 (void)_close(hashp->fp);
434
435 free(hashp);
436
437 if (save_errno) {
438 errno = save_errno;
439 return (ERROR);
440 }
441 return (SUCCESS);
442}
443/*
444 * Write modified pages to disk
445 *
446 * Returns:
447 * 0 == OK
448 * -1 ERROR
449 */
450static int
451hash_sync(const DB *dbp, u_int32_t flags)
452{
453 HTAB *hashp;
454
455 if (flags != 0) {
456 errno = EINVAL;
457 return (ERROR);
458 }
459
460 if (!dbp)
461 return (ERROR);
462
463 hashp = (HTAB *)dbp->internal;
464 if (!hashp->save_file)
465 return (0);
466 if (__buf_free(hashp, 0, 1) || flush_meta(hashp))
467 return (ERROR);
468 hashp->new_file = 0;
469 return (0);
470}
471
472/*
473 * Returns:
474 * 0 == OK
475 * -1 indicates that errno should be set
476 */
477static int
478flush_meta(HTAB *hashp)
479{
480 HASHHDR *whdrp;
481#if BYTE_ORDER == LITTLE_ENDIAN
482 HASHHDR whdr;
483#endif
484 int fp, i, wsize;
485
486 if (!hashp->save_file)
487 return (0);
488 hashp->MAGIC = HASHMAGIC;
489 hashp->VERSION = HASHVERSION;
490 hashp->H_CHARKEY = hashp->hash(CHARKEY, sizeof(CHARKEY));
491
492 fp = hashp->fp;
493 whdrp = &hashp->hdr;
494#if BYTE_ORDER == LITTLE_ENDIAN
495 whdrp = &whdr;
496 swap_header_copy(&hashp->hdr, whdrp);
497#endif
498 if ((wsize = pwrite(fp, whdrp, sizeof(HASHHDR), (off_t)0)) == -1)
499 return (-1);
500 else
501 if (wsize != sizeof(HASHHDR)) {
502 errno = EFTYPE;
503 hashp->error = errno;
504 return (-1);
505 }
506 for (i = 0; i < NCACHED; i++)
507 if (hashp->mapp[i])
508 if (__put_page(hashp, (char *)hashp->mapp[i],
509 hashp->BITMAPS[i], 0, 1))
510 return (-1);
511 return (0);
512}
513
514/*******************************SEARCH ROUTINES *****************************/
515/*
516 * All the access routines return
517 *
518 * Returns:
519 * 0 on SUCCESS
520 * 1 to indicate an external ERROR (i.e. key not found, etc)
521 * -1 to indicate an internal ERROR (i.e. out of memory, etc)
522 */
523static int
524hash_get(const DB *dbp, const DBT *key, DBT *data, u_int32_t flag)
525{
526 HTAB *hashp;
527
528 hashp = (HTAB *)dbp->internal;
529 if (flag) {
530 hashp->error = errno = EINVAL;
531 return (ERROR);
532 }
533 return (hash_access(hashp, HASH_GET, (DBT *)key, data));
534}
535
536static int
537hash_put(const DB *dbp, DBT *key, const DBT *data, u_int32_t flag)
538{
539 HTAB *hashp;
540
541 hashp = (HTAB *)dbp->internal;
542 if (flag && flag != R_NOOVERWRITE) {
543 hashp->error = EINVAL;
544 errno = EINVAL;
545 return (ERROR);
546 }
547 if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
548 hashp->error = errno = EPERM;
549 return (ERROR);
550 }
551 return (hash_access(hashp, flag == R_NOOVERWRITE ?
552 HASH_PUTNEW : HASH_PUT, (DBT *)key, (DBT *)data));
553}
554
555static int
556hash_delete(const DB *dbp, const DBT *key,
557 u_int32_t flag) /* Ignored */
558{
559 HTAB *hashp;
560
561 hashp = (HTAB *)dbp->internal;
562 if (flag && flag != R_CURSOR) {
563 hashp->error = errno = EINVAL;
564 return (ERROR);
565 }
566 if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
567 hashp->error = errno = EPERM;
568 return (ERROR);
569 }
570 return (hash_access(hashp, HASH_DELETE, (DBT *)key, NULL));
571}
572
573/*
574 * Assume that hashp has been set in wrapper routine.
575 */
576static int
577hash_access(HTAB *hashp, ACTION action, DBT *key, DBT *val)
578{
579 BUFHEAD *rbufp;
580 BUFHEAD *bufp, *save_bufp;
581 u_int16_t *bp;
582 int n, ndx, off, size;
583 char *kp;
584 u_int16_t pageno;
585
586#ifdef HASH_STATISTICS
587 hash_accesses++;
588#endif
589
590 off = hashp->BSIZE;
591 size = key->size;
592 kp = (char *)key->data;
593 rbufp = __get_buf(hashp, __call_hash(hashp, kp, size), NULL, 0);
594 if (!rbufp)
595 return (ERROR);
596 save_bufp = rbufp;
597
598 /* Pin the bucket chain */
599 rbufp->flags |= BUF_PIN;
600 for (bp = (u_int16_t *)rbufp->page, n = *bp++, ndx = 1; ndx < n;)
601 if (bp[1] >= REAL_KEY) {
602 /* Real key/data pair */
603 if (size == off - *bp &&
604 memcmp(kp, rbufp->page + *bp, size) == 0)
605 goto found;
606 off = bp[1];
607#ifdef HASH_STATISTICS
608 hash_collisions++;
609#endif
610 bp += 2;
611 ndx += 2;
612 } else if (bp[1] == OVFLPAGE) {
613 rbufp = __get_buf(hashp, *bp, rbufp, 0);
614 if (!rbufp) {
615 save_bufp->flags &= ~BUF_PIN;
616 return (ERROR);
617 }
618 /* FOR LOOP INIT */
619 bp = (u_int16_t *)rbufp->page;
620 n = *bp++;
621 ndx = 1;
622 off = hashp->BSIZE;
623 } else if (bp[1] < REAL_KEY) {
624 if ((ndx =
625 __find_bigpair(hashp, rbufp, ndx, kp, size)) > 0)
626 goto found;
627 if (ndx == -2) {
628 bufp = rbufp;
629 if (!(pageno =
630 __find_last_page(hashp, &bufp))) {
631 ndx = 0;
632 rbufp = bufp;
633 break; /* FOR */
634 }
635 rbufp = __get_buf(hashp, pageno, bufp, 0);
636 if (!rbufp) {
637 save_bufp->flags &= ~BUF_PIN;
638 return (ERROR);
639 }
640 /* FOR LOOP INIT */
641 bp = (u_int16_t *)rbufp->page;
642 n = *bp++;
643 ndx = 1;
644 off = hashp->BSIZE;
645 } else {
646 save_bufp->flags &= ~BUF_PIN;
647 return (ERROR);
648 }
649 }
650
651 /* Not found */
652 switch (action) {
653 case HASH_PUT:
654 case HASH_PUTNEW:
655 if (__addel(hashp, rbufp, key, val)) {
656 save_bufp->flags &= ~BUF_PIN;
657 return (ERROR);
658 } else {
659 save_bufp->flags &= ~BUF_PIN;
660 return (SUCCESS);
661 }
662 case HASH_GET:
663 case HASH_DELETE:
664 default:
665 save_bufp->flags &= ~BUF_PIN;
666 return (ABNORMAL);
667 }
668
669found:
670 switch (action) {
671 case HASH_PUTNEW:
672 save_bufp->flags &= ~BUF_PIN;
673 return (ABNORMAL);
674 case HASH_GET:
675 bp = (u_int16_t *)rbufp->page;
676 if (bp[ndx + 1] < REAL_KEY) {
677 if (__big_return(hashp, rbufp, ndx, val, 0))
678 return (ERROR);
679 } else {
680 val->data = (u_char *)rbufp->page + (int)bp[ndx + 1];
681 val->size = bp[ndx] - bp[ndx + 1];
682 }
683 break;
684 case HASH_PUT:
685 if ((__delpair(hashp, rbufp, ndx)) ||
686 (__addel(hashp, rbufp, key, val))) {
687 save_bufp->flags &= ~BUF_PIN;
688 return (ERROR);
689 }
690 break;
691 case HASH_DELETE:
692 if (__delpair(hashp, rbufp, ndx))
693 return (ERROR);
694 break;
695 default:
696 abort();
697 }
698 save_bufp->flags &= ~BUF_PIN;
699 return (SUCCESS);
700}
701
702static int
703hash_seq(const DB *dbp, DBT *key, DBT *data, u_int32_t flag)
704{
705 u_int32_t bucket;
706 BUFHEAD *bufp;
707 HTAB *hashp;
708 u_int16_t *bp, ndx;
709
710 hashp = (HTAB *)dbp->internal;
711 if (flag && flag != R_FIRST && flag != R_NEXT) {
712 hashp->error = errno = EINVAL;
713 return (ERROR);
714 }
715#ifdef HASH_STATISTICS
716 hash_accesses++;
717#endif
718 if ((hashp->cbucket < 0) || (flag == R_FIRST)) {
719 hashp->cbucket = 0;
720 hashp->cndx = 1;
721 hashp->cpage = NULL;
722 }
723
724 for (bp = NULL; !bp || !bp[0]; ) {
725 if (!(bufp = hashp->cpage)) {
726 for (bucket = hashp->cbucket;
727 bucket <= hashp->MAX_BUCKET;
728 bucket++, hashp->cndx = 1) {
729 bufp = __get_buf(hashp, bucket, NULL, 0);
730 if (!bufp)
731 return (ERROR);
732 hashp->cpage = bufp;
733 bp = (u_int16_t *)bufp->page;
734 if (bp[0])
735 break;
736 }
737 hashp->cbucket = bucket;
738 if ((u_int32_t)hashp->cbucket > hashp->MAX_BUCKET) {
739 hashp->cbucket = -1;
740 return (ABNORMAL);
741 }
742 } else
743 bp = (u_int16_t *)hashp->cpage->page;
744
745#ifdef DEBUG
746 assert(bp);
747 assert(bufp);
748#endif
749 while (bp[hashp->cndx + 1] == OVFLPAGE) {
750 bufp = hashp->cpage =
751 __get_buf(hashp, bp[hashp->cndx], bufp, 0);
752 if (!bufp)
753 return (ERROR);
754 bp = (u_int16_t *)(bufp->page);
755 hashp->cndx = 1;
756 }
757 if (!bp[0]) {
758 hashp->cpage = NULL;
759 ++hashp->cbucket;
760 }
761 }
762 ndx = hashp->cndx;
763 if (bp[ndx + 1] < REAL_KEY) {
764 if (__big_keydata(hashp, bufp, key, data, 1))
765 return (ERROR);
766 } else {
767 key->data = (u_char *)hashp->cpage->page + bp[ndx];
768 key->size = (ndx > 1 ? bp[ndx - 1] : hashp->BSIZE) - bp[ndx];
769 data->data = (u_char *)hashp->cpage->page + bp[ndx + 1];
770 data->size = bp[ndx] - bp[ndx + 1];
771 ndx += 2;
772 if (ndx > bp[0]) {
773 hashp->cpage = NULL;
774 hashp->cbucket++;
775 hashp->cndx = 1;
776 } else
777 hashp->cndx = ndx;
778 }
779 return (SUCCESS);
780}
781
782/********************************* UTILITIES ************************/
783
784/*
785 * Returns:
786 * 0 ==> OK
787 * -1 ==> Error
788 */
789int
790__expand_table(HTAB *hashp)
791{
792 u_int32_t old_bucket, new_bucket;
793 int dirsize, new_segnum, spare_ndx;
794
795#ifdef HASH_STATISTICS
796 hash_expansions++;
797#endif
798 new_bucket = ++hashp->MAX_BUCKET;
799 old_bucket = (hashp->MAX_BUCKET & hashp->LOW_MASK);
800
801 new_segnum = new_bucket >> hashp->SSHIFT;
802
803 /* Check if we need a new segment */
804 if (new_segnum >= hashp->nsegs) {
805 /* Check if we need to expand directory */
806 if (new_segnum >= hashp->DSIZE) {
807 /* Reallocate directory */
808 dirsize = hashp->DSIZE * sizeof(SEGMENT *);
809 if (!hash_realloc(&hashp->dir, dirsize, dirsize << 1))
810 return (-1);
811 hashp->DSIZE = dirsize << 1;
812 }
813 if ((hashp->dir[new_segnum] =
814 (SEGMENT)calloc(hashp->SGSIZE, sizeof(SEGMENT))) == NULL)
815 return (-1);
816 hashp->exsegs++;
817 hashp->nsegs++;
818 }
819 /*
820 * If the split point is increasing (MAX_BUCKET's log base 2
821 * * increases), we need to copy the current contents of the spare
822 * split bucket to the next bucket.
823 */
824 spare_ndx = __log2(hashp->MAX_BUCKET + 1);
825 if (spare_ndx > hashp->OVFL_POINT) {
826 hashp->SPARES[spare_ndx] = hashp->SPARES[hashp->OVFL_POINT];
827 hashp->OVFL_POINT = spare_ndx;
828 }
829
830 if (new_bucket > hashp->HIGH_MASK) {
831 /* Starting a new doubling */
832 hashp->LOW_MASK = hashp->HIGH_MASK;
833 hashp->HIGH_MASK = new_bucket | hashp->LOW_MASK;
834 }
835 /* Relocate records to the new bucket */
836 return (__split_page(hashp, old_bucket, new_bucket));
837}
838
839/*
840 * If realloc guarantees that the pointer is not destroyed if the realloc
841 * fails, then this routine can go away.
842 */
843static void *
844hash_realloc(SEGMENT **p_ptr, int oldsize, int newsize)
845{
846 void *p;
847
848 if ( (p = malloc(newsize)) ) {
849 memmove(p, *p_ptr, oldsize);
850 memset((char *)p + oldsize, 0, newsize - oldsize);
851 free(*p_ptr);
852 *p_ptr = p;
853 }
854 return (p);
855}
856
857u_int32_t
858__call_hash(HTAB *hashp, char *k, int len)
859{
860 unsigned int n, bucket;
861
862 n = hashp->hash(k, len);
863 bucket = n & hashp->HIGH_MASK;
864 if (bucket > hashp->MAX_BUCKET)
865 bucket = bucket & hashp->LOW_MASK;
866 return (bucket);
867}
868
869/*
870 * Allocate segment table. On error, destroy the table and set errno.
871 *
872 * Returns 0 on success
873 */
874static int
875alloc_segs(HTAB *hashp, int nsegs)
876{
877 int i;
878 SEGMENT store;
879
880 int save_errno;
881
882 if ((hashp->dir =
883 (SEGMENT *)calloc(hashp->DSIZE, sizeof(SEGMENT *))) == NULL) {
884 save_errno = errno;
885 (void)hdestroy(hashp);
886 errno = save_errno;
887 return (-1);
888 }
889 /* Allocate segments */
890 if ((store =
891 (SEGMENT)calloc(nsegs << hashp->SSHIFT, sizeof(SEGMENT))) == NULL) {
892 save_errno = errno;
893 (void)hdestroy(hashp);
894 errno = save_errno;
895 return (-1);
896 }
897 for (i = 0; i < nsegs; i++, hashp->nsegs++)
898 hashp->dir[i] = &store[i << hashp->SSHIFT];
899 return (0);
900}
901
902#if BYTE_ORDER == LITTLE_ENDIAN
903/*
904 * Hashp->hdr needs to be byteswapped.
905 */
906static void
907swap_header_copy(HASHHDR *srcp, HASHHDR *destp)
908{
909 int i;
910
911 P_32_COPY(srcp->magic, destp->magic);
912 P_32_COPY(srcp->version, destp->version);
913 P_32_COPY(srcp->lorder, destp->lorder);
914 P_32_COPY(srcp->bsize, destp->bsize);
915 P_32_COPY(srcp->bshift, destp->bshift);
916 P_32_COPY(srcp->dsize, destp->dsize);
917 P_32_COPY(srcp->ssize, destp->ssize);
918 P_32_COPY(srcp->sshift, destp->sshift);
919 P_32_COPY(srcp->ovfl_point, destp->ovfl_point);
920 P_32_COPY(srcp->last_freed, destp->last_freed);
921 P_32_COPY(srcp->max_bucket, destp->max_bucket);
922 P_32_COPY(srcp->high_mask, destp->high_mask);
923 P_32_COPY(srcp->low_mask, destp->low_mask);
924 P_32_COPY(srcp->ffactor, destp->ffactor);
925 P_32_COPY(srcp->nkeys, destp->nkeys);
926 P_32_COPY(srcp->hdrpages, destp->hdrpages);
927 P_32_COPY(srcp->h_charkey, destp->h_charkey);
928 for (i = 0; i < NCACHED; i++) {
929 P_32_COPY(srcp->spares[i], destp->spares[i]);
930 P_16_COPY(srcp->bitmaps[i], destp->bitmaps[i]);
931 }
932}
933
934static void
935swap_header(HTAB *hashp)
936{
937 HASHHDR *hdrp;
938 int i;
939
940 hdrp = &hashp->hdr;
941
942 M_32_SWAP(hdrp->magic);
943 M_32_SWAP(hdrp->version);
944 M_32_SWAP(hdrp->lorder);
945 M_32_SWAP(hdrp->bsize);
946 M_32_SWAP(hdrp->bshift);
947 M_32_SWAP(hdrp->dsize);
948 M_32_SWAP(hdrp->ssize);
949 M_32_SWAP(hdrp->sshift);
950 M_32_SWAP(hdrp->ovfl_point);
951 M_32_SWAP(hdrp->last_freed);
952 M_32_SWAP(hdrp->max_bucket);
953 M_32_SWAP(hdrp->high_mask);
954 M_32_SWAP(hdrp->low_mask);
955 M_32_SWAP(hdrp->ffactor);
956 M_32_SWAP(hdrp->nkeys);
957 M_32_SWAP(hdrp->hdrpages);
958 M_32_SWAP(hdrp->h_charkey);
959 for (i = 0; i < NCACHED; i++) {
960 M_32_SWAP(hdrp->spares[i]);
961 M_16_SWAP(hdrp->bitmaps[i]);
962 }
963}
964#endif
351
352 /*
353 * Divide number of elements by the fill factor and determine a
354 * desired number of buckets. Allocate space for the next greater
355 * power of two number of buckets.
356 */
357 nelem = (nelem - 1) / hashp->FFACTOR + 1;
358
359 l2 = __log2(MAX(nelem, 2));
360 nbuckets = 1 << l2;
361
362 hashp->SPARES[l2] = l2 + 1;
363 hashp->SPARES[l2 + 1] = l2 + 1;
364 hashp->OVFL_POINT = l2;
365 hashp->LAST_FREED = 2;
366
367 /* First bitmap page is at: splitpoint l2 page offset 1 */
368 if (__ibitmap(hashp, OADDR_OF(l2, 1), l2 + 1, 0))
369 return (-1);
370
371 hashp->MAX_BUCKET = hashp->LOW_MASK = nbuckets - 1;
372 hashp->HIGH_MASK = (nbuckets << 1) - 1;
373 hashp->HDRPAGES = ((MAX(sizeof(HASHHDR), MINHDRSIZE) - 1) >>
374 hashp->BSHIFT) + 1;
375
376 nsegs = (nbuckets - 1) / hashp->SGSIZE + 1;
377 nsegs = 1 << __log2(nsegs);
378
379 if (nsegs > hashp->DSIZE)
380 hashp->DSIZE = nsegs;
381 return (alloc_segs(hashp, nsegs));
382}
383
384/********************** DESTROY/CLOSE ROUTINES ************************/
385
386/*
387 * Flushes any changes to the file if necessary and destroys the hashp
388 * structure, freeing all allocated space.
389 */
390static int
391hdestroy(HTAB *hashp)
392{
393 int i, save_errno;
394
395 save_errno = 0;
396
397#ifdef HASH_STATISTICS
398 (void)fprintf(stderr, "hdestroy: accesses %ld collisions %ld\n",
399 hash_accesses, hash_collisions);
400 (void)fprintf(stderr, "hdestroy: expansions %ld\n",
401 hash_expansions);
402 (void)fprintf(stderr, "hdestroy: overflows %ld\n",
403 hash_overflows);
404 (void)fprintf(stderr, "keys %ld maxp %d segmentcount %d\n",
405 hashp->NKEYS, hashp->MAX_BUCKET, hashp->nsegs);
406
407 for (i = 0; i < NCACHED; i++)
408 (void)fprintf(stderr,
409 "spares[%d] = %d\n", i, hashp->SPARES[i]);
410#endif
411 /*
412 * Call on buffer manager to free buffers, and if required,
413 * write them to disk.
414 */
415 if (__buf_free(hashp, 1, hashp->save_file))
416 save_errno = errno;
417 if (hashp->dir) {
418 free(*hashp->dir); /* Free initial segments */
419 /* Free extra segments */
420 while (hashp->exsegs--)
421 free(hashp->dir[--hashp->nsegs]);
422 free(hashp->dir);
423 }
424 if (flush_meta(hashp) && !save_errno)
425 save_errno = errno;
426 /* Free Bigmaps */
427 for (i = 0; i < hashp->nmaps; i++)
428 if (hashp->mapp[i])
429 free(hashp->mapp[i]);
430
431 if (hashp->fp != -1)
432 (void)_close(hashp->fp);
433
434 free(hashp);
435
436 if (save_errno) {
437 errno = save_errno;
438 return (ERROR);
439 }
440 return (SUCCESS);
441}
442/*
443 * Write modified pages to disk
444 *
445 * Returns:
446 * 0 == OK
447 * -1 ERROR
448 */
449static int
450hash_sync(const DB *dbp, u_int32_t flags)
451{
452 HTAB *hashp;
453
454 if (flags != 0) {
455 errno = EINVAL;
456 return (ERROR);
457 }
458
459 if (!dbp)
460 return (ERROR);
461
462 hashp = (HTAB *)dbp->internal;
463 if (!hashp->save_file)
464 return (0);
465 if (__buf_free(hashp, 0, 1) || flush_meta(hashp))
466 return (ERROR);
467 hashp->new_file = 0;
468 return (0);
469}
470
471/*
472 * Returns:
473 * 0 == OK
474 * -1 indicates that errno should be set
475 */
476static int
477flush_meta(HTAB *hashp)
478{
479 HASHHDR *whdrp;
480#if BYTE_ORDER == LITTLE_ENDIAN
481 HASHHDR whdr;
482#endif
483 int fp, i, wsize;
484
485 if (!hashp->save_file)
486 return (0);
487 hashp->MAGIC = HASHMAGIC;
488 hashp->VERSION = HASHVERSION;
489 hashp->H_CHARKEY = hashp->hash(CHARKEY, sizeof(CHARKEY));
490
491 fp = hashp->fp;
492 whdrp = &hashp->hdr;
493#if BYTE_ORDER == LITTLE_ENDIAN
494 whdrp = &whdr;
495 swap_header_copy(&hashp->hdr, whdrp);
496#endif
497 if ((wsize = pwrite(fp, whdrp, sizeof(HASHHDR), (off_t)0)) == -1)
498 return (-1);
499 else
500 if (wsize != sizeof(HASHHDR)) {
501 errno = EFTYPE;
502 hashp->error = errno;
503 return (-1);
504 }
505 for (i = 0; i < NCACHED; i++)
506 if (hashp->mapp[i])
507 if (__put_page(hashp, (char *)hashp->mapp[i],
508 hashp->BITMAPS[i], 0, 1))
509 return (-1);
510 return (0);
511}
512
513/*******************************SEARCH ROUTINES *****************************/
514/*
515 * All the access routines return
516 *
517 * Returns:
518 * 0 on SUCCESS
519 * 1 to indicate an external ERROR (i.e. key not found, etc)
520 * -1 to indicate an internal ERROR (i.e. out of memory, etc)
521 */
522static int
523hash_get(const DB *dbp, const DBT *key, DBT *data, u_int32_t flag)
524{
525 HTAB *hashp;
526
527 hashp = (HTAB *)dbp->internal;
528 if (flag) {
529 hashp->error = errno = EINVAL;
530 return (ERROR);
531 }
532 return (hash_access(hashp, HASH_GET, (DBT *)key, data));
533}
534
535static int
536hash_put(const DB *dbp, DBT *key, const DBT *data, u_int32_t flag)
537{
538 HTAB *hashp;
539
540 hashp = (HTAB *)dbp->internal;
541 if (flag && flag != R_NOOVERWRITE) {
542 hashp->error = EINVAL;
543 errno = EINVAL;
544 return (ERROR);
545 }
546 if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
547 hashp->error = errno = EPERM;
548 return (ERROR);
549 }
550 return (hash_access(hashp, flag == R_NOOVERWRITE ?
551 HASH_PUTNEW : HASH_PUT, (DBT *)key, (DBT *)data));
552}
553
554static int
555hash_delete(const DB *dbp, const DBT *key,
556 u_int32_t flag) /* Ignored */
557{
558 HTAB *hashp;
559
560 hashp = (HTAB *)dbp->internal;
561 if (flag && flag != R_CURSOR) {
562 hashp->error = errno = EINVAL;
563 return (ERROR);
564 }
565 if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
566 hashp->error = errno = EPERM;
567 return (ERROR);
568 }
569 return (hash_access(hashp, HASH_DELETE, (DBT *)key, NULL));
570}
571
572/*
573 * Assume that hashp has been set in wrapper routine.
574 */
575static int
576hash_access(HTAB *hashp, ACTION action, DBT *key, DBT *val)
577{
578 BUFHEAD *rbufp;
579 BUFHEAD *bufp, *save_bufp;
580 u_int16_t *bp;
581 int n, ndx, off, size;
582 char *kp;
583 u_int16_t pageno;
584
585#ifdef HASH_STATISTICS
586 hash_accesses++;
587#endif
588
589 off = hashp->BSIZE;
590 size = key->size;
591 kp = (char *)key->data;
592 rbufp = __get_buf(hashp, __call_hash(hashp, kp, size), NULL, 0);
593 if (!rbufp)
594 return (ERROR);
595 save_bufp = rbufp;
596
597 /* Pin the bucket chain */
598 rbufp->flags |= BUF_PIN;
599 for (bp = (u_int16_t *)rbufp->page, n = *bp++, ndx = 1; ndx < n;)
600 if (bp[1] >= REAL_KEY) {
601 /* Real key/data pair */
602 if (size == off - *bp &&
603 memcmp(kp, rbufp->page + *bp, size) == 0)
604 goto found;
605 off = bp[1];
606#ifdef HASH_STATISTICS
607 hash_collisions++;
608#endif
609 bp += 2;
610 ndx += 2;
611 } else if (bp[1] == OVFLPAGE) {
612 rbufp = __get_buf(hashp, *bp, rbufp, 0);
613 if (!rbufp) {
614 save_bufp->flags &= ~BUF_PIN;
615 return (ERROR);
616 }
617 /* FOR LOOP INIT */
618 bp = (u_int16_t *)rbufp->page;
619 n = *bp++;
620 ndx = 1;
621 off = hashp->BSIZE;
622 } else if (bp[1] < REAL_KEY) {
623 if ((ndx =
624 __find_bigpair(hashp, rbufp, ndx, kp, size)) > 0)
625 goto found;
626 if (ndx == -2) {
627 bufp = rbufp;
628 if (!(pageno =
629 __find_last_page(hashp, &bufp))) {
630 ndx = 0;
631 rbufp = bufp;
632 break; /* FOR */
633 }
634 rbufp = __get_buf(hashp, pageno, bufp, 0);
635 if (!rbufp) {
636 save_bufp->flags &= ~BUF_PIN;
637 return (ERROR);
638 }
639 /* FOR LOOP INIT */
640 bp = (u_int16_t *)rbufp->page;
641 n = *bp++;
642 ndx = 1;
643 off = hashp->BSIZE;
644 } else {
645 save_bufp->flags &= ~BUF_PIN;
646 return (ERROR);
647 }
648 }
649
650 /* Not found */
651 switch (action) {
652 case HASH_PUT:
653 case HASH_PUTNEW:
654 if (__addel(hashp, rbufp, key, val)) {
655 save_bufp->flags &= ~BUF_PIN;
656 return (ERROR);
657 } else {
658 save_bufp->flags &= ~BUF_PIN;
659 return (SUCCESS);
660 }
661 case HASH_GET:
662 case HASH_DELETE:
663 default:
664 save_bufp->flags &= ~BUF_PIN;
665 return (ABNORMAL);
666 }
667
668found:
669 switch (action) {
670 case HASH_PUTNEW:
671 save_bufp->flags &= ~BUF_PIN;
672 return (ABNORMAL);
673 case HASH_GET:
674 bp = (u_int16_t *)rbufp->page;
675 if (bp[ndx + 1] < REAL_KEY) {
676 if (__big_return(hashp, rbufp, ndx, val, 0))
677 return (ERROR);
678 } else {
679 val->data = (u_char *)rbufp->page + (int)bp[ndx + 1];
680 val->size = bp[ndx] - bp[ndx + 1];
681 }
682 break;
683 case HASH_PUT:
684 if ((__delpair(hashp, rbufp, ndx)) ||
685 (__addel(hashp, rbufp, key, val))) {
686 save_bufp->flags &= ~BUF_PIN;
687 return (ERROR);
688 }
689 break;
690 case HASH_DELETE:
691 if (__delpair(hashp, rbufp, ndx))
692 return (ERROR);
693 break;
694 default:
695 abort();
696 }
697 save_bufp->flags &= ~BUF_PIN;
698 return (SUCCESS);
699}
700
701static int
702hash_seq(const DB *dbp, DBT *key, DBT *data, u_int32_t flag)
703{
704 u_int32_t bucket;
705 BUFHEAD *bufp;
706 HTAB *hashp;
707 u_int16_t *bp, ndx;
708
709 hashp = (HTAB *)dbp->internal;
710 if (flag && flag != R_FIRST && flag != R_NEXT) {
711 hashp->error = errno = EINVAL;
712 return (ERROR);
713 }
714#ifdef HASH_STATISTICS
715 hash_accesses++;
716#endif
717 if ((hashp->cbucket < 0) || (flag == R_FIRST)) {
718 hashp->cbucket = 0;
719 hashp->cndx = 1;
720 hashp->cpage = NULL;
721 }
722
723 for (bp = NULL; !bp || !bp[0]; ) {
724 if (!(bufp = hashp->cpage)) {
725 for (bucket = hashp->cbucket;
726 bucket <= hashp->MAX_BUCKET;
727 bucket++, hashp->cndx = 1) {
728 bufp = __get_buf(hashp, bucket, NULL, 0);
729 if (!bufp)
730 return (ERROR);
731 hashp->cpage = bufp;
732 bp = (u_int16_t *)bufp->page;
733 if (bp[0])
734 break;
735 }
736 hashp->cbucket = bucket;
737 if ((u_int32_t)hashp->cbucket > hashp->MAX_BUCKET) {
738 hashp->cbucket = -1;
739 return (ABNORMAL);
740 }
741 } else
742 bp = (u_int16_t *)hashp->cpage->page;
743
744#ifdef DEBUG
745 assert(bp);
746 assert(bufp);
747#endif
748 while (bp[hashp->cndx + 1] == OVFLPAGE) {
749 bufp = hashp->cpage =
750 __get_buf(hashp, bp[hashp->cndx], bufp, 0);
751 if (!bufp)
752 return (ERROR);
753 bp = (u_int16_t *)(bufp->page);
754 hashp->cndx = 1;
755 }
756 if (!bp[0]) {
757 hashp->cpage = NULL;
758 ++hashp->cbucket;
759 }
760 }
761 ndx = hashp->cndx;
762 if (bp[ndx + 1] < REAL_KEY) {
763 if (__big_keydata(hashp, bufp, key, data, 1))
764 return (ERROR);
765 } else {
766 key->data = (u_char *)hashp->cpage->page + bp[ndx];
767 key->size = (ndx > 1 ? bp[ndx - 1] : hashp->BSIZE) - bp[ndx];
768 data->data = (u_char *)hashp->cpage->page + bp[ndx + 1];
769 data->size = bp[ndx] - bp[ndx + 1];
770 ndx += 2;
771 if (ndx > bp[0]) {
772 hashp->cpage = NULL;
773 hashp->cbucket++;
774 hashp->cndx = 1;
775 } else
776 hashp->cndx = ndx;
777 }
778 return (SUCCESS);
779}
780
781/********************************* UTILITIES ************************/
782
783/*
784 * Returns:
785 * 0 ==> OK
786 * -1 ==> Error
787 */
788int
789__expand_table(HTAB *hashp)
790{
791 u_int32_t old_bucket, new_bucket;
792 int dirsize, new_segnum, spare_ndx;
793
794#ifdef HASH_STATISTICS
795 hash_expansions++;
796#endif
797 new_bucket = ++hashp->MAX_BUCKET;
798 old_bucket = (hashp->MAX_BUCKET & hashp->LOW_MASK);
799
800 new_segnum = new_bucket >> hashp->SSHIFT;
801
802 /* Check if we need a new segment */
803 if (new_segnum >= hashp->nsegs) {
804 /* Check if we need to expand directory */
805 if (new_segnum >= hashp->DSIZE) {
806 /* Reallocate directory */
807 dirsize = hashp->DSIZE * sizeof(SEGMENT *);
808 if (!hash_realloc(&hashp->dir, dirsize, dirsize << 1))
809 return (-1);
810 hashp->DSIZE = dirsize << 1;
811 }
812 if ((hashp->dir[new_segnum] =
813 (SEGMENT)calloc(hashp->SGSIZE, sizeof(SEGMENT))) == NULL)
814 return (-1);
815 hashp->exsegs++;
816 hashp->nsegs++;
817 }
818 /*
819 * If the split point is increasing (MAX_BUCKET's log base 2
820 * * increases), we need to copy the current contents of the spare
821 * split bucket to the next bucket.
822 */
823 spare_ndx = __log2(hashp->MAX_BUCKET + 1);
824 if (spare_ndx > hashp->OVFL_POINT) {
825 hashp->SPARES[spare_ndx] = hashp->SPARES[hashp->OVFL_POINT];
826 hashp->OVFL_POINT = spare_ndx;
827 }
828
829 if (new_bucket > hashp->HIGH_MASK) {
830 /* Starting a new doubling */
831 hashp->LOW_MASK = hashp->HIGH_MASK;
832 hashp->HIGH_MASK = new_bucket | hashp->LOW_MASK;
833 }
834 /* Relocate records to the new bucket */
835 return (__split_page(hashp, old_bucket, new_bucket));
836}
837
838/*
839 * If realloc guarantees that the pointer is not destroyed if the realloc
840 * fails, then this routine can go away.
841 */
842static void *
843hash_realloc(SEGMENT **p_ptr, int oldsize, int newsize)
844{
845 void *p;
846
847 if ( (p = malloc(newsize)) ) {
848 memmove(p, *p_ptr, oldsize);
849 memset((char *)p + oldsize, 0, newsize - oldsize);
850 free(*p_ptr);
851 *p_ptr = p;
852 }
853 return (p);
854}
855
856u_int32_t
857__call_hash(HTAB *hashp, char *k, int len)
858{
859 unsigned int n, bucket;
860
861 n = hashp->hash(k, len);
862 bucket = n & hashp->HIGH_MASK;
863 if (bucket > hashp->MAX_BUCKET)
864 bucket = bucket & hashp->LOW_MASK;
865 return (bucket);
866}
867
868/*
869 * Allocate segment table. On error, destroy the table and set errno.
870 *
871 * Returns 0 on success
872 */
873static int
874alloc_segs(HTAB *hashp, int nsegs)
875{
876 int i;
877 SEGMENT store;
878
879 int save_errno;
880
881 if ((hashp->dir =
882 (SEGMENT *)calloc(hashp->DSIZE, sizeof(SEGMENT *))) == NULL) {
883 save_errno = errno;
884 (void)hdestroy(hashp);
885 errno = save_errno;
886 return (-1);
887 }
888 /* Allocate segments */
889 if ((store =
890 (SEGMENT)calloc(nsegs << hashp->SSHIFT, sizeof(SEGMENT))) == NULL) {
891 save_errno = errno;
892 (void)hdestroy(hashp);
893 errno = save_errno;
894 return (-1);
895 }
896 for (i = 0; i < nsegs; i++, hashp->nsegs++)
897 hashp->dir[i] = &store[i << hashp->SSHIFT];
898 return (0);
899}
900
901#if BYTE_ORDER == LITTLE_ENDIAN
902/*
903 * Hashp->hdr needs to be byteswapped.
904 */
905static void
906swap_header_copy(HASHHDR *srcp, HASHHDR *destp)
907{
908 int i;
909
910 P_32_COPY(srcp->magic, destp->magic);
911 P_32_COPY(srcp->version, destp->version);
912 P_32_COPY(srcp->lorder, destp->lorder);
913 P_32_COPY(srcp->bsize, destp->bsize);
914 P_32_COPY(srcp->bshift, destp->bshift);
915 P_32_COPY(srcp->dsize, destp->dsize);
916 P_32_COPY(srcp->ssize, destp->ssize);
917 P_32_COPY(srcp->sshift, destp->sshift);
918 P_32_COPY(srcp->ovfl_point, destp->ovfl_point);
919 P_32_COPY(srcp->last_freed, destp->last_freed);
920 P_32_COPY(srcp->max_bucket, destp->max_bucket);
921 P_32_COPY(srcp->high_mask, destp->high_mask);
922 P_32_COPY(srcp->low_mask, destp->low_mask);
923 P_32_COPY(srcp->ffactor, destp->ffactor);
924 P_32_COPY(srcp->nkeys, destp->nkeys);
925 P_32_COPY(srcp->hdrpages, destp->hdrpages);
926 P_32_COPY(srcp->h_charkey, destp->h_charkey);
927 for (i = 0; i < NCACHED; i++) {
928 P_32_COPY(srcp->spares[i], destp->spares[i]);
929 P_16_COPY(srcp->bitmaps[i], destp->bitmaps[i]);
930 }
931}
932
933static void
934swap_header(HTAB *hashp)
935{
936 HASHHDR *hdrp;
937 int i;
938
939 hdrp = &hashp->hdr;
940
941 M_32_SWAP(hdrp->magic);
942 M_32_SWAP(hdrp->version);
943 M_32_SWAP(hdrp->lorder);
944 M_32_SWAP(hdrp->bsize);
945 M_32_SWAP(hdrp->bshift);
946 M_32_SWAP(hdrp->dsize);
947 M_32_SWAP(hdrp->ssize);
948 M_32_SWAP(hdrp->sshift);
949 M_32_SWAP(hdrp->ovfl_point);
950 M_32_SWAP(hdrp->last_freed);
951 M_32_SWAP(hdrp->max_bucket);
952 M_32_SWAP(hdrp->high_mask);
953 M_32_SWAP(hdrp->low_mask);
954 M_32_SWAP(hdrp->ffactor);
955 M_32_SWAP(hdrp->nkeys);
956 M_32_SWAP(hdrp->hdrpages);
957 M_32_SWAP(hdrp->h_charkey);
958 for (i = 0; i < NCACHED; i++) {
959 M_32_SWAP(hdrp->spares[i]);
960 M_16_SWAP(hdrp->bitmaps[i]);
961 }
962}
963#endif