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