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