xref: /netbsd-src/include/db.h (revision 0ac50079c4d1891d1c2a50f1c45806b97438bc6c)
1*0ac50079Schristos /*	$NetBSD: db.h,v 1.27 2016/09/24 20:11:43 christos Exp $	*/
24d2cbfceScgd 
361f28255Scgd /*-
4ff937b6eScgd  * Copyright (c) 1990, 1993, 1994
570a04c5aScgd  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
15039cc956Sagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  *
31ff937b6eScgd  *	@(#)db.h	8.7 (Berkeley) 6/16/94
3261f28255Scgd  */
3361f28255Scgd 
3461f28255Scgd #ifndef _DB_H_
3561f28255Scgd #define	_DB_H_
3661f28255Scgd 
37e75cc16eSproven #include <sys/types.h>
3861f28255Scgd #include <sys/cdefs.h>
3945e27c80Scgd 
4045e27c80Scgd #include <limits.h>
4161f28255Scgd 
42e75cc16eSproven #define	RET_ERROR	-1		/* Return values. */
43e75cc16eSproven #define	RET_SUCCESS	 0
44e75cc16eSproven #define	RET_SPECIAL	 1
4561f28255Scgd 
46ff937b6eScgd #define	MAX_PAGE_NUMBER	0xffffffff	/* >= # of pages in a file */
475f65228bSperry typedef uint32_t	pgno_t;
48ff937b6eScgd #define	MAX_PAGE_OFFSET	65535		/* >= # of bytes in a page */
495f65228bSperry typedef uint16_t	indx_t;
50ff937b6eScgd #define	MAX_REC_NUMBER	0xffffffff	/* >= # of records in a tree */
515f65228bSperry typedef uint32_t	recno_t;
52e75cc16eSproven 
53e75cc16eSproven /* Key/data structure -- a Data-Base Thang. */
5461f28255Scgd typedef struct {
55e75cc16eSproven 	void	*data;			/* data */
56e75cc16eSproven 	size_t	 size;			/* data length */
5761f28255Scgd } DBT;
5861f28255Scgd 
59e75cc16eSproven /* Routine flags. */
60e75cc16eSproven #define	R_CURSOR	1		/* del, put, seq */
617a241404Scgd #define	__R_UNUSED	2		/* UNUSED */
62e75cc16eSproven #define	R_FIRST		3		/* seq */
63e75cc16eSproven #define	R_IAFTER	4		/* put (RECNO) */
64e75cc16eSproven #define	R_IBEFORE	5		/* put (RECNO) */
65e75cc16eSproven #define	R_LAST		6		/* seq (BTREE, RECNO) */
66e75cc16eSproven #define	R_NEXT		7		/* seq */
67e75cc16eSproven #define	R_NOOVERWRITE	8		/* put */
68e75cc16eSproven #define	R_PREV		9		/* seq (BTREE, RECNO) */
69e75cc16eSproven #define	R_SETCURSOR	10		/* put (RECNO) */
707a241404Scgd #define	R_RECNOSYNC	11		/* sync (RECNO) */
71e75cc16eSproven 
72*0ac50079Schristos /*
73*0ac50079Schristos  * Recursive sequential scan.
74*0ac50079Schristos  *
75*0ac50079Schristos  * This avoids using sibling pointers, permitting (possibly partial)
76*0ac50079Schristos  * recovery from some kinds of btree corruption.  Start a sequential
77*0ac50079Schristos  * scan as usual, but use R_RNEXT or R_RPREV to move forward or
78*0ac50079Schristos  * backward.
79*0ac50079Schristos  *
80*0ac50079Schristos  * This probably doesn't work with btrees that allow duplicate keys.
81*0ac50079Schristos  * Database modifications during the scan can also modify the parent
82*0ac50079Schristos  * page stack needed for correct functioning.  Intermixing
83*0ac50079Schristos  * non-recursive traversal by using R_NEXT or R_PREV can also make the
84*0ac50079Schristos  * page stack inconsistent with the cursor and cause problems.
85*0ac50079Schristos  */
86*0ac50079Schristos #define R_RNEXT		128		/* seq (BTREE, RECNO) */
87*0ac50079Schristos #define R_RPREV		129		/* seq (BTREE, RECNO) */
88*0ac50079Schristos 
89e75cc16eSproven typedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE;
90e75cc16eSproven 
9145e27c80Scgd /*
9245e27c80Scgd  * !!!
9345e27c80Scgd  * The following flags are included in the dbopen(3) call as part of the
9445e27c80Scgd  * open(2) flags.  In order to avoid conflicts with the open flags, start
9545e27c80Scgd  * at the top of the 16 or 32-bit number space and work our way down.  If
9645e27c80Scgd  * the open flags were significantly expanded in the future, it could be
9745e27c80Scgd  * a problem.  Wish I'd left another flags word in the dbopen call.
9845e27c80Scgd  *
9945e27c80Scgd  * !!!
10045e27c80Scgd  * None of this stuff is implemented yet.  The only reason that it's here
10145e27c80Scgd  * is so that the access methods can skip copying the key/data pair when
10245e27c80Scgd  * the DB_LOCK flag isn't set.
10345e27c80Scgd  */
10445e27c80Scgd #if UINT_MAX > 65535
10545e27c80Scgd #define	DB_LOCK		0x20000000	/* Do locking. */
10645e27c80Scgd #define	DB_SHMEM	0x40000000	/* Use shared memory. */
10745e27c80Scgd #define	DB_TXN		0x80000000	/* Do transactions. */
10845e27c80Scgd #else
109ff937b6eScgd #define	DB_LOCK		    0x2000	/* Do locking. */
110ff937b6eScgd #define	DB_SHMEM	    0x4000	/* Use shared memory. */
111ff937b6eScgd #define	DB_TXN		    0x8000	/* Do transactions. */
11245e27c80Scgd #endif
113e75cc16eSproven 
114e75cc16eSproven /* Access method description structure. */
11561f28255Scgd typedef struct __db {
11645e27c80Scgd 	DBTYPE type;			/* Underlying db type. */
11719b7469aSperry 	int (*close)	(struct __db *);
11840b37a3bSjoerg 	int (*del)	(const struct __db *, const DBT *, unsigned int);
11940b37a3bSjoerg 	int (*get)	(const struct __db *, const DBT *, DBT *, unsigned int);
12040b37a3bSjoerg 	int (*put)	(const struct __db *, DBT *, const DBT *, unsigned int);
12140b37a3bSjoerg 	int (*seq)	(const struct __db *, DBT *, DBT *, unsigned int);
12240b37a3bSjoerg 	int (*sync)	(const struct __db *, unsigned int);
12332661c1fScgd 	void *internal;			/* Access method private. */
12419b7469aSperry 	int (*fd)	(const struct __db *);
12561f28255Scgd } DB;
12661f28255Scgd 
12761f28255Scgd #define	BTREEMAGIC	0x053162
128e75cc16eSproven #define	BTREEVERSION	3
12961f28255Scgd 
130e75cc16eSproven /* Structure used to pass parameters to the btree routines. */
13161f28255Scgd typedef struct {
13261f28255Scgd #define	R_DUP		0x01	/* duplicate keys */
13340b37a3bSjoerg 	unsigned long	flags;
13440b37a3bSjoerg 	unsigned int	cachesize;	/* bytes to cache */
135e75cc16eSproven 	int		maxkeypage;	/* maximum keys per page */
136e75cc16eSproven 	int		minkeypage;	/* minimum keys per page */
13740b37a3bSjoerg 	unsigned int	psize;		/* page size */
138ff937b6eScgd 	int	(*compare)	/* comparison function */
13919b7469aSperry 		(const DBT *, const DBT *);
140ff937b6eScgd 	size_t	(*prefix)	/* prefix function */
14119b7469aSperry 		(const DBT *, const DBT *);
14261f28255Scgd 	int	lorder;		/* byte order */
14361f28255Scgd } BTREEINFO;
14461f28255Scgd 
14561f28255Scgd #define	HASHMAGIC	0x061561
146e75cc16eSproven #define	HASHVERSION	2
14761f28255Scgd 
148e75cc16eSproven /* Structure used to pass parameters to the hashing routines. */
14961f28255Scgd typedef struct {
15040b37a3bSjoerg 	unsigned int	bsize;		/* bucket size */
15140b37a3bSjoerg 	unsigned int	ffactor;	/* fill factor */
15240b37a3bSjoerg 	unsigned int	nelem;		/* number of elements */
15340b37a3bSjoerg 	unsigned int	cachesize;	/* bytes to cache */
1545f65228bSperry 	uint32_t		/* hash function */
15519b7469aSperry 		(*hash)(const void *, size_t);
15661f28255Scgd 	int	lorder;		/* byte order */
15761f28255Scgd } HASHINFO;
15861f28255Scgd 
159e75cc16eSproven /* Structure used to pass parameters to the record routines. */
16061f28255Scgd typedef struct {
16161f28255Scgd #define	R_FIXEDLEN	0x01	/* fixed-length records */
162e75cc16eSproven #define	R_NOKEY		0x02	/* key not required */
163e75cc16eSproven #define	R_SNAPSHOT	0x04	/* snapshot the input */
16440b37a3bSjoerg 	unsigned long	flags;
16540b37a3bSjoerg 	unsigned int	cachesize;	/* bytes to cache */
16640b37a3bSjoerg 	unsigned int	psize;		/* page size */
167e75cc16eSproven 	int		lorder;		/* byte order */
16861f28255Scgd 	size_t		reclen;		/* record length (fixed-length records) */
16940b37a3bSjoerg 	uint8_t		bval;		/* delimiting byte (variable-length records */
1707a241404Scgd 	char		*bfname;	/* btree file name */
17161f28255Scgd } RECNOINFO;
17261f28255Scgd 
173ff937b6eScgd #ifdef __DBINTERFACE_PRIVATE
174e75cc16eSproven /*
175ff937b6eScgd  * Little endian <==> big endian 32-bit swap macros.
176ff937b6eScgd  *	M_32_SWAP	swap a memory location
177ff937b6eScgd  *	P_32_SWAP	swap a referenced memory location
178ff937b6eScgd  *	P_32_COPY	swap from one location to another
179e75cc16eSproven  */
180ff937b6eScgd #define	M_32_SWAP(a) {							\
1815f65228bSperry 	uint32_t _tmp = a;						\
182714fa78bSchristos 	((char *)(void *)&a)[0] = ((char *)(void *)&_tmp)[3];		\
183714fa78bSchristos 	((char *)(void *)&a)[1] = ((char *)(void *)&_tmp)[2];		\
184714fa78bSchristos 	((char *)(void *)&a)[2] = ((char *)(void *)&_tmp)[1];		\
185714fa78bSchristos 	((char *)(void *)&a)[3] = ((char *)(void *)&_tmp)[0];		\
1867a241404Scgd }
187ff937b6eScgd #define	P_32_SWAP(a) {							\
188c6a74378Sscw 	char  _tmp[4];							\
189c6a74378Sscw 	_tmp[0] = ((char *)(void *)a)[0];				\
190c6a74378Sscw 	_tmp[1] = ((char *)(void *)a)[1];				\
191c6a74378Sscw 	_tmp[2] = ((char *)(void *)a)[2];				\
192c6a74378Sscw 	_tmp[3] = ((char *)(void *)a)[3];				\
193c6a74378Sscw 	((char *)(void *)a)[0] = _tmp[3];				\
194c6a74378Sscw 	((char *)(void *)a)[1] = _tmp[2];				\
195c6a74378Sscw 	((char *)(void *)a)[2] = _tmp[1];				\
196c6a74378Sscw 	((char *)(void *)a)[3] = _tmp[0];				\
1977a241404Scgd }
198ff937b6eScgd #define	P_32_COPY(a, b) {						\
199714fa78bSchristos 	((char *)(void *)&(b))[0] = ((char *)(void *)&(a))[3];		\
200714fa78bSchristos 	((char *)(void *)&(b))[1] = ((char *)(void *)&(a))[2];		\
201714fa78bSchristos 	((char *)(void *)&(b))[2] = ((char *)(void *)&(a))[1];		\
202714fa78bSchristos 	((char *)(void *)&(b))[3] = ((char *)(void *)&(a))[0];		\
2037a241404Scgd }
20461f28255Scgd 
205e75cc16eSproven /*
206ff937b6eScgd  * Little endian <==> big endian 16-bit swap macros.
207ff937b6eScgd  *	M_16_SWAP	swap a memory location
208ff937b6eScgd  *	P_16_SWAP	swap a referenced memory location
209ff937b6eScgd  *	P_16_COPY	swap from one location to another
210e75cc16eSproven  */
211ff937b6eScgd #define	M_16_SWAP(a) {							\
2125f65228bSperry 	uint16_t _tmp = a;						\
213714fa78bSchristos 	((char *)(void *)&a)[0] = ((char *)(void *)&_tmp)[1];		\
214714fa78bSchristos 	((char *)(void *)&a)[1] = ((char *)(void *)&_tmp)[0];		\
2157a241404Scgd }
216ff937b6eScgd #define	P_16_SWAP(a) {							\
217c6a74378Sscw 	char  _tmp[2];							\
218c6a74378Sscw 	_tmp[0] = ((char *)(void *)a)[0];				\
219c6a74378Sscw 	_tmp[1] = ((char *)(void *)a)[1];				\
220c6a74378Sscw 	((char *)(void *)a)[0] = _tmp[1];				\
221c6a74378Sscw 	((char *)(void *)a)[1] = _tmp[0];				\
2227a241404Scgd }
223ff937b6eScgd #define	P_16_COPY(a, b) {						\
224714fa78bSchristos 	((char *)(void *)&(b))[0] = ((char *)(void *)&(a))[1];		\
225714fa78bSchristos 	((char *)(void *)&(b))[1] = ((char *)(void *)&(a))[0];		\
2267a241404Scgd }
227ff937b6eScgd #endif
22861f28255Scgd 
22961f28255Scgd __BEGIN_DECLS
23019b7469aSperry DB *dbopen(const char *, int, mode_t, DBTYPE, const void *);
23161f28255Scgd 
232e75cc16eSproven #ifdef __DBINTERFACE_PRIVATE
233fc500ab7Schristos 
2341aa0fee7Schristos #define _DBFIT(a, t) _DIAGASSERT(__type_fit(t, a))
235fc500ab7Schristos 
23619b7469aSperry DB	*__bt_open(const char *, int, mode_t, const BTREEINFO *, int);
23719b7469aSperry DB	*__hash_open(const char *, int, mode_t, const HASHINFO *, int);
23819b7469aSperry DB	*__rec_open(const char *, int, mode_t, const RECNOINFO *, int);
23919b7469aSperry void	 __dbpanic(DB *);
240115f51c7Schristos struct stat;
241115f51c7Schristos int	 __dbopen(const char *, int, mode_t, struct stat *);
242115f51c7Schristos int	 __dbtemp(const char *, struct stat *);
243e75cc16eSproven #endif
244e75cc16eSproven __END_DECLS
24561f28255Scgd #endif /* !_DB_H_ */
246