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