hash.c 24 KB

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