xref: /csrg-svn/include/db.h (revision 66190)
145703Sbostic /*-
261068Sbostic  * Copyright (c) 1990, 1993
361068Sbostic  *	The Regents of the University of California.  All rights reserved.
445703Sbostic  *
545703Sbostic  * %sccs.include.redist.c%
645703Sbostic  *
7*66190Sbostic  *	@(#)db.h	8.4 (Berkeley) 02/21/94
845703Sbostic  */
945703Sbostic 
1046359Sbostic #ifndef _DB_H_
1146359Sbostic #define	_DB_H_
1246359Sbostic 
1353271Sralph #include <sys/types.h>
1446948Sbostic #include <sys/cdefs.h>
1546948Sbostic 
1665302Sbostic #include <limits.h>
1765302Sbostic 
1850984Sbostic #define	RET_ERROR	-1		/* Return values. */
1950984Sbostic #define	RET_SUCCESS	 0
2050984Sbostic #define	RET_SPECIAL	 1
2150984Sbostic 
22*66190Sbostic #define	MAX_PAGE_NUMBER	0xffffffff	/* >= # of pages in a file */
23*66190Sbostic typedef u_int32_t	pgno_t;
24*66190Sbostic #define	MAX_PAGE_OFFSET	65535		/* >= # of bytes in a page */
25*66190Sbostic typedef u_int16_t	indx_t;
26*66190Sbostic #define	MAX_REC_NUMBER	0xffffffff	/* >= # of records in a tree */
27*66190Sbostic typedef u_int32_t	recno_t;
2850984Sbostic 
2950984Sbostic /* Key/data structure -- a Data-Base Thang. */
3050984Sbostic typedef struct {
3150984Sbostic 	void	*data;			/* data */
3250984Sbostic 	size_t	 size;			/* data length */
3350984Sbostic } DBT;
3450984Sbostic 
3551042Sbostic /* Routine flags. */
3656716Sbostic #define	R_CURSOR	1		/* del, put, seq */
3759861Sbostic #define	__R_UNUSED	2		/* UNUSED */
3851042Sbostic #define	R_FIRST		3		/* seq */
3951042Sbostic #define	R_IAFTER	4		/* put (RECNO) */
4051042Sbostic #define	R_IBEFORE	5		/* put (RECNO) */
4151042Sbostic #define	R_LAST		6		/* seq (BTREE, RECNO) */
4251042Sbostic #define	R_NEXT		7		/* seq */
4351042Sbostic #define	R_NOOVERWRITE	8		/* put */
4451042Sbostic #define	R_PREV		9		/* seq (BTREE, RECNO) */
4556716Sbostic #define	R_SETCURSOR	10		/* put (RECNO) */
4660057Sbostic #define	R_RECNOSYNC	11		/* sync (RECNO) */
4745703Sbostic 
4850984Sbostic typedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE;
4945703Sbostic 
5065302Sbostic /*
5165302Sbostic  * !!!
5265302Sbostic  * The following flags are included in the dbopen(3) call as part of the
5365302Sbostic  * open(2) flags.  In order to avoid conflicts with the open flags, start
5465302Sbostic  * at the top of the 16 or 32-bit number space and work our way down.  If
5565302Sbostic  * the open flags were significantly expanded in the future, it could be
5665302Sbostic  * a problem.  Wish I'd left another flags word in the dbopen call.
5765302Sbostic  *
5865302Sbostic  * !!!
5965302Sbostic  * None of this stuff is implemented yet.  The only reason that it's here
6065302Sbostic  * is so that the access methods can skip copying the key/data pair when
6165302Sbostic  * the DB_LOCK flag isn't set.
6265302Sbostic  */
6365302Sbostic #if UINT_MAX > 65535
6465302Sbostic #define	DB_LOCK		0x20000000	/* Do locking. */
6565302Sbostic #define	DB_SHMEM	0x40000000	/* Use shared memory. */
6665302Sbostic #define	DB_TXN		0x80000000	/* Do transactions. */
6765302Sbostic #else
68*66190Sbostic #define	DB_LOCK		    0x2000	/* Do locking. */
69*66190Sbostic #define	DB_SHMEM	    0x4000	/* Use shared memory. */
70*66190Sbostic #define	DB_TXN		    0x8000	/* Do transactions. */
7165302Sbostic #endif
7256892Sbostic 
7350984Sbostic /* Access method description structure. */
7446948Sbostic typedef struct __db {
7565302Sbostic 	DBTYPE type;			/* Underlying db type. */
7650984Sbostic 	int (*close)	__P((struct __db *));
7751203Sbostic 	int (*del)	__P((const struct __db *, const DBT *, u_int));
7851203Sbostic 	int (*get)	__P((const struct __db *, const DBT *, DBT *, u_int));
7956716Sbostic 	int (*put)	__P((const struct __db *, DBT *, const DBT *, u_int));
8051203Sbostic 	int (*seq)	__P((const struct __db *, DBT *, DBT *, u_int));
8160057Sbostic 	int (*sync)	__P((const struct __db *, u_int));
8265303Sbostic 	void *internal;			/* Access method private. */
8365303Sbostic 	int (*fd)	__P((const struct __db *));
8445703Sbostic } DB;
8545703Sbostic 
8645703Sbostic #define	BTREEMAGIC	0x053162
8750984Sbostic #define	BTREEVERSION	3
8845703Sbostic 
8950984Sbostic /* Structure used to pass parameters to the btree routines. */
9045703Sbostic typedef struct {
9145703Sbostic #define	R_DUP		0x01	/* duplicate keys */
92*66190Sbostic 	u_long	flags;
93*66190Sbostic 	u_int	cachesize;	/* bytes to cache */
94*66190Sbostic 	int	maxkeypage;	/* maximum keys per page */
95*66190Sbostic 	int	minkeypage;	/* minimum keys per page */
96*66190Sbostic 	u_int	psize;		/* page size */
97*66190Sbostic 	int	(*compare)	/* comparison function */
98*66190Sbostic 	    __P((const DBT *, const DBT *));
99*66190Sbostic 	size_t	(*prefix)	/* prefix function */
100*66190Sbostic 	    __P((const DBT *, const DBT *));
101*66190Sbostic 	int	lorder;		/* byte order */
10245703Sbostic } BTREEINFO;
10345703Sbostic 
10445703Sbostic #define	HASHMAGIC	0x061561
10551060Sbostic #define	HASHVERSION	2
10645703Sbostic 
10750984Sbostic /* Structure used to pass parameters to the hashing routines. */
10845703Sbostic typedef struct {
109*66190Sbostic 	u_int	bsize;		/* bucket size */
110*66190Sbostic 	u_int	ffactor;	/* fill factor */
111*66190Sbostic 	u_int	nelem;		/* number of elements */
112*66190Sbostic 	u_int	cachesize;	/* bytes to cache */
113*66190Sbostic 	u_int32_t		/* hash function */
114*66190Sbostic 		(*hash) __P((const void *, size_t));
115*66190Sbostic 	int	lorder;		/* byte order */
11645703Sbostic } HASHINFO;
11745703Sbostic 
11850984Sbostic /* Structure used to pass parameters to the record routines. */
11945703Sbostic typedef struct {
12045703Sbostic #define	R_FIXEDLEN	0x01	/* fixed-length records */
12150984Sbostic #define	R_NOKEY		0x02	/* key not required */
12250984Sbostic #define	R_SNAPSHOT	0x04	/* snapshot the input */
123*66190Sbostic 	u_long	flags;
124*66190Sbostic 	u_int	cachesize;	/* bytes to cache */
125*66190Sbostic 	u_int	psize;		/* page size */
126*66190Sbostic 	int	lorder;		/* byte order */
127*66190Sbostic 	size_t	reclen;		/* record length (fixed-length records) */
128*66190Sbostic 	u_char	bval;		/* delimiting byte (variable-length records */
12960057Sbostic 	char	*bfname;	/* btree file name */
13045703Sbostic } RECNOINFO;
13145703Sbostic 
132*66190Sbostic #ifdef __DBINTERFACE_PRIVATE
13351042Sbostic /*
134*66190Sbostic  * Little endian <==> big endian 32-bit swap macros.
135*66190Sbostic  *	M_32_SWAP	swap a memory location
136*66190Sbostic  *	P_32_SWAP	swap a referenced memory location
137*66190Sbostic  *	P_32_COPY	swap from one location to another
13851042Sbostic  */
139*66190Sbostic #define	M_32_SWAP(a) {							\
140*66190Sbostic 	u_int32_t _tmp = a;						\
14165302Sbostic 	((char *)&a)[0] = ((char *)&_tmp)[3];				\
14265302Sbostic 	((char *)&a)[1] = ((char *)&_tmp)[2];				\
14365302Sbostic 	((char *)&a)[2] = ((char *)&_tmp)[1];				\
14465302Sbostic 	((char *)&a)[3] = ((char *)&_tmp)[0];				\
14546359Sbostic }
146*66190Sbostic #define	P_32_SWAP(a) {							\
147*66190Sbostic 	u_int32_t _tmp = *(u_int32_t *)a;				\
14865302Sbostic 	((char *)a)[0] = ((char *)&_tmp)[3];				\
14965302Sbostic 	((char *)a)[1] = ((char *)&_tmp)[2];				\
15065302Sbostic 	((char *)a)[2] = ((char *)&_tmp)[1];				\
15165302Sbostic 	((char *)a)[3] = ((char *)&_tmp)[0];				\
15251042Sbostic }
153*66190Sbostic #define	P_32_COPY(a, b) {						\
15465302Sbostic 	((char *)&(b))[0] = ((char *)&(a))[3];				\
15565302Sbostic 	((char *)&(b))[1] = ((char *)&(a))[2];				\
15665302Sbostic 	((char *)&(b))[2] = ((char *)&(a))[1];				\
15765302Sbostic 	((char *)&(b))[3] = ((char *)&(a))[0];				\
15846359Sbostic }
15946359Sbostic 
16051042Sbostic /*
161*66190Sbostic  * Little endian <==> big endian 16-bit swap macros.
162*66190Sbostic  *	M_16_SWAP	swap a memory location
163*66190Sbostic  *	P_16_SWAP	swap a referenced memory location
164*66190Sbostic  *	P_16_COPY	swap from one location to another
16551042Sbostic  */
166*66190Sbostic #define	M_16_SWAP(a) {							\
167*66190Sbostic 	u_int16_t _tmp = a;						\
16865302Sbostic 	((char *)&a)[0] = ((char *)&_tmp)[1];				\
16965302Sbostic 	((char *)&a)[1] = ((char *)&_tmp)[0];				\
17046359Sbostic }
171*66190Sbostic #define	P_16_SWAP(a) {							\
172*66190Sbostic 	u_int16_t _tmp = *(u_int16_t *)a;				\
17365302Sbostic 	((char *)a)[0] = ((char *)&_tmp)[1];				\
17465302Sbostic 	((char *)a)[1] = ((char *)&_tmp)[0];				\
17551042Sbostic }
176*66190Sbostic #define	P_16_COPY(a, b) {						\
17765302Sbostic 	((char *)&(b))[0] = ((char *)&(a))[1];				\
17865302Sbostic 	((char *)&(b))[1] = ((char *)&(a))[0];				\
17946359Sbostic }
180*66190Sbostic #endif
18146359Sbostic 
18246359Sbostic __BEGIN_DECLS
18350984Sbostic DB *dbopen __P((const char *, int, int, DBTYPE, const void *));
18450984Sbostic 
18550984Sbostic #ifdef __DBINTERFACE_PRIVATE
18665302Sbostic DB	*__bt_open __P((const char *, int, int, const BTREEINFO *, int));
18765302Sbostic DB	*__hash_open __P((const char *, int, int, const HASHINFO *, int));
18865302Sbostic DB	*__rec_open __P((const char *, int, int, const RECNOINFO *, int));
18950984Sbostic void	 __dbpanic __P((DB *dbp));
19050984Sbostic #endif
19146359Sbostic __END_DECLS
19246359Sbostic #endif /* !_DB_H_ */
193