hash_page.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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_page.c 8.7 (Berkeley) 8/16/94";
  38. #endif /* LIBC_SCCS and not lint */
  39. /*
  40. * PACKAGE: hashing
  41. *
  42. * DESCRIPTION:
  43. * Page manipulation for hashing package.
  44. *
  45. * ROUTINES:
  46. *
  47. * External
  48. * __get_page
  49. * __add_ovflpage
  50. * Internal
  51. * overflow_page
  52. * open_temp
  53. */
  54. #include <sys/types.h>
  55. #include <errno.h>
  56. #include <fcntl.h>
  57. #include <signal.h>
  58. #include <stdio.h>
  59. #include <stdlib.h>
  60. #include <string.h>
  61. #include <unistd.h>
  62. #ifdef DEBUG
  63. #include <assert.h>
  64. #endif
  65. #include "../include/db.h"
  66. #include "hash.h"
  67. #include "page.h"
  68. #include "extern.h"
  69. static u_int32_t *fetch_bitmap __P((HTAB *, int));
  70. static u_int32_t first_free __P((u_int32_t));
  71. static int open_temp __P((HTAB *));
  72. static u_int16_t overflow_page __P((HTAB *));
  73. static void putpair __P((char *, const DBT *, const DBT *));
  74. static void squeeze_key __P((u_int16_t *, const DBT *, const DBT *));
  75. static int ugly_split
  76. __P((HTAB *, u_int32_t, BUFHEAD *, BUFHEAD *, int, int));
  77. #define PAGE_INIT(P) { \
  78. ((u_int16_t *)(P))[0] = 0; \
  79. ((u_int16_t *)(P))[1] = hashp->BSIZE - 3 * sizeof(u_int16_t); \
  80. ((u_int16_t *)(P))[2] = hashp->BSIZE; \
  81. }
  82. /*
  83. * This is called AFTER we have verified that there is room on the page for
  84. * the pair (PAIRFITS has returned true) so we go right ahead and start moving
  85. * stuff on.
  86. */
  87. static void
  88. putpair(p, key, val)
  89. char *p;
  90. const DBT *key, *val;
  91. {
  92. register u_int16_t *bp, n, off;
  93. bp = (u_int16_t *)p;
  94. /* Enter the key first. */
  95. n = bp[0];
  96. off = OFFSET(bp) - key->size;
  97. memmove(p + off, key->data, key->size);
  98. bp[++n] = off;
  99. /* Now the data. */
  100. off -= val->size;
  101. memmove(p + off, val->data, val->size);
  102. bp[++n] = off;
  103. /* Adjust page info. */
  104. bp[0] = n;
  105. bp[n + 1] = off - ((n + 3) * sizeof(u_int16_t));
  106. bp[n + 2] = off;
  107. }
  108. /*
  109. * Returns:
  110. * 0 OK
  111. * -1 error
  112. */
  113. extern int
  114. __delpair(hashp, bufp, ndx)
  115. HTAB *hashp;
  116. BUFHEAD *bufp;
  117. register int ndx;
  118. {
  119. register u_int16_t *bp, newoff;
  120. register int n;
  121. u_int16_t pairlen;
  122. bp = (u_int16_t *)bufp->page;
  123. n = bp[0];
  124. if (bp[ndx + 1] < REAL_KEY)
  125. return (__big_delete(hashp, bufp));
  126. if (ndx != 1)
  127. newoff = bp[ndx - 1];
  128. else
  129. newoff = hashp->BSIZE;
  130. pairlen = newoff - bp[ndx + 1];
  131. if (ndx != (n - 1)) {
  132. /* Hard Case -- need to shuffle keys */
  133. register int i;
  134. register char *src = bufp->page + (int)OFFSET(bp);
  135. register char *dst = src + (int)pairlen;
  136. memmove(dst, src, bp[ndx + 1] - OFFSET(bp));
  137. /* Now adjust the pointers */
  138. for (i = ndx + 2; i <= n; i += 2) {
  139. if (bp[i + 1] == OVFLPAGE) {
  140. bp[i - 2] = bp[i];
  141. bp[i - 1] = bp[i + 1];
  142. } else {
  143. bp[i - 2] = bp[i] + pairlen;
  144. bp[i - 1] = bp[i + 1] + pairlen;
  145. }
  146. }
  147. }
  148. /* Finally adjust the page data */
  149. bp[n] = OFFSET(bp) + pairlen;
  150. bp[n - 1] = bp[n + 1] + pairlen + 2 * sizeof(u_int16_t);
  151. bp[0] = n - 2;
  152. hashp->NKEYS--;
  153. bufp->flags |= BUF_MOD;
  154. return (0);
  155. }
  156. /*
  157. * Returns:
  158. * 0 ==> OK
  159. * -1 ==> Error
  160. */
  161. extern int
  162. __split_page(hashp, obucket, nbucket)
  163. HTAB *hashp;
  164. u_int32_t obucket, nbucket;
  165. {
  166. register BUFHEAD *new_bufp, *old_bufp;
  167. register u_int16_t *ino;
  168. register char *np;
  169. DBT key, val;
  170. int n, ndx, retval;
  171. u_int16_t copyto, diff, off, moved;
  172. char *op;
  173. copyto = (u_int16_t)hashp->BSIZE;
  174. off = (u_int16_t)hashp->BSIZE;
  175. old_bufp = __get_buf(hashp, obucket, NULL, 0);
  176. if (old_bufp == NULL)
  177. return (-1);
  178. new_bufp = __get_buf(hashp, nbucket, NULL, 0);
  179. if (new_bufp == NULL)
  180. return (-1);
  181. old_bufp->flags |= (BUF_MOD | BUF_PIN);
  182. new_bufp->flags |= (BUF_MOD | BUF_PIN);
  183. ino = (u_int16_t *)(op = old_bufp->page);
  184. np = new_bufp->page;
  185. moved = 0;
  186. for (n = 1, ndx = 1; n < ino[0]; n += 2) {
  187. if (ino[n + 1] < REAL_KEY) {
  188. retval = ugly_split(hashp, obucket, old_bufp, new_bufp,
  189. (int)copyto, (int)moved);
  190. old_bufp->flags &= ~BUF_PIN;
  191. new_bufp->flags &= ~BUF_PIN;
  192. return (retval);
  193. }
  194. key.data = (u_char *)op + ino[n];
  195. key.size = off - ino[n];
  196. if (__call_hash(hashp, key.data, key.size) == obucket) {
  197. /* Don't switch page */
  198. diff = copyto - off;
  199. if (diff) {
  200. copyto = ino[n + 1] + diff;
  201. memmove(op + copyto, op + ino[n + 1],
  202. off - ino[n + 1]);
  203. ino[ndx] = copyto + ino[n] - ino[n + 1];
  204. ino[ndx + 1] = copyto;
  205. } else
  206. copyto = ino[n + 1];
  207. ndx += 2;
  208. } else {
  209. /* Switch page */
  210. val.data = (u_char *)op + ino[n + 1];
  211. val.size = ino[n] - ino[n + 1];
  212. putpair(np, &key, &val);
  213. moved += 2;
  214. }
  215. off = ino[n + 1];
  216. }
  217. /* Now clean up the page */
  218. ino[0] -= moved;
  219. FREESPACE(ino) = copyto - sizeof(u_int16_t) * (ino[0] + 3);
  220. OFFSET(ino) = copyto;
  221. #ifdef DEBUG3
  222. (void)fprintf(stderr, "split %d/%d\n",
  223. ((u_int16_t *)np)[0] / 2,
  224. ((u_int16_t *)op)[0] / 2);
  225. #endif
  226. /* unpin both pages */
  227. old_bufp->flags &= ~BUF_PIN;
  228. new_bufp->flags &= ~BUF_PIN;
  229. return (0);
  230. }
  231. /*
  232. * Called when we encounter an overflow or big key/data page during split
  233. * handling. This is special cased since we have to begin checking whether
  234. * the key/data pairs fit on their respective pages and because we may need
  235. * overflow pages for both the old and new pages.
  236. *
  237. * The first page might be a page with regular key/data pairs in which case
  238. * we have a regular overflow condition and just need to go on to the next
  239. * page or it might be a big key/data pair in which case we need to fix the
  240. * big key/data pair.
  241. *
  242. * Returns:
  243. * 0 ==> success
  244. * -1 ==> failure
  245. */
  246. static int
  247. ugly_split(hashp, obucket, old_bufp, new_bufp, copyto, moved)
  248. HTAB *hashp;
  249. u_int32_t obucket; /* Same as __split_page. */
  250. BUFHEAD *old_bufp, *new_bufp;
  251. int copyto; /* First byte on page which contains key/data values. */
  252. int moved; /* Number of pairs moved to new page. */
  253. {
  254. register BUFHEAD *bufp; /* Buffer header for ino */
  255. register u_int16_t *ino; /* Page keys come off of */
  256. register u_int16_t *np; /* New page */
  257. register u_int16_t *op; /* Page keys go on to if they aren't moving */
  258. BUFHEAD *last_bfp; /* Last buf header OVFL needing to be freed */
  259. DBT key, val;
  260. SPLIT_RETURN ret;
  261. u_int16_t n, off, ov_addr, scopyto;
  262. char *cino; /* Character value of ino */
  263. bufp = old_bufp;
  264. ino = (u_int16_t *)old_bufp->page;
  265. np = (u_int16_t *)new_bufp->page;
  266. op = (u_int16_t *)old_bufp->page;
  267. last_bfp = NULL;
  268. scopyto = (u_int16_t)copyto; /* ANSI */
  269. n = ino[0] - 1;
  270. while (n < ino[0]) {
  271. if (ino[2] < REAL_KEY && ino[2] != OVFLPAGE) {
  272. if (__big_split(hashp, old_bufp,
  273. new_bufp, bufp, bufp->addr, obucket, &ret))
  274. return (-1);
  275. old_bufp = ret.oldp;
  276. if (!old_bufp)
  277. return (-1);
  278. op = (u_int16_t *)old_bufp->page;
  279. new_bufp = ret.newp;
  280. if (!new_bufp)
  281. return (-1);
  282. np = (u_int16_t *)new_bufp->page;
  283. bufp = ret.nextp;
  284. if (!bufp)
  285. return (0);
  286. cino = (char *)bufp->page;
  287. ino = (u_int16_t *)cino;
  288. last_bfp = ret.nextp;
  289. } else if (ino[n + 1] == OVFLPAGE) {
  290. ov_addr = ino[n];
  291. /*
  292. * Fix up the old page -- the extra 2 are the fields
  293. * which contained the overflow information.
  294. */
  295. ino[0] -= (moved + 2);
  296. FREESPACE(ino) =
  297. scopyto - sizeof(u_int16_t) * (ino[0] + 3);
  298. OFFSET(ino) = scopyto;
  299. bufp = __get_buf(hashp, ov_addr, bufp, 0);
  300. if (!bufp)
  301. return (-1);
  302. ino = (u_int16_t *)bufp->page;
  303. n = 1;
  304. scopyto = hashp->BSIZE;
  305. moved = 0;
  306. if (last_bfp)
  307. __free_ovflpage(hashp, last_bfp);
  308. last_bfp = bufp;
  309. }
  310. /* Move regular sized pairs of there are any */
  311. off = hashp->BSIZE;
  312. for (n = 1; (n < ino[0]) && (ino[n + 1] >= REAL_KEY); n += 2) {
  313. cino = (char *)ino;
  314. key.data = (u_char *)cino + ino[n];
  315. key.size = off - ino[n];
  316. val.data = (u_char *)cino + ino[n + 1];
  317. val.size = ino[n] - ino[n + 1];
  318. off = ino[n + 1];
  319. if (__call_hash(hashp, key.data, key.size) == obucket) {
  320. /* Keep on old page */
  321. if (PAIRFITS(op, (&key), (&val)))
  322. putpair((char *)op, &key, &val);
  323. else {
  324. old_bufp =
  325. __add_ovflpage(hashp, old_bufp);
  326. if (!old_bufp)
  327. return (-1);
  328. op = (u_int16_t *)old_bufp->page;
  329. putpair((char *)op, &key, &val);
  330. }
  331. old_bufp->flags |= BUF_MOD;
  332. } else {
  333. /* Move to new page */
  334. if (PAIRFITS(np, (&key), (&val)))
  335. putpair((char *)np, &key, &val);
  336. else {
  337. new_bufp =
  338. __add_ovflpage(hashp, new_bufp);
  339. if (!new_bufp)
  340. return (-1);
  341. np = (u_int16_t *)new_bufp->page;
  342. putpair((char *)np, &key, &val);
  343. }
  344. new_bufp->flags |= BUF_MOD;
  345. }
  346. }
  347. }
  348. if (last_bfp)
  349. __free_ovflpage(hashp, last_bfp);
  350. return (0);
  351. }
  352. /*
  353. * Add the given pair to the page
  354. *
  355. * Returns:
  356. * 0 ==> OK
  357. * 1 ==> failure
  358. */
  359. extern int
  360. __addel(hashp, bufp, key, val)
  361. HTAB *hashp;
  362. BUFHEAD *bufp;
  363. const DBT *key, *val;
  364. {
  365. register u_int16_t *bp, *sop;
  366. int do_expand;
  367. bp = (u_int16_t *)bufp->page;
  368. do_expand = 0;
  369. while (bp[0] && (bp[2] < REAL_KEY || bp[bp[0]] < REAL_KEY))
  370. /* Exception case */
  371. if (bp[2] == FULL_KEY_DATA && bp[0] == 2)
  372. /* This is the last page of a big key/data pair
  373. and we need to add another page */
  374. break;
  375. else if (bp[2] < REAL_KEY && bp[bp[0]] != OVFLPAGE) {
  376. bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
  377. if (!bufp)
  378. return (-1);
  379. bp = (u_int16_t *)bufp->page;
  380. } else
  381. /* Try to squeeze key on this page */
  382. if (FREESPACE(bp) > PAIRSIZE(key, val)) {
  383. squeeze_key(bp, key, val);
  384. return (0);
  385. } else {
  386. bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
  387. if (!bufp)
  388. return (-1);
  389. bp = (u_int16_t *)bufp->page;
  390. }
  391. if (PAIRFITS(bp, key, val))
  392. putpair(bufp->page, key, val);
  393. else {
  394. do_expand = 1;
  395. bufp = __add_ovflpage(hashp, bufp);
  396. if (!bufp)
  397. return (-1);
  398. sop = (u_int16_t *)bufp->page;
  399. if (PAIRFITS(sop, key, val))
  400. putpair((char *)sop, key, val);
  401. else
  402. if (__big_insert(hashp, bufp, key, val))
  403. return (-1);
  404. }
  405. bufp->flags |= BUF_MOD;
  406. /*
  407. * If the average number of keys per bucket exceeds the fill factor,
  408. * expand the table.
  409. */
  410. hashp->NKEYS++;
  411. if (do_expand ||
  412. (hashp->NKEYS / (hashp->MAX_BUCKET + 1) > hashp->FFACTOR))
  413. return (__expand_table(hashp));
  414. return (0);
  415. }
  416. /*
  417. *
  418. * Returns:
  419. * pointer on success
  420. * NULL on error
  421. */
  422. extern BUFHEAD *
  423. __add_ovflpage(hashp, bufp)
  424. HTAB *hashp;
  425. BUFHEAD *bufp;
  426. {
  427. register u_int16_t *sp;
  428. u_int16_t ndx, ovfl_num;
  429. #ifdef DEBUG1
  430. int tmp1, tmp2;
  431. #endif
  432. sp = (u_int16_t *)bufp->page;
  433. /* Check if we are dynamically determining the fill factor */
  434. if (hashp->FFACTOR == DEF_FFACTOR) {
  435. hashp->FFACTOR = sp[0] >> 1;
  436. if (hashp->FFACTOR < MIN_FFACTOR)
  437. hashp->FFACTOR = MIN_FFACTOR;
  438. }
  439. bufp->flags |= BUF_MOD;
  440. ovfl_num = overflow_page(hashp);
  441. #ifdef DEBUG1
  442. tmp1 = bufp->addr;
  443. tmp2 = bufp->ovfl ? bufp->ovfl->addr : 0;
  444. #endif
  445. if (!ovfl_num || !(bufp->ovfl = __get_buf(hashp, ovfl_num, bufp, 1)))
  446. return (NULL);
  447. bufp->ovfl->flags |= BUF_MOD;
  448. #ifdef DEBUG1
  449. (void)fprintf(stderr, "ADDOVFLPAGE: %d->ovfl was %d is now %d\n",
  450. tmp1, tmp2, bufp->ovfl->addr);
  451. #endif
  452. ndx = sp[0];
  453. /*
  454. * Since a pair is allocated on a page only if there's room to add
  455. * an overflow page, we know that the OVFL information will fit on
  456. * the page.
  457. */
  458. sp[ndx + 4] = OFFSET(sp);
  459. sp[ndx + 3] = FREESPACE(sp) - OVFLSIZE;
  460. sp[ndx + 1] = ovfl_num;
  461. sp[ndx + 2] = OVFLPAGE;
  462. sp[0] = ndx + 2;
  463. #ifdef HASH_STATISTICS
  464. hash_overflows++;
  465. #endif
  466. return (bufp->ovfl);
  467. }
  468. /*
  469. * Returns:
  470. * 0 indicates SUCCESS
  471. * -1 indicates FAILURE
  472. */
  473. extern int
  474. __get_page(hashp, p, bucket, is_bucket, is_disk, is_bitmap)
  475. HTAB *hashp;
  476. char *p;
  477. u_int32_t bucket;
  478. int is_bucket, is_disk, is_bitmap;
  479. {
  480. register int fd, page, size;
  481. int rsize;
  482. u_int16_t *bp;
  483. fd = hashp->fp;
  484. size = hashp->BSIZE;
  485. if ((fd == -1) || !is_disk) {
  486. PAGE_INIT(p);
  487. return (0);
  488. }
  489. if (is_bucket)
  490. page = BUCKET_TO_PAGE(bucket);
  491. else
  492. page = OADDR_TO_PAGE(bucket);
  493. if ((lseek(fd, (off_t)page << hashp->BSHIFT, SEEK_SET) == -1) ||
  494. ((rsize = read(fd, p, size)) == -1))
  495. return (-1);
  496. bp = (u_int16_t *)p;
  497. if (!rsize)
  498. bp[0] = 0; /* We hit the EOF, so initialize a new page */
  499. else
  500. if (rsize != size) {
  501. errno = EFTYPE;
  502. return (-1);
  503. }
  504. if (!is_bitmap && !bp[0]) {
  505. PAGE_INIT(p);
  506. } else
  507. if (hashp->LORDER != BYTE_ORDER) {
  508. register int i, max;
  509. if (is_bitmap) {
  510. max = hashp->BSIZE >> 2; /* divide by 4 */
  511. for (i = 0; i < max; i++)
  512. M_32_SWAP(((int *)p)[i]);
  513. } else {
  514. M_16_SWAP(bp[0]);
  515. max = bp[0] + 2;
  516. for (i = 1; i <= max; i++)
  517. M_16_SWAP(bp[i]);
  518. }
  519. }
  520. return (0);
  521. }
  522. /*
  523. * Write page p to disk
  524. *
  525. * Returns:
  526. * 0 ==> OK
  527. * -1 ==>failure
  528. */
  529. extern int
  530. __put_page(hashp, p, bucket, is_bucket, is_bitmap)
  531. HTAB *hashp;
  532. char *p;
  533. u_int32_t bucket;
  534. int is_bucket, is_bitmap;
  535. {
  536. register int fd, page, size;
  537. int wsize;
  538. size = hashp->BSIZE;
  539. if ((hashp->fp == -1) && open_temp(hashp))
  540. return (-1);
  541. fd = hashp->fp;
  542. if (hashp->LORDER != BYTE_ORDER) {
  543. register int i;
  544. register int max;
  545. if (is_bitmap) {
  546. max = hashp->BSIZE >> 2; /* divide by 4 */
  547. for (i = 0; i < max; i++)
  548. M_32_SWAP(((int *)p)[i]);
  549. } else {
  550. max = ((u_int16_t *)p)[0] + 2;
  551. for (i = 0; i <= max; i++)
  552. M_16_SWAP(((u_int16_t *)p)[i]);
  553. }
  554. }
  555. if (is_bucket)
  556. page = BUCKET_TO_PAGE(bucket);
  557. else
  558. page = OADDR_TO_PAGE(bucket);
  559. if ((lseek(fd, (off_t)page << hashp->BSHIFT, SEEK_SET) == -1) ||
  560. ((wsize = write(fd, p, size)) == -1))
  561. /* Errno is set */
  562. return (-1);
  563. if (wsize != size) {
  564. errno = EFTYPE;
  565. return (-1);
  566. }
  567. return (0);
  568. }
  569. #define BYTE_MASK ((1 << INT_BYTE_SHIFT) -1)
  570. /*
  571. * Initialize a new bitmap page. Bitmap pages are left in memory
  572. * once they are read in.
  573. */
  574. extern int
  575. __ibitmap(hashp, pnum, nbits, ndx)
  576. HTAB *hashp;
  577. int pnum, nbits, ndx;
  578. {
  579. u_int32_t *ip;
  580. int clearbytes, clearints;
  581. if ((ip = (u_int32_t *)malloc(hashp->BSIZE)) == NULL)
  582. return (1);
  583. hashp->nmaps++;
  584. clearints = ((nbits - 1) >> INT_BYTE_SHIFT) + 1;
  585. clearbytes = clearints << INT_TO_BYTE;
  586. (void)memset((char *)ip, 0, clearbytes);
  587. (void)memset(((char *)ip) + clearbytes, 0xFF,
  588. hashp->BSIZE - clearbytes);
  589. ip[clearints - 1] = ALL_SET << (nbits & BYTE_MASK);
  590. SETBIT(ip, 0);
  591. hashp->BITMAPS[ndx] = (u_int16_t)pnum;
  592. hashp->mapp[ndx] = ip;
  593. return (0);
  594. }
  595. static u_int32_t
  596. first_free(map)
  597. u_int32_t map;
  598. {
  599. register u_int32_t i, mask;
  600. mask = 0x1;
  601. for (i = 0; i < BITS_PER_MAP; i++) {
  602. if (!(mask & map))
  603. return (i);
  604. mask = mask << 1;
  605. }
  606. return (i);
  607. }
  608. static u_int16_t
  609. overflow_page(hashp)
  610. HTAB *hashp;
  611. {
  612. register u_int32_t *freep = 0;
  613. register int max_free, offset, splitnum;
  614. u_int16_t addr;
  615. int bit, first_page, free_bit, free_page, i, in_use_bits, j;
  616. #ifdef DEBUG2
  617. int tmp1, tmp2;
  618. #endif
  619. splitnum = hashp->OVFL_POINT;
  620. max_free = hashp->SPARES[splitnum];
  621. free_page = (max_free - 1) >> (hashp->BSHIFT + BYTE_SHIFT);
  622. free_bit = (max_free - 1) & ((hashp->BSIZE << BYTE_SHIFT) - 1);
  623. /* Look through all the free maps to find the first free block */
  624. first_page = hashp->LAST_FREED >>(hashp->BSHIFT + BYTE_SHIFT);
  625. for ( i = first_page; i <= free_page; i++ ) {
  626. if (!(freep = (u_int32_t *)hashp->mapp[i]) &&
  627. !(freep = fetch_bitmap(hashp, i)))
  628. return (0);
  629. if (i == free_page)
  630. in_use_bits = free_bit;
  631. else
  632. in_use_bits = (hashp->BSIZE << BYTE_SHIFT) - 1;
  633. if (i == first_page) {
  634. bit = hashp->LAST_FREED &
  635. ((hashp->BSIZE << BYTE_SHIFT) - 1);
  636. j = bit / BITS_PER_MAP;
  637. bit = bit & ~(BITS_PER_MAP - 1);
  638. } else {
  639. bit = 0;
  640. j = 0;
  641. }
  642. for (; bit <= in_use_bits; j++, bit += BITS_PER_MAP)
  643. if (freep[j] != ALL_SET)
  644. goto found;
  645. }
  646. /* No Free Page Found */
  647. hashp->LAST_FREED = hashp->SPARES[splitnum];
  648. hashp->SPARES[splitnum]++;
  649. offset = hashp->SPARES[splitnum] -
  650. (splitnum ? hashp->SPARES[splitnum - 1] : 0);
  651. #define OVMSG "HASH: Out of overflow pages. Increase page size\n"
  652. if (offset > SPLITMASK) {
  653. if (++splitnum >= NCACHED) {
  654. if (write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1) < 0) {
  655. }
  656. return (0);
  657. }
  658. hashp->OVFL_POINT = splitnum;
  659. hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1];
  660. hashp->SPARES[splitnum-1]--;
  661. offset = 1;
  662. }
  663. /* Check if we need to allocate a new bitmap page */
  664. if (free_bit == (hashp->BSIZE << BYTE_SHIFT) - 1) {
  665. free_page++;
  666. if (free_page >= NCACHED) {
  667. if (write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1) < 0) {
  668. }
  669. return (0);
  670. }
  671. /*
  672. * This is tricky. The 1 indicates that you want the new page
  673. * allocated with 1 clear bit. Actually, you are going to
  674. * allocate 2 pages from this map. The first is going to be
  675. * the map page, the second is the overflow page we were
  676. * looking for. The init_bitmap routine automatically, sets
  677. * the first bit of itself to indicate that the bitmap itself
  678. * is in use. We would explicitly set the second bit, but
  679. * don't have to if we tell init_bitmap not to leave it clear
  680. * in the first place.
  681. */
  682. if (__ibitmap(hashp,
  683. (int)OADDR_OF(splitnum, offset), 1, free_page))
  684. return (0);
  685. hashp->SPARES[splitnum]++;
  686. #ifdef DEBUG2
  687. free_bit = 2;
  688. #endif
  689. offset++;
  690. if (offset > SPLITMASK) {
  691. if (++splitnum >= NCACHED) {
  692. if (write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1) < 0) {
  693. }
  694. return (0);
  695. }
  696. hashp->OVFL_POINT = splitnum;
  697. hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1];
  698. hashp->SPARES[splitnum-1]--;
  699. offset = 0;
  700. }
  701. } else {
  702. /*
  703. * Free_bit addresses the last used bit. Bump it to address
  704. * the first available bit.
  705. */
  706. free_bit++;
  707. SETBIT(freep, free_bit);
  708. }
  709. /* Calculate address of the new overflow page */
  710. addr = OADDR_OF(splitnum, offset);
  711. #ifdef DEBUG2
  712. (void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
  713. addr, free_bit, free_page);
  714. #endif
  715. return (addr);
  716. found:
  717. bit = bit + first_free(freep[j]);
  718. SETBIT(freep, bit);
  719. #ifdef DEBUG2
  720. tmp1 = bit;
  721. tmp2 = i;
  722. #endif
  723. /*
  724. * Bits are addressed starting with 0, but overflow pages are addressed
  725. * beginning at 1. Bit is a bit addressnumber, so we need to increment
  726. * it to convert it to a page number.
  727. */
  728. bit = 1 + bit + (i * (hashp->BSIZE << BYTE_SHIFT));
  729. if (bit >= hashp->LAST_FREED)
  730. hashp->LAST_FREED = bit - 1;
  731. /* Calculate the split number for this page */
  732. for (i = 0; (i < splitnum) && (bit > hashp->SPARES[i]); i++);
  733. offset = (i ? bit - hashp->SPARES[i - 1] : bit);
  734. if (offset >= SPLITMASK)
  735. return (0); /* Out of overflow pages */
  736. addr = OADDR_OF(i, offset);
  737. #ifdef DEBUG2
  738. (void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
  739. addr, tmp1, tmp2);
  740. #endif
  741. /* Allocate and return the overflow page */
  742. return (addr);
  743. }
  744. /*
  745. * Mark this overflow page as free.
  746. */
  747. extern void
  748. __free_ovflpage(hashp, obufp)
  749. HTAB *hashp;
  750. BUFHEAD *obufp;
  751. {
  752. register u_int16_t addr;
  753. u_int32_t *freep;
  754. int bit_address, free_page, free_bit;
  755. u_int16_t ndx;
  756. addr = obufp->addr;
  757. #ifdef DEBUG1
  758. (void)fprintf(stderr, "Freeing %d\n", addr);
  759. #endif
  760. ndx = (((u_int16_t)addr) >> SPLITSHIFT);
  761. bit_address =
  762. (ndx ? hashp->SPARES[ndx - 1] : 0) + (addr & SPLITMASK) - 1;
  763. if (bit_address < hashp->LAST_FREED)
  764. hashp->LAST_FREED = bit_address;
  765. free_page = (bit_address >> (hashp->BSHIFT + BYTE_SHIFT));
  766. free_bit = bit_address & ((hashp->BSIZE << BYTE_SHIFT) - 1);
  767. if (!(freep = hashp->mapp[free_page]))
  768. freep = fetch_bitmap(hashp, free_page);
  769. #ifdef DEBUG
  770. /*
  771. * This had better never happen. It means we tried to read a bitmap
  772. * that has already had overflow pages allocated off it, and we
  773. * failed to read it from the file.
  774. */
  775. if (!freep)
  776. assert(0);
  777. #endif
  778. CLRBIT(freep, free_bit);
  779. #ifdef DEBUG2
  780. (void)fprintf(stderr, "FREE_OVFLPAGE: ADDR: %d BIT: %d PAGE %d\n",
  781. obufp->addr, free_bit, free_page);
  782. #endif
  783. __reclaim_buf(hashp, obufp);
  784. }
  785. /*
  786. * Returns:
  787. * 0 success
  788. * -1 failure
  789. */
  790. static int
  791. open_temp(hashp)
  792. HTAB *hashp;
  793. {
  794. sigset_t set, oset;
  795. static char namestr[] = "_hashXXXXXX";
  796. /* Block signals; make sure file goes away at process exit. */
  797. (void)sigfillset(&set);
  798. (void)sigprocmask(SIG_BLOCK, &set, &oset);
  799. if ((hashp->fp = mkstemp(namestr)) != -1) {
  800. (void)unlink(namestr);
  801. (void)fcntl(hashp->fp, F_SETFD, 1);
  802. }
  803. (void)sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL);
  804. return (hashp->fp != -1 ? 0 : -1);
  805. }
  806. /*
  807. * We have to know that the key will fit, but the last entry on the page is
  808. * an overflow pair, so we need to shift things.
  809. */
  810. static void
  811. squeeze_key(sp, key, val)
  812. u_int16_t *sp;
  813. const DBT *key, *val;
  814. {
  815. register char *p;
  816. u_int16_t free_space, n, off, pageno;
  817. p = (char *)sp;
  818. n = sp[0];
  819. free_space = FREESPACE(sp);
  820. off = OFFSET(sp);
  821. pageno = sp[n - 1];
  822. off -= key->size;
  823. sp[n - 1] = off;
  824. memmove(p + off, key->data, key->size);
  825. off -= val->size;
  826. sp[n] = off;
  827. memmove(p + off, val->data, val->size);
  828. sp[0] = n + 2;
  829. sp[n + 1] = pageno;
  830. sp[n + 2] = OVFLPAGE;
  831. FREESPACE(sp) = free_space - PAIRSIZE(key, val);
  832. OFFSET(sp) = off;
  833. }
  834. static u_int32_t *
  835. fetch_bitmap(hashp, ndx)
  836. HTAB *hashp;
  837. int ndx;
  838. {
  839. if (ndx >= hashp->nmaps)
  840. return (NULL);
  841. if ((hashp->mapp[ndx] = (u_int32_t *)malloc(hashp->BSIZE)) == NULL)
  842. return (NULL);
  843. if (__get_page(hashp,
  844. (char *)hashp->mapp[ndx], hashp->BITMAPS[ndx], 0, 1, 1)) {
  845. free(hashp->mapp[ndx]);
  846. return (NULL);
  847. }
  848. return (hashp->mapp[ndx]);
  849. }
  850. #ifdef DEBUG4
  851. int
  852. print_chain(addr)
  853. int addr;
  854. {
  855. BUFHEAD *bufp;
  856. short *bp, oaddr;
  857. (void)fprintf(stderr, "%d ", addr);
  858. bufp = __get_buf(hashp, addr, NULL, 0);
  859. bp = (short *)bufp->page;
  860. while (bp[0] && ((bp[bp[0]] == OVFLPAGE) ||
  861. ((bp[0] > 2) && bp[2] < REAL_KEY))) {
  862. oaddr = bp[bp[0] - 1];
  863. (void)fprintf(stderr, "%d ", (int)oaddr);
  864. bufp = __get_buf(hashp, (int)oaddr, bufp, 0);
  865. bp = (short *)bufp->page;
  866. }
  867. (void)fprintf(stderr, "\n");
  868. }
  869. #endif