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