1 /*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)db.h 5.25 (Berkeley) 05/22/93 8 */ 9 10 #ifndef _DB_H_ 11 #define _DB_H_ 12 13 #include <sys/types.h> 14 #include <sys/cdefs.h> 15 16 #define RET_ERROR -1 /* Return values. */ 17 #define RET_SUCCESS 0 18 #define RET_SPECIAL 1 19 20 #define MAX_PAGE_NUMBER ULONG_MAX /* >= # of pages in a file */ 21 typedef u_long pgno_t; 22 #define MAX_PAGE_OFFSET USHRT_MAX /* >= # of bytes in a page */ 23 typedef u_short indx_t; 24 #define MAX_REC_NUMBER ULONG_MAX /* >= # of records in a tree */ 25 typedef u_long recno_t; 26 27 /* Key/data structure -- a Data-Base Thang. */ 28 typedef struct { 29 void *data; /* data */ 30 size_t size; /* data length */ 31 } DBT; 32 33 /* Routine flags. */ 34 #define R_CURSOR 1 /* del, put, seq */ 35 #define __R_UNUSED 2 /* UNUSED */ 36 #define R_FIRST 3 /* seq */ 37 #define R_IAFTER 4 /* put (RECNO) */ 38 #define R_IBEFORE 5 /* put (RECNO) */ 39 #define R_LAST 6 /* seq (BTREE, RECNO) */ 40 #define R_NEXT 7 /* seq */ 41 #define R_NOOVERWRITE 8 /* put */ 42 #define R_PREV 9 /* seq (BTREE, RECNO) */ 43 #define R_SETCURSOR 10 /* put (RECNO) */ 44 #define R_RECNOSYNC 11 /* sync (RECNO) */ 45 46 typedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE; 47 48 #define __USE_OPEN_FLAGS \ 49 (O_CREAT|O_EXCL|O_EXLOCK|O_RDONLY|O_RDWR|O_SHLOCK|O_TRUNC) 50 51 /* Access method description structure. */ 52 typedef struct __db { 53 DBTYPE type; /* underlying db type */ 54 int (*close) __P((struct __db *)); 55 int (*del) __P((const struct __db *, const DBT *, u_int)); 56 int (*fd) __P((const struct __db *)); 57 int (*get) __P((const struct __db *, const DBT *, DBT *, u_int)); 58 int (*put) __P((const struct __db *, DBT *, const DBT *, u_int)); 59 int (*seq) __P((const struct __db *, DBT *, DBT *, u_int)); 60 int (*sync) __P((const struct __db *, u_int)); 61 void *internal; /* access method private */ 62 } DB; 63 64 #define BTREEMAGIC 0x053162 65 #define BTREEVERSION 3 66 67 /* Structure used to pass parameters to the btree routines. */ 68 typedef struct { 69 #define R_DUP 0x01 /* duplicate keys */ 70 u_long flags; 71 int cachesize; /* bytes to cache */ 72 int maxkeypage; /* maximum keys per page */ 73 int minkeypage; /* minimum keys per page */ 74 int psize; /* page size */ 75 /* comparison, prefix functions */ 76 int (*compare) __P((const DBT *, const DBT *)); 77 int (*prefix) __P((const DBT *, const DBT *)); 78 int lorder; /* byte order */ 79 } BTREEINFO; 80 81 #define HASHMAGIC 0x061561 82 #define HASHVERSION 2 83 84 /* Structure used to pass parameters to the hashing routines. */ 85 typedef struct { 86 int bsize; /* bucket size */ 87 int ffactor; /* fill factor */ 88 int nelem; /* number of elements */ 89 int cachesize; /* bytes to cache */ 90 /* hash function */ 91 int (*hash) __P((const void *, size_t)); 92 int lorder; /* byte order */ 93 } HASHINFO; 94 95 /* Structure used to pass parameters to the record routines. */ 96 typedef struct { 97 #define R_FIXEDLEN 0x01 /* fixed-length records */ 98 #define R_NOKEY 0x02 /* key not required */ 99 #define R_SNAPSHOT 0x04 /* snapshot the input */ 100 u_long flags; 101 int cachesize; /* bytes to cache */ 102 int psize; /* page size */ 103 int lorder; /* byte order */ 104 size_t reclen; /* record length (fixed-length records) */ 105 u_char bval; /* delimiting byte (variable-length records */ 106 char *bfname; /* btree file name */ 107 } RECNOINFO; 108 109 /* 110 * Little endian <==> big endian long swap macros. 111 * BLSWAP swap a memory location 112 * BLPSWAP swap a referenced memory location 113 * BLSWAP_COPY swap from one location to another 114 */ 115 #define BLSWAP(a) { \ 116 u_long _tmp = a; \ 117 ((char *)&a)[0] = ((char *)&_tmp)[3]; \ 118 ((char *)&a)[1] = ((char *)&_tmp)[2]; \ 119 ((char *)&a)[2] = ((char *)&_tmp)[1]; \ 120 ((char *)&a)[3] = ((char *)&_tmp)[0]; \ 121 } 122 #define BLPSWAP(a) { \ 123 u_long _tmp = *(u_long *)a; \ 124 ((char *)a)[0] = ((char *)&_tmp)[3]; \ 125 ((char *)a)[1] = ((char *)&_tmp)[2]; \ 126 ((char *)a)[2] = ((char *)&_tmp)[1]; \ 127 ((char *)a)[3] = ((char *)&_tmp)[0]; \ 128 } 129 #define BLSWAP_COPY(a, b) { \ 130 ((char *)&(b))[0] = ((char *)&(a))[3]; \ 131 ((char *)&(b))[1] = ((char *)&(a))[2]; \ 132 ((char *)&(b))[2] = ((char *)&(a))[1]; \ 133 ((char *)&(b))[3] = ((char *)&(a))[0]; \ 134 } 135 136 /* 137 * Little endian <==> big endian short swap macros. 138 * BSSWAP swap a memory location 139 * BSPSWAP swap a referenced memory location 140 * BSSWAP_COPY swap from one location to another 141 */ 142 #define BSSWAP(a) { \ 143 u_short _tmp = a; \ 144 ((char *)&a)[0] = ((char *)&_tmp)[1]; \ 145 ((char *)&a)[1] = ((char *)&_tmp)[0]; \ 146 } 147 #define BSPSWAP(a) { \ 148 u_short _tmp = *(u_short *)a; \ 149 ((char *)a)[0] = ((char *)&_tmp)[1]; \ 150 ((char *)a)[1] = ((char *)&_tmp)[0]; \ 151 } 152 #define BSSWAP_COPY(a, b) { \ 153 ((char *)&(b))[0] = ((char *)&(a))[1]; \ 154 ((char *)&(b))[1] = ((char *)&(a))[0]; \ 155 } 156 157 __BEGIN_DECLS 158 DB *dbopen __P((const char *, int, int, DBTYPE, const void *)); 159 160 #ifdef __DBINTERFACE_PRIVATE 161 DB *__bt_open __P((const char *, int, int, const BTREEINFO *)); 162 DB *__hash_open __P((const char *, int, int, const HASHINFO *)); 163 DB *__rec_open __P((const char *, int, int, const RECNOINFO *)); 164 void __dbpanic __P((DB *dbp)); 165 #endif 166 __END_DECLS 167 #endif /* !_DB_H_ */ 168