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