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