db.h revision 1539
11539Srgrimes/*-
21539Srgrimes * Copyright (c) 1990, 1993
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.
131539Srgrimes * 3. All advertising materials mentioning features or use of this software
141539Srgrimes *    must display the following acknowledgement:
151539Srgrimes *	This product includes software developed by the University of
161539Srgrimes *	California, Berkeley and its contributors.
171539Srgrimes * 4. Neither the name of the University nor the names of its contributors
181539Srgrimes *    may be used to endorse or promote products derived from this software
191539Srgrimes *    without specific prior written permission.
201539Srgrimes *
211539Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221539Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231539Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241539Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251539Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261539Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271539Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281539Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291539Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301539Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311539Srgrimes * SUCH DAMAGE.
321539Srgrimes *
331539Srgrimes *	@(#)db.h	8.4 (Berkeley) 2/21/94
341539Srgrimes */
351539Srgrimes
361539Srgrimes#ifndef _DB_H_
371539Srgrimes#define	_DB_H_
381539Srgrimes
391539Srgrimes#include <sys/types.h>
401539Srgrimes#include <sys/cdefs.h>
411539Srgrimes
421539Srgrimes#include <limits.h>
431539Srgrimes
441539Srgrimes#define	RET_ERROR	-1		/* Return values. */
451539Srgrimes#define	RET_SUCCESS	 0
461539Srgrimes#define	RET_SPECIAL	 1
471539Srgrimes
481539Srgrimes#define	MAX_PAGE_NUMBER	0xffffffff	/* >= # of pages in a file */
491539Srgrimestypedef u_int32_t	pgno_t;
501539Srgrimes#define	MAX_PAGE_OFFSET	65535		/* >= # of bytes in a page */
511539Srgrimestypedef u_int16_t	indx_t;
521539Srgrimes#define	MAX_REC_NUMBER	0xffffffff	/* >= # of records in a tree */
531539Srgrimestypedef u_int32_t	recno_t;
541539Srgrimes
551539Srgrimes/* Key/data structure -- a Data-Base Thang. */
561539Srgrimestypedef struct {
571539Srgrimes	void	*data;			/* data */
581539Srgrimes	size_t	 size;			/* data length */
591539Srgrimes} DBT;
601539Srgrimes
611539Srgrimes/* Routine flags. */
621539Srgrimes#define	R_CURSOR	1		/* del, put, seq */
631539Srgrimes#define	__R_UNUSED	2		/* UNUSED */
641539Srgrimes#define	R_FIRST		3		/* seq */
651539Srgrimes#define	R_IAFTER	4		/* put (RECNO) */
661539Srgrimes#define	R_IBEFORE	5		/* put (RECNO) */
671539Srgrimes#define	R_LAST		6		/* seq (BTREE, RECNO) */
681539Srgrimes#define	R_NEXT		7		/* seq */
691539Srgrimes#define	R_NOOVERWRITE	8		/* put */
701539Srgrimes#define	R_PREV		9		/* seq (BTREE, RECNO) */
711539Srgrimes#define	R_SETCURSOR	10		/* put (RECNO) */
721539Srgrimes#define	R_RECNOSYNC	11		/* sync (RECNO) */
731539Srgrimes
741539Srgrimestypedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE;
751539Srgrimes
761539Srgrimes/*
771539Srgrimes * !!!
781539Srgrimes * The following flags are included in the dbopen(3) call as part of the
791539Srgrimes * open(2) flags.  In order to avoid conflicts with the open flags, start
801539Srgrimes * at the top of the 16 or 32-bit number space and work our way down.  If
811539Srgrimes * the open flags were significantly expanded in the future, it could be
821539Srgrimes * a problem.  Wish I'd left another flags word in the dbopen call.
831539Srgrimes *
841539Srgrimes * !!!
851539Srgrimes * None of this stuff is implemented yet.  The only reason that it's here
861539Srgrimes * is so that the access methods can skip copying the key/data pair when
871539Srgrimes * the DB_LOCK flag isn't set.
881539Srgrimes */
891539Srgrimes#if UINT_MAX > 65535
901539Srgrimes#define	DB_LOCK		0x20000000	/* Do locking. */
911539Srgrimes#define	DB_SHMEM	0x40000000	/* Use shared memory. */
921539Srgrimes#define	DB_TXN		0x80000000	/* Do transactions. */
931539Srgrimes#else
941539Srgrimes#define	DB_LOCK		    0x2000	/* Do locking. */
951539Srgrimes#define	DB_SHMEM	    0x4000	/* Use shared memory. */
961539Srgrimes#define	DB_TXN		    0x8000	/* Do transactions. */
971539Srgrimes#endif
981539Srgrimes
991539Srgrimes/* Access method description structure. */
1001539Srgrimestypedef struct __db {
1011539Srgrimes	DBTYPE type;			/* Underlying db type. */
1021539Srgrimes	int (*close)	__P((struct __db *));
1031539Srgrimes	int (*del)	__P((const struct __db *, const DBT *, u_int));
1041539Srgrimes	int (*get)	__P((const struct __db *, const DBT *, DBT *, u_int));
1051539Srgrimes	int (*put)	__P((const struct __db *, DBT *, const DBT *, u_int));
1061539Srgrimes	int (*seq)	__P((const struct __db *, DBT *, DBT *, u_int));
1071539Srgrimes	int (*sync)	__P((const struct __db *, u_int));
1081539Srgrimes	void *internal;			/* Access method private. */
1091539Srgrimes	int (*fd)	__P((const struct __db *));
1101539Srgrimes} DB;
1111539Srgrimes
1121539Srgrimes#define	BTREEMAGIC	0x053162
1131539Srgrimes#define	BTREEVERSION	3
1141539Srgrimes
1151539Srgrimes/* Structure used to pass parameters to the btree routines. */
1161539Srgrimestypedef struct {
1171539Srgrimes#define	R_DUP		0x01	/* duplicate keys */
1181539Srgrimes	u_long	flags;
1191539Srgrimes	u_int	cachesize;	/* bytes to cache */
1201539Srgrimes	int	maxkeypage;	/* maximum keys per page */
1211539Srgrimes	int	minkeypage;	/* minimum keys per page */
1221539Srgrimes	u_int	psize;		/* page size */
1231539Srgrimes	int	(*compare)	/* comparison function */
1241539Srgrimes	    __P((const DBT *, const DBT *));
1251539Srgrimes	size_t	(*prefix)	/* prefix function */
1261539Srgrimes	    __P((const DBT *, const DBT *));
1271539Srgrimes	int	lorder;		/* byte order */
1281539Srgrimes} BTREEINFO;
1291539Srgrimes
1301539Srgrimes#define	HASHMAGIC	0x061561
1311539Srgrimes#define	HASHVERSION	2
1321539Srgrimes
1331539Srgrimes/* Structure used to pass parameters to the hashing routines. */
1341539Srgrimestypedef struct {
1351539Srgrimes	u_int	bsize;		/* bucket size */
1361539Srgrimes	u_int	ffactor;	/* fill factor */
1371539Srgrimes	u_int	nelem;		/* number of elements */
1381539Srgrimes	u_int	cachesize;	/* bytes to cache */
1391539Srgrimes	u_int32_t		/* hash function */
1401539Srgrimes		(*hash) __P((const void *, size_t));
1411539Srgrimes	int	lorder;		/* byte order */
1421539Srgrimes} HASHINFO;
1431539Srgrimes
1441539Srgrimes/* Structure used to pass parameters to the record routines. */
1451539Srgrimestypedef struct {
1461539Srgrimes#define	R_FIXEDLEN	0x01	/* fixed-length records */
1471539Srgrimes#define	R_NOKEY		0x02	/* key not required */
1481539Srgrimes#define	R_SNAPSHOT	0x04	/* snapshot the input */
1491539Srgrimes	u_long	flags;
1501539Srgrimes	u_int	cachesize;	/* bytes to cache */
1511539Srgrimes	u_int	psize;		/* page size */
1521539Srgrimes	int	lorder;		/* byte order */
1531539Srgrimes	size_t	reclen;		/* record length (fixed-length records) */
1541539Srgrimes	u_char	bval;		/* delimiting byte (variable-length records */
1551539Srgrimes	char	*bfname;	/* btree file name */
1561539Srgrimes} RECNOINFO;
1571539Srgrimes
1581539Srgrimes#ifdef __DBINTERFACE_PRIVATE
1591539Srgrimes/*
1601539Srgrimes * Little endian <==> big endian 32-bit swap macros.
1611539Srgrimes *	M_32_SWAP	swap a memory location
1621539Srgrimes *	P_32_SWAP	swap a referenced memory location
1631539Srgrimes *	P_32_COPY	swap from one location to another
1641539Srgrimes */
1651539Srgrimes#define	M_32_SWAP(a) {							\
1661539Srgrimes	u_int32_t _tmp = a;						\
1671539Srgrimes	((char *)&a)[0] = ((char *)&_tmp)[3];				\
1681539Srgrimes	((char *)&a)[1] = ((char *)&_tmp)[2];				\
1691539Srgrimes	((char *)&a)[2] = ((char *)&_tmp)[1];				\
1701539Srgrimes	((char *)&a)[3] = ((char *)&_tmp)[0];				\
1711539Srgrimes}
1721539Srgrimes#define	P_32_SWAP(a) {							\
1731539Srgrimes	u_int32_t _tmp = *(u_int32_t *)a;				\
1741539Srgrimes	((char *)a)[0] = ((char *)&_tmp)[3];				\
1751539Srgrimes	((char *)a)[1] = ((char *)&_tmp)[2];				\
1761539Srgrimes	((char *)a)[2] = ((char *)&_tmp)[1];				\
1771539Srgrimes	((char *)a)[3] = ((char *)&_tmp)[0];				\
1781539Srgrimes}
1791539Srgrimes#define	P_32_COPY(a, b) {						\
1801539Srgrimes	((char *)&(b))[0] = ((char *)&(a))[3];				\
1811539Srgrimes	((char *)&(b))[1] = ((char *)&(a))[2];				\
1821539Srgrimes	((char *)&(b))[2] = ((char *)&(a))[1];				\
1831539Srgrimes	((char *)&(b))[3] = ((char *)&(a))[0];				\
1841539Srgrimes}
1851539Srgrimes
1861539Srgrimes/*
1871539Srgrimes * Little endian <==> big endian 16-bit swap macros.
1881539Srgrimes *	M_16_SWAP	swap a memory location
1891539Srgrimes *	P_16_SWAP	swap a referenced memory location
1901539Srgrimes *	P_16_COPY	swap from one location to another
1911539Srgrimes */
1921539Srgrimes#define	M_16_SWAP(a) {							\
1931539Srgrimes	u_int16_t _tmp = a;						\
1941539Srgrimes	((char *)&a)[0] = ((char *)&_tmp)[1];				\
1951539Srgrimes	((char *)&a)[1] = ((char *)&_tmp)[0];				\
1961539Srgrimes}
1971539Srgrimes#define	P_16_SWAP(a) {							\
1981539Srgrimes	u_int16_t _tmp = *(u_int16_t *)a;				\
1991539Srgrimes	((char *)a)[0] = ((char *)&_tmp)[1];				\
2001539Srgrimes	((char *)a)[1] = ((char *)&_tmp)[0];				\
2011539Srgrimes}
2021539Srgrimes#define	P_16_COPY(a, b) {						\
2031539Srgrimes	((char *)&(b))[0] = ((char *)&(a))[1];				\
2041539Srgrimes	((char *)&(b))[1] = ((char *)&(a))[0];				\
2051539Srgrimes}
2061539Srgrimes#endif
2071539Srgrimes
2081539Srgrimes__BEGIN_DECLS
2091539SrgrimesDB *dbopen __P((const char *, int, int, DBTYPE, const void *));
2101539Srgrimes
2111539Srgrimes#ifdef __DBINTERFACE_PRIVATE
2121539SrgrimesDB	*__bt_open __P((const char *, int, int, const BTREEINFO *, int));
2131539SrgrimesDB	*__hash_open __P((const char *, int, int, const HASHINFO *, int));
2141539SrgrimesDB	*__rec_open __P((const char *, int, int, const RECNOINFO *, int));
2151539Srgrimesvoid	 __dbpanic __P((DB *dbp));
2161539Srgrimes#endif
2171539Srgrimes__END_DECLS
2181539Srgrimes#endif /* !_DB_H_ */
219