11539Srgrimes/*-
214288Spst * Copyright (c) 1990, 1993, 1994
31539Srgrimes *	The Regents of the University of California.  All rights reserved.
41539Srgrimes *
51539Srgrimes * Redistribution and use in source and binary forms, with or without
61539Srgrimes * modification, are permitted provided that the following conditions
71539Srgrimes * are met:
81539Srgrimes * 1. Redistributions of source code must retain the above copyright
91539Srgrimes *    notice, this list of conditions and the following disclaimer.
101539Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111539Srgrimes *    notice, this list of conditions and the following disclaimer in the
121539Srgrimes *    documentation and/or other materials provided with the distribution.
13203964Simp * 3. Neither the name of the University nor the names of its contributors
141539Srgrimes *    may be used to endorse or promote products derived from this software
151539Srgrimes *    without specific prior written permission.
161539Srgrimes *
171539Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181539Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191539Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201539Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211539Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221539Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231539Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241539Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251539Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261539Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271539Srgrimes * SUCH DAMAGE.
281539Srgrimes *
2914288Spst *	@(#)db.h	8.7 (Berkeley) 6/16/94
3093032Simp * $FreeBSD$
311539Srgrimes */
321539Srgrimes
331539Srgrimes#ifndef _DB_H_
341539Srgrimes#define	_DB_H_
351539Srgrimes
361539Srgrimes#include <sys/types.h>
371539Srgrimes#include <sys/cdefs.h>
381539Srgrimes
391539Srgrimes#include <limits.h>
401539Srgrimes
411539Srgrimes#define	RET_ERROR	-1		/* Return values. */
421539Srgrimes#define	RET_SUCCESS	 0
431539Srgrimes#define	RET_SPECIAL	 1
441539Srgrimes
451539Srgrimes#define	MAX_PAGE_NUMBER	0xffffffff	/* >= # of pages in a file */
46189827Sdastypedef uint32_t	pgno_t;
471539Srgrimes#define	MAX_PAGE_OFFSET	65535		/* >= # of bytes in a page */
48189827Sdastypedef uint16_t	indx_t;
491539Srgrimes#define	MAX_REC_NUMBER	0xffffffff	/* >= # of records in a tree */
50189827Sdastypedef uint32_t	recno_t;
511539Srgrimes
521539Srgrimes/* Key/data structure -- a Data-Base Thang. */
531539Srgrimestypedef struct {
541539Srgrimes	void	*data;			/* data */
551539Srgrimes	size_t	 size;			/* data length */
561539Srgrimes} DBT;
571539Srgrimes
581539Srgrimes/* Routine flags. */
591539Srgrimes#define	R_CURSOR	1		/* del, put, seq */
601539Srgrimes#define	__R_UNUSED	2		/* UNUSED */
611539Srgrimes#define	R_FIRST		3		/* seq */
621539Srgrimes#define	R_IAFTER	4		/* put (RECNO) */
631539Srgrimes#define	R_IBEFORE	5		/* put (RECNO) */
641539Srgrimes#define	R_LAST		6		/* seq (BTREE, RECNO) */
651539Srgrimes#define	R_NEXT		7		/* seq */
661539Srgrimes#define	R_NOOVERWRITE	8		/* put */
671539Srgrimes#define	R_PREV		9		/* seq (BTREE, RECNO) */
681539Srgrimes#define	R_SETCURSOR	10		/* put (RECNO) */
691539Srgrimes#define	R_RECNOSYNC	11		/* sync (RECNO) */
701539Srgrimes
711539Srgrimestypedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE;
721539Srgrimes
731539Srgrimes/*
741539Srgrimes * !!!
751539Srgrimes * The following flags are included in the dbopen(3) call as part of the
761539Srgrimes * open(2) flags.  In order to avoid conflicts with the open flags, start
771539Srgrimes * at the top of the 16 or 32-bit number space and work our way down.  If
781539Srgrimes * the open flags were significantly expanded in the future, it could be
791539Srgrimes * a problem.  Wish I'd left another flags word in the dbopen call.
801539Srgrimes *
811539Srgrimes * !!!
821539Srgrimes * None of this stuff is implemented yet.  The only reason that it's here
831539Srgrimes * is so that the access methods can skip copying the key/data pair when
841539Srgrimes * the DB_LOCK flag isn't set.
851539Srgrimes */
861539Srgrimes#if UINT_MAX > 65535
871539Srgrimes#define	DB_LOCK		0x20000000	/* Do locking. */
881539Srgrimes#define	DB_SHMEM	0x40000000	/* Use shared memory. */
891539Srgrimes#define	DB_TXN		0x80000000	/* Do transactions. */
901539Srgrimes#else
911539Srgrimes#define	DB_LOCK		    0x2000	/* Do locking. */
921539Srgrimes#define	DB_SHMEM	    0x4000	/* Use shared memory. */
931539Srgrimes#define	DB_TXN		    0x8000	/* Do transactions. */
941539Srgrimes#endif
951539Srgrimes
961539Srgrimes/* Access method description structure. */
971539Srgrimestypedef struct __db {
981539Srgrimes	DBTYPE type;			/* Underlying db type. */
9993032Simp	int (*close)(struct __db *);
100189827Sdas	int (*del)(const struct __db *, const DBT *, unsigned int);
101189827Sdas	int (*get)(const struct __db *, const DBT *, DBT *, unsigned int);
102189827Sdas	int (*put)(const struct __db *, DBT *, const DBT *, unsigned int);
103189827Sdas	int (*seq)(const struct __db *, DBT *, DBT *, unsigned int);
104189827Sdas	int (*sync)(const struct __db *, unsigned int);
1051539Srgrimes	void *internal;			/* Access method private. */
10693032Simp	int (*fd)(const struct __db *);
1071539Srgrimes} DB;
1081539Srgrimes
1091539Srgrimes#define	BTREEMAGIC	0x053162
1101539Srgrimes#define	BTREEVERSION	3
1111539Srgrimes
1121539Srgrimes/* Structure used to pass parameters to the btree routines. */
1131539Srgrimestypedef struct {
1141539Srgrimes#define	R_DUP		0x01	/* duplicate keys */
115189827Sdas	unsigned long	flags;
116189827Sdas	unsigned int	cachesize;	/* bytes to cache */
117189827Sdas	int		maxkeypage;	/* maximum keys per page */
118189827Sdas	int		minkeypage;	/* minimum keys per page */
119189827Sdas	unsigned int	psize;		/* page size */
120189827Sdas	int		(*compare)	/* comparison function */
121189827Sdas			    (const DBT *, const DBT *);
122189827Sdas	size_t		(*prefix)	/* prefix function */
123189827Sdas			    (const DBT *, const DBT *);
124189827Sdas	int		lorder;		/* byte order */
1251539Srgrimes} BTREEINFO;
1261539Srgrimes
1271539Srgrimes#define	HASHMAGIC	0x061561
1281539Srgrimes#define	HASHVERSION	2
1291539Srgrimes
1301539Srgrimes/* Structure used to pass parameters to the hashing routines. */
1311539Srgrimestypedef struct {
132189827Sdas	unsigned int	bsize;		/* bucket size */
133189827Sdas	unsigned int	ffactor;	/* fill factor */
134189827Sdas	unsigned int	nelem;		/* number of elements */
135189827Sdas	unsigned int	cachesize;	/* bytes to cache */
136189827Sdas	uint32_t			/* hash function */
13793032Simp		(*hash)(const void *, size_t);
1381539Srgrimes	int	lorder;		/* byte order */
1391539Srgrimes} HASHINFO;
1401539Srgrimes
1411539Srgrimes/* Structure used to pass parameters to the record routines. */
1421539Srgrimestypedef struct {
1431539Srgrimes#define	R_FIXEDLEN	0x01	/* fixed-length records */
1441539Srgrimes#define	R_NOKEY		0x02	/* key not required */
1451539Srgrimes#define	R_SNAPSHOT	0x04	/* snapshot the input */
146189827Sdas	unsigned long	flags;
147189827Sdas	unsigned int	cachesize; /* bytes to cache */
148189827Sdas	unsigned int	psize;	/* page size */
149189827Sdas	int		lorder;	/* byte order */
150189827Sdas	size_t		reclen;	/* record length (fixed-length records) */
151189827Sdas	unsigned char	bval;	/* delimiting byte (variable-length records */
15214288Spst	char	*bfname;	/* btree file name */
1531539Srgrimes} RECNOINFO;
1541539Srgrimes
1551539Srgrimes#ifdef __DBINTERFACE_PRIVATE
1561539Srgrimes/*
1571539Srgrimes * Little endian <==> big endian 32-bit swap macros.
1581539Srgrimes *	M_32_SWAP	swap a memory location
1591539Srgrimes *	P_32_SWAP	swap a referenced memory location
1601539Srgrimes *	P_32_COPY	swap from one location to another
1611539Srgrimes */
1621539Srgrimes#define	M_32_SWAP(a) {							\
163189827Sdas	uint32_t _tmp = a;						\
1641539Srgrimes	((char *)&a)[0] = ((char *)&_tmp)[3];				\
1651539Srgrimes	((char *)&a)[1] = ((char *)&_tmp)[2];				\
1661539Srgrimes	((char *)&a)[2] = ((char *)&_tmp)[1];				\
1671539Srgrimes	((char *)&a)[3] = ((char *)&_tmp)[0];				\
1681539Srgrimes}
1691539Srgrimes#define	P_32_SWAP(a) {							\
170189827Sdas	uint32_t _tmp = *(uint32_t *)a;					\
1711539Srgrimes	((char *)a)[0] = ((char *)&_tmp)[3];				\
1721539Srgrimes	((char *)a)[1] = ((char *)&_tmp)[2];				\
1731539Srgrimes	((char *)a)[2] = ((char *)&_tmp)[1];				\
1741539Srgrimes	((char *)a)[3] = ((char *)&_tmp)[0];				\
1751539Srgrimes}
1761539Srgrimes#define	P_32_COPY(a, b) {						\
1771539Srgrimes	((char *)&(b))[0] = ((char *)&(a))[3];				\
1781539Srgrimes	((char *)&(b))[1] = ((char *)&(a))[2];				\
1791539Srgrimes	((char *)&(b))[2] = ((char *)&(a))[1];				\
1801539Srgrimes	((char *)&(b))[3] = ((char *)&(a))[0];				\
1811539Srgrimes}
1821539Srgrimes
1831539Srgrimes/*
1841539Srgrimes * Little endian <==> big endian 16-bit swap macros.
1851539Srgrimes *	M_16_SWAP	swap a memory location
1861539Srgrimes *	P_16_SWAP	swap a referenced memory location
1871539Srgrimes *	P_16_COPY	swap from one location to another
1881539Srgrimes */
1891539Srgrimes#define	M_16_SWAP(a) {							\
190189827Sdas	uint16_t _tmp = a;						\
1911539Srgrimes	((char *)&a)[0] = ((char *)&_tmp)[1];				\
1921539Srgrimes	((char *)&a)[1] = ((char *)&_tmp)[0];				\
1931539Srgrimes}
1941539Srgrimes#define	P_16_SWAP(a) {							\
195189827Sdas	uint16_t _tmp = *(uint16_t *)a;					\
1961539Srgrimes	((char *)a)[0] = ((char *)&_tmp)[1];				\
1971539Srgrimes	((char *)a)[1] = ((char *)&_tmp)[0];				\
1981539Srgrimes}
1991539Srgrimes#define	P_16_COPY(a, b) {						\
2001539Srgrimes	((char *)&(b))[0] = ((char *)&(a))[1];				\
2011539Srgrimes	((char *)&(b))[1] = ((char *)&(a))[0];				\
2021539Srgrimes}
2031539Srgrimes#endif
2041539Srgrimes
2051539Srgrimes__BEGIN_DECLS
206189827Sdas#if __BSD_VISIBLE
20793032SimpDB *dbopen(const char *, int, int, DBTYPE, const void *);
208189827Sdas#endif
2091539Srgrimes
2101539Srgrimes#ifdef __DBINTERFACE_PRIVATE
21193032SimpDB	*__bt_open(const char *, int, int, const BTREEINFO *, int);
21293032SimpDB	*__hash_open(const char *, int, int, const HASHINFO *, int);
21393032SimpDB	*__rec_open(const char *, int, int, const RECNOINFO *, int);
21493032Simpvoid	 __dbpanic(DB *dbp);
2151539Srgrimes#endif
2161539Srgrimes__END_DECLS
2171539Srgrimes#endif /* !_DB_H_ */
218