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