1 /*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * 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 5.25 (Berkeley) 5/22/93 34 */ 35 36 #ifndef _DB_H_ 37 #define _DB_H_ 38 39 #include <sys/types.h> 40 #include <sys/cdefs.h> 41 42 #define RET_ERROR -1 /* Return values. */ 43 #define RET_SUCCESS 0 44 #define RET_SPECIAL 1 45 46 #define MAX_PAGE_NUMBER ULONG_MAX /* >= # of pages in a file */ 47 typedef u_long pgno_t; 48 #define MAX_PAGE_OFFSET USHRT_MAX /* >= # of bytes in a page */ 49 typedef u_short indx_t; 50 #define MAX_REC_NUMBER ULONG_MAX /* >= # of records in a tree */ 51 typedef u_long recno_t; 52 53 /* Key/data structure -- a Data-Base Thang. */ 54 typedef struct { 55 void *data; /* data */ 56 size_t size; /* data length */ 57 } DBT; 58 59 /* Routine flags. */ 60 #define R_CURSOR 1 /* del, put, seq */ 61 #define __R_UNUSED 2 /* UNUSED */ 62 #define R_FIRST 3 /* seq */ 63 #define R_IAFTER 4 /* put (RECNO) */ 64 #define R_IBEFORE 5 /* put (RECNO) */ 65 #define R_LAST 6 /* seq (BTREE, RECNO) */ 66 #define R_NEXT 7 /* seq */ 67 #define R_NOOVERWRITE 8 /* put */ 68 #define R_PREV 9 /* seq (BTREE, RECNO) */ 69 #define R_SETCURSOR 10 /* put (RECNO) */ 70 #define R_RECNOSYNC 11 /* sync (RECNO) */ 71 72 typedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE; 73 74 #define __USE_OPEN_FLAGS \ 75 (O_CREAT|O_EXCL|O_EXLOCK|O_RDONLY|O_RDWR|O_SHLOCK|O_TRUNC) 76 77 /* Access method description structure. */ 78 typedef struct __db { 79 DBTYPE type; /* underlying db type */ 80 int (*close) __P((struct __db *)); 81 int (*del) __P((const struct __db *, const DBT *, u_int)); 82 int (*fd) __P((const struct __db *)); 83 int (*get) __P((const struct __db *, const DBT *, DBT *, u_int)); 84 int (*put) __P((const struct __db *, DBT *, const DBT *, u_int)); 85 int (*seq) __P((const struct __db *, DBT *, DBT *, u_int)); 86 int (*sync) __P((const struct __db *, u_int)); 87 void *internal; /* access method private */ 88 } DB; 89 90 #define BTREEMAGIC 0x053162 91 #define BTREEVERSION 3 92 93 /* Structure used to pass parameters to the btree routines. */ 94 typedef struct { 95 #define R_DUP 0x01 /* duplicate keys */ 96 u_long flags; 97 int cachesize; /* bytes to cache */ 98 int maxkeypage; /* maximum keys per page */ 99 int minkeypage; /* minimum keys per page */ 100 int psize; /* page size */ 101 /* comparison, prefix functions */ 102 int (*compare) __P((const DBT *, const DBT *)); 103 int (*prefix) __P((const DBT *, const DBT *)); 104 int lorder; /* byte order */ 105 } BTREEINFO; 106 107 #define HASHMAGIC 0x061561 108 #define HASHVERSION 2 109 110 /* Structure used to pass parameters to the hashing routines. */ 111 typedef struct { 112 int bsize; /* bucket size */ 113 int ffactor; /* fill factor */ 114 int nelem; /* number of elements */ 115 int cachesize; /* bytes to cache */ 116 /* hash function */ 117 int (*hash) __P((const void *, size_t)); 118 int lorder; /* byte order */ 119 } HASHINFO; 120 121 /* Structure used to pass parameters to the record routines. */ 122 typedef struct { 123 #define R_FIXEDLEN 0x01 /* fixed-length records */ 124 #define R_NOKEY 0x02 /* key not required */ 125 #define R_SNAPSHOT 0x04 /* snapshot the input */ 126 u_long flags; 127 int cachesize; /* bytes to cache */ 128 int psize; /* page size */ 129 int lorder; /* byte order */ 130 size_t reclen; /* record length (fixed-length records) */ 131 u_char bval; /* delimiting byte (variable-length records */ 132 char *bfname; /* btree file name */ 133 } RECNOINFO; 134 135 /* 136 * Little endian <==> big endian long swap macros. 137 * BLSWAP swap a memory location 138 * BLPSWAP swap a referenced memory location 139 * BLSWAP_COPY swap from one location to another 140 */ 141 #define BLSWAP(a) { \ 142 u_long _tmp = a; \ 143 ((char *)&a)[0] = ((char *)&_tmp)[3]; \ 144 ((char *)&a)[1] = ((char *)&_tmp)[2]; \ 145 ((char *)&a)[2] = ((char *)&_tmp)[1]; \ 146 ((char *)&a)[3] = ((char *)&_tmp)[0]; \ 147 } 148 #define BLPSWAP(a) { \ 149 u_long _tmp = *(u_long *)a; \ 150 ((char *)a)[0] = ((char *)&_tmp)[3]; \ 151 ((char *)a)[1] = ((char *)&_tmp)[2]; \ 152 ((char *)a)[2] = ((char *)&_tmp)[1]; \ 153 ((char *)a)[3] = ((char *)&_tmp)[0]; \ 154 } 155 #define BLSWAP_COPY(a, b) { \ 156 ((char *)&(b))[0] = ((char *)&(a))[3]; \ 157 ((char *)&(b))[1] = ((char *)&(a))[2]; \ 158 ((char *)&(b))[2] = ((char *)&(a))[1]; \ 159 ((char *)&(b))[3] = ((char *)&(a))[0]; \ 160 } 161 162 /* 163 * Little endian <==> big endian short swap macros. 164 * BSSWAP swap a memory location 165 * BSPSWAP swap a referenced memory location 166 * BSSWAP_COPY swap from one location to another 167 */ 168 #define BSSWAP(a) { \ 169 u_short _tmp = a; \ 170 ((char *)&a)[0] = ((char *)&_tmp)[1]; \ 171 ((char *)&a)[1] = ((char *)&_tmp)[0]; \ 172 } 173 #define BSPSWAP(a) { \ 174 u_short _tmp = *(u_short *)a; \ 175 ((char *)a)[0] = ((char *)&_tmp)[1]; \ 176 ((char *)a)[1] = ((char *)&_tmp)[0]; \ 177 } 178 #define BSSWAP_COPY(a, b) { \ 179 ((char *)&(b))[0] = ((char *)&(a))[1]; \ 180 ((char *)&(b))[1] = ((char *)&(a))[0]; \ 181 } 182 183 __BEGIN_DECLS 184 DB *dbopen __P((const char *, int, int, DBTYPE, const void *)); 185 186 #ifdef __DBINTERFACE_PRIVATE 187 DB *__bt_open __P((const char *, int, int, const BTREEINFO *)); 188 DB *__hash_open __P((const char *, int, int, const HASHINFO *)); 189 DB *__rec_open __P((const char *, int, int, const RECNOINFO *)); 190 void __dbpanic __P((DB *dbp)); 191 #endif 192 __END_DECLS 193 #endif /* !_DB_H_ */ 194