1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3 * Copyright (c) 1990, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. ***REMOVED*** - see
15 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)db.h 8.7 (Berkeley) 6/16/94
44 #include <sys/types.h>
50 #ifdef __DBINTERFACE_PRIVATE
52 #ifdef HAVE_SYS_CDEFS_H
53 #include <sys/cdefs.h>
58 #ifdef HAVE_SYS_BYTEORDER_H
59 #include <sys/byteorder.h>
62 #if defined(__linux) || defined(__BEOS__)
65 #define BYTE_ORDER __BYTE_ORDER
66 #define BIG_ENDIAN __BIG_ENDIAN
67 #define LITTLE_ENDIAN __LITTLE_ENDIAN
72 #define BYTE_ORDER BIG_ENDIAN
73 #define BIG_ENDIAN 4321
74 #define LITTLE_ENDIAN 1234 /* LSB first: i386, vax, all NT risc */
78 #define BIG_ENDIAN 4321
79 #define LITTLE_ENDIAN 1234 /* LSB first: i386, vax, all NT risc */
82 /* compat.h is only in 4.1.3 machines. - dp */
87 * Need to find a general way of defining endian-ness in SunOS 5.3
88 * SunOS 5.4 defines _BIG_ENDIAN and _LITTLE_ENDIAN
89 * SunOS 5.3 does nothing like this.
94 #if defined(_BIG_ENDIAN)
95 #define BYTE_ORDER BIG_ENDIAN
96 #elif defined(_LITTLE_ENDIAN)
97 #define BYTE_ORDER LITTLE_ENDIAN
98 #elif !defined(__SVR4)
99 /* 4.1.3 is always BIG_ENDIAN as it was released only on sparc platforms. */
100 #define BYTE_ORDER BIG_ENDIAN
101 #elif !defined(vax) && !defined(ntohl) && !defined(lint) && !defined(i386)
102 /* 5.3 big endian. Copied this above line from sys/byteorder.h */
103 /* Now we are in a 5.3 SunOS rather non 5.4 or above SunOS */
104 #define BYTE_ORDER BIG_ENDIAN
106 #define BYTE_ORDER LITTLE_ENDIAN
109 #endif /* !BYTE_ORDER */
112 #if defined(__hpux) || defined(__hppa)
113 #define BYTE_ORDER BIG_ENDIAN
114 #define BIG_ENDIAN 4321
115 #define LITTLE_ENDIAN 1234 /* LSB first: i386, vax, all NT risc */
118 #if defined(AIXV3) || defined(AIX)
119 /* BYTE_ORDER, LITTLE_ENDIAN, BIG_ENDIAN are all defined here */
120 #include <sys/machine.h>
125 #include <machine/endian.h>
132 #define BYTE_ORDER LITTLE_ENDIAN
133 #define BIG_ENDIAN 4321
134 #define LITTLE_ENDIAN 1234
139 #include <sys/endian.h>
144 #include <sys/param.h>
146 #define LITTLE_ENDIAN 1234
147 #define BIG_ENDIAN 4321
148 #define BYTE_ORDER LITTLE_ENDIAN
153 /* #include <sys/hetero.h> */
154 #define BYTE_ORDER BIG_ENDIAN
155 #define BIG_ENDIAN 4321
156 #define LITTLE_ENDIAN 1234
164 #define BYTE_ORDER LITTLE_ENDIAN
165 #define LITTLE_ENDIAN 1234 /* LSB first: i386, vax, all NT risc */
166 #define BIG_ENDIAN 4321
170 #define BIG_ENDIAN 4321
171 #define LITTLE_ENDIAN 1234
172 #define BYTE_ORDER BIG_ENDIAN
175 #endif /* __DBINTERFACE_PRIVATE */
178 #define MAXPATHLEN 1024
183 #if defined(_WINDOWS) || defined(XP_OS2)
188 #define MAXPATHLEN 1024
191 #define EFTYPE EINVAL /* POSIX 1003.1 format errno. */
193 #ifndef STDERR_FILENO
194 #define STDIN_FILENO 0 /* ANSI C #defines */
195 #define STDOUT_FILENO 1
196 #define STDERR_FILENO 2
199 #ifndef O_ACCMODE /* POSIX 1003.1 access mode mask. */
200 #define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR)
207 #define O_ACCMODE 3 /* Mask for file access modes */
210 int mkstemp(const char *path);
212 #endif /* MACINTOSH */
214 #if !defined(_WINDOWS) && !defined(macintosh)
215 #include <sys/stat.h>
219 /* define EFTYPE since most don't */
221 #define EFTYPE EINVAL /* POSIX 1003.1 format errno. */
224 #define RET_ERROR -1 /* Return values. */
225 #define RET_SUCCESS 0
226 #define RET_SPECIAL 1
228 #define MAX_PAGE_NUMBER 0xffffffff /* >= # of pages in a file */
231 typedef uint32 pgno_t;
234 #define MAX_PAGE_OFFSET 65535 /* >= # of bytes in a page */
235 typedef uint16 indx_t;
236 #define MAX_REC_NUMBER 0xffffffff /* >= # of records in a tree */
237 typedef uint32 recno_t;
239 /* Key/data structure -- a Data-Base Thang. */
241 void *data; /* data */
242 size_t size; /* data length */
246 #define R_CURSOR 1 /* del, put, seq */
247 #define __R_UNUSED 2 /* UNUSED */
248 #define R_FIRST 3 /* seq */
249 #define R_IAFTER 4 /* put (RECNO) */
250 #define R_IBEFORE 5 /* put (RECNO) */
251 #define R_LAST 6 /* seq (BTREE, RECNO) */
252 #define R_NEXT 7 /* seq */
253 #define R_NOOVERWRITE 8 /* put */
254 #define R_PREV 9 /* seq (BTREE, RECNO) */
255 #define R_SETCURSOR 10 /* put (RECNO) */
256 #define R_RECNOSYNC 11 /* sync (RECNO) */
258 typedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE;
260 typedef enum { LockOutDatabase, UnlockDatabase } DBLockFlagEnum;
264 * The following flags are included in the dbopen(3) call as part of the
265 * open(2) flags. In order to avoid conflicts with the open flags, start
266 * at the top of the 16 or 32-bit number space and work our way down. If
267 * the open flags were significantly expanded in the future, it could be
268 * a problem. Wish I'd left another flags word in the dbopen call.
271 * None of this stuff is implemented yet. The only reason that it's here
272 * is so that the access methods can skip copying the key/data pair when
273 * the DB_LOCK flag isn't set.
276 #define DB_LOCK 0x20000000 /* Do locking. */
277 #define DB_SHMEM 0x40000000 /* Use shared memory. */
278 #define DB_TXN 0x80000000 /* Do transactions. */
280 #define DB_LOCK 0x2000 /* Do locking. */
281 #define DB_SHMEM 0x4000 /* Use shared memory. */
282 #define DB_TXN 0x8000 /* Do transactions. */
285 /* Access method description structure. */
286 typedef struct __db {
287 DBTYPE type; /* Underlying db type. */
288 int (*close) (struct __db *);
289 int (*del) (const struct __db *, const DBT *, uint);
290 int (*get) (const struct __db *, const DBT *, DBT *, uint);
291 int (*put) (const struct __db *, DBT *, const DBT *, uint);
292 int (*seq) (const struct __db *, DBT *, DBT *, uint);
293 int (*sync) (const struct __db *, uint);
294 void *internal; /* Access method private. */
295 int (*fd) (const struct __db *);
298 #define BTREEMAGIC 0x053162
299 #define BTREEVERSION 3
301 /* Structure used to pass parameters to the btree routines. */
303 #define R_DUP 0x01 /* duplicate keys */
305 uint cachesize; /* bytes to cache */
306 int maxkeypage; /* maximum keys per page */
307 int minkeypage; /* minimum keys per page */
308 uint psize; /* page size */
309 int (*compare) /* comparison function */
310 (const DBT *, const DBT *);
311 size_t (*prefix) /* prefix function */
312 (const DBT *, const DBT *);
313 int lorder; /* byte order */
316 #define HASHMAGIC 0x061561
317 #define HASHVERSION 2
319 /* Structure used to pass parameters to the hashing routines. */
321 uint bsize; /* bucket size */
322 uint ffactor; /* fill factor */
323 uint nelem; /* number of elements */
324 uint cachesize; /* bytes to cache */
325 uint32 /* hash function */
326 (*hash) (const void *, size_t);
327 int lorder; /* byte order */
330 /* Structure used to pass parameters to the record routines. */
332 #define R_FIXEDLEN 0x01 /* fixed-length records */
333 #define R_NOKEY 0x02 /* key not required */
334 #define R_SNAPSHOT 0x04 /* snapshot the input */
336 uint cachesize; /* bytes to cache */
337 uint psize; /* page size */
338 int lorder; /* byte order */
339 size_t reclen; /* record length (fixed-length records) */
340 uint8 bval; /* delimiting byte (variable-length records */
341 char *bfname; /* btree file name */
344 #ifdef __DBINTERFACE_PRIVATE
346 * Little endian <==> big endian 32-bit swap macros.
347 * M_32_SWAP swap a memory location
348 * P_32_SWAP swap a referenced memory location
349 * P_32_COPY swap from one location to another
351 #define M_32_SWAP(a) { \
353 ((char *)&a)[0] = ((char *)&_tmp)[3]; \
354 ((char *)&a)[1] = ((char *)&_tmp)[2]; \
355 ((char *)&a)[2] = ((char *)&_tmp)[1]; \
356 ((char *)&a)[3] = ((char *)&_tmp)[0]; \
358 #define P_32_SWAP(a) { \
359 uint32 _tmp = *(uint32 *)a; \
360 ((char *)a)[0] = ((char *)&_tmp)[3]; \
361 ((char *)a)[1] = ((char *)&_tmp)[2]; \
362 ((char *)a)[2] = ((char *)&_tmp)[1]; \
363 ((char *)a)[3] = ((char *)&_tmp)[0]; \
365 #define P_32_COPY(a, b) { \
366 ((char *)&(b))[0] = ((char *)&(a))[3]; \
367 ((char *)&(b))[1] = ((char *)&(a))[2]; \
368 ((char *)&(b))[2] = ((char *)&(a))[1]; \
369 ((char *)&(b))[3] = ((char *)&(a))[0]; \
373 * Little endian <==> big endian 16-bit swap macros.
374 * M_16_SWAP swap a memory location
375 * P_16_SWAP swap a referenced memory location
376 * P_16_COPY swap from one location to another
378 #define M_16_SWAP(a) { \
380 ((char *)&a)[0] = ((char *)&_tmp)[1]; \
381 ((char *)&a)[1] = ((char *)&_tmp)[0]; \
383 #define P_16_SWAP(a) { \
384 uint16 _tmp = *(uint16 *)a; \
385 ((char *)a)[0] = ((char *)&_tmp)[1]; \
386 ((char *)a)[1] = ((char *)&_tmp)[0]; \
388 #define P_16_COPY(a, b) { \
389 ((char *)&(b))[0] = ((char *)&(a))[1]; \
390 ((char *)&(b))[1] = ((char *)&(a))[0]; \
397 dbopen (const char *, int, int, DBTYPE, const void *);
399 /* set or unset a global lock flag to disable the
400 * opening of any DBM file
402 void dbSetOrClearDBLock(DBLockFlagEnum type);
404 #ifdef __DBINTERFACE_PRIVATE
405 DB *__bt_open (const char *, int, int, const BTREEINFO *, int);
406 DB *__hash_open (const char *, int, int, const HASHINFO *, int);
407 DB *__rec_open (const char *, int, int, const RECNOINFO *, int);
408 void __dbpanic (DB *dbp);