xref: /original-bsd/include/db.h (revision f737e041)
1 /*-
2  * Copyright (c) 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)db.h	8.4 (Berkeley) 02/21/94
8  */
9 
10 #ifndef _DB_H_
11 #define	_DB_H_
12 
13 #include <sys/types.h>
14 #include <sys/cdefs.h>
15 
16 #include <limits.h>
17 
18 #define	RET_ERROR	-1		/* Return values. */
19 #define	RET_SUCCESS	 0
20 #define	RET_SPECIAL	 1
21 
22 #define	MAX_PAGE_NUMBER	0xffffffff	/* >= # of pages in a file */
23 typedef u_int32_t	pgno_t;
24 #define	MAX_PAGE_OFFSET	65535		/* >= # of bytes in a page */
25 typedef u_int16_t	indx_t;
26 #define	MAX_REC_NUMBER	0xffffffff	/* >= # of records in a tree */
27 typedef u_int32_t	recno_t;
28 
29 /* Key/data structure -- a Data-Base Thang. */
30 typedef struct {
31 	void	*data;			/* data */
32 	size_t	 size;			/* data length */
33 } DBT;
34 
35 /* Routine flags. */
36 #define	R_CURSOR	1		/* del, put, seq */
37 #define	__R_UNUSED	2		/* UNUSED */
38 #define	R_FIRST		3		/* seq */
39 #define	R_IAFTER	4		/* put (RECNO) */
40 #define	R_IBEFORE	5		/* put (RECNO) */
41 #define	R_LAST		6		/* seq (BTREE, RECNO) */
42 #define	R_NEXT		7		/* seq */
43 #define	R_NOOVERWRITE	8		/* put */
44 #define	R_PREV		9		/* seq (BTREE, RECNO) */
45 #define	R_SETCURSOR	10		/* put (RECNO) */
46 #define	R_RECNOSYNC	11		/* sync (RECNO) */
47 
48 typedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE;
49 
50 /*
51  * !!!
52  * The following flags are included in the dbopen(3) call as part of the
53  * open(2) flags.  In order to avoid conflicts with the open flags, start
54  * at the top of the 16 or 32-bit number space and work our way down.  If
55  * the open flags were significantly expanded in the future, it could be
56  * a problem.  Wish I'd left another flags word in the dbopen call.
57  *
58  * !!!
59  * None of this stuff is implemented yet.  The only reason that it's here
60  * is so that the access methods can skip copying the key/data pair when
61  * the DB_LOCK flag isn't set.
62  */
63 #if UINT_MAX > 65535
64 #define	DB_LOCK		0x20000000	/* Do locking. */
65 #define	DB_SHMEM	0x40000000	/* Use shared memory. */
66 #define	DB_TXN		0x80000000	/* Do transactions. */
67 #else
68 #define	DB_LOCK		    0x2000	/* Do locking. */
69 #define	DB_SHMEM	    0x4000	/* Use shared memory. */
70 #define	DB_TXN		    0x8000	/* Do transactions. */
71 #endif
72 
73 /* Access method description structure. */
74 typedef struct __db {
75 	DBTYPE type;			/* Underlying db type. */
76 	int (*close)	__P((struct __db *));
77 	int (*del)	__P((const struct __db *, const DBT *, u_int));
78 	int (*get)	__P((const struct __db *, const DBT *, DBT *, u_int));
79 	int (*put)	__P((const struct __db *, DBT *, const DBT *, u_int));
80 	int (*seq)	__P((const struct __db *, DBT *, DBT *, u_int));
81 	int (*sync)	__P((const struct __db *, u_int));
82 	void *internal;			/* Access method private. */
83 	int (*fd)	__P((const struct __db *));
84 } DB;
85 
86 #define	BTREEMAGIC	0x053162
87 #define	BTREEVERSION	3
88 
89 /* Structure used to pass parameters to the btree routines. */
90 typedef struct {
91 #define	R_DUP		0x01	/* duplicate keys */
92 	u_long	flags;
93 	u_int	cachesize;	/* bytes to cache */
94 	int	maxkeypage;	/* maximum keys per page */
95 	int	minkeypage;	/* minimum keys per page */
96 	u_int	psize;		/* page size */
97 	int	(*compare)	/* comparison function */
98 	    __P((const DBT *, const DBT *));
99 	size_t	(*prefix)	/* prefix function */
100 	    __P((const DBT *, const DBT *));
101 	int	lorder;		/* byte order */
102 } BTREEINFO;
103 
104 #define	HASHMAGIC	0x061561
105 #define	HASHVERSION	2
106 
107 /* Structure used to pass parameters to the hashing routines. */
108 typedef struct {
109 	u_int	bsize;		/* bucket size */
110 	u_int	ffactor;	/* fill factor */
111 	u_int	nelem;		/* number of elements */
112 	u_int	cachesize;	/* bytes to cache */
113 	u_int32_t		/* hash function */
114 		(*hash) __P((const void *, size_t));
115 	int	lorder;		/* byte order */
116 } HASHINFO;
117 
118 /* Structure used to pass parameters to the record routines. */
119 typedef struct {
120 #define	R_FIXEDLEN	0x01	/* fixed-length records */
121 #define	R_NOKEY		0x02	/* key not required */
122 #define	R_SNAPSHOT	0x04	/* snapshot the input */
123 	u_long	flags;
124 	u_int	cachesize;	/* bytes to cache */
125 	u_int	psize;		/* page size */
126 	int	lorder;		/* byte order */
127 	size_t	reclen;		/* record length (fixed-length records) */
128 	u_char	bval;		/* delimiting byte (variable-length records */
129 	char	*bfname;	/* btree file name */
130 } RECNOINFO;
131 
132 #ifdef __DBINTERFACE_PRIVATE
133 /*
134  * Little endian <==> big endian 32-bit swap macros.
135  *	M_32_SWAP	swap a memory location
136  *	P_32_SWAP	swap a referenced memory location
137  *	P_32_COPY	swap from one location to another
138  */
139 #define	M_32_SWAP(a) {							\
140 	u_int32_t _tmp = a;						\
141 	((char *)&a)[0] = ((char *)&_tmp)[3];				\
142 	((char *)&a)[1] = ((char *)&_tmp)[2];				\
143 	((char *)&a)[2] = ((char *)&_tmp)[1];				\
144 	((char *)&a)[3] = ((char *)&_tmp)[0];				\
145 }
146 #define	P_32_SWAP(a) {							\
147 	u_int32_t _tmp = *(u_int32_t *)a;				\
148 	((char *)a)[0] = ((char *)&_tmp)[3];				\
149 	((char *)a)[1] = ((char *)&_tmp)[2];				\
150 	((char *)a)[2] = ((char *)&_tmp)[1];				\
151 	((char *)a)[3] = ((char *)&_tmp)[0];				\
152 }
153 #define	P_32_COPY(a, b) {						\
154 	((char *)&(b))[0] = ((char *)&(a))[3];				\
155 	((char *)&(b))[1] = ((char *)&(a))[2];				\
156 	((char *)&(b))[2] = ((char *)&(a))[1];				\
157 	((char *)&(b))[3] = ((char *)&(a))[0];				\
158 }
159 
160 /*
161  * Little endian <==> big endian 16-bit swap macros.
162  *	M_16_SWAP	swap a memory location
163  *	P_16_SWAP	swap a referenced memory location
164  *	P_16_COPY	swap from one location to another
165  */
166 #define	M_16_SWAP(a) {							\
167 	u_int16_t _tmp = a;						\
168 	((char *)&a)[0] = ((char *)&_tmp)[1];				\
169 	((char *)&a)[1] = ((char *)&_tmp)[0];				\
170 }
171 #define	P_16_SWAP(a) {							\
172 	u_int16_t _tmp = *(u_int16_t *)a;				\
173 	((char *)a)[0] = ((char *)&_tmp)[1];				\
174 	((char *)a)[1] = ((char *)&_tmp)[0];				\
175 }
176 #define	P_16_COPY(a, b) {						\
177 	((char *)&(b))[0] = ((char *)&(a))[1];				\
178 	((char *)&(b))[1] = ((char *)&(a))[0];				\
179 }
180 #endif
181 
182 __BEGIN_DECLS
183 DB *dbopen __P((const char *, int, int, DBTYPE, const void *));
184 
185 #ifdef __DBINTERFACE_PRIVATE
186 DB	*__bt_open __P((const char *, int, int, const BTREEINFO *, int));
187 DB	*__hash_open __P((const char *, int, int, const HASHINFO *, int));
188 DB	*__rec_open __P((const char *, int, int, const RECNOINFO *, int));
189 void	 __dbpanic __P((DB *dbp));
190 #endif
191 __END_DECLS
192 #endif /* !_DB_H_ */
193