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