db.h revision 1.4 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.21 (Berkeley) 2/14/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_CURSORLOG 2 /* put (RECNO) */
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
71 typedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE;
72
73 #define __USE_OPEN_FLAGS \
74 (O_CREAT|O_EXCL|O_EXLOCK|O_RDONLY|O_RDWR|O_SHLOCK|O_TRUNC)
75
76 /* Access method description structure. */
77 typedef struct __db {
78 DBTYPE type; /* underlying db type */
79 int (*close) __P((struct __db *));
80 int (*del) __P((const struct __db *, const DBT *, u_int));
81 int (*get) __P((const struct __db *, const DBT *, DBT *, u_int));
82 int (*put) __P((const struct __db *, DBT *, const DBT *, u_int));
83 int (*seq) __P((const struct __db *, DBT *, DBT *, u_int));
84 int (*sync) __P((const struct __db *));
85 void *internal; /* access method private */
86 } DB;
87
88 #define BTREEMAGIC 0x053162
89 #define BTREEVERSION 3
90
91 /* Structure used to pass parameters to the btree routines. */
92 typedef struct {
93 #define R_DUP 0x01 /* duplicate keys */
94 u_long flags;
95 int cachesize; /* bytes to cache */
96 int maxkeypage; /* maximum keys per page */
97 int minkeypage; /* minimum keys per page */
98 int psize; /* page size */
99 /* comparison, prefix functions */
100 int (*compare) __P((const DBT *, const DBT *));
101 int (*prefix) __P((const DBT *, const DBT *));
102 int lorder; /* byte order */
103 } BTREEINFO;
104
105 #define HASHMAGIC 0x061561
106 #define HASHVERSION 2
107
108 /* Structure used to pass parameters to the hashing routines. */
109 typedef struct {
110 int bsize; /* bucket size */
111 int ffactor; /* fill factor */
112 int nelem; /* number of elements */
113 int cachesize; /* bytes to cache */
114 int (*hash)(); /* hash function */
115 int lorder; /* byte order */
116 } HASHINFO;
117
118 /* Structure used to pass parameters to the record routines. */
119 typedef struct {
120 #define R_FIXEDLEN 0x01 /* fixed-length records */
121 #define R_NOKEY 0x02 /* key not required */
122 #define R_SNAPSHOT 0x04 /* snapshot the input */
123 u_long flags;
124 int cachesize; /* bytes to cache */
125 int lorder; /* byte order */
126 size_t reclen; /* record length (fixed-length records) */
127 u_char bval; /* delimiting byte (variable-length records */
128 } RECNOINFO;
129
130 /* Key structure for the record routines. */
131 typedef struct {
132 u_long number;
133 u_long offset;
134 u_long length;
135 #define R_LENGTH 0x01 /* length is valid */
136 #define R_NUMBER 0x02 /* record number is valid */
137 #define R_OFFSET 0x04 /* offset is valid */
138 u_char valid;
139 } RECNOKEY;
140
141 /*
142 * Little endian <==> big endian long swap macros.
143 * BLSWAP swap a memory location
144 * BLPSWAP swap a referenced memory location
145 * BLSWAP_COPY swap from one location to another
146 */
147 #define BLSWAP(a) { \
148 u_long _tmp = a; \
149 ((char *)&a)[0] = ((char *)&_tmp)[3]; \
150 ((char *)&a)[1] = ((char *)&_tmp)[2]; \
151 ((char *)&a)[2] = ((char *)&_tmp)[1]; \
152 ((char *)&a)[3] = ((char *)&_tmp)[0]; \
153 }
154 #define BLPSWAP(a) { \
155 u_long _tmp = *(u_long *)a; \
156 ((char *)a)[0] = ((char *)&_tmp)[3]; \
157 ((char *)a)[1] = ((char *)&_tmp)[2]; \
158 ((char *)a)[2] = ((char *)&_tmp)[1]; \
159 ((char *)a)[3] = ((char *)&_tmp)[0]; \
160 }
161 #define BLSWAP_COPY(a, b) { \
162 ((char *)&(b))[0] = ((char *)&(a))[3]; \
163 ((char *)&(b))[1] = ((char *)&(a))[2]; \
164 ((char *)&(b))[2] = ((char *)&(a))[1]; \
165 ((char *)&(b))[3] = ((char *)&(a))[0]; \
166 }
167
168 /*
169 * Little endian <==> big endian short swap macros.
170 * BSSWAP swap a memory location
171 * BSPSWAP swap a referenced memory location
172 * BSSWAP_COPY swap from one location to another
173 */
174 #define BSSWAP(a) { \
175 u_short _tmp = a; \
176 ((char *)&a)[0] = ((char *)&_tmp)[1]; \
177 ((char *)&a)[1] = ((char *)&_tmp)[0]; \
178 }
179 #define BSPSWAP(a) { \
180 u_short _tmp = *(u_short *)a; \
181 ((char *)a)[0] = ((char *)&_tmp)[1]; \
182 ((char *)a)[1] = ((char *)&_tmp)[0]; \
183 }
184 #define BSSWAP_COPY(a, b) { \
185 ((char *)&(b))[0] = ((char *)&(a))[1]; \
186 ((char *)&(b))[1] = ((char *)&(a))[0]; \
187 }
188
189 __BEGIN_DECLS
190 DB *dbopen __P((const char *, int, int, DBTYPE, const void *));
191
192 #ifdef __DBINTERFACE_PRIVATE
193 DB *__bt_open __P((const char *, int, int, const BTREEINFO *));
194 DB *__hash_open __P((const char *, int, int, const HASHINFO *));
195 DB *__rec_open __P((const char *, int, int, const RECNOINFO *));
196 void __dbpanic __P((DB *dbp));
197 #endif
198 __END_DECLS
199 #endif /* !_DB_H_ */
200