home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / db-1.6 / part02 < prev    next >
Encoding:
Text File  |  1993-07-05  |  91.9 KB  |  2,978 lines

  1. Newsgroups: comp.sources.unix
  2. From: bostic@cs.berkeley.edu (Keith Bostic)
  3. Subject: v26i281: db-1.6 - A New Hashing Package for UNIX(tm) (updates dbm/ndbm), Part02/09
  4. Sender: unix-sources-moderator@gw.home.vix.com
  5. Approved: vixie@gw.home.vix.com
  6.  
  7. Submitted-By: bostic@cs.berkeley.edu (Keith Bostic)
  8. Posting-Number: Volume 26, Issue 281
  9. Archive-Name: db-1.6/part02
  10.  
  11. #! /bin/sh
  12. # This is a shell archive.  Remove anything before this line, then unpack
  13. # it by saving it into a file and typing "sh file".  To overwrite existing
  14. # files, type "sh file -c".  You can also feed this as standard input via
  15. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  16. # will see the following message at the end:
  17. #        "End of archive 2 (of 9)."
  18. # Contents:  PORT/clib/memmove.c PORT/include/cdefs.h
  19. #   PORT/include/mpool.h PORT/sys/cdefs.h PORT/sys/mpool.h
  20. #   btree/bt_close.c btree/bt_conv.c btree/bt_search.c
  21. #   hash/hash_func.c hash/ndbm.c hash/page.h man/hash.3
  22. #   recno/rec_close.c recno/rec_delete.c recno/rec_search.c
  23. #   recno/rec_seq.c test/hash.tests/driver2.c test/hash.tests/tdel.c
  24. #   test/hash.tests/thash4.c
  25. # Wrapped by vixie@gw.home.vix.com on Mon Jul  5 15:27:20 1993
  26. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  27. if test -f 'PORT/clib/memmove.c' -a "${1}" != "-c" ; then 
  28.   echo shar: Will not clobber existing file \"'PORT/clib/memmove.c'\"
  29. else
  30. echo shar: Extracting \"'PORT/clib/memmove.c'\" \(4175 characters\)
  31. sed "s/^X//" >'PORT/clib/memmove.c' <<'END_OF_FILE'
  32. X/*-
  33. X * Copyright (c) 1990, 1993
  34. X *    The Regents of the University of California.  All rights reserved.
  35. X *
  36. X * This code is derived from software contributed to Berkeley by
  37. X * Chris Torek.
  38. X *
  39. X * Redistribution and use in source and binary forms, with or without
  40. X * modification, are permitted provided that the following conditions
  41. X * are met:
  42. X * 1. Redistributions of source code must retain the above copyright
  43. X *    notice, this list of conditions and the following disclaimer.
  44. X * 2. Redistributions in binary form must reproduce the above copyright
  45. X *    notice, this list of conditions and the following disclaimer in the
  46. X *    documentation and/or other materials provided with the distribution.
  47. X * 3. All advertising materials mentioning features or use of this software
  48. X *    must display the following acknowledgement:
  49. X *    This product includes software developed by the University of
  50. X *    California, Berkeley and its contributors.
  51. X * 4. Neither the name of the University nor the names of its contributors
  52. X *    may be used to endorse or promote products derived from this software
  53. X *    without specific prior written permission.
  54. X *
  55. X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  56. X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  57. X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  58. X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  59. X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  60. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  61. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  62. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  63. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  64. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  65. X * SUCH DAMAGE.
  66. X */
  67. X
  68. X#if defined(LIBC_SCCS) && !defined(lint)
  69. Xstatic char sccsid[] = "@(#)bcopy.c    8.1 (Berkeley) 6/4/93";
  70. X#endif /* LIBC_SCCS and not lint */
  71. X
  72. X#include <sys/cdefs.h>
  73. X#include <string.h>
  74. X
  75. X/*
  76. X * sizeof(word) MUST BE A POWER OF TWO
  77. X * SO THAT wmask BELOW IS ALL ONES
  78. X */
  79. Xtypedef    int word;        /* "word" used for optimal copy speed */
  80. X
  81. X#define    wsize    sizeof(word)
  82. X#define    wmask    (wsize - 1)
  83. X
  84. X/*
  85. X * Copy a block of memory, handling overlap.
  86. X * This is the routine that actually implements
  87. X * (the portable versions of) bcopy, memcpy, and memmove.
  88. X */
  89. X#ifdef MEMCOPY
  90. Xvoid *
  91. Xmemcpy(dst0, src0, length)
  92. X#else
  93. X#ifdef MEMMOVE
  94. Xvoid *
  95. Xmemmove(dst0, src0, length)
  96. X#else
  97. Xvoid
  98. Xbcopy(src0, dst0, length)
  99. X#endif
  100. X#endif
  101. X    void *dst0;
  102. X    const void *src0;
  103. X    register size_t length;
  104. X{
  105. X    register char *dst = dst0;
  106. X    register const char *src = src0;
  107. X    register size_t t;
  108. X
  109. X    if (length == 0 || dst == src)        /* nothing to do */
  110. X        goto done;
  111. X
  112. X    /*
  113. X     * Macros: loop-t-times; and loop-t-times, t>0
  114. X     */
  115. X#define    TLOOP(s) if (t) TLOOP1(s)
  116. X#define    TLOOP1(s) do { s; } while (--t)
  117. X
  118. X    if ((unsigned long)dst < (unsigned long)src) {
  119. X        /*
  120. X         * Copy forward.
  121. X         */
  122. X        t = (int)src;    /* only need low bits */
  123. X        if ((t | (int)dst) & wmask) {
  124. X            /*
  125. X             * Try to align operands.  This cannot be done
  126. X             * unless the low bits match.
  127. X             */
  128. X            if ((t ^ (int)dst) & wmask || length < wsize)
  129. X                t = length;
  130. X            else
  131. X                t = wsize - (t & wmask);
  132. X            length -= t;
  133. X            TLOOP1(*dst++ = *src++);
  134. X        }
  135. X        /*
  136. X         * Copy whole words, then mop up any trailing bytes.
  137. X         */
  138. X        t = length / wsize;
  139. X        TLOOP(*(word *)dst = *(word *)src; src += wsize; dst += wsize);
  140. X        t = length & wmask;
  141. X        TLOOP(*dst++ = *src++);
  142. X    } else {
  143. X        /*
  144. X         * Copy backwards.  Otherwise essentially the same.
  145. X         * Alignment works as before, except that it takes
  146. X         * (t&wmask) bytes to align, not wsize-(t&wmask).
  147. X         */
  148. X        src += length;
  149. X        dst += length;
  150. X        t = (int)src;
  151. X        if ((t | (int)dst) & wmask) {
  152. X            if ((t ^ (int)dst) & wmask || length <= wsize)
  153. X                t = length;
  154. X            else
  155. X                t &= wmask;
  156. X            length -= t;
  157. X            TLOOP1(*--dst = *--src);
  158. X        }
  159. X        t = length / wsize;
  160. X        TLOOP(src -= wsize; dst -= wsize; *(word *)dst = *(word *)src);
  161. X        t = length & wmask;
  162. X        TLOOP(*--dst = *--src);
  163. X    }
  164. Xdone:
  165. X#if defined(MEMCOPY) || defined(MEMMOVE)
  166. X    return (dst0);
  167. X#else
  168. X    return;
  169. X#endif
  170. X}
  171. END_OF_FILE
  172. if test 4175 -ne `wc -c <'PORT/clib/memmove.c'`; then
  173.     echo shar: \"'PORT/clib/memmove.c'\" unpacked with wrong size!
  174. fi
  175. # end of 'PORT/clib/memmove.c'
  176. fi
  177. if test -f 'PORT/include/cdefs.h' -a "${1}" != "-c" ; then 
  178.   echo shar: Will not clobber existing file \"'PORT/include/cdefs.h'\"
  179. else
  180. echo shar: Extracting \"'PORT/include/cdefs.h'\" \(3638 characters\)
  181. sed "s/^X//" >'PORT/include/cdefs.h' <<'END_OF_FILE'
  182. X/*
  183. X * Copyright (c) 1991, 1993
  184. X *    The Regents of the University of California.  All rights reserved.
  185. X *
  186. X * Redistribution and use in source and binary forms, with or without
  187. X * modification, are permitted provided that the following conditions
  188. X * are met:
  189. X * 1. Redistributions of source code must retain the above copyright
  190. X *    notice, this list of conditions and the following disclaimer.
  191. X * 2. Redistributions in binary form must reproduce the above copyright
  192. X *    notice, this list of conditions and the following disclaimer in the
  193. X *    documentation and/or other materials provided with the distribution.
  194. X * 3. All advertising materials mentioning features or use of this software
  195. X *    must display the following acknowledgement:
  196. X *    This product includes software developed by the University of
  197. X *    California, Berkeley and its contributors.
  198. X * 4. Neither the name of the University nor the names of its contributors
  199. X *    may be used to endorse or promote products derived from this software
  200. X *    without specific prior written permission.
  201. X *
  202. X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  203. X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  204. X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  205. X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  206. X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  207. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  208. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  209. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  210. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  211. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  212. X * SUCH DAMAGE.
  213. X *
  214. X *    @(#)cdefs.h    8.1 (Berkeley) 6/2/93
  215. X */
  216. X
  217. X#ifndef    _CDEFS_H_
  218. X#define    _CDEFS_H_
  219. X
  220. X#if defined(__cplusplus)
  221. X#define    __BEGIN_DECLS    extern "C" {
  222. X#define    __END_DECLS    };
  223. X#else
  224. X#define    __BEGIN_DECLS
  225. X#define    __END_DECLS
  226. X#endif
  227. X
  228. X/*
  229. X * The __CONCAT macro is used to concatenate parts of symbol names, e.g.
  230. X * with "#define OLD(foo) __CONCAT(old,foo)", OLD(foo) produces oldfoo.
  231. X * The __CONCAT macro is a bit tricky -- make sure you don't put spaces
  232. X * in between its arguments.  __CONCAT can also concatenate double-quoted
  233. X * strings produced by the __STRING macro, but this only works with ANSI C.
  234. X */
  235. X#if defined(__STDC__) || defined(__cplusplus)
  236. X#define    __P(protos)    protos        /* full-blown ANSI C */
  237. X#define    __CONCAT(x,y)    x ## y
  238. X#define    __STRING(x)    #x
  239. X
  240. X#else    /* !(__STDC__ || __cplusplus) */
  241. X#define    __P(protos)    ()        /* traditional C preprocessor */
  242. X#define    __CONCAT(x,y)    x/**/y
  243. X#define    __STRING(x)    "x"
  244. X
  245. X#ifdef __GNUC__
  246. X#define    const        __const        /* GCC: ANSI C with -traditional */
  247. X#define    inline        __inline
  248. X#define    signed        __signed
  249. X#define    volatile    __volatile
  250. X
  251. X#else    /* !__GNUC__ */
  252. X#define    const                /* delete ANSI C keywords */
  253. X#define    inline
  254. X#define    signed
  255. X#define    volatile
  256. X#endif    /* !__GNUC__ */
  257. X#endif    /* !(__STDC__ || __cplusplus) */
  258. X
  259. X/*
  260. X * GCC has extensions for declaring functions as `pure' (always returns
  261. X * the same value given the same inputs, i.e., has no external state and
  262. X * no side effects) and `dead' (nonreturning).  These mainly affect
  263. X * optimization and warnings.  Unfortunately, GCC complains if these are
  264. X * used under strict ANSI mode (`gcc -ansi -pedantic'), hence we need to
  265. X * define them only if compiling without this.
  266. X */
  267. X#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
  268. X#define __dead __volatile
  269. X#define __pure __const
  270. X#else
  271. X#define __dead
  272. X#define __pure
  273. X#endif
  274. X
  275. X#endif /* !_CDEFS_H_ */
  276. END_OF_FILE
  277. if test 3638 -ne `wc -c <'PORT/include/cdefs.h'`; then
  278.     echo shar: \"'PORT/include/cdefs.h'\" unpacked with wrong size!
  279. fi
  280. # end of 'PORT/include/cdefs.h'
  281. fi
  282. if test -f 'PORT/include/mpool.h' -a "${1}" != "-c" ; then 
  283.   echo shar: Will not clobber existing file \"'PORT/include/mpool.h'\"
  284. else
  285. echo shar: Extracting \"'PORT/include/mpool.h'\" \(5225 characters\)
  286. sed "s/^X//" >'PORT/include/mpool.h' <<'END_OF_FILE'
  287. X/*-
  288. X * Copyright (c) 1991, 1993
  289. X *    The Regents of the University of California.  All rights reserved.
  290. X *
  291. X * Redistribution and use in source and binary forms, with or without
  292. X * modification, are permitted provided that the following conditions
  293. X * are met:
  294. X * 1. Redistributions of source code must retain the above copyright
  295. X *    notice, this list of conditions and the following disclaimer.
  296. X * 2. Redistributions in binary form must reproduce the above copyright
  297. X *    notice, this list of conditions and the following disclaimer in the
  298. X *    documentation and/or other materials provided with the distribution.
  299. X * 3. All advertising materials mentioning features or use of this software
  300. X *    must display the following acknowledgement:
  301. X *    This product includes software developed by the University of
  302. X *    California, Berkeley and its contributors.
  303. X * 4. Neither the name of the University nor the names of its contributors
  304. X *    may be used to endorse or promote products derived from this software
  305. X *    without specific prior written permission.
  306. X *
  307. X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  308. X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  309. X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  310. X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  311. X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  312. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  313. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  314. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  315. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  316. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  317. X * SUCH DAMAGE.
  318. X *
  319. X *    @(#)mpool.h    8.1 (Berkeley) 6/2/93
  320. X */
  321. X
  322. X/*
  323. X * The memory pool scheme is a simple one.  Each in memory page is referenced
  324. X * by a bucket which is threaded in three ways.  All active pages are threaded
  325. X * on a hash chain (hashed by the page number) and an lru chain.  Inactive
  326. X * pages are threaded on a free chain.  Each reference to a memory pool is
  327. X * handed an MPOOL which is the opaque cookie passed to all of the memory
  328. X * routines.
  329. X */
  330. X#define    HASHSIZE    128
  331. X#define    HASHKEY(pgno)    ((pgno - 1) % HASHSIZE)
  332. X
  333. X/* The BKT structures are the elements of the lists. */
  334. Xtypedef struct BKT {
  335. X    struct BKT    *hnext;        /* next hash bucket */
  336. X    struct BKT    *hprev;        /* previous hash bucket */
  337. X    struct BKT    *cnext;        /* next free/lru bucket */
  338. X    struct BKT    *cprev;        /* previous free/lru bucket */
  339. X    void        *page;        /* page */
  340. X    pgno_t        pgno;        /* page number */
  341. X
  342. X#define    MPOOL_DIRTY    0x01        /* page needs to be written */
  343. X#define    MPOOL_PINNED    0x02        /* page is pinned into memory */
  344. X    unsigned long    flags;        /* flags */
  345. X} BKT;
  346. X
  347. X/* The BKTHDR structures are the heads of the lists. */
  348. Xtypedef struct BKTHDR {
  349. X    struct BKT    *hnext;        /* next hash bucket */
  350. X    struct BKT    *hprev;        /* previous hash bucket */
  351. X    struct BKT    *cnext;        /* next free/lru bucket */
  352. X    struct BKT    *cprev;        /* previous free/lru bucket */
  353. X} BKTHDR;
  354. X
  355. Xtypedef struct MPOOL {
  356. X    BKTHDR    free;            /* The free list. */
  357. X    BKTHDR    lru;            /* The LRU list. */
  358. X    BKTHDR    hashtable[HASHSIZE];    /* Hashed list by page number. */
  359. X    pgno_t    curcache;        /* Current number of cached pages. */
  360. X    pgno_t    maxcache;        /* Max number of cached pages. */
  361. X    pgno_t    npages;            /* Number of pages in the file. */
  362. X    u_long    pagesize;        /* File page size. */
  363. X    int    fd;            /* File descriptor. */
  364. X                    /* Page in conversion routine. */
  365. X    void    (*pgin) __P((void *, pgno_t, void *));
  366. X                    /* Page out conversion routine. */
  367. X    void    (*pgout) __P((void *, pgno_t, void *));
  368. X    void    *pgcookie;        /* Cookie for page in/out routines. */
  369. X#ifdef STATISTICS
  370. X    unsigned long    cachehit;
  371. X    unsigned long    cachemiss;
  372. X    unsigned long    pagealloc;
  373. X    unsigned long    pageflush;
  374. X    unsigned long    pageget;
  375. X    unsigned long    pagenew;
  376. X    unsigned long    pageput;
  377. X    unsigned long    pageread;
  378. X    unsigned long    pagewrite;
  379. X#endif
  380. X} MPOOL;
  381. X
  382. X#ifdef __MPOOLINTERFACE_PRIVATE
  383. X/* Macros to insert/delete into/from hash chain. */
  384. X#define rmhash(bp) { \
  385. X        (bp)->hprev->hnext = (bp)->hnext; \
  386. X        (bp)->hnext->hprev = (bp)->hprev; \
  387. X}
  388. X#define inshash(bp, pg) { \
  389. X    hp = &mp->hashtable[HASHKEY(pg)]; \
  390. X        (bp)->hnext = hp->hnext; \
  391. X        (bp)->hprev = (struct BKT *)hp; \
  392. X        hp->hnext->hprev = (bp); \
  393. X        hp->hnext = (bp); \
  394. X}
  395. X
  396. X/* Macros to insert/delete into/from lru and free chains. */
  397. X#define    rmchain(bp) { \
  398. X        (bp)->cprev->cnext = (bp)->cnext; \
  399. X        (bp)->cnext->cprev = (bp)->cprev; \
  400. X}
  401. X#define inschain(bp, dp) { \
  402. X        (bp)->cnext = (dp)->cnext; \
  403. X        (bp)->cprev = (struct BKT *)(dp); \
  404. X        (dp)->cnext->cprev = (bp); \
  405. X        (dp)->cnext = (bp); \
  406. X}
  407. X#endif
  408. X
  409. X__BEGIN_DECLS
  410. XMPOOL    *mpool_open __P((DBT *, int, pgno_t, pgno_t));
  411. Xvoid     mpool_filter __P((MPOOL *, void (*)(void *, pgno_t, void *),
  412. X        void (*)(void *, pgno_t, void *), void *));
  413. Xvoid    *mpool_new __P((MPOOL *, pgno_t *));
  414. Xvoid    *mpool_get __P((MPOOL *, pgno_t, u_int));
  415. Xint     mpool_put __P((MPOOL *, void *, u_int));
  416. Xint     mpool_sync __P((MPOOL *));
  417. Xint     mpool_close __P((MPOOL *));
  418. X#ifdef STATISTICS
  419. Xvoid     mpool_stat __P((MPOOL *));
  420. X#endif
  421. X__END_DECLS
  422. END_OF_FILE
  423. if test 5225 -ne `wc -c <'PORT/include/mpool.h'`; then
  424.     echo shar: \"'PORT/include/mpool.h'\" unpacked with wrong size!
  425. fi
  426. # end of 'PORT/include/mpool.h'
  427. fi
  428. if test -f 'PORT/sys/cdefs.h' -a "${1}" != "-c" ; then 
  429.   echo shar: Will not clobber existing file \"'PORT/sys/cdefs.h'\"
  430. else
  431. echo shar: Extracting \"'PORT/sys/cdefs.h'\" \(3638 characters\)
  432. sed "s/^X//" >'PORT/sys/cdefs.h' <<'END_OF_FILE'
  433. X/*
  434. X * Copyright (c) 1991, 1993
  435. X *    The Regents of the University of California.  All rights reserved.
  436. X *
  437. X * Redistribution and use in source and binary forms, with or without
  438. X * modification, are permitted provided that the following conditions
  439. X * are met:
  440. X * 1. Redistributions of source code must retain the above copyright
  441. X *    notice, this list of conditions and the following disclaimer.
  442. X * 2. Redistributions in binary form must reproduce the above copyright
  443. X *    notice, this list of conditions and the following disclaimer in the
  444. X *    documentation and/or other materials provided with the distribution.
  445. X * 3. All advertising materials mentioning features or use of this software
  446. X *    must display the following acknowledgement:
  447. X *    This product includes software developed by the University of
  448. X *    California, Berkeley and its contributors.
  449. X * 4. Neither the name of the University nor the names of its contributors
  450. X *    may be used to endorse or promote products derived from this software
  451. X *    without specific prior written permission.
  452. X *
  453. X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  454. X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  455. X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  456. X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  457. X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  458. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  459. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  460. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  461. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  462. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  463. X * SUCH DAMAGE.
  464. X *
  465. X *    @(#)cdefs.h    8.1 (Berkeley) 6/2/93
  466. X */
  467. X
  468. X#ifndef    _CDEFS_H_
  469. X#define    _CDEFS_H_
  470. X
  471. X#if defined(__cplusplus)
  472. X#define    __BEGIN_DECLS    extern "C" {
  473. X#define    __END_DECLS    };
  474. X#else
  475. X#define    __BEGIN_DECLS
  476. X#define    __END_DECLS
  477. X#endif
  478. X
  479. X/*
  480. X * The __CONCAT macro is used to concatenate parts of symbol names, e.g.
  481. X * with "#define OLD(foo) __CONCAT(old,foo)", OLD(foo) produces oldfoo.
  482. X * The __CONCAT macro is a bit tricky -- make sure you don't put spaces
  483. X * in between its arguments.  __CONCAT can also concatenate double-quoted
  484. X * strings produced by the __STRING macro, but this only works with ANSI C.
  485. X */
  486. X#if defined(__STDC__) || defined(__cplusplus)
  487. X#define    __P(protos)    protos        /* full-blown ANSI C */
  488. X#define    __CONCAT(x,y)    x ## y
  489. X#define    __STRING(x)    #x
  490. X
  491. X#else    /* !(__STDC__ || __cplusplus) */
  492. X#define    __P(protos)    ()        /* traditional C preprocessor */
  493. X#define    __CONCAT(x,y)    x/**/y
  494. X#define    __STRING(x)    "x"
  495. X
  496. X#ifdef __GNUC__
  497. X#define    const        __const        /* GCC: ANSI C with -traditional */
  498. X#define    inline        __inline
  499. X#define    signed        __signed
  500. X#define    volatile    __volatile
  501. X
  502. X#else    /* !__GNUC__ */
  503. X#define    const                /* delete ANSI C keywords */
  504. X#define    inline
  505. X#define    signed
  506. X#define    volatile
  507. X#endif    /* !__GNUC__ */
  508. X#endif    /* !(__STDC__ || __cplusplus) */
  509. X
  510. X/*
  511. X * GCC has extensions for declaring functions as `pure' (always returns
  512. X * the same value given the same inputs, i.e., has no external state and
  513. X * no side effects) and `dead' (nonreturning).  These mainly affect
  514. X * optimization and warnings.  Unfortunately, GCC complains if these are
  515. X * used under strict ANSI mode (`gcc -ansi -pedantic'), hence we need to
  516. X * define them only if compiling without this.
  517. X */
  518. X#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
  519. X#define __dead __volatile
  520. X#define __pure __const
  521. X#else
  522. X#define __dead
  523. X#define __pure
  524. X#endif
  525. X
  526. X#endif /* !_CDEFS_H_ */
  527. END_OF_FILE
  528. if test 3638 -ne `wc -c <'PORT/sys/cdefs.h'`; then
  529.     echo shar: \"'PORT/sys/cdefs.h'\" unpacked with wrong size!
  530. fi
  531. # end of 'PORT/sys/cdefs.h'
  532. fi
  533. if test -f 'PORT/sys/mpool.h' -a "${1}" != "-c" ; then 
  534.   echo shar: Will not clobber existing file \"'PORT/sys/mpool.h'\"
  535. else
  536. echo shar: Extracting \"'PORT/sys/mpool.h'\" \(5225 characters\)
  537. sed "s/^X//" >'PORT/sys/mpool.h' <<'END_OF_FILE'
  538. X/*-
  539. X * Copyright (c) 1991, 1993
  540. X *    The Regents of the University of California.  All rights reserved.
  541. X *
  542. X * Redistribution and use in source and binary forms, with or without
  543. X * modification, are permitted provided that the following conditions
  544. X * are met:
  545. X * 1. Redistributions of source code must retain the above copyright
  546. X *    notice, this list of conditions and the following disclaimer.
  547. X * 2. Redistributions in binary form must reproduce the above copyright
  548. X *    notice, this list of conditions and the following disclaimer in the
  549. X *    documentation and/or other materials provided with the distribution.
  550. X * 3. All advertising materials mentioning features or use of this software
  551. X *    must display the following acknowledgement:
  552. X *    This product includes software developed by the University of
  553. X *    California, Berkeley and its contributors.
  554. X * 4. Neither the name of the University nor the names of its contributors
  555. X *    may be used to endorse or promote products derived from this software
  556. X *    without specific prior written permission.
  557. X *
  558. X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  559. X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  560. X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  561. X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  562. X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  563. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  564. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  565. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  566. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  567. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  568. X * SUCH DAMAGE.
  569. X *
  570. X *    @(#)mpool.h    8.1 (Berkeley) 6/2/93
  571. X */
  572. X
  573. X/*
  574. X * The memory pool scheme is a simple one.  Each in memory page is referenced
  575. X * by a bucket which is threaded in three ways.  All active pages are threaded
  576. X * on a hash chain (hashed by the page number) and an lru chain.  Inactive
  577. X * pages are threaded on a free chain.  Each reference to a memory pool is
  578. X * handed an MPOOL which is the opaque cookie passed to all of the memory
  579. X * routines.
  580. X */
  581. X#define    HASHSIZE    128
  582. X#define    HASHKEY(pgno)    ((pgno - 1) % HASHSIZE)
  583. X
  584. X/* The BKT structures are the elements of the lists. */
  585. Xtypedef struct BKT {
  586. X    struct BKT    *hnext;        /* next hash bucket */
  587. X    struct BKT    *hprev;        /* previous hash bucket */
  588. X    struct BKT    *cnext;        /* next free/lru bucket */
  589. X    struct BKT    *cprev;        /* previous free/lru bucket */
  590. X    void        *page;        /* page */
  591. X    pgno_t        pgno;        /* page number */
  592. X
  593. X#define    MPOOL_DIRTY    0x01        /* page needs to be written */
  594. X#define    MPOOL_PINNED    0x02        /* page is pinned into memory */
  595. X    unsigned long    flags;        /* flags */
  596. X} BKT;
  597. X
  598. X/* The BKTHDR structures are the heads of the lists. */
  599. Xtypedef struct BKTHDR {
  600. X    struct BKT    *hnext;        /* next hash bucket */
  601. X    struct BKT    *hprev;        /* previous hash bucket */
  602. X    struct BKT    *cnext;        /* next free/lru bucket */
  603. X    struct BKT    *cprev;        /* previous free/lru bucket */
  604. X} BKTHDR;
  605. X
  606. Xtypedef struct MPOOL {
  607. X    BKTHDR    free;            /* The free list. */
  608. X    BKTHDR    lru;            /* The LRU list. */
  609. X    BKTHDR    hashtable[HASHSIZE];    /* Hashed list by page number. */
  610. X    pgno_t    curcache;        /* Current number of cached pages. */
  611. X    pgno_t    maxcache;        /* Max number of cached pages. */
  612. X    pgno_t    npages;            /* Number of pages in the file. */
  613. X    u_long    pagesize;        /* File page size. */
  614. X    int    fd;            /* File descriptor. */
  615. X                    /* Page in conversion routine. */
  616. X    void    (*pgin) __P((void *, pgno_t, void *));
  617. X                    /* Page out conversion routine. */
  618. X    void    (*pgout) __P((void *, pgno_t, void *));
  619. X    void    *pgcookie;        /* Cookie for page in/out routines. */
  620. X#ifdef STATISTICS
  621. X    unsigned long    cachehit;
  622. X    unsigned long    cachemiss;
  623. X    unsigned long    pagealloc;
  624. X    unsigned long    pageflush;
  625. X    unsigned long    pageget;
  626. X    unsigned long    pagenew;
  627. X    unsigned long    pageput;
  628. X    unsigned long    pageread;
  629. X    unsigned long    pagewrite;
  630. X#endif
  631. X} MPOOL;
  632. X
  633. X#ifdef __MPOOLINTERFACE_PRIVATE
  634. X/* Macros to insert/delete into/from hash chain. */
  635. X#define rmhash(bp) { \
  636. X        (bp)->hprev->hnext = (bp)->hnext; \
  637. X        (bp)->hnext->hprev = (bp)->hprev; \
  638. X}
  639. X#define inshash(bp, pg) { \
  640. X    hp = &mp->hashtable[HASHKEY(pg)]; \
  641. X        (bp)->hnext = hp->hnext; \
  642. X        (bp)->hprev = (struct BKT *)hp; \
  643. X        hp->hnext->hprev = (bp); \
  644. X        hp->hnext = (bp); \
  645. X}
  646. X
  647. X/* Macros to insert/delete into/from lru and free chains. */
  648. X#define    rmchain(bp) { \
  649. X        (bp)->cprev->cnext = (bp)->cnext; \
  650. X        (bp)->cnext->cprev = (bp)->cprev; \
  651. X}
  652. X#define inschain(bp, dp) { \
  653. X        (bp)->cnext = (dp)->cnext; \
  654. X        (bp)->cprev = (struct BKT *)(dp); \
  655. X        (dp)->cnext->cprev = (bp); \
  656. X        (dp)->cnext = (bp); \
  657. X}
  658. X#endif
  659. X
  660. X__BEGIN_DECLS
  661. XMPOOL    *mpool_open __P((DBT *, int, pgno_t, pgno_t));
  662. Xvoid     mpool_filter __P((MPOOL *, void (*)(void *, pgno_t, void *),
  663. X        void (*)(void *, pgno_t, void *), void *));
  664. Xvoid    *mpool_new __P((MPOOL *, pgno_t *));
  665. Xvoid    *mpool_get __P((MPOOL *, pgno_t, u_int));
  666. Xint     mpool_put __P((MPOOL *, void *, u_int));
  667. Xint     mpool_sync __P((MPOOL *));
  668. Xint     mpool_close __P((MPOOL *));
  669. X#ifdef STATISTICS
  670. Xvoid     mpool_stat __P((MPOOL *));
  671. X#endif
  672. X__END_DECLS
  673. END_OF_FILE
  674. if test 5225 -ne `wc -c <'PORT/sys/mpool.h'`; then
  675.     echo shar: \"'PORT/sys/mpool.h'\" unpacked with wrong size!
  676. fi
  677. # end of 'PORT/sys/mpool.h'
  678. fi
  679. if test -f 'btree/bt_close.c' -a "${1}" != "-c" ; then 
  680.   echo shar: Will not clobber existing file \"'btree/bt_close.c'\"
  681. else
  682. echo shar: Extracting \"'btree/bt_close.c'\" \(4880 characters\)
  683. sed "s/^X//" >'btree/bt_close.c' <<'END_OF_FILE'
  684. X/*-
  685. X * Copyright (c) 1990, 1993
  686. X *    The Regents of the University of California.  All rights reserved.
  687. X *
  688. X * This code is derived from software contributed to Berkeley by
  689. X * Mike Olson.
  690. X *
  691. X * Redistribution and use in source and binary forms, with or without
  692. X * modification, are permitted provided that the following conditions
  693. X * are met:
  694. X * 1. Redistributions of source code must retain the above copyright
  695. X *    notice, this list of conditions and the following disclaimer.
  696. X * 2. Redistributions in binary form must reproduce the above copyright
  697. X *    notice, this list of conditions and the following disclaimer in the
  698. X *    documentation and/or other materials provided with the distribution.
  699. X * 3. All advertising materials mentioning features or use of this software
  700. X *    must display the following acknowledgement:
  701. X *    This product includes software developed by the University of
  702. X *    California, Berkeley and its contributors.
  703. X * 4. Neither the name of the University nor the names of its contributors
  704. X *    may be used to endorse or promote products derived from this software
  705. X *    without specific prior written permission.
  706. X *
  707. X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  708. X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  709. X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  710. X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  711. X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  712. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  713. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  714. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  715. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  716. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  717. X * SUCH DAMAGE.
  718. X */
  719. X
  720. X#if defined(LIBC_SCCS) && !defined(lint)
  721. Xstatic char sccsid[] = "@(#)bt_close.c    8.1 (Berkeley) 6/4/93";
  722. X#endif /* LIBC_SCCS and not lint */
  723. X
  724. X#include <sys/param.h>
  725. X
  726. X#include <errno.h>
  727. X#include <stdio.h>
  728. X#include <stdlib.h>
  729. X#include <string.h>
  730. X#include <unistd.h>
  731. X
  732. X#include <db.h>
  733. X#include "btree.h"
  734. X
  735. Xstatic int bt_meta __P((BTREE *));
  736. X
  737. X/*
  738. X * BT_CLOSE -- Close a btree.
  739. X *
  740. X * Parameters:
  741. X *    dbp:    pointer to access method
  742. X *
  743. X * Returns:
  744. X *    RET_ERROR, RET_SUCCESS
  745. X */
  746. Xint
  747. X__bt_close(dbp)
  748. X    DB *dbp;
  749. X{
  750. X    BTREE *t;
  751. X    int fd;
  752. X
  753. X    t = dbp->internal;
  754. X
  755. X    /*
  756. X     * Delete any already deleted record that we've been saving
  757. X     * because the cursor pointed to it.
  758. X     */
  759. X    if (ISSET(t, B_DELCRSR) && __bt_crsrdel(t, &t->bt_bcursor))
  760. X        return (RET_ERROR);
  761. X
  762. X    if (__bt_sync(dbp, 0) == RET_ERROR)
  763. X        return (RET_ERROR);
  764. X
  765. X    if (mpool_close(t->bt_mp) == RET_ERROR)
  766. X        return (RET_ERROR);
  767. X
  768. X    if (t->bt_stack)
  769. X        free(t->bt_stack);
  770. X    if (t->bt_kbuf)
  771. X        free(t->bt_kbuf);
  772. X    if (t->bt_dbuf)
  773. X        free(t->bt_dbuf);
  774. X
  775. X    fd = t->bt_fd;
  776. X    free(t);
  777. X    free(dbp);
  778. X    return (close(fd) ? RET_ERROR : RET_SUCCESS);
  779. X}
  780. X
  781. X/*
  782. X * BT_SYNC -- sync the btree to disk.
  783. X *
  784. X * Parameters:
  785. X *    dbp:    pointer to access method
  786. X *
  787. X * Returns:
  788. X *    RET_SUCCESS, RET_ERROR.
  789. X */
  790. Xint
  791. X__bt_sync(dbp, flags)
  792. X    const DB *dbp;
  793. X    u_int flags;
  794. X{
  795. X    BTREE *t;
  796. X    int status;
  797. X    PAGE *h;
  798. X    void *p;
  799. X
  800. X    if (flags != 0) {
  801. X        errno = EINVAL;
  802. X        return (RET_ERROR);
  803. X    }
  804. X
  805. X    t = dbp->internal;
  806. X
  807. X    if (ISSET(t, B_INMEM | B_RDONLY) || !ISSET(t, B_MODIFIED))
  808. X        return (RET_SUCCESS);
  809. X
  810. X    if (ISSET(t, B_METADIRTY) && bt_meta(t) == RET_ERROR)
  811. X        return (RET_ERROR);
  812. X
  813. X    /*
  814. X     * Nastiness.  If the cursor has been marked for deletion, but not
  815. X     * actually deleted, we have to make a copy of the page, delete the
  816. X     * key/data item, sync the file, and then restore the original page
  817. X     * contents.
  818. X     */
  819. X    if (ISSET(t, B_DELCRSR)) {
  820. X        if ((p = malloc(t->bt_psize)) == NULL)
  821. X            return (RET_ERROR);
  822. X        if ((h = mpool_get(t->bt_mp, t->bt_bcursor.pgno, 0)) == NULL)
  823. X            return (RET_ERROR);
  824. X        memmove(p, h, t->bt_psize);
  825. X        if ((status =
  826. X            __bt_dleaf(t, h, t->bt_bcursor.index)) == RET_ERROR)
  827. X            goto ecrsr;
  828. X        mpool_put(t->bt_mp, h, MPOOL_DIRTY);
  829. X    }
  830. X        
  831. X    if ((status = mpool_sync(t->bt_mp)) == RET_SUCCESS)
  832. X        CLR(t, B_MODIFIED);
  833. X
  834. Xecrsr:    if (ISSET(t, B_DELCRSR)) {
  835. X        if ((h = mpool_get(t->bt_mp, t->bt_bcursor.pgno, 0)) == NULL)
  836. X            return (RET_ERROR);
  837. X        memmove(h, p, t->bt_psize);
  838. X        free(p);
  839. X        mpool_put(t->bt_mp, h, MPOOL_DIRTY);
  840. X    }
  841. X    return (status);
  842. X}
  843. X
  844. X/*
  845. X * BT_META -- write the tree meta data to disk.
  846. X *
  847. X * Parameters:
  848. X *    t:    tree
  849. X *
  850. X * Returns:
  851. X *    RET_ERROR, RET_SUCCESS
  852. X */
  853. Xstatic int
  854. Xbt_meta(t)
  855. X    BTREE *t;
  856. X{
  857. X    BTMETA m;
  858. X    void *p;
  859. X
  860. X    if ((p = mpool_get(t->bt_mp, P_META, 0)) == NULL)
  861. X        return (RET_ERROR);
  862. X
  863. X    /* Fill in metadata. */
  864. X    m.m_magic = BTREEMAGIC;
  865. X    m.m_version = BTREEVERSION;
  866. X    m.m_psize = t->bt_psize;
  867. X    m.m_free = t->bt_free;
  868. X    m.m_nrecs = t->bt_nrecs;
  869. X    m.m_flags = t->bt_flags & SAVEMETA;
  870. X
  871. X    memmove(p, &m, sizeof(BTMETA));
  872. X    mpool_put(t->bt_mp, p, MPOOL_DIRTY);
  873. X    return (RET_SUCCESS);
  874. X}
  875. END_OF_FILE
  876. if test 4880 -ne `wc -c <'btree/bt_close.c'`; then
  877.     echo shar: \"'btree/bt_close.c'\" unpacked with wrong size!
  878. fi
  879. # end of 'btree/bt_close.c'
  880. fi
  881. if test -f 'btree/bt_conv.c' -a "${1}" != "-c" ; then 
  882.   echo shar: Will not clobber existing file \"'btree/bt_conv.c'\"
  883. else
  884. echo shar: Extracting \"'btree/bt_conv.c'\" \(5253 characters\)
  885. sed "s/^X//" >'btree/bt_conv.c' <<'END_OF_FILE'
  886. X/*-
  887. X * Copyright (c) 1990, 1993
  888. X *    The Regents of the University of California.  All rights reserved.
  889. X *
  890. X * This code is derived from software contributed to Berkeley by
  891. X * Mike Olson.
  892. X *
  893. X * Redistribution and use in source and binary forms, with or without
  894. X * modification, are permitted provided that the following conditions
  895. X * are met:
  896. X * 1. Redistributions of source code must retain the above copyright
  897. X *    notice, this list of conditions and the following disclaimer.
  898. X * 2. Redistributions in binary form must reproduce the above copyright
  899. X *    notice, this list of conditions and the following disclaimer in the
  900. X *    documentation and/or other materials provided with the distribution.
  901. X * 3. All advertising materials mentioning features or use of this software
  902. X *    must display the following acknowledgement:
  903. X *    This product includes software developed by the University of
  904. X *    California, Berkeley and its contributors.
  905. X * 4. Neither the name of the University nor the names of its contributors
  906. X *    may be used to endorse or promote products derived from this software
  907. X *    without specific prior written permission.
  908. X *
  909. X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  910. X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  911. X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  912. X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  913. X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  914. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  915. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  916. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  917. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  918. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  919. X * SUCH DAMAGE.
  920. X */
  921. X
  922. X#if defined(LIBC_SCCS) && !defined(lint)
  923. Xstatic char sccsid[] = "@(#)bt_conv.c    8.1 (Berkeley) 6/4/93";
  924. X#endif /* LIBC_SCCS and not lint */
  925. X
  926. X#include <sys/param.h>
  927. X
  928. X#include <stdio.h>
  929. X
  930. X#include <db.h>
  931. X#include "btree.h"
  932. X
  933. Xstatic void mswap __P((PAGE *));
  934. X
  935. X/*
  936. X * __BT_BPGIN, __BT_BPGOUT --
  937. X *    Convert host-specific number layout to/from the host-independent
  938. X *    format stored on disk.
  939. X *
  940. X * Parameters:
  941. X *    t:    tree
  942. X *    pg:    page number
  943. X *    h:    page to convert
  944. X */
  945. Xvoid
  946. X__bt_pgin(t, pg, pp)
  947. X    void *t;
  948. X    pgno_t pg;
  949. X    void *pp;
  950. X{
  951. X    PAGE *h;
  952. X    int i, top;
  953. X    u_char flags;
  954. X    char *p;
  955. X
  956. X    if (!ISSET(((BTREE *)t), B_NEEDSWAP))
  957. X        return;
  958. X    if (pg == P_META) {
  959. X        mswap(pp);
  960. X        return;
  961. X    }
  962. X
  963. X    h = pp;
  964. X    BLSWAP(h->pgno);
  965. X    BLSWAP(h->prevpg);
  966. X    BLSWAP(h->nextpg);
  967. X    BLSWAP(h->flags);
  968. X    BSSWAP(h->lower);
  969. X    BSSWAP(h->upper);
  970. X
  971. X    top = NEXTINDEX(h);
  972. X    if ((h->flags & P_TYPE) == P_BINTERNAL)
  973. X        for (i = 0; i < top; i++) {
  974. X            BSSWAP(h->linp[i]);
  975. X            p = (char *)GETBINTERNAL(h, i);
  976. X            BLPSWAP(p);
  977. X            p += sizeof(size_t);
  978. X            BLPSWAP(p);
  979. X            p += sizeof(pgno_t);
  980. X            if (*(u_char *)p & P_BIGKEY) {
  981. X                p += sizeof(u_char);
  982. X                BLPSWAP(p);
  983. X                p += sizeof(pgno_t);
  984. X                BLPSWAP(p);
  985. X            }
  986. X        }
  987. X    else if ((h->flags & P_TYPE) == P_BLEAF)
  988. X        for (i = 0; i < top; i++) {
  989. X            BSSWAP(h->linp[i]);
  990. X            p = (char *)GETBLEAF(h, i);
  991. X            BLPSWAP(p);
  992. X            p += sizeof(size_t);
  993. X            BLPSWAP(p);
  994. X            p += sizeof(size_t);
  995. X            flags = *(u_char *)p;
  996. X            if (flags & (P_BIGKEY | P_BIGDATA)) {
  997. X                p += sizeof(u_char);
  998. X                if (flags & P_BIGKEY) {
  999. X                    BLPSWAP(p);
  1000. X                    p += sizeof(pgno_t);
  1001. X                    BLPSWAP(p);
  1002. X                }
  1003. X                if (flags & P_BIGDATA) {
  1004. X                    p += sizeof(size_t);
  1005. X                    BLPSWAP(p);
  1006. X                    p += sizeof(pgno_t);
  1007. X                    BLPSWAP(p);
  1008. X                }
  1009. X            }
  1010. X        }
  1011. X}
  1012. X
  1013. Xvoid
  1014. X__bt_pgout(t, pg, pp)
  1015. X    void *t;
  1016. X    pgno_t pg;
  1017. X    void *pp;
  1018. X{
  1019. X    PAGE *h;
  1020. X    int i, top;
  1021. X    u_char flags;
  1022. X    char *p;
  1023. X
  1024. X    if (!ISSET(((BTREE *)t), B_NEEDSWAP))
  1025. X        return;
  1026. X    if (pg == P_META) {
  1027. X        mswap(pp);
  1028. X        return;
  1029. X    }
  1030. X
  1031. X    h = pp;
  1032. X    top = NEXTINDEX(h);
  1033. X    if ((h->flags & P_TYPE) == P_BINTERNAL)
  1034. X        for (i = 0; i < top; i++) {
  1035. X            p = (char *)GETBINTERNAL(h, i);
  1036. X            BLPSWAP(p);
  1037. X            p += sizeof(size_t);
  1038. X            BLPSWAP(p);
  1039. X            p += sizeof(pgno_t);
  1040. X            if (*(u_char *)p & P_BIGKEY) {
  1041. X                p += sizeof(u_char);
  1042. X                BLPSWAP(p);
  1043. X                p += sizeof(pgno_t);
  1044. X                BLPSWAP(p);
  1045. X            }
  1046. X            BSSWAP(h->linp[i]);
  1047. X        }
  1048. X    else if ((h->flags & P_TYPE) == P_BLEAF)
  1049. X        for (i = 0; i < top; i++) {
  1050. X            p = (char *)GETBLEAF(h, i);
  1051. X            BLPSWAP(p);
  1052. X            p += sizeof(size_t);
  1053. X            BLPSWAP(p);
  1054. X            p += sizeof(size_t);
  1055. X            flags = *(u_char *)p;
  1056. X            if (flags & (P_BIGKEY | P_BIGDATA)) {
  1057. X                p += sizeof(u_char);
  1058. X                if (flags & P_BIGKEY) {
  1059. X                    BLPSWAP(p);
  1060. X                    p += sizeof(pgno_t);
  1061. X                    BLPSWAP(p);
  1062. X                }
  1063. X                if (flags & P_BIGDATA) {
  1064. X                    p += sizeof(size_t);
  1065. X                    BLPSWAP(p);
  1066. X                    p += sizeof(pgno_t);
  1067. X                    BLPSWAP(p);
  1068. X                }
  1069. X            }
  1070. X            BSSWAP(h->linp[i]);
  1071. X        }
  1072. X
  1073. X    BLSWAP(h->pgno);
  1074. X    BLSWAP(h->prevpg);
  1075. X    BLSWAP(h->nextpg);
  1076. X    BLSWAP(h->flags);
  1077. X    BSSWAP(h->lower);
  1078. X    BSSWAP(h->upper);
  1079. X}
  1080. X
  1081. X/*
  1082. X * MSWAP -- Actually swap the bytes on the meta page.
  1083. X *
  1084. X * Parameters:
  1085. X *    p:    page to convert
  1086. X */
  1087. Xstatic void
  1088. Xmswap(pg)
  1089. X    PAGE *pg;
  1090. X{
  1091. X    char *p;
  1092. X
  1093. X    p = (char *)pg;
  1094. X    BLPSWAP(p);        /* m_magic */
  1095. X    p += sizeof(u_long);
  1096. X    BLPSWAP(p);        /* m_version */
  1097. X    p += sizeof(u_long);
  1098. X    BLPSWAP(p);        /* m_psize */
  1099. X    p += sizeof(u_long);
  1100. X    BLPSWAP(p);        /* m_free */
  1101. X    p += sizeof(u_long);
  1102. X    BLPSWAP(p);        /* m_nrecs */
  1103. X    p += sizeof(u_long);
  1104. X    BLPSWAP(p);        /* m_flags */
  1105. X    p += sizeof(u_long);
  1106. X}
  1107. END_OF_FILE
  1108. if test 5253 -ne `wc -c <'btree/bt_conv.c'`; then
  1109.     echo shar: \"'btree/bt_conv.c'\" unpacked with wrong size!
  1110. fi
  1111. # end of 'btree/bt_conv.c'
  1112. fi
  1113. if test -f 'btree/bt_search.c' -a "${1}" != "-c" ; then 
  1114.   echo shar: Will not clobber existing file \"'btree/bt_search.c'\"
  1115. else
  1116. echo shar: Extracting \"'btree/bt_search.c'\" \(3809 characters\)
  1117. sed "s/^X//" >'btree/bt_search.c' <<'END_OF_FILE'
  1118. X/*-
  1119. X * Copyright (c) 1990, 1993
  1120. X *    The Regents of the University of California.  All rights reserved.
  1121. X *
  1122. X * This code is derived from software contributed to Berkeley by
  1123. X * Mike Olson.
  1124. X *
  1125. X * Redistribution and use in source and binary forms, with or without
  1126. X * modification, are permitted provided that the following conditions
  1127. X * are met:
  1128. X * 1. Redistributions of source code must retain the above copyright
  1129. X *    notice, this list of conditions and the following disclaimer.
  1130. X * 2. Redistributions in binary form must reproduce the above copyright
  1131. X *    notice, this list of conditions and the following disclaimer in the
  1132. X *    documentation and/or other materials provided with the distribution.
  1133. X * 3. All advertising materials mentioning features or use of this software
  1134. X *    must display the following acknowledgement:
  1135. X *    This product includes software developed by the University of
  1136. X *    California, Berkeley and its contributors.
  1137. X * 4. Neither the name of the University nor the names of its contributors
  1138. X *    may be used to endorse or promote products derived from this software
  1139. X *    without specific prior written permission.
  1140. X *
  1141. X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  1142. X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  1143. X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  1144. X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  1145. X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  1146. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  1147. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  1148. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  1149. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  1150. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  1151. X * SUCH DAMAGE.
  1152. X */
  1153. X
  1154. X#if defined(LIBC_SCCS) && !defined(lint)
  1155. Xstatic char sccsid[] = "@(#)bt_search.c    8.1 (Berkeley) 6/4/93";
  1156. X#endif /* LIBC_SCCS and not lint */
  1157. X
  1158. X#include <sys/types.h>
  1159. X
  1160. X#include <stdio.h>
  1161. X
  1162. X#include <db.h>
  1163. X#include "btree.h"
  1164. X
  1165. X/*
  1166. X * __BT_SEARCH -- Search a btree for a key.
  1167. X *
  1168. X * Parameters:
  1169. X *    t:    tree to search
  1170. X *    key:    key to find
  1171. X *    exactp:    pointer to exact match flag
  1172. X *
  1173. X * Returns:
  1174. X *    EPG for matching record, if any, or the EPG for the location of the
  1175. X *    key, if it were inserted into the tree.
  1176. X *
  1177. X * Warnings:
  1178. X *    The EPG returned is in static memory, and will be overwritten by the
  1179. X *    next search of any kind in any tree.
  1180. X */
  1181. XEPG *
  1182. X__bt_search(t, key, exactp)
  1183. X    BTREE *t;
  1184. X    const DBT *key;
  1185. X    int *exactp;
  1186. X{
  1187. X    register indx_t index;
  1188. X    register int base, cmp, lim;
  1189. X    register PAGE *h;
  1190. X    pgno_t pg;
  1191. X    static EPG e;
  1192. X
  1193. X    BT_CLR(t);
  1194. X    for (pg = P_ROOT;;) {
  1195. X        if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
  1196. X            return (NULL);
  1197. X
  1198. X        /* Do a binary search on the current page. */
  1199. X        e.page = h;
  1200. X        for (base = 0, lim = NEXTINDEX(h); lim; lim >>= 1) {
  1201. X            e.index = index = base + (lim >> 1);
  1202. X            if ((cmp = __bt_cmp(t, key, &e)) == 0) {
  1203. X                if (h->flags & P_BLEAF) {
  1204. X                    *exactp = 1;
  1205. X                    return (&e);
  1206. X                }
  1207. X                goto next;
  1208. X            }
  1209. X            if (cmp > 0) {
  1210. X                base = index + 1;
  1211. X                --lim;
  1212. X            }
  1213. X        }
  1214. X
  1215. X        /* If it's a leaf page, we're done. */
  1216. X        if (h->flags & P_BLEAF) {
  1217. X            e.index = base;
  1218. X            *exactp = 0;
  1219. X            return (&e);
  1220. X        }
  1221. X
  1222. X        /*
  1223. X         * No match found.  Base is the smallest index greater than
  1224. X         * key and may be zero or a last + 1 index.  If it's non-zero,
  1225. X         * decrement by one, and record the internal page which should
  1226. X         * be a parent page for the key.  If a split later occurs, the
  1227. X         * inserted page will be to the right of the saved page.
  1228. X         */
  1229. X        index = base ? base - 1 : base;
  1230. X
  1231. Xnext:        if (__bt_push(t, h->pgno, index) == RET_ERROR)
  1232. X            return (NULL);
  1233. X        pg = GETBINTERNAL(h, index)->pgno;
  1234. X        mpool_put(t->bt_mp, h, 0);
  1235. X    }
  1236. X}
  1237. END_OF_FILE
  1238. if test 3809 -ne `wc -c <'btree/bt_search.c'`; then
  1239.     echo shar: \"'btree/bt_search.c'\" unpacked with wrong size!
  1240. fi
  1241. # end of 'btree/bt_search.c'
  1242. fi
  1243. if test -f 'hash/hash_func.c' -a "${1}" != "-c" ; then 
  1244.   echo shar: Will not clobber existing file \"'hash/hash_func.c'\"
  1245. else
  1246. echo shar: Extracting \"'hash/hash_func.c'\" \(4699 characters\)
  1247. sed "s/^X//" >'hash/hash_func.c' <<'END_OF_FILE'
  1248. X/*-
  1249. X * Copyright (c) 1990, 1993
  1250. X *    The Regents of the University of California.  All rights reserved.
  1251. X *
  1252. X * This code is derived from software contributed to Berkeley by
  1253. X * Margo Seltzer.
  1254. X *
  1255. X * Redistribution and use in source and binary forms, with or without
  1256. X * modification, are permitted provided that the following conditions
  1257. X * are met:
  1258. X * 1. Redistributions of source code must retain the above copyright
  1259. X *    notice, this list of conditions and the following disclaimer.
  1260. X * 2. Redistributions in binary form must reproduce the above copyright
  1261. X *    notice, this list of conditions and the following disclaimer in the
  1262. X *    documentation and/or other materials provided with the distribution.
  1263. X * 3. All advertising materials mentioning features or use of this software
  1264. X *    must display the following acknowledgement:
  1265. X *    This product includes software developed by the University of
  1266. X *    California, Berkeley and its contributors.
  1267. X * 4. Neither the name of the University nor the names of its contributors
  1268. X *    may be used to endorse or promote products derived from this software
  1269. X *    without specific prior written permission.
  1270. X *
  1271. X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  1272. X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  1273. X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  1274. X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  1275. X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  1276. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  1277. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  1278. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  1279. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  1280. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  1281. X * SUCH DAMAGE.
  1282. X */
  1283. X
  1284. X#if defined(LIBC_SCCS) && !defined(lint)
  1285. Xstatic char sccsid[] = "@(#)hash_func.c    8.1 (Berkeley) 6/4/93";
  1286. X#endif /* LIBC_SCCS and not lint */
  1287. X
  1288. X#include <sys/types.h>
  1289. X
  1290. X#include <db.h>
  1291. X#include "hash.h"
  1292. X#include "page.h"
  1293. X#include "extern.h"
  1294. X
  1295. Xstatic int hash1 __P((u_char *, int));
  1296. Xstatic int hash2 __P((u_char *, int));
  1297. Xstatic int hash3 __P((u_char *, int));
  1298. Xstatic int hash4 __P((u_char *, int));
  1299. X
  1300. X/* Global default hash function */
  1301. Xint (*__default_hash) __P((u_char *, int)) = hash4;
  1302. X
  1303. X/******************************* HASH FUNCTIONS **************************/
  1304. X/*
  1305. X * Assume that we've already split the bucket to which this key hashes,
  1306. X * calculate that bucket, and check that in fact we did already split it.
  1307. X *
  1308. X * This came from ejb's hsearch.
  1309. X */
  1310. X
  1311. X#define PRIME1        37
  1312. X#define PRIME2        1048583
  1313. X
  1314. Xstatic int
  1315. Xhash1(key, len)
  1316. X    register u_char *key;
  1317. X    register int len;
  1318. X{
  1319. X    register int h;
  1320. X
  1321. X    h = 0;
  1322. X    /* Convert string to integer */
  1323. X    while (len--)
  1324. X        h = h * PRIME1 ^ (*key++ - ' ');
  1325. X    h %= PRIME2;
  1326. X    return (h);
  1327. X}
  1328. X
  1329. X/*
  1330. X * Phong's linear congruential hash
  1331. X */
  1332. X#define dcharhash(h, c)    ((h) = 0x63c63cd9*(h) + 0x9c39c33d + (c))
  1333. X
  1334. Xstatic int
  1335. Xhash2(key, len)
  1336. X    register u_char *key;
  1337. X    int len;
  1338. X{
  1339. X    register u_char *e, c;
  1340. X    register int h;
  1341. X
  1342. X    e = key + len;
  1343. X    for (h = 0; key != e;) {
  1344. X        c = *key++;
  1345. X        if (!c && key > e)
  1346. X            break;
  1347. X        dcharhash(h, c);
  1348. X    }
  1349. X    return (h);
  1350. X}
  1351. X
  1352. X/*
  1353. X * This is INCREDIBLY ugly, but fast.  We break the string up into 8 byte
  1354. X * units.  On the first time through the loop we get the "leftover bytes"
  1355. X * (strlen % 8).  On every other iteration, we perform 8 HASHC's so we handle
  1356. X * all 8 bytes.  Essentially, this saves us 7 cmp & branch instructions.  If
  1357. X * this routine is heavily used enough, it's worth the ugly coding.
  1358. X *
  1359. X * OZ's original sdbm hash
  1360. X */
  1361. Xstatic int
  1362. Xhash3(key, len)
  1363. X    register u_char *key;
  1364. X    register int len;
  1365. X{
  1366. X    register int n, loop;
  1367. X
  1368. X#define HASHC   n = *key++ + 65599 * n
  1369. X
  1370. X    n = 0;
  1371. X    if (len > 0) {
  1372. X        loop = (len + 8 - 1) >> 3;
  1373. X
  1374. X        switch (len & (8 - 1)) {
  1375. X        case 0:
  1376. X            do {    /* All fall throughs */
  1377. X                HASHC;
  1378. X        case 7:
  1379. X                HASHC;
  1380. X        case 6:
  1381. X                HASHC;
  1382. X        case 5:
  1383. X                HASHC;
  1384. X        case 4:
  1385. X                HASHC;
  1386. X        case 3:
  1387. X                HASHC;
  1388. X        case 2:
  1389. X                HASHC;
  1390. X        case 1:
  1391. X                HASHC;
  1392. X            } while (--loop);
  1393. X        }
  1394. X
  1395. X    }
  1396. X    return (n);
  1397. X}
  1398. X
  1399. X/* Hash function from Chris Torek. */
  1400. Xstatic int
  1401. Xhash4(key, len)
  1402. X    register u_char *key;
  1403. X    register int len;
  1404. X{
  1405. X    register int h, loop;
  1406. X
  1407. X#define HASH4a   h = (h << 5) - h + *key++;
  1408. X#define HASH4b   h = (h << 5) + h + *key++;
  1409. X#define HASH4 HASH4b
  1410. X
  1411. X    h = 0;
  1412. X    if (len > 0) {
  1413. X        loop = (len + 8 - 1) >> 3;
  1414. X
  1415. X        switch (len & (8 - 1)) {
  1416. X        case 0:
  1417. X            do {    /* All fall throughs */
  1418. X                HASH4;
  1419. X        case 7:
  1420. X                HASH4;
  1421. X        case 6:
  1422. X                HASH4;
  1423. X        case 5:
  1424. X                HASH4;
  1425. X        case 4:
  1426. X                HASH4;
  1427. X        case 3:
  1428. X                HASH4;
  1429. X        case 2:
  1430. X                HASH4;
  1431. X        case 1:
  1432. X                HASH4;
  1433. X            } while (--loop);
  1434. X        }
  1435. X
  1436. X    }
  1437. X    return (h);
  1438. X}
  1439. END_OF_FILE
  1440. if test 4699 -ne `wc -c <'hash/hash_func.c'`; then
  1441.     echo shar: \"'hash/hash_func.c'\" unpacked with wrong size!
  1442. fi
  1443. # end of 'hash/hash_func.c'
  1444. fi
  1445. if test -f 'hash/ndbm.c' -a "${1}" != "-c" ; then 
  1446.   echo shar: Will not clobber existing file \"'hash/ndbm.c'\"
  1447. else
  1448. echo shar: Extracting \"'hash/ndbm.c'\" \(4334 characters\)
  1449. sed "s/^X//" >'hash/ndbm.c' <<'END_OF_FILE'
  1450. X/*-
  1451. X * Copyright (c) 1990, 1993
  1452. X *    The Regents of the University of California.  All rights reserved.
  1453. X *
  1454. X * This code is derived from software contributed to Berkeley by
  1455. X * Margo Seltzer.
  1456. X *
  1457. X * Redistribution and use in source and binary forms, with or without
  1458. X * modification, are permitted provided that the following conditions
  1459. X * are met:
  1460. X * 1. Redistributions of source code must retain the above copyright
  1461. X *    notice, this list of conditions and the following disclaimer.
  1462. X * 2. Redistributions in binary form must reproduce the above copyright
  1463. X *    notice, this list of conditions and the following disclaimer in the
  1464. X *    documentation and/or other materials provided with the distribution.
  1465. X * 3. All advertising materials mentioning features or use of this software
  1466. X *    must display the following acknowledgement:
  1467. X *    This product includes software developed by the University of
  1468. X *    California, Berkeley and its contributors.
  1469. X * 4. Neither the name of the University nor the names of its contributors
  1470. X *    may be used to endorse or promote products derived from this software
  1471. X *    without specific prior written permission.
  1472. X *
  1473. X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  1474. X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  1475. X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  1476. X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  1477. X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  1478. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  1479. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  1480. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  1481. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  1482. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  1483. X * SUCH DAMAGE.
  1484. X */
  1485. X
  1486. X#if defined(LIBC_SCCS) && !defined(lint)
  1487. Xstatic char sccsid[] = "@(#)ndbm.c    8.1 (Berkeley) 6/4/93";
  1488. X#endif /* LIBC_SCCS and not lint */
  1489. X
  1490. X/*
  1491. X * This package provides a dbm compatible interface to the new hashing
  1492. X * package described in db(3).
  1493. X */
  1494. X
  1495. X#include <sys/param.h>
  1496. X
  1497. X#include <ndbm.h>
  1498. X#include <stdio.h>
  1499. X#include <string.h>
  1500. X
  1501. X#include "hash.h"
  1502. X
  1503. X/*
  1504. X * Returns:
  1505. X *     *DBM on success
  1506. X *     NULL on failure
  1507. X */
  1508. Xextern DBM *
  1509. Xdbm_open(file, flags, mode)
  1510. X    const char *file;
  1511. X    int flags, mode;
  1512. X{
  1513. X    HASHINFO info;
  1514. X    char path[MAXPATHLEN];
  1515. X
  1516. X    info.bsize = 4096;
  1517. X    info.ffactor = 40;
  1518. X    info.nelem = 1;
  1519. X    info.cachesize = NULL;
  1520. X    info.hash = NULL;
  1521. X    info.lorder = 0;
  1522. X    (void)strcpy(path, file);
  1523. X    (void)strcat(path, DBM_SUFFIX);
  1524. X    return ((DBM *)__hash_open(path, flags, mode, &info));
  1525. X}
  1526. X
  1527. Xextern void
  1528. Xdbm_close(db)
  1529. X    DBM *db;
  1530. X{
  1531. X    (void)(db->close)(db);
  1532. X}
  1533. X
  1534. X/*
  1535. X * Returns:
  1536. X *    DATUM on success
  1537. X *    NULL on failure
  1538. X */
  1539. Xextern datum
  1540. Xdbm_fetch(db, key)
  1541. X    DBM *db;
  1542. X    datum key;
  1543. X{
  1544. X    datum retval;
  1545. X    int status;
  1546. X
  1547. X    status = (db->get)(db, (DBT *)&key, (DBT *)&retval, 0);
  1548. X    if (status) {
  1549. X        retval.dptr = NULL;
  1550. X        retval.dsize = 0;
  1551. X    }
  1552. X    return (retval);
  1553. X}
  1554. X
  1555. X/*
  1556. X * Returns:
  1557. X *    DATUM on success
  1558. X *    NULL on failure
  1559. X */
  1560. Xextern datum
  1561. Xdbm_firstkey(db)
  1562. X    DBM *db;
  1563. X{
  1564. X    int status;
  1565. X    datum retdata, retkey;
  1566. X
  1567. X    status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_FIRST);
  1568. X    if (status)
  1569. X        retkey.dptr = NULL;
  1570. X    return (retkey);
  1571. X}
  1572. X
  1573. X/*
  1574. X * Returns:
  1575. X *    DATUM on success
  1576. X *    NULL on failure
  1577. X */
  1578. Xextern datum
  1579. Xdbm_nextkey(db)
  1580. X    DBM *db;
  1581. X{
  1582. X    int status;
  1583. X    datum retdata, retkey;
  1584. X
  1585. X    status = (db->seq)(db, (DBT *)&retkey, (DBT *)&retdata, R_NEXT);
  1586. X    if (status)
  1587. X        retkey.dptr = NULL;
  1588. X    return (retkey);
  1589. X}
  1590. X/*
  1591. X * Returns:
  1592. X *     0 on success
  1593. X *    <0 failure
  1594. X */
  1595. Xextern int
  1596. Xdbm_delete(db, key)
  1597. X    DBM *db;
  1598. X    datum key;
  1599. X{
  1600. X    int status;
  1601. X
  1602. X    status = (db->del)(db, (DBT *)&key, 0);
  1603. X    if (status)
  1604. X        return (-1);
  1605. X    else
  1606. X        return (0);
  1607. X}
  1608. X
  1609. X/*
  1610. X * Returns:
  1611. X *     0 on success
  1612. X *    <0 failure
  1613. X *     1 if DBM_INSERT and entry exists
  1614. X */
  1615. Xextern int
  1616. Xdbm_store(db, key, content, flags)
  1617. X    DBM *db;
  1618. X    datum key, content;
  1619. X    int flags;
  1620. X{
  1621. X    return ((db->put)(db, (DBT *)&key, (DBT *)&content,
  1622. X        (flags == DBM_INSERT) ? R_NOOVERWRITE : 0));
  1623. X}
  1624. X
  1625. Xextern int
  1626. Xdbm_error(db)
  1627. X    DBM *db;
  1628. X{
  1629. X    HTAB *hp;
  1630. X
  1631. X    hp = (HTAB *)db->internal;
  1632. X    return (hp->errno);
  1633. X}
  1634. X
  1635. Xextern int
  1636. Xdbm_clearerr(db)
  1637. X    DBM *db;
  1638. X{
  1639. X    HTAB *hp;
  1640. X
  1641. X    hp = (HTAB *)db->internal;
  1642. X    hp->errno = 0;
  1643. X    return (0);
  1644. X}
  1645. X
  1646. Xextern int
  1647. Xdbm_dirfno(db)
  1648. X    DBM *db;
  1649. X{
  1650. X    return(((HTAB *)db->internal)->fp);
  1651. X}
  1652. END_OF_FILE
  1653. if test 4334 -ne `wc -c <'hash/ndbm.c'`; then
  1654.     echo shar: \"'hash/ndbm.c'\" unpacked with wrong size!
  1655. fi
  1656. # end of 'hash/ndbm.c'
  1657. fi
  1658. if test -f 'hash/page.h' -a "${1}" != "-c" ; then 
  1659.   echo shar: Will not clobber existing file \"'hash/page.h'\"
  1660. else
  1661. echo shar: Extracting \"'hash/page.h'\" \(3610 characters\)
  1662. sed "s/^X//" >'hash/page.h' <<'END_OF_FILE'
  1663. X/*-
  1664. X * Copyright (c) 1990, 1993
  1665. X *    The Regents of the University of California.  All rights reserved.
  1666. X *
  1667. X * This code is derived from software contributed to Berkeley by
  1668. X * Margo Seltzer.
  1669. X *
  1670. X * Redistribution and use in source and binary forms, with or without
  1671. X * modification, are permitted provided that the following conditions
  1672. X * are met:
  1673. X * 1. Redistributions of source code must retain the above copyright
  1674. X *    notice, this list of conditions and the following disclaimer.
  1675. X * 2. Redistributions in binary form must reproduce the above copyright
  1676. X *    notice, this list of conditions and the following disclaimer in the
  1677. X *    documentation and/or other materials provided with the distribution.
  1678. X * 3. All advertising materials mentioning features or use of this software
  1679. X *    must display the following acknowledgement:
  1680. X *    This product includes software developed by the University of
  1681. X *    California, Berkeley and its contributors.
  1682. X * 4. Neither the name of the University nor the names of its contributors
  1683. X *    may be used to endorse or promote products derived from this software
  1684. X *    without specific prior written permission.
  1685. X *
  1686. X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  1687. X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  1688. X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  1689. X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  1690. X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  1691. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  1692. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  1693. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  1694. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  1695. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  1696. X * SUCH DAMAGE.
  1697. X *
  1698. X *    @(#)page.h    8.1 (Berkeley) 6/6/93
  1699. X */
  1700. X
  1701. X/*
  1702. X * Definitions for hashing page file format.
  1703. X */
  1704. X
  1705. X/*
  1706. X * routines dealing with a data page
  1707. X *
  1708. X * page format:
  1709. X *    +------------------------------+
  1710. X * p    | n | keyoff | datoff | keyoff |
  1711. X *     +------------+--------+--------+
  1712. X *    | datoff | free  |  ptr  | --> |
  1713. X *    +--------+---------------------+
  1714. X *    |     F R E E A R E A       |
  1715. X *    +--------------+---------------+
  1716. X *    |  <---- - - - | data          |
  1717. X *    +--------+-----+----+----------+
  1718. X *    |  key   | data     | key      |
  1719. X *    +--------+----------+----------+
  1720. X *
  1721. X * Pointer to the free space is always:  p[p[0] + 2]
  1722. X * Amount of free space on the page is:  p[p[0] + 1]
  1723. X */
  1724. X
  1725. X/*
  1726. X * How many bytes required for this pair?
  1727. X *    2 shorts in the table at the top of the page + room for the
  1728. X *    key and room for the data
  1729. X *
  1730. X * We prohibit entering a pair on a page unless there is also room to append
  1731. X * an overflow page. The reason for this it that you can get in a situation
  1732. X * where a single key/data pair fits on a page, but you can't append an
  1733. X * overflow page and later you'd have to split the key/data and handle like
  1734. X * a big pair.
  1735. X * You might as well do this up front.
  1736. X */
  1737. X
  1738. X#define    PAIRSIZE(K,D)    (2*sizeof(u_short) + (K)->size + (D)->size)
  1739. X#define BIGOVERHEAD    (4*sizeof(u_short))
  1740. X#define KEYSIZE(K)    (4*sizeof(u_short) + (K)->size);
  1741. X#define OVFLSIZE    (2*sizeof(u_short))
  1742. X#define FREESPACE(P)    ((P)[(P)[0]+1])
  1743. X#define    OFFSET(P)    ((P)[(P)[0]+2])
  1744. X#define PAIRFITS(P,K,D) \
  1745. X    (((P)[2] >= REAL_KEY) && \
  1746. X        (PAIRSIZE((K),(D)) + OVFLSIZE) <= FREESPACE((P)))
  1747. X#define PAGE_META(N)    (((N)+3) * sizeof(u_short))
  1748. X
  1749. Xtypedef struct {
  1750. X    BUFHEAD *newp;
  1751. X    BUFHEAD *oldp;
  1752. X    BUFHEAD *nextp;
  1753. X    u_short next_addr;
  1754. X}       SPLIT_RETURN;
  1755. END_OF_FILE
  1756. if test 3610 -ne `wc -c <'hash/page.h'`; then
  1757.     echo shar: \"'hash/page.h'\" unpacked with wrong size!
  1758. fi
  1759. # end of 'hash/page.h'
  1760. fi
  1761. if test -f 'man/hash.3' -a "${1}" != "-c" ; then 
  1762.   echo shar: Will not clobber existing file \"'man/hash.3'\"
  1763. else
  1764. echo shar: Extracting \"'man/hash.3'\" \(5184 characters\)
  1765. sed "s/^X//" >'man/hash.3' <<'END_OF_FILE'
  1766. X.\" Copyright (c) 1990, 1993
  1767. X.\"    The Regents of the University of California.  All rights reserved.
  1768. X.\"
  1769. X.\" Redistribution and use in source and binary forms, with or without
  1770. X.\" modification, are permitted provided that the following conditions
  1771. X.\" are met:
  1772. X.\" 1. Redistributions of source code must retain the above copyright
  1773. X.\"    notice, this list of conditions and the following disclaimer.
  1774. X.\" 2. Redistributions in binary form must reproduce the above copyright
  1775. X.\"    notice, this list of conditions and the following disclaimer in the
  1776. X.\"    documentation and/or other materials provided with the distribution.
  1777. X.\" 3. All advertising materials mentioning features or use of this software
  1778. X.\"    must display the following acknowledgement:
  1779. X.\"    This product includes software developed by the University of
  1780. X.\"    California, Berkeley and its contributors.
  1781. X.\" 4. Neither the name of the University nor the names of its contributors
  1782. X.\"    may be used to endorse or promote products derived from this software
  1783. X.\"    without specific prior written permission.
  1784. X.\"
  1785. X.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  1786. X.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  1787. X.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  1788. X.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  1789. X.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  1790. X.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  1791. X.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  1792. X.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  1793. X.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  1794. X.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  1795. X.\" SUCH DAMAGE.
  1796. X.\"
  1797. X.\"    @(#)hash.3    8.1 (Berkeley) 6/6/93
  1798. X.\"
  1799. X.TH HASH 3 "June 6, 1993"
  1800. X.UC 7
  1801. X.SH NAME
  1802. Xhash \- hash database access method
  1803. X.SH SYNOPSIS
  1804. X.nf
  1805. X.ft B
  1806. X#include <sys/types.h>
  1807. X#include <db.h>
  1808. X.ft R
  1809. X.fi
  1810. X.SH DESCRIPTION
  1811. XThe routine
  1812. X.IR dbopen
  1813. Xis the library interface to database files.
  1814. XOne of the supported file formats is hash files.
  1815. XThe general description of the database access methods is in
  1816. X.IR dbopen (3),
  1817. Xthis manual page describes only the hash specific information.
  1818. X.PP
  1819. XThe hash data structure is an extensible, dynamic hashing scheme.
  1820. X.PP
  1821. XThe access method specific data structure provided to
  1822. X.I dbopen
  1823. Xis defined in the <db.h> include file as follows:
  1824. X.sp
  1825. Xtypedef struct {
  1826. X.RS
  1827. Xint bsize;
  1828. X.br
  1829. Xint cachesize;
  1830. X.br
  1831. Xint ffactor;
  1832. X.br
  1833. Xu_long (*hash)(const void *, size_t);
  1834. X.br
  1835. Xint lorder;
  1836. X.br
  1837. Xint nelem;
  1838. X.RE
  1839. X} HASHINFO;
  1840. X.PP
  1841. XThe elements of this structure are as follows:
  1842. X.TP
  1843. Xbsize
  1844. X.I Bsize
  1845. Xdefines the hash table bucket size, and is, by default, 256 bytes.
  1846. XIt may be preferable to increase the page size for disk-resident tables
  1847. Xand tables with large data items.
  1848. X.TP
  1849. Xcachesize
  1850. XA suggested maximum size, in bytes, of the memory cache.
  1851. XThis value is
  1852. X.B only
  1853. Xadvisory, and the access method will allocate more memory rather
  1854. Xthan fail.
  1855. X.TP
  1856. Xffactor
  1857. X.I Ffactor
  1858. Xindicates a desired density within the hash table.
  1859. XIt is an approximation of the number of keys allowed to accumulate in any
  1860. Xone bucket, determining when the hash table grows or shrinks.
  1861. XThe default value is 8.
  1862. X.TP
  1863. Xhash
  1864. X.I Hash
  1865. Xis a user defined hash function.
  1866. XSince no hash function performs equally well on all possible data, the
  1867. Xuser may find that the built-in hash function does poorly on a particular
  1868. Xdata set.
  1869. XUser specified hash functions must take two arguments (a pointer to a byte
  1870. Xstring and a length) and return an u_long to be used as the hash value.
  1871. X.TP
  1872. Xlorder
  1873. XThe byte order for integers in the stored database metadata.
  1874. XThe number should represent the order as an integer; for example, 
  1875. Xbig endian order would be the number 4,321.
  1876. XIf
  1877. X.I lorder
  1878. Xis 0 (no order is specified) the current host order is used.
  1879. XIf the  file already exists, the specified value is ignored and the
  1880. Xvalue specified when the tree was created is used.
  1881. X.TP
  1882. Xnelem
  1883. X.I Nelem
  1884. Xis an estimate of the final size of the hash table.
  1885. XIf not set or set too low, hash tables will expand gracefully as keys
  1886. Xare entered, although a slight performance degradation may be noticed.
  1887. XThe default value is 1.
  1888. X.PP
  1889. XIf the file already exists (and the O_TRUNC flag is not specified), the
  1890. Xvalues specified for the parameters bsize, ffactor, lorder and nelem are
  1891. Xignored and the values specified when the tree was created are used.
  1892. X.PP
  1893. XIf a hash function is specified,
  1894. X.I hash_open
  1895. Xwill attempt to determine if the hash function specified is the same as
  1896. Xthe one with which the database was created, and will fail if it is not.
  1897. X.PP
  1898. XBackward compatible interfaces to the routines described in
  1899. X.IR dbm (3),
  1900. Xand
  1901. X.IR ndbm (3)
  1902. Xare provided, however, these interfaces are not compatible with
  1903. Xprevious file formats.
  1904. X.SH "SEE ALSO"
  1905. X.IR btree (3),
  1906. X.IR dbopen (3),
  1907. X.IR mpool (3),
  1908. X.IR recno (3)
  1909. X.br
  1910. X.IR "Dynamic Hash Tables" ,
  1911. XPer-Ake Larson, Communications of the ACM, April 1988.
  1912. X.br
  1913. X.IR "A New Hash Package for UNIX" ,
  1914. XMargo Seltzer, USENIX Proceedings, Winter 1991.
  1915. X.SH BUGS
  1916. XOnly big and little endian byte order is supported.
  1917. END_OF_FILE
  1918. if test 5184 -ne `wc -c <'man/hash.3'`; then
  1919.     echo shar: \"'man/hash.3'\" unpacked with wrong size!
  1920. fi
  1921. # end of 'man/hash.3'
  1922. fi
  1923. if test -f 'recno/rec_close.c' -a "${1}" != "-c" ; then 
  1924.   echo shar: Will not clobber existing file \"'recno/rec_close.c'\"
  1925. else
  1926. echo shar: Extracting \"'recno/rec_close.c'\" \(4186 characters\)
  1927. sed "s/^X//" >'recno/rec_close.c' <<'END_OF_FILE'
  1928. X/*-
  1929. X * Copyright (c) 1990, 1993
  1930. X *    The Regents of the University of California.  All rights reserved.
  1931. X *
  1932. X * Redistribution and use in source and binary forms, with or without
  1933. X * modification, are permitted provided that the following conditions
  1934. X * are met:
  1935. X * 1. Redistributions of source code must retain the above copyright
  1936. X *    notice, this list of conditions and the following disclaimer.
  1937. X * 2. Redistributions in binary form must reproduce the above copyright
  1938. X *    notice, this list of conditions and the following disclaimer in the
  1939. X *    documentation and/or other materials provided with the distribution.
  1940. X * 3. All advertising materials mentioning features or use of this software
  1941. X *    must display the following acknowledgement:
  1942. X *    This product includes software developed by the University of
  1943. X *    California, Berkeley and its contributors.
  1944. X * 4. Neither the name of the University nor the names of its contributors
  1945. X *    may be used to endorse or promote products derived from this software
  1946. X *    without specific prior written permission.
  1947. X *
  1948. X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  1949. X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  1950. X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  1951. X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  1952. X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  1953. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  1954. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  1955. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  1956. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  1957. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  1958. X * SUCH DAMAGE.
  1959. X */
  1960. X
  1961. X#if defined(LIBC_SCCS) && !defined(lint)
  1962. Xstatic char sccsid[] = "@(#)rec_close.c    8.1 (Berkeley) 6/4/93";
  1963. X#endif /* LIBC_SCCS and not lint */
  1964. X
  1965. X#include <sys/types.h>
  1966. X#include <sys/uio.h>
  1967. X#include <sys/mman.h>
  1968. X
  1969. X#include <errno.h>
  1970. X#include <limits.h>
  1971. X#include <stdio.h>
  1972. X#include <unistd.h>
  1973. X
  1974. X#include <db.h>
  1975. X#include "recno.h"
  1976. X
  1977. X/*
  1978. X * __REC_CLOSE -- Close a recno tree.
  1979. X *
  1980. X * Parameters:
  1981. X *    dbp:    pointer to access method
  1982. X *
  1983. X * Returns:
  1984. X *    RET_ERROR, RET_SUCCESS
  1985. X */
  1986. Xint
  1987. X__rec_close(dbp)
  1988. X    DB *dbp;
  1989. X{
  1990. X    BTREE *t;
  1991. X    int rval;
  1992. X
  1993. X    if (__rec_sync(dbp, 0) == RET_ERROR)
  1994. X        return (RET_ERROR);
  1995. X
  1996. X    /* Committed to closing. */
  1997. X    t = dbp->internal;
  1998. X
  1999. X    rval = RET_SUCCESS;
  2000. X    if (ISSET(t, R_MEMMAPPED) && munmap(t->bt_smap, t->bt_msize))
  2001. X        rval = RET_ERROR;
  2002. X
  2003. X    if (!ISSET(t, R_INMEM))
  2004. X        if (ISSET(t, R_CLOSEFP)) {
  2005. X            if (fclose(t->bt_rfp))
  2006. X                rval = RET_ERROR;
  2007. X        } else
  2008. X            if (close(t->bt_rfd))
  2009. X                rval = RET_ERROR;
  2010. X
  2011. X    if (__bt_close(dbp) == RET_ERROR)
  2012. X        rval = RET_ERROR;
  2013. X
  2014. X    return (rval);
  2015. X}
  2016. X
  2017. X/*
  2018. X * __REC_SYNC -- sync the recno tree to disk.
  2019. X *
  2020. X * Parameters:
  2021. X *    dbp:    pointer to access method
  2022. X *
  2023. X * Returns:
  2024. X *    RET_SUCCESS, RET_ERROR.
  2025. X */
  2026. Xint
  2027. X__rec_sync(dbp, flags)
  2028. X    const DB *dbp;
  2029. X    u_int flags;
  2030. X{
  2031. X    struct iovec iov[2];
  2032. X    BTREE *t;
  2033. X    DBT data, key;
  2034. X    off_t off;
  2035. X    recno_t scursor, trec;
  2036. X    int status;
  2037. X
  2038. X    t = dbp->internal;
  2039. X
  2040. X    if (flags == R_RECNOSYNC)
  2041. X        return (__bt_sync(dbp, 0));
  2042. X
  2043. X    if (ISSET(t, R_RDONLY | R_INMEM) || !ISSET(t, R_MODIFIED))
  2044. X        return (RET_SUCCESS);
  2045. X
  2046. X    /* Read any remaining records into the tree. */
  2047. X    if (!ISSET(t, R_EOF) && t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
  2048. X        return (RET_ERROR);
  2049. X
  2050. X    /* Rewind the file descriptor. */
  2051. X    if (lseek(t->bt_rfd, (off_t)0, SEEK_SET) != 0)
  2052. X        return (RET_ERROR);
  2053. X
  2054. X    iov[1].iov_base = "\n";
  2055. X    iov[1].iov_len = 1;
  2056. X    scursor = t->bt_rcursor;
  2057. X
  2058. X    key.size = sizeof(recno_t);
  2059. X    key.data = &trec;
  2060. X
  2061. X    status = (dbp->seq)(dbp, &key, &data, R_FIRST);
  2062. X        while (status == RET_SUCCESS) {
  2063. X        iov[0].iov_base = data.data;
  2064. X        iov[0].iov_len = data.size;
  2065. X        if (writev(t->bt_rfd, iov, 2) != data.size + 1)
  2066. X            return (RET_ERROR);
  2067. X                status = (dbp->seq)(dbp, &key, &data, R_NEXT);
  2068. X        }
  2069. X    t->bt_rcursor = scursor;
  2070. X    if (status == RET_ERROR)
  2071. X        return (RET_ERROR);
  2072. X    if ((off = lseek(t->bt_rfd, (off_t)0, SEEK_CUR)) == -1)
  2073. X        return (RET_ERROR);
  2074. X    if (ftruncate(t->bt_rfd, off))
  2075. X        return (RET_ERROR);
  2076. X    CLR(t, R_MODIFIED);
  2077. X    return (RET_SUCCESS);
  2078. X}
  2079. END_OF_FILE
  2080. if test 4186 -ne `wc -c <'recno/rec_close.c'`; then
  2081.     echo shar: \"'recno/rec_close.c'\" unpacked with wrong size!
  2082. fi
  2083. # end of 'recno/rec_close.c'
  2084. fi
  2085. if test -f 'recno/rec_delete.c' -a "${1}" != "-c" ; then 
  2086.   echo shar: Will not clobber existing file \"'recno/rec_delete.c'\"
  2087. else
  2088. echo shar: Extracting \"'recno/rec_delete.c'\" \(5406 characters\)
  2089. sed "s/^X//" >'recno/rec_delete.c' <<'END_OF_FILE'
  2090. X/*-
  2091. X * Copyright (c) 1990, 1993
  2092. X *    The Regents of the University of California.  All rights reserved.
  2093. X *
  2094. X * This code is derived from software contributed to Berkeley by
  2095. X * Mike Olson.
  2096. X *
  2097. X * Redistribution and use in source and binary forms, with or without
  2098. X * modification, are permitted provided that the following conditions
  2099. X * are met:
  2100. X * 1. Redistributions of source code must retain the above copyright
  2101. X *    notice, this list of conditions and the following disclaimer.
  2102. X * 2. Redistributions in binary form must reproduce the above copyright
  2103. X *    notice, this list of conditions and the following disclaimer in the
  2104. X *    documentation and/or other materials provided with the distribution.
  2105. X * 3. All advertising materials mentioning features or use of this software
  2106. X *    must display the following acknowledgement:
  2107. X *    This product includes software developed by the University of
  2108. X *    California, Berkeley and its contributors.
  2109. X * 4. Neither the name of the University nor the names of its contributors
  2110. X *    may be used to endorse or promote products derived from this software
  2111. X *    without specific prior written permission.
  2112. X *
  2113. X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  2114. X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  2115. X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  2116. X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  2117. X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  2118. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  2119. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  2120. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  2121. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  2122. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  2123. X * SUCH DAMAGE.
  2124. X */
  2125. X
  2126. X#if defined(LIBC_SCCS) && !defined(lint)
  2127. Xstatic char sccsid[] = "@(#)rec_delete.c    8.1 (Berkeley) 6/4/93";
  2128. X#endif /* LIBC_SCCS and not lint */
  2129. X
  2130. X#include <sys/types.h>
  2131. X
  2132. X#include <errno.h>
  2133. X#include <stdio.h>
  2134. X#include <string.h>
  2135. X
  2136. X#include <db.h>
  2137. X#include "recno.h"
  2138. X
  2139. Xstatic int rec_rdelete __P((BTREE *, recno_t));
  2140. X
  2141. X/*
  2142. X * __REC_DELETE -- Delete the item(s) referenced by a key.
  2143. X *
  2144. X * Parameters:
  2145. X *    dbp:    pointer to access method
  2146. X *    key:    key to delete
  2147. X *    flags:    R_CURSOR if deleting what the cursor references
  2148. X *
  2149. X * Returns:
  2150. X *    RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
  2151. X */
  2152. Xint
  2153. X__rec_delete(dbp, key, flags)
  2154. X    const DB *dbp;
  2155. X    const DBT *key;
  2156. X    u_int flags;
  2157. X{
  2158. X    BTREE *t;
  2159. X    recno_t nrec;
  2160. X    int status;
  2161. X
  2162. X    t = dbp->internal;
  2163. X    switch(flags) {
  2164. X    case 0:
  2165. X        if ((nrec = *(recno_t *)key->data) == 0)
  2166. X            goto einval;
  2167. X        if (nrec > t->bt_nrecs)
  2168. X            return (RET_SPECIAL);
  2169. X        --nrec;
  2170. X        status = rec_rdelete(t, nrec);
  2171. X        break;
  2172. X    case R_CURSOR:
  2173. X        if (!ISSET(t, B_SEQINIT))
  2174. X            goto einval;
  2175. X        if (t->bt_nrecs == 0)
  2176. X            return (RET_SPECIAL);
  2177. X        status = rec_rdelete(t, t->bt_rcursor - 1);
  2178. X        if (status == RET_SUCCESS)
  2179. X            --t->bt_rcursor;
  2180. X        break;
  2181. X    default:
  2182. Xeinval:        errno = EINVAL;
  2183. X        return (RET_ERROR);
  2184. X    }
  2185. X
  2186. X    if (status == RET_SUCCESS)
  2187. X        SET(t, B_MODIFIED | R_MODIFIED);
  2188. X    return (status);
  2189. X}
  2190. X
  2191. X/*
  2192. X * REC_RDELETE -- Delete the data matching the specified key.
  2193. X *
  2194. X * Parameters:
  2195. X *    tree:    tree
  2196. X *    nrec:    record to delete
  2197. X *
  2198. X * Returns:
  2199. X *    RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
  2200. X */
  2201. Xstatic int
  2202. Xrec_rdelete(t, nrec)
  2203. X    BTREE *t;
  2204. X    recno_t nrec;
  2205. X{
  2206. X    EPG *e;
  2207. X    PAGE *h;
  2208. X    int status;
  2209. X
  2210. X    /* Find the record; __rec_search pins the page. */
  2211. X    if ((e = __rec_search(t, nrec, SDELETE)) == NULL) {
  2212. X        mpool_put(t->bt_mp, e->page, 0);
  2213. X        return (RET_ERROR);
  2214. X    }
  2215. X
  2216. X    /* Delete the record. */
  2217. X    h = e->page;
  2218. X    status = __rec_dleaf(t, h, e->index);
  2219. X    if (status != RET_SUCCESS) {
  2220. X        mpool_put(t->bt_mp, h, 0);
  2221. X        return (status);
  2222. X    }
  2223. X    mpool_put(t->bt_mp, h, MPOOL_DIRTY);
  2224. X    return (RET_SUCCESS);
  2225. X}
  2226. X
  2227. X/*
  2228. X * __REC_DLEAF -- Delete a single record from a recno leaf page.
  2229. X *
  2230. X * Parameters:
  2231. X *    t:    tree
  2232. X *    index:    index on current page to delete
  2233. X *
  2234. X * Returns:
  2235. X *    RET_SUCCESS, RET_ERROR.
  2236. X */
  2237. Xint
  2238. X__rec_dleaf(t, h, index)
  2239. X    BTREE *t;
  2240. X    PAGE *h;
  2241. X    int index;
  2242. X{
  2243. X    register RLEAF *rl;
  2244. X    register indx_t *ip, offset;
  2245. X    register size_t nbytes;
  2246. X    register int cnt;
  2247. X    char *from;
  2248. X    void *to;
  2249. X
  2250. X    /*
  2251. X     * Delete a record from a recno leaf page.  Internal records are never
  2252. X     * deleted from internal pages, regardless of the records that caused
  2253. X     * them to be added being deleted.  Pages made empty by deletion are
  2254. X     * not reclaimed.  They are, however, made available for reuse.
  2255. X     *
  2256. X     * Pack the remaining entries at the end of the page, shift the indices
  2257. X     * down, overwriting the deleted record and its index.  If the record
  2258. X     * uses overflow pages, make them available for reuse.
  2259. X     */
  2260. X    to = rl = GETRLEAF(h, index);
  2261. X    if (rl->flags & P_BIGDATA && __ovfl_delete(t, rl->bytes) == RET_ERROR)
  2262. X        return (RET_ERROR);
  2263. X    nbytes = NRLEAF(rl);
  2264. X
  2265. X    /*
  2266. X     * Compress the key/data pairs.  Compress and adjust the [BR]LEAF
  2267. X     * offsets.  Reset the headers.
  2268. X     */
  2269. X    from = (char *)h + h->upper;
  2270. X    memmove(from + nbytes, from, (char *)to - from);
  2271. X    h->upper += nbytes;
  2272. X
  2273. X    offset = h->linp[index];
  2274. X    for (cnt = &h->linp[index] - (ip = &h->linp[0]); cnt--; ++ip)
  2275. X        if (ip[0] < offset)
  2276. X            ip[0] += nbytes;
  2277. X    for (cnt = &h->linp[NEXTINDEX(h)] - ip; --cnt; ++ip)
  2278. X        ip[0] = ip[1] < offset ? ip[1] + nbytes : ip[1];
  2279. X    h->lower -= sizeof(indx_t);
  2280. X    --t->bt_nrecs;
  2281. X    return (RET_SUCCESS);
  2282. X}
  2283. END_OF_FILE
  2284. if test 5406 -ne `wc -c <'recno/rec_delete.c'`; then
  2285.     echo shar: \"'recno/rec_delete.c'\" unpacked with wrong size!
  2286. fi
  2287. # end of 'recno/rec_delete.c'
  2288. fi
  2289. if test -f 'recno/rec_search.c' -a "${1}" != "-c" ; then 
  2290.   echo shar: Will not clobber existing file \"'recno/rec_search.c'\"
  2291. else
  2292. echo shar: Extracting \"'recno/rec_search.c'\" \(3852 characters\)
  2293. sed "s/^X//" >'recno/rec_search.c' <<'END_OF_FILE'
  2294. X/*-
  2295. X * Copyright (c) 1990, 1993
  2296. X *    The Regents of the University of California.  All rights reserved.
  2297. X *
  2298. X * Redistribution and use in source and binary forms, with or without
  2299. X * modification, are permitted provided that the following conditions
  2300. X * are met:
  2301. X * 1. Redistributions of source code must retain the above copyright
  2302. X *    notice, this list of conditions and the following disclaimer.
  2303. X * 2. Redistributions in binary form must reproduce the above copyright
  2304. X *    notice, this list of conditions and the following disclaimer in the
  2305. X *    documentation and/or other materials provided with the distribution.
  2306. X * 3. All advertising materials mentioning features or use of this software
  2307. X *    must display the following acknowledgement:
  2308. X *    This product includes software developed by the University of
  2309. X *    California, Berkeley and its contributors.
  2310. X * 4. Neither the name of the University nor the names of its contributors
  2311. X *    may be used to endorse or promote products derived from this software
  2312. X *    without specific prior written permission.
  2313. X *
  2314. X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  2315. X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  2316. X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  2317. X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  2318. X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  2319. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  2320. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  2321. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  2322. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  2323. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  2324. X * SUCH DAMAGE.
  2325. X */
  2326. X
  2327. X#if defined(LIBC_SCCS) && !defined(lint)
  2328. Xstatic char sccsid[] = "@(#)rec_search.c    8.1 (Berkeley) 6/4/93";
  2329. X#endif /* LIBC_SCCS and not lint */
  2330. X
  2331. X#include <sys/types.h>
  2332. X
  2333. X#include <errno.h>
  2334. X#include <stdio.h>
  2335. X
  2336. X#include <db.h>
  2337. X#include "recno.h"
  2338. X
  2339. X/*
  2340. X * __REC_SEARCH -- Search a btree for a key.
  2341. X *
  2342. X * Parameters:
  2343. X *    t:    tree to search
  2344. X *    recno:    key to find
  2345. X *    op:     search operation
  2346. X *
  2347. X * Returns:
  2348. X *    EPG for matching record, if any, or the EPG for the location of the
  2349. X *    key, if it were inserted into the tree.
  2350. X *
  2351. X * Warnings:
  2352. X *    The EPG returned is in static memory, and will be overwritten by the
  2353. X *    next search of any kind in any tree.
  2354. X */
  2355. XEPG *
  2356. X__rec_search(t, recno, op)
  2357. X    BTREE *t;
  2358. X    recno_t recno;
  2359. X    enum SRCHOP op;
  2360. X{
  2361. X    static EPG e;
  2362. X    register indx_t index;
  2363. X    register PAGE *h;
  2364. X    EPGNO *parent;
  2365. X    RINTERNAL *r;
  2366. X    pgno_t pg;
  2367. X    indx_t top;
  2368. X    recno_t total;
  2369. X    int serrno;
  2370. X
  2371. X    BT_CLR(t);
  2372. X    for (pg = P_ROOT, total = 0;;) {
  2373. X        if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
  2374. X            goto err;
  2375. X        if (h->flags & P_RLEAF) {
  2376. X            e.page = h;
  2377. X            e.index = recno - total;
  2378. X            return (&e);
  2379. X        }
  2380. X        for (index = 0, top = NEXTINDEX(h);;) {
  2381. X            r = GETRINTERNAL(h, index);
  2382. X            if (++index == top || total + r->nrecs > recno)
  2383. X                break;
  2384. X            total += r->nrecs;
  2385. X        }
  2386. X
  2387. X        if (__bt_push(t, pg, index - 1) == RET_ERROR)
  2388. X            return (NULL);
  2389. X        
  2390. X        pg = r->pgno;
  2391. X        switch (op) {
  2392. X        case SDELETE:
  2393. X            --GETRINTERNAL(h, (index - 1))->nrecs;
  2394. X            mpool_put(t->bt_mp, h, MPOOL_DIRTY);
  2395. X            break;
  2396. X        case SINSERT:
  2397. X            ++GETRINTERNAL(h, (index - 1))->nrecs;
  2398. X            mpool_put(t->bt_mp, h, MPOOL_DIRTY);
  2399. X            break;
  2400. X        case SEARCH:
  2401. X            mpool_put(t->bt_mp, h, 0);
  2402. X            break;
  2403. X        }
  2404. X
  2405. X    }
  2406. X    /* Try and recover the tree. */
  2407. Xerr:    serrno = errno;
  2408. X    if (op != SEARCH)
  2409. X        while  ((parent = BT_POP(t)) != NULL) {
  2410. X            if ((h = mpool_get(t->bt_mp, parent->pgno, 0)) == NULL)
  2411. X                break;
  2412. X            if (op == SINSERT)
  2413. X                --GETRINTERNAL(h, parent->index)->nrecs;
  2414. X            else
  2415. X                ++GETRINTERNAL(h, parent->index)->nrecs;
  2416. X                        mpool_put(t->bt_mp, h, MPOOL_DIRTY);
  2417. X                }
  2418. X    errno = serrno;
  2419. X    return (NULL);
  2420. X}
  2421. END_OF_FILE
  2422. if test 3852 -ne `wc -c <'recno/rec_search.c'`; then
  2423.     echo shar: \"'recno/rec_search.c'\" unpacked with wrong size!
  2424. fi
  2425. # end of 'recno/rec_search.c'
  2426. fi
  2427. if test -f 'recno/rec_seq.c' -a "${1}" != "-c" ; then 
  2428.   echo shar: Will not clobber existing file \"'recno/rec_seq.c'\"
  2429. else
  2430. echo shar: Extracting \"'recno/rec_seq.c'\" \(3591 characters\)
  2431. sed "s/^X//" >'recno/rec_seq.c' <<'END_OF_FILE'
  2432. X/*-
  2433. X * Copyright (c) 1991, 1993
  2434. X *    The Regents of the University of California.  All rights reserved.
  2435. X *
  2436. X * Redistribution and use in source and binary forms, with or without
  2437. X * modification, are permitted provided that the following conditions
  2438. X * are met:
  2439. X * 1. Redistributions of source code must retain the above copyright
  2440. X *    notice, this list of conditions and the following disclaimer.
  2441. X * 2. Redistributions in binary form must reproduce the above copyright
  2442. X *    notice, this list of conditions and the following disclaimer in the
  2443. X *    documentation and/or other materials provided with the distribution.
  2444. X * 3. All advertising materials mentioning features or use of this software
  2445. X *    must display the following acknowledgement:
  2446. X *    This product includes software developed by the University of
  2447. X *    California, Berkeley and its contributors.
  2448. X * 4. Neither the name of the University nor the names of its contributors
  2449. X *    may be used to endorse or promote products derived from this software
  2450. X *    without specific prior written permission.
  2451. X *
  2452. X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  2453. X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  2454. X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  2455. X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  2456. X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  2457. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  2458. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  2459. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  2460. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  2461. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  2462. X * SUCH DAMAGE.
  2463. X */
  2464. X
  2465. X#ifndef lint
  2466. Xstatic char sccsid[] = "@(#)rec_seq.c    8.1 (Berkeley) 6/4/93";
  2467. X#endif /* not lint */
  2468. X
  2469. X#include <sys/types.h>
  2470. X
  2471. X#include <errno.h>
  2472. X#include <limits.h>
  2473. X#include <stdio.h>
  2474. X#include <string.h>
  2475. X
  2476. X#include <db.h>
  2477. X#include "recno.h"
  2478. X
  2479. X/*
  2480. X * __REC_SEQ -- Recno sequential scan interface.
  2481. X *
  2482. X * Parameters:
  2483. X *    dbp:    pointer to access method
  2484. X *    key:    key for positioning and return value
  2485. X *    data:    data return value
  2486. X *    flags:    R_CURSOR, R_FIRST, R_LAST, R_NEXT, R_PREV.
  2487. X *
  2488. X * Returns:
  2489. X *    RET_ERROR, RET_SUCCESS or RET_SPECIAL if there's no next key.
  2490. X */
  2491. Xint
  2492. X__rec_seq(dbp, key, data, flags)
  2493. X    const DB *dbp;
  2494. X    DBT *key, *data;
  2495. X    u_int flags;
  2496. X{
  2497. X    BTREE *t;
  2498. X    EPG *e;
  2499. X    recno_t nrec;
  2500. X    int status;
  2501. X
  2502. X    t = dbp->internal;
  2503. X    switch(flags) {
  2504. X    case R_CURSOR:
  2505. X        if ((nrec = *(recno_t *)key->data) == 0)
  2506. X            goto einval;
  2507. X        break;
  2508. X    case R_NEXT:
  2509. X        if (ISSET(t, B_SEQINIT)) {
  2510. X            nrec = t->bt_rcursor + 1;
  2511. X            break;
  2512. X        }
  2513. X        /* FALLTHROUGH */
  2514. X    case R_FIRST:
  2515. X        nrec = 1;
  2516. X        break;
  2517. X    case R_PREV:
  2518. X        if (ISSET(t, B_SEQINIT)) {
  2519. X            if ((nrec = t->bt_rcursor - 1) == 0)
  2520. X                return (RET_SPECIAL);
  2521. X            break;
  2522. X        }
  2523. X        /* FALLTHROUGH */
  2524. X    case R_LAST:
  2525. X        if (!ISSET(t, R_EOF | R_INMEM) &&
  2526. X            t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
  2527. X            return (RET_ERROR);
  2528. X        nrec = t->bt_nrecs;
  2529. X        break;
  2530. X    default:
  2531. Xeinval:        errno = EINVAL;
  2532. X        return (RET_ERROR);
  2533. X    }
  2534. X    
  2535. X    if (t->bt_nrecs == 0 || nrec > t->bt_nrecs) {
  2536. X        if (!ISSET(t, R_EOF | R_INMEM) &&
  2537. X            (status = t->bt_irec(t, nrec)) != RET_SUCCESS)
  2538. X            return (status);
  2539. X        if (t->bt_nrecs == 0 || nrec > t->bt_nrecs)
  2540. X            return (RET_SPECIAL);
  2541. X    }
  2542. X
  2543. X    if ((e = __rec_search(t, nrec - 1, SEARCH)) == NULL)
  2544. X        return (RET_ERROR);
  2545. X
  2546. X    SET(t, B_SEQINIT);
  2547. X    t->bt_rcursor = nrec;
  2548. X
  2549. X    status = __rec_ret(t, e, nrec, key, data);
  2550. X
  2551. X    mpool_put(t->bt_mp, e->page, 0);
  2552. X    return (status);
  2553. X}
  2554. END_OF_FILE
  2555. if test 3591 -ne `wc -c <'recno/rec_seq.c'`; then
  2556.     echo shar: \"'recno/rec_seq.c'\" unpacked with wrong size!
  2557. fi
  2558. # end of 'recno/rec_seq.c'
  2559. fi
  2560. if test -f 'test/hash.tests/driver2.c' -a "${1}" != "-c" ; then 
  2561.   echo shar: Will not clobber existing file \"'test/hash.tests/driver2.c'\"
  2562. else
  2563. echo shar: Extracting \"'test/hash.tests/driver2.c'\" \(3531 characters\)
  2564. sed "s/^X//" >'test/hash.tests/driver2.c' <<'END_OF_FILE'
  2565. X/*-
  2566. X * Copyright (c) 1991, 1993
  2567. X *    The Regents of the University of California.  All rights reserved.
  2568. X *
  2569. X * This code is derived from software contributed to Berkeley by
  2570. X * Margo Seltzer.
  2571. X *
  2572. X * Redistribution and use in source and binary forms, with or without
  2573. X * modification, are permitted provided that the following conditions
  2574. X * are met:
  2575. X * 1. Redistributions of source code must retain the above copyright
  2576. X *    notice, this list of conditions and the following disclaimer.
  2577. X * 2. Redistributions in binary form must reproduce the above copyright
  2578. X *    notice, this list of conditions and the following disclaimer in the
  2579. X *    documentation and/or other materials provided with the distribution.
  2580. X * 3. All advertising materials mentioning features or use of this software
  2581. X *    must display the following acknowledgement:
  2582. X *    This product includes software developed by the University of
  2583. X *    California, Berkeley and its contributors.
  2584. X * 4. Neither the name of the University nor the names of its contributors
  2585. X *    may be used to endorse or promote products derived from this software
  2586. X *    without specific prior written permission.
  2587. X *
  2588. X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  2589. X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  2590. X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  2591. X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  2592. X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  2593. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  2594. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  2595. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  2596. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  2597. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  2598. X * SUCH DAMAGE.
  2599. X */
  2600. X
  2601. X#ifndef lint
  2602. Xstatic char copyright[] =
  2603. X"@(#) Copyright (c) 1991, 1993\n\
  2604. X    The Regents of the University of California.  All rights reserved.\n";
  2605. X#endif /* not lint */
  2606. X
  2607. X#ifndef lint
  2608. Xstatic char sccsid[] = "@(#)driver2.c    8.1 (Berkeley) 6/4/93";
  2609. X#endif /* not lint */
  2610. X
  2611. X/*
  2612. X * Test driver, to try to tackle the large ugly-split problem.
  2613. X */
  2614. X
  2615. X#include <sys/file.h>
  2616. X#include <stdio.h>
  2617. X#include "ndbm.h"
  2618. X
  2619. Xint my_hash(key, len)
  2620. X    char    *key;
  2621. X    int    len;
  2622. X{
  2623. X    return(17);        /* So I'm cruel... */
  2624. X}
  2625. X
  2626. Xmain(argc, argv)
  2627. X    int    argc;
  2628. X{
  2629. X    DB    *db;
  2630. X    DBT    key, content;
  2631. X    char    keybuf[2049];
  2632. X    char    contentbuf[2049];
  2633. X    char    buf[256];
  2634. X    int    i;
  2635. X    HASHINFO    info;
  2636. X
  2637. X    info.bsize = 1024;
  2638. X    info.ffactor = 5;
  2639. X    info.nelem = 1;
  2640. X    info.cachesize = NULL;
  2641. X#ifdef HASH_ID_PROGRAM_SPECIFIED
  2642. X    info.hash_id = HASH_ID_PROGRAM_SPECIFIED;
  2643. X    info.hash_func = my_hash;
  2644. X#else
  2645. X    info.hash = my_hash;
  2646. X#endif
  2647. X    info.lorder = 0;
  2648. X    if (!(db = dbopen("bigtest", O_RDWR | O_CREAT, 0644, DB_HASH, &info))) {
  2649. X        sprintf(buf, "dbopen: failed on file bigtest");
  2650. X        perror(buf);
  2651. X        exit(1);
  2652. X    }
  2653. X    srandom(17);
  2654. X    key.data = keybuf;
  2655. X    content.data = contentbuf;
  2656. X    bzero(keybuf, sizeof(keybuf));
  2657. X    bzero(contentbuf, sizeof(contentbuf));
  2658. X    for (i=1; i <= 500; i++) {
  2659. X        key.size = 128 + (random()&1023);
  2660. X        content.size = 128 + (random()&1023);
  2661. X/*        printf("%d: Key size %d, data size %d\n", i, key.size,
  2662. X               content.size); */
  2663. X        sprintf(keybuf, "Key #%d", i);
  2664. X        sprintf(contentbuf, "Contents #%d", i);
  2665. X        if ((db->put)(db, &key, &content, R_NOOVERWRITE)) {
  2666. X            sprintf(buf, "dbm_store #%d", i);
  2667. X            perror(buf);
  2668. X        }
  2669. X    }
  2670. X    if ((db->close)(db)) {
  2671. X        perror("closing hash file");
  2672. X        exit(1);
  2673. X    }
  2674. X    exit(0);
  2675. X}
  2676. X
  2677. X    
  2678. X
  2679. END_OF_FILE
  2680. if test 3531 -ne `wc -c <'test/hash.tests/driver2.c'`; then
  2681.     echo shar: \"'test/hash.tests/driver2.c'\" unpacked with wrong size!
  2682. fi
  2683. # end of 'test/hash.tests/driver2.c'
  2684. fi
  2685. if test -f 'test/hash.tests/tdel.c' -a "${1}" != "-c" ; then 
  2686.   echo shar: Will not clobber existing file \"'test/hash.tests/tdel.c'\"
  2687. else
  2688. echo shar: Extracting \"'test/hash.tests/tdel.c'\" \(3671 characters\)
  2689. sed "s/^X//" >'test/hash.tests/tdel.c' <<'END_OF_FILE'
  2690. X/*-
  2691. X * Copyright (c) 1991, 1993
  2692. X *    The Regents of the University of California.  All rights reserved.
  2693. X *
  2694. X * This code is derived from software contributed to Berkeley by
  2695. X * Margo Seltzer.
  2696. X *
  2697. X * Redistribution and use in source and binary forms, with or without
  2698. X * modification, are permitted provided that the following conditions
  2699. X * are met:
  2700. X * 1. Redistributions of source code must retain the above copyright
  2701. X *    notice, this list of conditions and the following disclaimer.
  2702. X * 2. Redistributions in binary form must reproduce the above copyright
  2703. X *    notice, this list of conditions and the following disclaimer in the
  2704. X *    documentation and/or other materials provided with the distribution.
  2705. X * 3. All advertising materials mentioning features or use of this software
  2706. X *    must display the following acknowledgement:
  2707. X *    This product includes software developed by the University of
  2708. X *    California, Berkeley and its contributors.
  2709. X * 4. Neither the name of the University nor the names of its contributors
  2710. X *    may be used to endorse or promote products derived from this software
  2711. X *    without specific prior written permission.
  2712. X *
  2713. X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  2714. X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  2715. X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  2716. X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  2717. X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  2718. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  2719. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  2720. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  2721. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  2722. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  2723. X * SUCH DAMAGE.
  2724. X */
  2725. X
  2726. X#ifndef lint
  2727. Xstatic char copyright[] =
  2728. X"@(#) Copyright (c) 1991, 1993\n\
  2729. X    The Regents of the University of California.  All rights reserved.\n";
  2730. X#endif /* not lint */
  2731. X
  2732. X#ifndef lint
  2733. Xstatic char sccsid[] = "@(#)tdel.c    8.1 (Berkeley) 6/4/93";
  2734. X#endif /* not lint */
  2735. X
  2736. X#include <sys/types.h>
  2737. X#include <sys/file.h>
  2738. X#include <db.h>
  2739. X#include <stdio.h>
  2740. X
  2741. X#define INITIAL    25000
  2742. X#define MAXWORDS    25000           /* # of elements in search table */
  2743. X
  2744. X/* Usage: thash pagesize fillfactor file */
  2745. Xchar    wp1[8192];
  2746. Xchar    wp2[8192];
  2747. Xmain(argc, argv)
  2748. Xchar **argv;
  2749. X{
  2750. X    DBT item, key;
  2751. X    DB    *dbp;
  2752. X    HASHINFO ctl;
  2753. X    FILE *fp;
  2754. X    int    stat;
  2755. X
  2756. X    int i = 0;
  2757. X
  2758. X    argv++;
  2759. X    ctl.nelem = INITIAL;
  2760. X    ctl.hash = NULL;
  2761. X    ctl.bsize = atoi(*argv++);
  2762. X    ctl.ffactor = atoi(*argv++);
  2763. X    ctl.cachesize = 1024 * 1024;    /* 1 MEG */
  2764. X    ctl.lorder = 0;
  2765. X    argc -= 2;
  2766. X    if (!(dbp = dbopen( NULL, O_CREAT|O_RDWR, 0400, DB_HASH, &ctl))) {
  2767. X        /* create table */
  2768. X        fprintf(stderr, "cannot create: hash table size %d)\n",
  2769. X            INITIAL);
  2770. X        exit(1);
  2771. X    }
  2772. X
  2773. X    key.data = wp1;
  2774. X    item.data = wp2;
  2775. X    while ( fgets(wp1, 8192, stdin) &&
  2776. X        fgets(wp2, 8192, stdin) &&
  2777. X        i++ < MAXWORDS) {
  2778. X/*
  2779. X* put info in structure, and structure in the item
  2780. X*/
  2781. X        key.size = strlen(wp1);
  2782. X        item.size = strlen(wp2);
  2783. X
  2784. X/*
  2785. X * enter key/data pair into the table
  2786. X */
  2787. X        if ((dbp->put)(dbp, &key, &item, R_NOOVERWRITE) != NULL) {
  2788. X            fprintf(stderr, "cannot enter: key %s\n",
  2789. X                item.data);
  2790. X            exit(1);
  2791. X        }            
  2792. X    }
  2793. X
  2794. X    if ( --argc ) {
  2795. X        fp = fopen ( argv[0], "r");
  2796. X        i = 0;
  2797. X        while ( fgets(wp1, 8192, fp) &&
  2798. X            fgets(wp2, 8192, fp) &&
  2799. X            i++ < MAXWORDS) {
  2800. X            key.size = strlen(wp1);
  2801. X            stat = (dbp->del)(dbp, &key, 0);
  2802. X            if (stat) {
  2803. X            fprintf ( stderr, "Error retrieving %s\n", key.data );
  2804. X            exit(1);
  2805. X            } 
  2806. X        }
  2807. X        fclose(fp);
  2808. X    }
  2809. X    (dbp->close)(dbp);
  2810. X    exit(0);
  2811. X}
  2812. END_OF_FILE
  2813. if test 3671 -ne `wc -c <'test/hash.tests/tdel.c'`; then
  2814.     echo shar: \"'test/hash.tests/tdel.c'\" unpacked with wrong size!
  2815. fi
  2816. # end of 'test/hash.tests/tdel.c'
  2817. fi
  2818. if test -f 'test/hash.tests/thash4.c' -a "${1}" != "-c" ; then 
  2819.   echo shar: Will not clobber existing file \"'test/hash.tests/thash4.c'\"
  2820. else
  2821. echo shar: Extracting \"'test/hash.tests/thash4.c'\" \(3996 characters\)
  2822. sed "s/^X//" >'test/hash.tests/thash4.c' <<'END_OF_FILE'
  2823. X/*-
  2824. X * Copyright (c) 1991, 1993
  2825. X *    The Regents of the University of California.  All rights reserved.
  2826. X *
  2827. X * This code is derived from software contributed to Berkeley by
  2828. X * Margo Seltzer.
  2829. X *
  2830. X * Redistribution and use in source and binary forms, with or without
  2831. X * modification, are permitted provided that the following conditions
  2832. X * are met:
  2833. X * 1. Redistributions of source code must retain the above copyright
  2834. X *    notice, this list of conditions and the following disclaimer.
  2835. X * 2. Redistributions in binary form must reproduce the above copyright
  2836. X *    notice, this list of conditions and the following disclaimer in the
  2837. X *    documentation and/or other materials provided with the distribution.
  2838. X * 3. All advertising materials mentioning features or use of this software
  2839. X *    must display the following acknowledgement:
  2840. X *    This product includes software developed by the University of
  2841. X *    California, Berkeley and its contributors.
  2842. X * 4. Neither the name of the University nor the names of its contributors
  2843. X *    may be used to endorse or promote products derived from this software
  2844. X *    without specific prior written permission.
  2845. X *
  2846. X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  2847. X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  2848. X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  2849. X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  2850. X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  2851. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  2852. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  2853. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  2854. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  2855. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  2856. X * SUCH DAMAGE.
  2857. X */
  2858. X
  2859. X#ifndef lint
  2860. Xstatic char copyright[] =
  2861. X"@(#) Copyright (c) 1991, 1993\n\
  2862. X    The Regents of the University of California.  All rights reserved.\n";
  2863. X#endif /* not lint */
  2864. X
  2865. X#ifndef lint
  2866. Xstatic char sccsid[] = "@(#)thash4.c    8.1 (Berkeley) 6/4/93";
  2867. X#endif /* not lint */
  2868. X
  2869. X#include <sys/types.h>
  2870. X#include <sys/file.h>
  2871. X#include <sys/timeb.h>
  2872. X#include <stdio.h>
  2873. X#include <errno.h>
  2874. X#include <db.h>
  2875. X
  2876. X#define INITIAL    25000
  2877. X#define MAXWORDS    25000           /* # of elements in search table */
  2878. X
  2879. X/* Usage: thash pagesize fillfactor file */
  2880. Xchar    wp1[8192];
  2881. Xchar    wp2[8192];
  2882. Xmain(argc, argv)
  2883. Xchar **argv;
  2884. X{
  2885. X    DBT item, key, res;
  2886. X    DB *dbp;
  2887. X    HASHINFO ctl;
  2888. X    FILE *fp;
  2889. X    int    stat;
  2890. X    time_t    t;
  2891. X
  2892. X    int i = 0;
  2893. X
  2894. X    argv++;
  2895. X    ctl.hash = NULL;
  2896. X    ctl.bsize = atoi(*argv++);
  2897. X    ctl.ffactor = atoi(*argv++);
  2898. X    ctl.nelem = atoi(*argv++);
  2899. X    ctl.cachesize = atoi(*argv++);
  2900. X    ctl.lorder = 0;
  2901. X    if (!(dbp = dbopen( NULL, O_CREAT|O_RDWR, 0400, DB_HASH, &ctl))) {
  2902. X        /* create table */
  2903. X        fprintf(stderr, "cannot create: hash table size %d)\n",
  2904. X            INITIAL);
  2905. X        fprintf(stderr, "\terrno: %d\n", errno);
  2906. X        exit(1);
  2907. X    }
  2908. X
  2909. X    key.data = wp1;
  2910. X    item.data = wp2;
  2911. X    while ( fgets(wp1, 8192, stdin) && 
  2912. X        fgets(wp2, 8192, stdin) && 
  2913. X        i++ < MAXWORDS) {
  2914. X/*
  2915. X* put info in structure, and structure in the item
  2916. X*/
  2917. X        key.size = strlen(wp1);
  2918. X        item.size = strlen(wp2);
  2919. X
  2920. X/*
  2921. X * enter key/data pair into the table
  2922. X */
  2923. X        if ((dbp->put)(dbp, &key, &item, R_NOOVERWRITE) != NULL) {
  2924. X            fprintf(stderr, "cannot enter: key %s\n",
  2925. X                item.data);
  2926. X            fprintf(stderr, "\terrno: %d\n", errno);
  2927. X            exit(1);
  2928. X        }            
  2929. X    }
  2930. X
  2931. X    if ( --argc ) {
  2932. X        fp = fopen ( argv[0], "r");
  2933. X        i = 0;
  2934. X        while ( fgets(wp1, 256, fp) && 
  2935. X            fgets(wp2, 8192, fp) && 
  2936. X            i++ < MAXWORDS) {
  2937. X
  2938. X            key.size = strlen(wp1);
  2939. X            stat = (dbp->get)(dbp, &key, &res, 0);
  2940. X            if (stat < 0 ) {
  2941. X            fprintf ( stderr, "Error retrieving %s\n", key.data );
  2942. X            fprintf(stderr, "\terrno: %d\n", errno);
  2943. X            exit(1);
  2944. X            } else if ( stat > 0 ) {
  2945. X            fprintf ( stderr, "%s not found\n", key.data );
  2946. X            fprintf(stderr, "\terrno: %d\n", errno);
  2947. X            exit(1);
  2948. X            }
  2949. X        }
  2950. X        fclose(fp);
  2951. X    }
  2952. X    dbp->close(dbp);
  2953. X    exit(0);
  2954. X}
  2955. END_OF_FILE
  2956. if test 3996 -ne `wc -c <'test/hash.tests/thash4.c'`; then
  2957.     echo shar: \"'test/hash.tests/thash4.c'\" unpacked with wrong size!
  2958. fi
  2959. # end of 'test/hash.tests/thash4.c'
  2960. fi
  2961. echo shar: End of archive 2 \(of 9\).
  2962. cp /dev/null ark2isdone
  2963. MISSING=""
  2964. for I in 1 2 3 4 5 6 7 8 9 ; do
  2965.     if test ! -f ark${I}isdone ; then
  2966.     MISSING="${MISSING} ${I}"
  2967.     fi
  2968. done
  2969. if test "${MISSING}" = "" ; then
  2970.     echo You have unpacked all 9 archives.
  2971.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  2972. else
  2973.     echo You still need to unpack the following archives:
  2974.     echo "        " ${MISSING}
  2975. fi
  2976. ##  End of shell archive.
  2977. exit 0
  2978.