source: trunk/src/emx/bsd/db/hash/hash.c@ 277

Last change on this file since 277 was 277, checked in by bird, 23 years ago

#357: Initial adjustments.

  • Property cvs2svn:cvs-rev set to 1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 24.0 KB
Line 
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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#if defined(LIBC_SCCS) && !defined(lint)
38static char sccsid[] = "@(#)hash.c 8.9 (Berkeley) 6/16/94";
39#endif /* LIBC_SCCS and not lint */
40
41#include <sys/param.h>
42#ifdef __EMX__
43#include <sys/types.h>
44#endif /* not __EMX__ */
45#include <sys/stat.h>
46
47#include <errno.h>
48#include <fcntl.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52#include <unistd.h>
53#ifdef DEBUG
54#include <assert.h>
55#endif
56
57#include <db.h>
58#include "hash.h"
59#include "page.h"
60#include "extern.h"
61
62static int alloc_segs __P((HTAB *, int));
63static int flush_meta __P((HTAB *));
64static int hash_access __P((HTAB *, ACTION, DBT *, DBT *));
65static int hash_close __P((DB *));
66static int hash_delete __P((const DB *, const DBT *, u_int32_t));
67static int hash_fd __P((const DB *));
68static int hash_get __P((const DB *, const DBT *, DBT *, u_int32_t));
69static int hash_put __P((const DB *, DBT *, const DBT *, u_int32_t));
70static void *hash_realloc __P((SEGMENT **, int, int));
71static int hash_seq __P((const DB *, DBT *, DBT *, u_int32_t));
72static int hash_sync __P((const DB *, u_int32_t));
73static int hdestroy __P((HTAB *));
74static HTAB *init_hash __P((HTAB *, const char *, HASHINFO *));
75static int init_htab __P((HTAB *, int));
76#if BYTE_ORDER == LITTLE_ENDIAN
77static void swap_header __P((HTAB *));
78static void swap_header_copy __P((HASHHDR *, HASHHDR *));
79#endif
80
81/* Fast arithmetic, relying on powers of 2, */
82#define MOD(x, y) ((x) & ((y) - 1))
83
84#define RETURN_ERROR(ERR, LOC) { save_errno = ERR; goto LOC; }
85
86/* Return values */
87#define SUCCESS (0)
88#define ERROR (-1)
89#define ABNORMAL (1)
90
91#ifdef HASH_STATISTICS
92int hash_accesses, hash_collisions, hash_expansions, hash_overflows;
93#endif
94
95/************************** INTERFACE ROUTINES ***************************/
96/* OPEN/CLOSE */
97
98extern DB *
99__hash_open(file, flags, mode, info, dflags)
100 const char *file;
101 int flags, mode, dflags;
102 const HASHINFO *info; /* Special directives for create */
103{
104 HTAB *hashp;
105 struct stat statbuf;
106 DB *dbp;
107 int bpages, hdrsize, new_table, nsegs, save_errno;
108
109 if ((flags & O_ACCMODE) == O_WRONLY) {
110 errno = EINVAL;
111 return (NULL);
112 }
113
114 if (!(hashp = (HTAB *)calloc(1, sizeof(HTAB))))
115 return (NULL);
116 hashp->fp = -1;
117
118 /*
119 * Even if user wants write only, we need to be able to read
120 * the actual file, so we need to open it read/write. But, the
121 * field in the hashp structure needs to be accurate so that
122 * we can check accesses.
123 */
124 hashp->flags = flags;
125
126 new_table = 0;
127 if (!file || (flags & O_TRUNC) ||
128 (stat(file, &statbuf) && (errno == ENOENT))) {
129 if (errno == ENOENT)
130 errno = 0; /* Just in case someone looks at errno */
131 new_table = 1;
132 }
133 if (file) {
134 if ((hashp->fp = open(file, flags, mode)) == -1)
135 RETURN_ERROR(errno, error0);
136 (void)fcntl(hashp->fp, F_SETFD, 1);
137 }
138 if (new_table) {
139 if (!(hashp = init_hash(hashp, file, (HASHINFO *)info)))
140 RETURN_ERROR(errno, error1);
141 } else {
142 /* Table already exists */
143 if (info && info->hash)
144 hashp->hash = info->hash;
145 else
146 hashp->hash = __default_hash;
147
148 hdrsize = read(hashp->fp, &hashp->hdr, sizeof(HASHHDR));
149#if BYTE_ORDER == LITTLE_ENDIAN
150 swap_header(hashp);
151#endif
152 if (hdrsize == -1)
153 RETURN_ERROR(errno, error1);
154 if (hdrsize != sizeof(HASHHDR))
155 RETURN_ERROR(EFTYPE, error1);
156 /* Verify file type, versions and hash function */
157 if (hashp->MAGIC != HASHMAGIC)
158 RETURN_ERROR(EFTYPE, error1);
159#define OLDHASHVERSION 1
160 if (hashp->VERSION != HASHVERSION &&
161 hashp->VERSION != OLDHASHVERSION)
162 RETURN_ERROR(EFTYPE, error1);
163 if (hashp->hash(CHARKEY, sizeof(CHARKEY)) != hashp->H_CHARKEY)
164 RETURN_ERROR(EFTYPE, error1);
165 /*
166 * Figure out how many segments we need. Max_Bucket is the
167 * maximum bucket number, so the number of buckets is
168 * max_bucket + 1.
169 */
170 nsegs = (hashp->MAX_BUCKET + 1 + hashp->SGSIZE - 1) /
171 hashp->SGSIZE;
172 hashp->nsegs = 0;
173 if (alloc_segs(hashp, nsegs))
174 /*
175 * If alloc_segs fails, table will have been destroyed
176 * and errno will have been set.
177 */
178 return (NULL);
179 /* Read in bitmaps */
180 bpages = (hashp->SPARES[hashp->OVFL_POINT] +
181 (hashp->BSIZE << BYTE_SHIFT) - 1) >>
182 (hashp->BSHIFT + BYTE_SHIFT);
183
184 hashp->nmaps = bpages;
185 (void)memset(&hashp->mapp[0], 0, bpages * sizeof(u_int32_t *));
186 }
187
188 /* Initialize Buffer Manager */
189 if (info && info->cachesize)
190 __buf_init(hashp, info->cachesize);
191 else
192 __buf_init(hashp, DEF_BUFSIZE);
193
194 hashp->new_file = new_table;
195 hashp->save_file = file && (hashp->flags & O_RDWR);
196 hashp->cbucket = -1;
197 if (!(dbp = (DB *)malloc(sizeof(DB)))) {
198 save_errno = errno;
199 hdestroy(hashp);
200 errno = save_errno;
201 return (NULL);
202 }
203 dbp->internal = hashp;
204 dbp->close = hash_close;
205 dbp->del = hash_delete;
206 dbp->fd = hash_fd;
207 dbp->get = hash_get;
208 dbp->put = hash_put;
209 dbp->seq = hash_seq;
210 dbp->sync = hash_sync;
211 dbp->type = DB_HASH;
212
213#ifdef DEBUG
214 (void)fprintf(stderr,
215"%s\n%s%x\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",
216 "init_htab:",
217 "TABLE POINTER ", hashp,
218 "BUCKET SIZE ", hashp->BSIZE,
219 "BUCKET SHIFT ", hashp->BSHIFT,
220 "DIRECTORY SIZE ", hashp->DSIZE,
221 "SEGMENT SIZE ", hashp->SGSIZE,
222 "SEGMENT SHIFT ", hashp->SSHIFT,
223 "FILL FACTOR ", hashp->FFACTOR,
224 "MAX BUCKET ", hashp->MAX_BUCKET,
225 "OVFL POINT ", hashp->OVFL_POINT,
226 "LAST FREED ", hashp->LAST_FREED,
227 "HIGH MASK ", hashp->HIGH_MASK,
228 "LOW MASK ", hashp->LOW_MASK,
229 "NSEGS ", hashp->nsegs,
230 "NKEYS ", hashp->NKEYS);
231#endif
232#ifdef HASH_STATISTICS
233 hash_overflows = hash_accesses = hash_collisions = hash_expansions = 0;
234#endif
235 return (dbp);
236
237error1:
238 if (hashp != NULL)
239 (void)close(hashp->fp);
240
241error0:
242 free(hashp);
243 errno = save_errno;
244 return (NULL);
245}
246
247static int
248hash_close(dbp)
249 DB *dbp;
250{
251 HTAB *hashp;
252 int retval;
253
254 if (!dbp)
255 return (ERROR);
256
257 hashp = (HTAB *)dbp->internal;
258 retval = hdestroy(hashp);
259 free(dbp);
260 return (retval);
261}
262
263static int
264hash_fd(dbp)
265 const DB *dbp;
266{
267 HTAB *hashp;
268
269 if (!dbp)
270 return (ERROR);
271
272 hashp = (HTAB *)dbp->internal;
273 if (hashp->fp == -1) {
274 errno = ENOENT;
275 return (-1);
276 }
277 return (hashp->fp);
278}
279
280/************************** LOCAL CREATION ROUTINES **********************/
281static HTAB *
282init_hash(hashp, file, info)
283 HTAB *hashp;
284 const char *file;
285 HASHINFO *info;
286{
287 struct stat statbuf;
288 int nelem;
289
290 nelem = 1;
291 hashp->NKEYS = 0;
292 hashp->LORDER = BYTE_ORDER;
293 hashp->BSIZE = DEF_BUCKET_SIZE;
294 hashp->BSHIFT = DEF_BUCKET_SHIFT;
295 hashp->SGSIZE = DEF_SEGSIZE;
296 hashp->SSHIFT = DEF_SEGSIZE_SHIFT;
297 hashp->DSIZE = DEF_DIRSIZE;
298 hashp->FFACTOR = DEF_FFACTOR;
299 hashp->hash = __default_hash;
300 memset(hashp->SPARES, 0, sizeof(hashp->SPARES));
301 memset(hashp->BITMAPS, 0, sizeof (hashp->BITMAPS));
302
303 /* Fix bucket size to be optimal for file system */
304 if (file != NULL) {
305 if (stat(file, &statbuf))
306 return (NULL);
307#ifdef __EMX__
308 hashp->BSIZE = 512;
309#else /* not __EMX__ */
310 hashp->BSIZE = statbuf.st_blksize;
311#endif /* not __EMX__ */
312 hashp->BSHIFT = __log2(hashp->BSIZE);
313 }
314
315 if (info) {
316 if (info->bsize) {
317 /* Round pagesize up to power of 2 */
318 hashp->BSHIFT = __log2(info->bsize);
319 hashp->BSIZE = 1 << hashp->BSHIFT;
320 if (hashp->BSIZE > MAX_BSIZE) {
321 errno = EINVAL;
322 return (NULL);
323 }
324 }
325 if (info->ffactor)
326 hashp->FFACTOR = info->ffactor;
327 if (info->hash)
328 hashp->hash = info->hash;
329 if (info->nelem)
330 nelem = info->nelem;
331 if (info->lorder) {
332 if (info->lorder != BIG_ENDIAN &&
333 info->lorder != LITTLE_ENDIAN) {
334 errno = EINVAL;
335 return (NULL);
336 }
337 hashp->LORDER = info->lorder;
338 }
339 }
340 /* init_htab should destroy the table and set errno if it fails */
341 if (init_htab(hashp, nelem))
342 return (NULL);
343 else
344 return (hashp);
345}
346/*
347 * This calls alloc_segs which may run out of memory. Alloc_segs will destroy
348 * the table and set errno, so we just pass the error information along.
349 *
350 * Returns 0 on No Error
351 */
352static int
353init_htab(hashp, nelem)
354 HTAB *hashp;
355 int nelem;
356{
357 register int nbuckets, nsegs;
358 int l2;
359
360 /*
361 * Divide number of elements by the fill factor and determine a
362 * desired number of buckets. Allocate space for the next greater
363 * power of two number of buckets.
364 */
365 nelem = (nelem - 1) / hashp->FFACTOR + 1;
366
367 l2 = __log2(MAX(nelem, 2));
368 nbuckets = 1 << l2;
369
370 hashp->SPARES[l2] = l2 + 1;
371 hashp->SPARES[l2 + 1] = l2 + 1;
372 hashp->OVFL_POINT = l2;
373 hashp->LAST_FREED = 2;
374
375 /* First bitmap page is at: splitpoint l2 page offset 1 */
376 if (__ibitmap(hashp, OADDR_OF(l2, 1), l2 + 1, 0))
377 return (-1);
378
379 hashp->MAX_BUCKET = hashp->LOW_MASK = nbuckets - 1;
380 hashp->HIGH_MASK = (nbuckets << 1) - 1;
381 hashp->HDRPAGES = ((MAX(sizeof(HASHHDR), MINHDRSIZE) - 1) >>
382 hashp->BSHIFT) + 1;
383
384 nsegs = (nbuckets - 1) / hashp->SGSIZE + 1;
385 nsegs = 1 << __log2(nsegs);
386
387 if (nsegs > hashp->DSIZE)
388 hashp->DSIZE = nsegs;
389 return (alloc_segs(hashp, nsegs));
390}
391
392/********************** DESTROY/CLOSE ROUTINES ************************/
393
394/*
395 * Flushes any changes to the file if necessary and destroys the hashp
396 * structure, freeing all allocated space.
397 */
398static int
399hdestroy(hashp)
400 HTAB *hashp;
401{
402 int i, save_errno;
403
404 save_errno = 0;
405
406#ifdef HASH_STATISTICS
407 (void)fprintf(stderr, "hdestroy: accesses %ld collisions %ld\n",
408 hash_accesses, hash_collisions);
409 (void)fprintf(stderr, "hdestroy: expansions %ld\n",
410 hash_expansions);
411 (void)fprintf(stderr, "hdestroy: overflows %ld\n",
412 hash_overflows);
413 (void)fprintf(stderr, "keys %ld maxp %d segmentcount %d\n",
414 hashp->NKEYS, hashp->MAX_BUCKET, hashp->nsegs);
415
416 for (i = 0; i < NCACHED; i++)
417 (void)fprintf(stderr,
418 "spares[%d] = %d\n", i, hashp->SPARES[i]);
419#endif
420 /*
421 * Call on buffer manager to free buffers, and if required,
422 * write them to disk.
423 */
424 if (__buf_free(hashp, 1, hashp->save_file))
425 save_errno = errno;
426 if (hashp->dir) {
427 free(*hashp->dir); /* Free initial segments */
428 /* Free extra segments */
429 while (hashp->exsegs--)
430 free(hashp->dir[--hashp->nsegs]);
431 free(hashp->dir);
432 }
433 if (flush_meta(hashp) && !save_errno)
434 save_errno = errno;
435 /* Free Bigmaps */
436 for (i = 0; i < hashp->nmaps; i++)
437 if (hashp->mapp[i])
438 free(hashp->mapp[i]);
439
440 if (hashp->fp != -1)
441 (void)close(hashp->fp);
442#ifdef __EMX__
443 if (hashp->tempname != NULL) {
444 (void)unlink (hashp->tempname);
445 free (hashp->tempname);
446 }
447#endif /* __EMX__ */
448
449 free(hashp);
450
451 if (save_errno) {
452 errno = save_errno;
453 return (ERROR);
454 }
455 return (SUCCESS);
456}
457/*
458 * Write modified pages to disk
459 *
460 * Returns:
461 * 0 == OK
462 * -1 ERROR
463 */
464static int
465hash_sync(dbp, flags)
466 const DB *dbp;
467 u_int32_t flags;
468{
469 HTAB *hashp;
470
471 if (flags != 0) {
472 errno = EINVAL;
473 return (ERROR);
474 }
475
476 if (!dbp)
477 return (ERROR);
478
479 hashp = (HTAB *)dbp->internal;
480 if (!hashp->save_file)
481 return (0);
482 if (__buf_free(hashp, 0, 1) || flush_meta(hashp))
483 return (ERROR);
484 hashp->new_file = 0;
485 return (0);
486}
487
488/*
489 * Returns:
490 * 0 == OK
491 * -1 indicates that errno should be set
492 */
493static int
494flush_meta(hashp)
495 HTAB *hashp;
496{
497 HASHHDR *whdrp;
498#if BYTE_ORDER == LITTLE_ENDIAN
499 HASHHDR whdr;
500#endif
501 int fp, i, wsize;
502
503 if (!hashp->save_file)
504 return (0);
505 hashp->MAGIC = HASHMAGIC;
506 hashp->VERSION = HASHVERSION;
507 hashp->H_CHARKEY = hashp->hash(CHARKEY, sizeof(CHARKEY));
508
509 fp = hashp->fp;
510 whdrp = &hashp->hdr;
511#if BYTE_ORDER == LITTLE_ENDIAN
512 whdrp = &whdr;
513 swap_header_copy(&hashp->hdr, whdrp);
514#endif
515 if ((lseek(fp, (off_t)0, SEEK_SET) == -1) ||
516 ((wsize = write(fp, whdrp, sizeof(HASHHDR))) == -1))
517 return (-1);
518 else
519 if (wsize != sizeof(HASHHDR)) {
520 errno = EFTYPE;
521 hashp->dbmerrno = errno; /* bird: errno */
522 return (-1);
523 }
524 for (i = 0; i < NCACHED; i++)
525 if (hashp->mapp[i])
526 if (__put_page(hashp, (char *)hashp->mapp[i],
527 hashp->BITMAPS[i], 0, 1))
528 return (-1);
529 return (0);
530}
531
532/*******************************SEARCH ROUTINES *****************************/
533/*
534 * All the access routines return
535 *
536 * Returns:
537 * 0 on SUCCESS
538 * 1 to indicate an external ERROR (i.e. key not found, etc)
539 * -1 to indicate an internal ERROR (i.e. out of memory, etc)
540 */
541static int
542hash_get(dbp, key, data, flag)
543 const DB *dbp;
544 const DBT *key;
545 DBT *data;
546 u_int32_t flag;
547{
548 HTAB *hashp;
549
550 hashp = (HTAB *)dbp->internal;
551 if (flag) {
552 hashp->dbmerrno = errno = EINVAL; /* bird: errno */
553 return (ERROR);
554 }
555 return (hash_access(hashp, HASH_GET, (DBT *)key, data));
556}
557
558static int
559hash_put(dbp, key, data, flag)
560 const DB *dbp;
561 DBT *key;
562 const DBT *data;
563 u_int32_t flag;
564{
565 HTAB *hashp;
566
567 hashp = (HTAB *)dbp->internal;
568 if (flag && flag != R_NOOVERWRITE) {
569 hashp->dbmerrno = errno = EINVAL; /* bird: errno */
570 return (ERROR);
571 }
572 if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
573 hashp->dbmerrno = errno = EPERM; /* bird: errno */
574 return (ERROR);
575 }
576 return (hash_access(hashp, flag == R_NOOVERWRITE ?
577 HASH_PUTNEW : HASH_PUT, (DBT *)key, (DBT *)data));
578}
579
580static int
581hash_delete(dbp, key, flag)
582 const DB *dbp;
583 const DBT *key;
584 u_int32_t flag; /* Ignored */
585{
586 HTAB *hashp;
587
588 hashp = (HTAB *)dbp->internal;
589 if (flag && flag != R_CURSOR) {
590 hashp->dbmerrno = errno = EINVAL; /* bird: errno */
591 return (ERROR);
592 }
593 if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
594 hashp->dbmerrno = errno = EPERM; /* bird: errno */
595 return (ERROR);
596 }
597 return (hash_access(hashp, HASH_DELETE, (DBT *)key, NULL));
598}
599
600/*
601 * Assume that hashp has been set in wrapper routine.
602 */
603static int
604hash_access(hashp, action, key, val)
605 HTAB *hashp;
606 ACTION action;
607 DBT *key, *val;
608{
609 register BUFHEAD *rbufp;
610 BUFHEAD *bufp, *save_bufp;
611 register u_int16_t *bp;