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
159 #if defined(_WINDOWS) || defined(XP_OS2_VACPP)
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
193 #define MAXPATHLEN CCHMAXPATH
195 #define ENOTDIR EBADPOS
196 #define S_ISDIR(s) ((s) & S_IFDIR)
199 #define EFTYPE EINVAL /* POSIX 1003.1 format errno. */
201 #ifndef STDERR_FILENO
202 #define STDIN_FILENO 0 /* ANSI C #defines */
203 #define STDOUT_FILENO 1
204 #define STDERR_FILENO 2
207 #ifndef O_ACCMODE /* POSIX 1003.1 access mode mask. */
208 #define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR)
215 #define O_ACCMODE 3 /* Mask for file access modes */
218 int mkstemp(const char *path
);
220 #endif /* MACINTOSH */
222 #if !defined(_WINDOWS) && !defined(macintosh)
223 #include <sys/stat.h>
227 /* define EFTYPE since most don't */
229 #define EFTYPE EINVAL /* POSIX 1003.1 format errno. */
232 #define RET_ERROR -1 /* Return values. */
233 #define RET_SUCCESS 0
234 #define RET_SPECIAL 1
236 #define MAX_PAGE_NUMBER 0xffffffff /* >= # of pages in a file */
239 typedef uint32 pgno_t
;
242 #define MAX_PAGE_OFFSET 65535 /* >= # of bytes in a page */
243 typedef uint16 indx_t
;
244 #define MAX_REC_NUMBER 0xffffffff /* >= # of records in a tree */
245 typedef uint32 recno_t
;
247 /* Key/data structure -- a Data-Base Thang. */
249 void *data
; /* data */
250 size_t size
; /* data length */
254 #define R_CURSOR 1 /* del, put, seq */
255 #define __R_UNUSED 2 /* UNUSED */
256 #define R_FIRST 3 /* seq */
257 #define R_IAFTER 4 /* put (RECNO) */
258 #define R_IBEFORE 5 /* put (RECNO) */
259 #define R_LAST 6 /* seq (BTREE, RECNO) */
260 #define R_NEXT 7 /* seq */
261 #define R_NOOVERWRITE 8 /* put */
262 #define R_PREV 9 /* seq (BTREE, RECNO) */
263 #define R_SETCURSOR 10 /* put (RECNO) */
264 #define R_RECNOSYNC 11 /* sync (RECNO) */
266 typedef enum { DB_BTREE
, DB_HASH
, DB_RECNO
} DBTYPE
;
268 typedef enum { LockOutDatabase
, UnlockDatabase
} DBLockFlagEnum
;
272 * The following flags are included in the dbopen(3) call as part of the
273 * open(2) flags. In order to avoid conflicts with the open flags, start
274 * at the top of the 16 or 32-bit number space and work our way down. If
275 * the open flags were significantly expanded in the future, it could be
276 * a problem. Wish I'd left another flags word in the dbopen call.
279 * None of this stuff is implemented yet. The only reason that it's here
280 * is so that the access methods can skip copying the key/data pair when
281 * the DB_LOCK flag isn't set.
284 #define DB_LOCK 0x20000000 /* Do locking. */
285 #define DB_SHMEM 0x40000000 /* Use shared memory. */
286 #define DB_TXN 0x80000000 /* Do transactions. */
288 #define DB_LOCK 0x2000 /* Do locking. */
289 #define DB_SHMEM 0x4000 /* Use shared memory. */
290 #define DB_TXN 0x8000 /* Do transactions. */
293 /* Access method description structure. */
294 typedef struct __db
{
295 DBTYPE type
; /* Underlying db type. */
296 int (*close
) (struct __db
*);
297 int (*del
) (const struct __db
*, const DBT
*, uint
);
298 int (*get
) (const struct __db
*, const DBT
*, DBT
*, uint
);
299 int (*put
) (const struct __db
*, DBT
*, const DBT
*, uint
);
300 int (*seq
) (const struct __db
*, DBT
*, DBT
*, uint
);
301 int (*sync
) (const struct __db
*, uint
);
302 void *internal
; /* Access method private. */
303 int (*fd
) (const struct __db
*);
306 #define BTREEMAGIC 0x053162
307 #define BTREEVERSION 3
309 /* Structure used to pass parameters to the btree routines. */
311 #define R_DUP 0x01 /* duplicate keys */
313 uint cachesize
; /* bytes to cache */
314 int maxkeypage
; /* maximum keys per page */
315 int minkeypage
; /* minimum keys per page */
316 uint psize
; /* page size */
317 int (*compare
) /* comparison function */
318 (const DBT
*, const DBT
*);
319 size_t (*prefix
) /* prefix function */
320 (const DBT
*, const DBT
*);
321 int lorder
; /* byte order */
324 #define HASHMAGIC 0x061561
325 #define HASHVERSION 2
327 /* Structure used to pass parameters to the hashing routines. */
329 uint bsize
; /* bucket size */
330 uint ffactor
; /* fill factor */
331 uint nelem
; /* number of elements */
332 uint cachesize
; /* bytes to cache */
333 uint32
/* hash function */
334 (*hash
) (const void *, size_t);
335 int lorder
; /* byte order */
338 /* Structure used to pass parameters to the record routines. */
340 #define R_FIXEDLEN 0x01 /* fixed-length records */
341 #define R_NOKEY 0x02 /* key not required */
342 #define R_SNAPSHOT 0x04 /* snapshot the input */
344 uint cachesize
; /* bytes to cache */
345 uint psize
; /* page size */
346 int lorder
; /* byte order */
347 size_t reclen
; /* record length (fixed-length records) */
348 uint8 bval
; /* delimiting byte (variable-length records */
349 char *bfname
; /* btree file name */
352 #ifdef __DBINTERFACE_PRIVATE
354 * Little endian <==> big endian 32-bit swap macros.
355 * M_32_SWAP swap a memory location
356 * P_32_SWAP swap a referenced memory location
357 * P_32_COPY swap from one location to another
359 #define M_32_SWAP(a) { \
361 ((char *)&a)[0] = ((char *)&_tmp)[3]; \
362 ((char *)&a)[1] = ((char *)&_tmp)[2]; \
363 ((char *)&a)[2] = ((char *)&_tmp)[1]; \
364 ((char *)&a)[3] = ((char *)&_tmp)[0]; \
366 #define P_32_SWAP(a) { \
367 uint32 _tmp = *(uint32 *)a; \
368 ((char *)a)[0] = ((char *)&_tmp)[3]; \
369 ((char *)a)[1] = ((char *)&_tmp)[2]; \
370 ((char *)a)[2] = ((char *)&_tmp)[1]; \
371 ((char *)a)[3] = ((char *)&_tmp)[0]; \
373 #define P_32_COPY(a, b) { \
374 ((char *)&(b))[0] = ((char *)&(a))[3]; \
375 ((char *)&(b))[1] = ((char *)&(a))[2]; \
376 ((char *)&(b))[2] = ((char *)&(a))[1]; \
377 ((char *)&(b))[3] = ((char *)&(a))[0]; \
381 * Little endian <==> big endian 16-bit swap macros.
382 * M_16_SWAP swap a memory location
383 * P_16_SWAP swap a referenced memory location
384 * P_16_COPY swap from one location to another
386 #define M_16_SWAP(a) { \
388 ((char *)&a)[0] = ((char *)&_tmp)[1]; \
389 ((char *)&a)[1] = ((char *)&_tmp)[0]; \
391 #define P_16_SWAP(a) { \
392 uint16 _tmp = *(uint16 *)a; \
393 ((char *)a)[0] = ((char *)&_tmp)[1]; \
394 ((char *)a)[1] = ((char *)&_tmp)[0]; \
396 #define P_16_COPY(a, b) { \
397 ((char *)&(b))[0] = ((char *)&(a))[1]; \
398 ((char *)&(b))[1] = ((char *)&(a))[0]; \
403 #if defined(__WATCOMC__) || defined(__WATCOM_CPLUSPLUS__)
408 dbopen (const char *, int, int, DBTYPE
, const void *);
410 /* set or unset a global lock flag to disable the
411 * opening of any DBM file
413 void dbSetOrClearDBLock(DBLockFlagEnum type
);
415 #ifdef __DBINTERFACE_PRIVATE
416 DB
*__bt_open (const char *, int, int, const BTREEINFO
*, int);
417 DB
*__hash_open (const char *, int, int, const HASHINFO
*, int);
418 DB
*__rec_open (const char *, int, int, const RECNOINFO
*, int);
419 void __dbpanic (DB
*dbp
);