xref: /netbsd-src/include/db.h (revision cda4f8f6ee55684e8d311b86c99ea59191e6b74f)
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  *	@(#)db.h	8.1 (Berkeley) 6/2/93
34  */
35 
36 #ifndef _DB_H_
37 #define	_DB_H_
38 
39 #include <sys/types.h>
40 #include <sys/cdefs.h>
41 #include <machine/endian.h>
42 
43 #define	RET_ERROR	-1		/* Return values. */
44 #define	RET_SUCCESS	 0
45 #define	RET_SPECIAL	 1
46 
47 #define	MAX_PAGE_NUMBER	ULONG_MAX	/* >= # of pages in a file */
48 typedef u_long	pgno_t;
49 #define	MAX_PAGE_OFFSET	USHRT_MAX	/* >= # of bytes in a page */
50 typedef u_short	indx_t;
51 #define	MAX_REC_NUMBER	ULONG_MAX	/* >= # of records in a tree */
52 typedef u_long	recno_t;
53 
54 /* Key/data structure -- a Data-Base Thang. */
55 typedef struct {
56 	void	*data;			/* data */
57 	size_t	 size;			/* data length */
58 } DBT;
59 
60 /* Routine flags. */
61 #define	R_CURSOR	1		/* del, put, seq */
62 #define	__R_UNUSED	2		/* UNUSED */
63 #define	R_FIRST		3		/* seq */
64 #define	R_IAFTER	4		/* put (RECNO) */
65 #define	R_IBEFORE	5		/* put (RECNO) */
66 #define	R_LAST		6		/* seq (BTREE, RECNO) */
67 #define	R_NEXT		7		/* seq */
68 #define	R_NOOVERWRITE	8		/* put */
69 #define	R_PREV		9		/* seq (BTREE, RECNO) */
70 #define	R_SETCURSOR	10		/* put (RECNO) */
71 #define	R_RECNOSYNC	11		/* sync (RECNO) */
72 
73 typedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE;
74 
75 #define	__USE_OPEN_FLAGS \
76 	(O_CREAT|O_EXCL|O_EXLOCK|O_RDONLY|O_RDWR|O_SHLOCK|O_TRUNC)
77 
78 /* Access method description structure. */
79 typedef struct __db {
80 	DBTYPE type;			/* underlying db type */
81 	int (*close)	__P((struct __db *));
82 	int (*del)	__P((const struct __db *, const DBT *, u_int));
83 	int (*fd)	__P((const struct __db *));
84 	int (*get)	__P((const struct __db *, const DBT *, DBT *, u_int));
85 	int (*put)	__P((const struct __db *, DBT *, const DBT *, u_int));
86 	int (*seq)	__P((const struct __db *, DBT *, DBT *, u_int));
87 	int (*sync)	__P((const struct __db *, u_int));
88 	void *internal;			/* access method private */
89 } DB;
90 
91 #define	BTREEMAGIC	0x053162
92 #define	BTREEVERSION	3
93 
94 /* Structure used to pass parameters to the btree routines. */
95 typedef struct {
96 #define	R_DUP		0x01	/* duplicate keys */
97 	u_long	 flags;
98 	int	 cachesize;	/* bytes to cache */
99 	int	 maxkeypage;	/* maximum keys per page */
100 	int	 minkeypage;	/* minimum keys per page */
101 	int	 psize;		/* page size */
102 				/* comparison, prefix functions */
103 	int	 (*compare)	__P((const DBT *, const DBT *));
104 	int	 (*prefix)	__P((const DBT *, const DBT *));
105 	int	 lorder;	/* byte order */
106 } BTREEINFO;
107 
108 #define	HASHMAGIC	0x061561
109 #define	HASHVERSION	2
110 
111 /* Structure used to pass parameters to the hashing routines. */
112 typedef struct {
113 	int	 bsize;		/* bucket size */
114 	int	 ffactor;	/* fill factor */
115 	int	 nelem;		/* number of elements */
116 	int	 cachesize;	/* bytes to cache */
117 				/* hash function */
118 	int	 (*hash) __P((const void *, size_t));
119 	int	 lorder;	/* byte order */
120 } HASHINFO;
121 
122 /* Structure used to pass parameters to the record routines. */
123 typedef struct {
124 #define	R_FIXEDLEN	0x01	/* fixed-length records */
125 #define	R_NOKEY		0x02	/* key not required */
126 #define	R_SNAPSHOT	0x04	/* snapshot the input */
127 	u_long	 flags;
128 	int	 cachesize;	/* bytes to cache */
129 	int	 psize;		/* page size */
130 	int	 lorder;	/* byte order */
131 	size_t	 reclen;	/* record length (fixed-length records) */
132 	u_char	 bval;		/* delimiting byte (variable-length records */
133 	char	*bfname;	/* btree file name */
134 } RECNOINFO;
135 
136 /*
137  * Little endian <==> big endian long swap macros.
138  *	BLSWAP		swap a memory location
139  *	BLPSWAP		swap a referenced memory location
140  *	BLSWAP_COPY	swap from one location to another
141  */
142 #define BLSWAP(a) { \
143 	u_long _tmp = a; \
144 	((char *)&a)[0] = ((char *)&_tmp)[3]; \
145 	((char *)&a)[1] = ((char *)&_tmp)[2]; \
146 	((char *)&a)[2] = ((char *)&_tmp)[1]; \
147 	((char *)&a)[3] = ((char *)&_tmp)[0]; \
148 }
149 #define	BLPSWAP(a) { \
150 	u_long _tmp = *(u_long *)a; \
151 	((char *)a)[0] = ((char *)&_tmp)[3]; \
152 	((char *)a)[1] = ((char *)&_tmp)[2]; \
153 	((char *)a)[2] = ((char *)&_tmp)[1]; \
154 	((char *)a)[3] = ((char *)&_tmp)[0]; \
155 }
156 #define	BLSWAP_COPY(a, b) { \
157 	((char *)&(b))[0] = ((char *)&(a))[3]; \
158 	((char *)&(b))[1] = ((char *)&(a))[2]; \
159 	((char *)&(b))[2] = ((char *)&(a))[1]; \
160 	((char *)&(b))[3] = ((char *)&(a))[0]; \
161 }
162 
163 /*
164  * Little endian <==> big endian short swap macros.
165  *	BSSWAP		swap a memory location
166  *	BSPSWAP		swap a referenced memory location
167  *	BSSWAP_COPY	swap from one location to another
168  */
169 #define BSSWAP(a) { \
170 	u_short _tmp = a; \
171 	((char *)&a)[0] = ((char *)&_tmp)[1]; \
172 	((char *)&a)[1] = ((char *)&_tmp)[0]; \
173 }
174 #define BSPSWAP(a) { \
175 	u_short _tmp = *(u_short *)a; \
176 	((char *)a)[0] = ((char *)&_tmp)[1]; \
177 	((char *)a)[1] = ((char *)&_tmp)[0]; \
178 }
179 #define BSSWAP_COPY(a, b) { \
180 	((char *)&(b))[0] = ((char *)&(a))[1]; \
181 	((char *)&(b))[1] = ((char *)&(a))[0]; \
182 }
183 
184 __BEGIN_DECLS
185 DB *dbopen __P((const char *, int, int, DBTYPE, const void *));
186 
187 #ifdef __DBINTERFACE_PRIVATE
188 DB	*__bt_open __P((const char *, int, int, const BTREEINFO *));
189 DB	*__hash_open __P((const char *, int, int, const HASHINFO *));
190 DB	*__rec_open __P((const char *, int, int, const RECNOINFO *));
191 void	 __dbpanic __P((DB *dbp));
192 #endif
193 __END_DECLS
194 #endif /* !_DB_H_ */
195