| 1 | /*- |
| 2 | * Copyright (c) 1990, 1993 |
| 3 | * The Regents of the University of California. All rights reserved. |
| 4 | * |
| 5 | * %sccs.include.redist.c% |
| 6 | * |
| 7 | * @(#)db.h 8.3 (Berkeley) %G% |
| 8 | */ |
| 9 | |
| 10 | #ifndef _DB_H_ |
| 11 | #define _DB_H_ |
| 12 | |
| 13 | #include <sys/types.h> |
| 14 | #include <sys/cdefs.h> |
| 15 | |
| 16 | #include <limits.h> |
| 17 | |
| 18 | #define RET_ERROR -1 /* Return values. */ |
| 19 | #define RET_SUCCESS 0 |
| 20 | #define RET_SPECIAL 1 |
| 21 | |
| 22 | #define MAX_PAGE_NUMBER ULONG_MAX /* >= # of pages in a file */ |
| 23 | typedef u_long pgno_t; |
| 24 | #define MAX_PAGE_OFFSET USHRT_MAX /* >= # of bytes in a page */ |
| 25 | typedef u_short indx_t; |
| 26 | #define MAX_REC_NUMBER ULONG_MAX /* >= # of records in a tree */ |
| 27 | typedef u_long recno_t; |
| 28 | |
| 29 | /* Key/data structure -- a Data-Base Thang. */ |
| 30 | typedef struct { |
| 31 | void *data; /* data */ |
| 32 | size_t size; /* data length */ |
| 33 | } DBT; |
| 34 | |
| 35 | /* Routine flags. */ |
| 36 | #define R_CURSOR 1 /* del, put, seq */ |
| 37 | #define __R_UNUSED 2 /* UNUSED */ |
| 38 | #define R_FIRST 3 /* seq */ |
| 39 | #define R_IAFTER 4 /* put (RECNO) */ |
| 40 | #define R_IBEFORE 5 /* put (RECNO) */ |
| 41 | #define R_LAST 6 /* seq (BTREE, RECNO) */ |
| 42 | #define R_NEXT 7 /* seq */ |
| 43 | #define R_NOOVERWRITE 8 /* put */ |
| 44 | #define R_PREV 9 /* seq (BTREE, RECNO) */ |
| 45 | #define R_SETCURSOR 10 /* put (RECNO) */ |
| 46 | #define R_RECNOSYNC 11 /* sync (RECNO) */ |
| 47 | |
| 48 | typedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE; |
| 49 | |
| 50 | /* |
| 51 | * !!! |
| 52 | * The following flags are included in the dbopen(3) call as part of the |
| 53 | * open(2) flags. In order to avoid conflicts with the open flags, start |
| 54 | * at the top of the 16 or 32-bit number space and work our way down. If |
| 55 | * the open flags were significantly expanded in the future, it could be |
| 56 | * a problem. Wish I'd left another flags word in the dbopen call. |
| 57 | * |
| 58 | * !!! |
| 59 | * None of this stuff is implemented yet. The only reason that it's here |
| 60 | * is so that the access methods can skip copying the key/data pair when |
| 61 | * the DB_LOCK flag isn't set. |
| 62 | */ |
| 63 | #if UINT_MAX > 65535 |
| 64 | #define DB_LOCK 0x20000000 /* Do locking. */ |
| 65 | #define DB_SHMEM 0x40000000 /* Use shared memory. */ |
| 66 | #define DB_TXN 0x80000000 /* Do transactions. */ |
| 67 | #else |
| 68 | #define DB_LOCK 0x00002000 /* Do locking. */ |
| 69 | #define DB_SHMEM 0x00004000 /* Use shared memory. */ |
| 70 | #define DB_TXN 0x00008000 /* Do transactions. */ |
| 71 | #endif |
| 72 | |
| 73 | /* Access method description structure. */ |
| 74 | typedef struct __db { |
| 75 | DBTYPE type; /* Underlying db type. */ |
| 76 | int (*close) __P((struct __db *)); |
| 77 | int (*del) __P((const struct __db *, const DBT *, u_int)); |
| 78 | int (*get) __P((const struct __db *, const DBT *, DBT *, u_int)); |
| 79 | int (*put) __P((const struct __db *, DBT *, const DBT *, u_int)); |
| 80 | int (*seq) __P((const struct __db *, DBT *, DBT *, u_int)); |
| 81 | int (*sync) __P((const struct __db *, u_int)); |
| 82 | void *internal; /* Access method private. */ |
| 83 | int (*fd) __P((const struct __db *)); |
| 84 | } DB; |
| 85 | |
| 86 | #define BTREEMAGIC 0x053162 |
| 87 | #define BTREEVERSION 3 |
| 88 | |
| 89 | /* Structure used to pass parameters to the btree routines. */ |
| 90 | typedef struct { |
| 91 | #define R_DUP 0x01 /* duplicate keys */ |
| 92 | u_long flags; |
| 93 | int cachesize; /* bytes to cache */ |
| 94 | int maxkeypage; /* maximum keys per page */ |
| 95 | int minkeypage; /* minimum keys per page */ |
| 96 | int psize; /* page size */ |
| 97 | /* comparison, prefix functions */ |
| 98 | int (*compare) __P((const DBT *, const DBT *)); |
| 99 | int (*prefix) __P((const DBT *, const DBT *)); |
| 100 | int lorder; /* byte order */ |
| 101 | } BTREEINFO; |
| 102 | |
| 103 | #define HASHMAGIC 0x061561 |
| 104 | #define HASHVERSION 2 |
| 105 | |
| 106 | /* Structure used to pass parameters to the hashing routines. */ |
| 107 | typedef struct { |
| 108 | int bsize; /* bucket size */ |
| 109 | int ffactor; /* fill factor */ |
| 110 | int nelem; /* number of elements */ |
| 111 | int cachesize; /* bytes to cache */ |
| 112 | /* hash function */ |
| 113 | int (*hash) __P((const void *, size_t)); |
| 114 | int lorder; /* byte order */ |
| 115 | } HASHINFO; |
| 116 | |
| 117 | /* Structure used to pass parameters to the record routines. */ |
| 118 | typedef struct { |
| 119 | #define R_FIXEDLEN 0x01 /* fixed-length records */ |
| 120 | #define R_NOKEY 0x02 /* key not required */ |
| 121 | #define R_SNAPSHOT 0x04 /* snapshot the input */ |
| 122 | u_long flags; |
| 123 | int cachesize; /* bytes to cache */ |
| 124 | int psize; /* page size */ |
| 125 | int lorder; /* byte order */ |
| 126 | size_t reclen; /* record length (fixed-length records) */ |
| 127 | u_char bval; /* delimiting byte (variable-length records */ |
| 128 | char *bfname; /* btree file name */ |
| 129 | } RECNOINFO; |
| 130 | |
| 131 | /* |
| 132 | * Little endian <==> big endian long swap macros. |
| 133 | * BLSWAP swap a memory location |
| 134 | * BLPSWAP swap a referenced memory location |
| 135 | * BLSWAP_COPY swap from one location to another |
| 136 | */ |
| 137 | #define BLSWAP(a) { \ |
| 138 | u_long _tmp = a; \ |
| 139 | ((char *)&a)[0] = ((char *)&_tmp)[3]; \ |
| 140 | ((char *)&a)[1] = ((char *)&_tmp)[2]; \ |
| 141 | ((char *)&a)[2] = ((char *)&_tmp)[1]; \ |
| 142 | ((char *)&a)[3] = ((char *)&_tmp)[0]; \ |
| 143 | } |
| 144 | #define BLPSWAP(a) { \ |
| 145 | u_long _tmp = *(u_long *)a; \ |
| 146 | ((char *)a)[0] = ((char *)&_tmp)[3]; \ |
| 147 | ((char *)a)[1] = ((char *)&_tmp)[2]; \ |
| 148 | ((char *)a)[2] = ((char *)&_tmp)[1]; \ |
| 149 | ((char *)a)[3] = ((char *)&_tmp)[0]; \ |
| 150 | } |
| 151 | #define BLSWAP_COPY(a, b) { \ |
| 152 | ((char *)&(b))[0] = ((char *)&(a))[3]; \ |
| 153 | ((char *)&(b))[1] = ((char *)&(a))[2]; \ |
| 154 | ((char *)&(b))[2] = ((char *)&(a))[1]; \ |
| 155 | ((char *)&(b))[3] = ((char *)&(a))[0]; \ |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Little endian <==> big endian short swap macros. |
| 160 | * BSSWAP swap a memory location |
| 161 | * BSPSWAP swap a referenced memory location |
| 162 | * BSSWAP_COPY swap from one location to another |
| 163 | */ |
| 164 | #define BSSWAP(a) { \ |
| 165 | u_short _tmp = a; \ |
| 166 | ((char *)&a)[0] = ((char *)&_tmp)[1]; \ |
| 167 | ((char *)&a)[1] = ((char *)&_tmp)[0]; \ |
| 168 | } |
| 169 | #define BSPSWAP(a) { \ |
| 170 | u_short _tmp = *(u_short *)a; \ |
| 171 | ((char *)a)[0] = ((char *)&_tmp)[1]; \ |
| 172 | ((char *)a)[1] = ((char *)&_tmp)[0]; \ |
| 173 | } |
| 174 | #define BSSWAP_COPY(a, b) { \ |
| 175 | ((char *)&(b))[0] = ((char *)&(a))[1]; \ |
| 176 | ((char *)&(b))[1] = ((char *)&(a))[0]; \ |
| 177 | } |
| 178 | |
| 179 | __BEGIN_DECLS |
| 180 | DB *dbopen __P((const char *, int, int, DBTYPE, const void *)); |
| 181 | |
| 182 | #ifdef __DBINTERFACE_PRIVATE |
| 183 | DB *__bt_open __P((const char *, int, int, const BTREEINFO *, int)); |
| 184 | DB *__hash_open __P((const char *, int, int, const HASHINFO *, int)); |
| 185 | DB *__rec_open __P((const char *, int, int, const RECNOINFO *, int)); |
| 186 | void __dbpanic __P((DB *dbp)); |
| 187 | #endif |
| 188 | __END_DECLS |
| 189 | #endif /* !_DB_H_ */ |