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