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