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