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.20 (Berkeley) 11/18/92 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 index_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_CURSORLOG 2 /* put (RECNO) */ 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 45 typedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE; 46 47 #define __USE_OPEN_FLAGS \ 48 (O_CREAT|O_EXCL|O_EXLOCK|O_RDONLY|O_RDWR|O_SHLOCK|O_TRUNC) 49 50 /* Access method description structure. */ 51 typedef struct __db { 52 DBTYPE type; /* underlying db type */ 53 int (*close) __P((struct __db *)); 54 int (*del) __P((const struct __db *, const DBT *, u_int)); 55 int (*get) __P((const struct __db *, const DBT *, DBT *, u_int)); 56 int (*put) __P((const struct __db *, DBT *, const DBT *, u_int)); 57 int (*seq) __P((const struct __db *, DBT *, DBT *, u_int)); 58 int (*sync) __P((const struct __db *)); 59 void *internal; /* access method private */ 60 } DB; 61 62 #define BTREEMAGIC 0x053162 63 #define BTREEVERSION 3 64 65 /* Structure used to pass parameters to the btree routines. */ 66 typedef struct { 67 #define R_DUP 0x01 /* duplicate keys */ 68 u_long flags; 69 int cachesize; /* bytes to cache */ 70 int maxkeypage; /* maximum keys per page */ 71 int minkeypage; /* minimum keys per page */ 72 int psize; /* page size */ 73 /* comparison, prefix functions */ 74 int (*compare) __P((const DBT *, const DBT *)); 75 int (*prefix) __P((const DBT *, const DBT *)); 76 int lorder; /* byte order */ 77 } BTREEINFO; 78 79 #define HASHMAGIC 0x061561 80 #define HASHVERSION 2 81 82 /* Structure used to pass parameters to the hashing routines. */ 83 typedef struct { 84 int bsize; /* bucket size */ 85 int ffactor; /* fill factor */ 86 int nelem; /* number of elements */ 87 int cachesize; /* bytes to cache */ 88 int (*hash)(); /* hash function */ 89 int lorder; /* byte order */ 90 } HASHINFO; 91 92 /* Structure used to pass parameters to the record routines. */ 93 typedef struct { 94 #define R_FIXEDLEN 0x01 /* fixed-length records */ 95 #define R_NOKEY 0x02 /* key not required */ 96 #define R_SNAPSHOT 0x04 /* snapshot the input */ 97 u_long flags; 98 int cachesize; /* bytes to cache */ 99 int lorder; /* byte order */ 100 size_t reclen; /* record length (fixed-length records) */ 101 u_char bval; /* delimiting byte (variable-length records */ 102 } RECNOINFO; 103 104 /* Key structure for the record routines. */ 105 typedef struct { 106 u_long number; 107 u_long offset; 108 u_long length; 109 #define R_LENGTH 0x01 /* length is valid */ 110 #define R_NUMBER 0x02 /* record number is valid */ 111 #define R_OFFSET 0x04 /* offset is valid */ 112 u_char valid; 113 } RECNOKEY; 114 115 /* 116 * Little endian <==> big endian long swap macros. 117 * BLSWAP swap a memory location 118 * BLPSWAP swap a referenced memory location 119 * BLSWAP_COPY swap from one location to another 120 */ 121 #define BLSWAP(a) { \ 122 u_long _tmp = a; \ 123 ((char *)&a)[0] = ((char *)&_tmp)[3]; \ 124 ((char *)&a)[1] = ((char *)&_tmp)[2]; \ 125 ((char *)&a)[2] = ((char *)&_tmp)[1]; \ 126 ((char *)&a)[3] = ((char *)&_tmp)[0]; \ 127 } 128 #define BLPSWAP(a) { \ 129 u_long _tmp = *(u_long *)a; \ 130 ((char *)a)[0] = ((char *)&_tmp)[3]; \ 131 ((char *)a)[1] = ((char *)&_tmp)[2]; \ 132 ((char *)a)[2] = ((char *)&_tmp)[1]; \ 133 ((char *)a)[3] = ((char *)&_tmp)[0]; \ 134 } 135 #define BLSWAP_COPY(a, b) { \ 136 ((char *)&(b))[0] = ((char *)&(a))[3]; \ 137 ((char *)&(b))[1] = ((char *)&(a))[2]; \ 138 ((char *)&(b))[2] = ((char *)&(a))[1]; \ 139 ((char *)&(b))[3] = ((char *)&(a))[0]; \ 140 } 141 142 /* 143 * Little endian <==> big endian short swap macros. 144 * BSSWAP swap a memory location 145 * BSPSWAP swap a referenced memory location 146 * BSSWAP_COPY swap from one location to another 147 */ 148 #define BSSWAP(a) { \ 149 u_short _tmp = a; \ 150 ((char *)&a)[0] = ((char *)&_tmp)[1]; \ 151 ((char *)&a)[1] = ((char *)&_tmp)[0]; \ 152 } 153 #define BSPSWAP(a) { \ 154 u_short _tmp = *(u_short *)a; \ 155 ((char *)a)[0] = ((char *)&_tmp)[1]; \ 156 ((char *)a)[1] = ((char *)&_tmp)[0]; \ 157 } 158 #define BSSWAP_COPY(a, b) { \ 159 ((char *)&(b))[0] = ((char *)&(a))[1]; \ 160 ((char *)&(b))[1] = ((char *)&(a))[0]; \ 161 } 162 163 __BEGIN_DECLS 164 DB *dbopen __P((const char *, int, int, DBTYPE, const void *)); 165 166 #ifdef __DBINTERFACE_PRIVATE 167 DB *__bt_open __P((const char *, int, int, const BTREEINFO *)); 168 DB *__hash_open __P((const char *, int, int, const HASHINFO *)); 169 DB *__rec_open __P((const char *, int, int, const RECNOINFO *)); 170 void __dbpanic __P((DB *dbp)); 171 #endif 172 __END_DECLS 173 #endif /* !_DB_H_ */ 174