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