| 1 | /*- | 
|---|
| 2 | * Copyright (c) 1990, 1993, 1994 | 
|---|
| 3 | *      The Regents of the University of California.  All rights reserved. | 
|---|
| 4 | * | 
|---|
| 5 | * Redistribution and use in source and binary forms, with or without | 
|---|
| 6 | * modification, are permitted provided that the following conditions | 
|---|
| 7 | * are met: | 
|---|
| 8 | * 1. Redistributions of source code must retain the above copyright | 
|---|
| 9 | *    notice, this list of conditions and the following disclaimer. | 
|---|
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | 
|---|
| 11 | *    notice, this list of conditions and the following disclaimer in the | 
|---|
| 12 | *    documentation and/or other materials provided with the distribution. | 
|---|
| 13 | * 3. All advertising materials mentioning features or use of this software | 
|---|
| 14 | *    must display the following acknowledgement: | 
|---|
| 15 | *      This product includes software developed by the University of | 
|---|
| 16 | *      California, Berkeley and its contributors. | 
|---|
| 17 | * 4. Neither the name of the University nor the names of its contributors | 
|---|
| 18 | *    may be used to endorse or promote products derived from this software | 
|---|
| 19 | *    without specific prior written permission. | 
|---|
| 20 | * | 
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | 
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 
|---|
| 24 | * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | 
|---|
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
|---|
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 
|---|
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 
|---|
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 
|---|
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 
|---|
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
|---|
| 31 | * SUCH DAMAGE. | 
|---|
| 32 | * | 
|---|
| 33 | *      @(#)db.h        8.7 (Berkeley) 6/16/94 | 
|---|
| 34 | */ | 
|---|
| 35 |  | 
|---|
| 36 | #ifndef _DB_H_ | 
|---|
| 37 | #define _DB_H_ | 
|---|
| 38 |  | 
|---|
| 39 | #if defined (__cplusplus) | 
|---|
| 40 | extern "C" { | 
|---|
| 41 | #endif | 
|---|
| 42 |  | 
|---|
| 43 | #include <sys/types.h> | 
|---|
| 44 | #include <sys/cdefs.h> | 
|---|
| 45 |  | 
|---|
| 46 | #include <limits.h> | 
|---|
| 47 |  | 
|---|
| 48 | #ifdef __DBINTERFACE_PRIVATE | 
|---|
| 49 | #include <compat.h> | 
|---|
| 50 | #endif | 
|---|
| 51 |  | 
|---|
| 52 | #define RET_ERROR       -1              /* Return values. */ | 
|---|
| 53 | #define RET_SUCCESS      0 | 
|---|
| 54 | #define RET_SPECIAL      1 | 
|---|
| 55 |  | 
|---|
| 56 | #if 0 | 
|---|
| 57 | #ifndef __BIT_TYPES_DEFINED__ | 
|---|
| 58 | #define __BIT_TYPES_DEFINED__ | 
|---|
| 59 | typedef __signed char              int8_t; | 
|---|
| 60 | typedef unsigned char            u_int8_t; | 
|---|
| 61 | typedef short                     int16_t; | 
|---|
| 62 | typedef unsigned short          u_int16_t; | 
|---|
| 63 | typedef int                       int32_t; | 
|---|
| 64 | typedef unsigned int            u_int32_t; | 
|---|
| 65 | #ifdef WE_DONT_NEED_QUADS | 
|---|
| 66 | typedef long long                 int64_t; | 
|---|
| 67 | typedef unsigned long long      u_int64_t; | 
|---|
| 68 | #endif | 
|---|
| 69 | #endif | 
|---|
| 70 | #else | 
|---|
| 71 | #include <sys/inttypes.h> | 
|---|
| 72 | #endif | 
|---|
| 73 |  | 
|---|
| 74 | #define MAX_PAGE_NUMBER 0xffffffff      /* >= # of pages in a file */ | 
|---|
| 75 | typedef u_int32_t       pgno_t; | 
|---|
| 76 | #define MAX_PAGE_OFFSET 65535           /* >= # of bytes in a page */ | 
|---|
| 77 | typedef u_int16_t       indx_t; | 
|---|
| 78 | #define MAX_REC_NUMBER  0xffffffff      /* >= # of records in a tree */ | 
|---|
| 79 | typedef u_int32_t       recno_t; | 
|---|
| 80 |  | 
|---|
| 81 | /* Key/data structure -- a Data-Base Thang. */ | 
|---|
| 82 | typedef struct { | 
|---|
| 83 | void    *data;                  /* data */ | 
|---|
| 84 | size_t   size;                  /* data length */ | 
|---|
| 85 | } DBT; | 
|---|
| 86 |  | 
|---|
| 87 | /* Routine flags. */ | 
|---|
| 88 | #define R_CURSOR        1               /* del, put, seq */ | 
|---|
| 89 | #define __R_UNUSED      2               /* UNUSED */ | 
|---|
| 90 | #define R_FIRST         3               /* seq */ | 
|---|
| 91 | #define R_IAFTER        4               /* put (RECNO) */ | 
|---|
| 92 | #define R_IBEFORE       5               /* put (RECNO) */ | 
|---|
| 93 | #define R_LAST          6               /* seq (BTREE, RECNO) */ | 
|---|
| 94 | #define R_NEXT          7               /* seq */ | 
|---|
| 95 | #define R_NOOVERWRITE   8               /* put */ | 
|---|
| 96 | #define R_PREV          9               /* seq (BTREE, RECNO) */ | 
|---|
| 97 | #define R_SETCURSOR     10              /* put (RECNO) */ | 
|---|
| 98 | #define R_RECNOSYNC     11              /* sync (RECNO) */ | 
|---|
| 99 |  | 
|---|
| 100 | typedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE; | 
|---|
| 101 |  | 
|---|
| 102 | /* | 
|---|
| 103 | * !!! | 
|---|
| 104 | * The following flags are included in the dbopen(3) call as part of the | 
|---|
| 105 | * open(2) flags.  In order to avoid conflicts with the open flags, start | 
|---|
| 106 | * at the top of the 16 or 32-bit number space and work our way down.  If | 
|---|
| 107 | * the open flags were significantly expanded in the future, it could be | 
|---|
| 108 | * a problem.  Wish I'd left another flags word in the dbopen call. | 
|---|
| 109 | * | 
|---|
| 110 | * !!! | 
|---|
| 111 | * None of this stuff is implemented yet.  The only reason that it's here | 
|---|
| 112 | * is so that the access methods can skip copying the key/data pair when | 
|---|
| 113 | * the DB_LOCK flag isn't set. | 
|---|
| 114 | */ | 
|---|
| 115 | #if UINT_MAX > 65535 | 
|---|
| 116 | #define DB_LOCK         0x20000000      /* Do locking. */ | 
|---|
| 117 | #define DB_SHMEM        0x40000000      /* Use shared memory. */ | 
|---|
| 118 | #define DB_TXN          0x80000000      /* Do transactions. */ | 
|---|
| 119 | #else | 
|---|
| 120 | #define DB_LOCK             0x2000      /* Do locking. */ | 
|---|
| 121 | #define DB_SHMEM            0x4000      /* Use shared memory. */ | 
|---|
| 122 | #define DB_TXN              0x8000      /* Do transactions. */ | 
|---|
| 123 | #endif | 
|---|
| 124 |  | 
|---|
| 125 | /* Access method description structure. */ | 
|---|
| 126 | typedef struct __db { | 
|---|
| 127 | DBTYPE type;                    /* Underlying db type. */ | 
|---|
| 128 | int (*close)    __P((struct __db *)); | 
|---|
| 129 | int (*del)      __P((const struct __db *, const DBT *, u_int)); | 
|---|
| 130 | int (*get)      __P((const struct __db *, const DBT *, DBT *, u_int)); | 
|---|
| 131 | int (*put)      __P((const struct __db *, DBT *, const DBT *, u_int)); | 
|---|
| 132 | int (*seq)      __P((const struct __db *, DBT *, DBT *, u_int)); | 
|---|
| 133 | int (*sync)     __P((const struct __db *, u_int)); | 
|---|
| 134 | void *internal;                 /* Access method private. */ | 
|---|
| 135 | int (*fd)       __P((const struct __db *)); | 
|---|
| 136 | } DB; | 
|---|
| 137 |  | 
|---|
| 138 | #define BTREEMAGIC      0x053162 | 
|---|
| 139 | #define BTREEVERSION    3 | 
|---|
| 140 |  | 
|---|
| 141 | /* Structure used to pass parameters to the btree routines. */ | 
|---|
| 142 | typedef struct { | 
|---|
| 143 | #define R_DUP           0x01    /* duplicate keys */ | 
|---|
| 144 | u_long  flags; | 
|---|
| 145 | u_int   cachesize;      /* bytes to cache */ | 
|---|
| 146 | int     maxkeypage;     /* maximum keys per page */ | 
|---|
| 147 | int     minkeypage;     /* minimum keys per page */ | 
|---|
| 148 | u_int   psize;          /* page size */ | 
|---|
| 149 | int     (*compare)      /* comparison function */ | 
|---|
| 150 | __P((const DBT *, const DBT *)); | 
|---|
| 151 | size_t  (*prefix)       /* prefix function */ | 
|---|
| 152 | __P((const DBT *, const DBT *)); | 
|---|
| 153 | int     lorder;         /* byte order */ | 
|---|
| 154 | } BTREEINFO; | 
|---|
| 155 |  | 
|---|
| 156 | #define HASHMAGIC       0x061561 | 
|---|
| 157 | #define HASHVERSION     2 | 
|---|
| 158 |  | 
|---|
| 159 | /* Structure used to pass parameters to the hashing routines. */ | 
|---|
| 160 | typedef struct { | 
|---|
| 161 | u_int   bsize;          /* bucket size */ | 
|---|
| 162 | u_int   ffactor;        /* fill factor */ | 
|---|
| 163 | u_int   nelem;          /* number of elements */ | 
|---|
| 164 | u_int   cachesize;      /* bytes to cache */ | 
|---|
| 165 | u_int32_t               /* hash function */ | 
|---|
| 166 | (*hash) __P((const void *, size_t)); | 
|---|
| 167 | int     lorder;         /* byte order */ | 
|---|
| 168 | } HASHINFO; | 
|---|
| 169 |  | 
|---|
| 170 | /* Structure used to pass parameters to the record routines. */ | 
|---|
| 171 | typedef struct { | 
|---|
| 172 | #define R_FIXEDLEN      0x01    /* fixed-length records */ | 
|---|
| 173 | #define R_NOKEY         0x02    /* key not required */ | 
|---|
| 174 | #define R_SNAPSHOT      0x04    /* snapshot the input */ | 
|---|
| 175 | u_long  flags; | 
|---|
| 176 | u_int   cachesize;      /* bytes to cache */ | 
|---|
| 177 | u_int   psize;          /* page size */ | 
|---|
| 178 | int     lorder;         /* byte order */ | 
|---|
| 179 | size_t  reclen;         /* record length (fixed-length records) */ | 
|---|
| 180 | u_char  bval;           /* delimiting byte (variable-length records */ | 
|---|
| 181 | char    *bfname;        /* btree file name */ | 
|---|
| 182 | } RECNOINFO; | 
|---|
| 183 |  | 
|---|
| 184 | #ifdef __DBINTERFACE_PRIVATE | 
|---|
| 185 | /* | 
|---|
| 186 | * Little endian <==> big endian 32-bit swap macros. | 
|---|
| 187 | *      M_32_SWAP       swap a memory location | 
|---|
| 188 | *      P_32_SWAP       swap a referenced memory location | 
|---|
| 189 | *      P_32_COPY       swap from one location to another | 
|---|
| 190 | */ | 
|---|
| 191 | #define M_32_SWAP(a) {                                                  \ | 
|---|
| 192 | u_int32_t _tmp = a;                                             \ | 
|---|
| 193 | ((char *)&a)[0] = ((char *)&_tmp)[3];                           \ | 
|---|
| 194 | ((char *)&a)[1] = ((char *)&_tmp)[2];                           \ | 
|---|
| 195 | ((char *)&a)[2] = ((char *)&_tmp)[1];                           \ | 
|---|
| 196 | ((char *)&a)[3] = ((char *)&_tmp)[0];                           \ | 
|---|
| 197 | } | 
|---|
| 198 | #define P_32_SWAP(a) {                                                  \ | 
|---|
| 199 | u_int32_t _tmp = *(u_int32_t *)a;                               \ | 
|---|
| 200 | ((char *)a)[0] = ((char *)&_tmp)[3];                            \ | 
|---|
| 201 | ((char *)a)[1] = ((char *)&_tmp)[2];                            \ | 
|---|
| 202 | ((char *)a)[2] = ((char *)&_tmp)[1];                            \ | 
|---|
| 203 | ((char *)a)[3] = ((char *)&_tmp)[0];                            \ | 
|---|
| 204 | } | 
|---|
| 205 | #define P_32_COPY(a, b) {                                               \ | 
|---|
| 206 | ((char *)&(b))[0] = ((char *)&(a))[3];                          \ | 
|---|
| 207 | ((char *)&(b))[1] = ((char *)&(a))[2];                          \ | 
|---|
| 208 | ((char *)&(b))[2] = ((char *)&(a))[1];                          \ | 
|---|
| 209 | ((char *)&(b))[3] = ((char *)&(a))[0];                          \ | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|
| 212 | /* | 
|---|
| 213 | * Little endian <==> big endian 16-bit swap macros. | 
|---|
| 214 | *      M_16_SWAP       swap a memory location | 
|---|
| 215 | *      P_16_SWAP       swap a referenced memory location | 
|---|
| 216 | *      P_16_COPY       swap from one location to another | 
|---|
| 217 | */ | 
|---|
| 218 | #define M_16_SWAP(a) {                                                  \ | 
|---|
| 219 | u_int16_t _tmp = a;                                             \ | 
|---|
| 220 | ((char *)&a)[0] = ((char *)&_tmp)[1];                           \ | 
|---|
| 221 | ((char *)&a)[1] = ((char *)&_tmp)[0];                           \ | 
|---|
| 222 | } | 
|---|
| 223 | #define P_16_SWAP(a) {                                                  \ | 
|---|
| 224 | u_int16_t _tmp = *(u_int16_t *)a;                               \ | 
|---|
| 225 | ((char *)a)[0] = ((char *)&_tmp)[1];                            \ | 
|---|
| 226 | ((char *)a)[1] = ((char *)&_tmp)[0];                            \ | 
|---|
| 227 | } | 
|---|
| 228 | #define P_16_COPY(a, b) {                                               \ | 
|---|
| 229 | ((char *)&(b))[0] = ((char *)&(a))[1];                          \ | 
|---|
| 230 | ((char *)&(b))[1] = ((char *)&(a))[0];                          \ | 
|---|
| 231 | } | 
|---|
| 232 | #endif | 
|---|
| 233 |  | 
|---|
| 234 | __BEGIN_DECLS | 
|---|
| 235 | DB *dbopen __P((const char *, int, int, DBTYPE, const void *)); | 
|---|
| 236 |  | 
|---|
| 237 | #ifdef __DBINTERFACE_PRIVATE | 
|---|
| 238 | DB      *__bt_open __P((const char *, int, int, const BTREEINFO *, int)); | 
|---|
| 239 | DB      *__hash_open __P((const char *, int, int, const HASHINFO *, int)); | 
|---|
| 240 | DB      *__rec_open __P((const char *, int, int, const RECNOINFO *, int)); | 
|---|
| 241 | void     __dbpanic __P((DB *dbp)); | 
|---|
| 242 | #endif | 
|---|
| 243 | __END_DECLS | 
|---|
| 244 |  | 
|---|
| 245 | #if defined (__cplusplus) | 
|---|
| 246 | } | 
|---|
| 247 | #endif | 
|---|
| 248 |  | 
|---|
| 249 | #endif /* !_DB_H_ */ | 
|---|