Home | History | Annotate | Line # | Download | only in include
db.h revision 1.5
      1 /*-
      2  * Copyright (c) 1990 The Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  *
     33  *	@(#)db.h	5.21 (Berkeley) 2/14/93
     34  */
     35 
     36 #ifndef _DB_H_
     37 #define	_DB_H_
     38 
     39 #include <sys/types.h>
     40 #include <sys/cdefs.h>
     41 #include <machine/endian.h>
     42 
     43 #define	RET_ERROR	-1		/* Return values. */
     44 #define	RET_SUCCESS	 0
     45 #define	RET_SPECIAL	 1
     46 
     47 #define	MAX_PAGE_NUMBER	ULONG_MAX	/* >= # of pages in a file */
     48 typedef u_long	pgno_t;
     49 #define	MAX_PAGE_OFFSET	USHRT_MAX	/* >= # of bytes in a page */
     50 typedef u_short	indx_t;
     51 #define	MAX_REC_NUMBER	ULONG_MAX	/* >= # of records in a tree */
     52 typedef u_long	recno_t;
     53 
     54 /* Key/data structure -- a Data-Base Thang. */
     55 typedef struct {
     56 	void	*data;			/* data */
     57 	size_t	 size;			/* data length */
     58 } DBT;
     59 
     60 /* Routine flags. */
     61 #define	R_CURSOR	1		/* del, put, seq */
     62 #define	R_CURSORLOG	2		/* put (RECNO) */
     63 #define	R_FIRST		3		/* seq */
     64 #define	R_IAFTER	4		/* put (RECNO) */
     65 #define	R_IBEFORE	5		/* put (RECNO) */
     66 #define	R_LAST		6		/* seq (BTREE, RECNO) */
     67 #define	R_NEXT		7		/* seq */
     68 #define	R_NOOVERWRITE	8		/* put */
     69 #define	R_PREV		9		/* seq (BTREE, RECNO) */
     70 #define	R_SETCURSOR	10		/* put (RECNO) */
     71 
     72 typedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE;
     73 
     74 #define	__USE_OPEN_FLAGS \
     75 	(O_CREAT|O_EXCL|O_EXLOCK|O_RDONLY|O_RDWR|O_SHLOCK|O_TRUNC)
     76 
     77 /* Access method description structure. */
     78 typedef struct __db {
     79 	DBTYPE type;			/* underlying db type */
     80 	int (*close)	__P((struct __db *));
     81 	int (*del)	__P((const struct __db *, const DBT *, u_int));
     82 	int (*get)	__P((const struct __db *, const DBT *, DBT *, u_int));
     83 	int (*put)	__P((const struct __db *, DBT *, const DBT *, u_int));
     84 	int (*seq)	__P((const struct __db *, DBT *, DBT *, u_int));
     85 	int (*sync)	__P((const struct __db *));
     86 	void *internal;			/* access method private */
     87 } DB;
     88 
     89 #define	BTREEMAGIC	0x053162
     90 #define	BTREEVERSION	3
     91 
     92 /* Structure used to pass parameters to the btree routines. */
     93 typedef struct {
     94 #define	R_DUP		0x01	/* duplicate keys */
     95 	u_long flags;
     96 	int cachesize;		/* bytes to cache */
     97 	int maxkeypage;		/* maximum keys per page */
     98 	int minkeypage;		/* minimum keys per page */
     99 	int psize;		/* page size */
    100 				/* comparison, prefix functions */
    101 	int (*compare)	__P((const DBT *, const DBT *));
    102 	int (*prefix)	__P((const DBT *, const DBT *));
    103 	int lorder;		/* byte order */
    104 } BTREEINFO;
    105 
    106 #define	HASHMAGIC	0x061561
    107 #define	HASHVERSION	2
    108 
    109 /* Structure used to pass parameters to the hashing routines. */
    110 typedef struct {
    111 	int bsize;		/* bucket size */
    112 	int ffactor;		/* fill factor */
    113 	int nelem;		/* number of elements */
    114 	int cachesize;		/* bytes to cache */
    115 	int (*hash)();		/* hash function */
    116 	int lorder;		/* byte order */
    117 } HASHINFO;
    118 
    119 /* Structure used to pass parameters to the record routines. */
    120 typedef struct {
    121 #define	R_FIXEDLEN	0x01	/* fixed-length records */
    122 #define	R_NOKEY		0x02	/* key not required */
    123 #define	R_SNAPSHOT	0x04	/* snapshot the input */
    124 	u_long flags;
    125 	int cachesize;		/* bytes to cache */
    126 	int lorder;		/* byte order */
    127 	size_t reclen;		/* record length (fixed-length records) */
    128 	u_char bval;		/* delimiting byte (variable-length records */
    129 } RECNOINFO;
    130 
    131 /* Key structure for the record routines. */
    132 typedef struct {
    133 	u_long number;
    134 	u_long offset;
    135 	u_long length;
    136 #define	R_LENGTH	0x01	/* length is valid */
    137 #define	R_NUMBER	0x02	/* record number is valid */
    138 #define	R_OFFSET	0x04	/* offset is valid */
    139 	u_char valid;
    140 } RECNOKEY;
    141 
    142 /*
    143  * Little endian <==> big endian long swap macros.
    144  *	BLSWAP		swap a memory location
    145  *	BLPSWAP		swap a referenced memory location
    146  *	BLSWAP_COPY	swap from one location to another
    147  */
    148 #define BLSWAP(a) { a = __byte_swap_long(a); }
    149 #define	BLPSWAP(a) BLSWAP((*a))
    150 #define	BLSWAP_COPY(a, b) { b = __byte_swap_long(a); }
    151 
    152 /*
    153  * Little endian <==> big endian short swap macros.
    154  *	BSSWAP		swap a memory location
    155  *	BSPSWAP		swap a referenced memory location
    156  *	BSSWAP_COPY	swap from one location to another
    157  */
    158 #define BSSWAP(a) { a = __byte_swap_word(a); }
    159 #define BSPSWAP(a)  BSSWAP((*a))
    160 #define BSSWAP_COPY(a, b) { b = __byte_swap_word(a); }
    161 
    162 __BEGIN_DECLS
    163 DB *dbopen __P((const char *, int, int, DBTYPE, const void *));
    164 
    165 #ifdef __DBINTERFACE_PRIVATE
    166 DB	*__bt_open __P((const char *, int, int, const BTREEINFO *));
    167 DB	*__hash_open __P((const char *, int, int, const HASHINFO *));
    168 DB	*__rec_open __P((const char *, int, int, const RECNOINFO *));
    169 void	 __dbpanic __P((DB *dbp));
    170 #endif
    171 __END_DECLS
    172 #endif /* !_DB_H_ */
    173