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