| 1 |
/* $NetBSD: db.h,v 1.4 2014/03/14 22:08:18 ryoon Exp $ */ |
| 2 |
|
| 3 |
/*- |
| 4 |
* Copyright (c) 1990, 1993, 1994 |
| 5 |
* The Regents of the University of California. All rights reserved. |
| 6 |
* |
| 7 |
* Redistribution and use in source and binary forms, with or without |
| 8 |
* modification, are permitted provided that the following conditions |
| 9 |
* are met: |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer in the |
| 14 |
* documentation and/or other materials provided with the distribution. |
| 15 |
* 3. Neither the name of the University nor the names of its contributors |
| 16 |
* may be used to endorse or promote products derived from this software |
| 17 |
* without specific prior written permission. |
| 18 |
* |
| 19 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 20 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 23 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 24 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 25 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 26 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 27 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 28 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 29 |
* SUCH DAMAGE. |
| 30 |
* |
| 31 |
* @(#)db.h 8.7 (Berkeley) 6/16/94 |
| 32 |
*/ |
| 33 |
|
| 34 |
#ifndef _NBCOMPAT_DB_H_ |
| 35 |
#define _NBCOMPAT_DB_H_ |
| 36 |
|
| 37 |
#include <nbcompat/types.h> |
| 38 |
#include <nbcompat/cdefs.h> |
| 39 |
|
| 40 |
#include <nbcompat/limits.h> |
| 41 |
|
| 42 |
#define RET_ERROR -1 /* Return values. */ |
| 43 |
#define RET_SUCCESS 0 |
| 44 |
#define RET_SPECIAL 1 |
| 45 |
|
| 46 |
/* |
| 47 |
* XXX |
| 48 |
* SGI/IRIX already has a pgno_t. |
| 49 |
*/ |
| 50 |
#ifdef __sgi |
| 51 |
#define pgno_t db_pgno_t |
| 52 |
#endif |
| 53 |
|
| 54 |
/* |
| 55 |
* SCO OpenServer 5.0.7/3.2 has no MAXPATHLEN, but it has PATH_MAX (256). |
| 56 |
* in limits.h. But it is not usable under ordinal condition. |
| 57 |
*/ |
| 58 |
#if !defined(MAXPATHLEN) |
| 59 |
#if defined(_SCO_DS) |
| 60 |
#define MAXPATHLEN 1024 |
| 61 |
#endif |
| 62 |
#endif |
| 63 |
|
| 64 |
#define MAX_PAGE_NUMBER 0xffffffff /* >= # of pages in a file */ |
| 65 |
typedef uint32_t pgno_t; |
| 66 |
#define MAX_PAGE_OFFSET 65535 /* >= # of bytes in a page */ |
| 67 |
typedef uint16_t indx_t; |
| 68 |
#define MAX_REC_NUMBER 0xffffffff /* >= # of records in a tree */ |
| 69 |
typedef uint32_t recno_t; |
| 70 |
|
| 71 |
/* Key/data structure -- a Data-Base Thang. */ |
| 72 |
typedef struct { |
| 73 |
void *data; /* data */ |
| 74 |
size_t size; /* data length */ |
| 75 |
} DBT; |
| 76 |
|
| 77 |
/* Routine flags. */ |
| 78 |
#define R_CURSOR 1 /* del, put, seq */ |
| 79 |
#define __R_UNUSED 2 /* UNUSED */ |
| 80 |
#define R_FIRST 3 /* seq */ |
| 81 |
#define R_IAFTER 4 /* put (RECNO) */ |
| 82 |
#define R_IBEFORE 5 /* put (RECNO) */ |
| 83 |
#define R_LAST 6 /* seq (BTREE, RECNO) */ |
| 84 |
#define R_NEXT 7 /* seq */ |
| 85 |
#define R_NOOVERWRITE 8 /* put */ |
| 86 |
#define R_PREV 9 /* seq (BTREE, RECNO) */ |
| 87 |
#define R_SETCURSOR 10 /* put (RECNO) */ |
| 88 |
#define R_RECNOSYNC 11 /* sync (RECNO) */ |
| 89 |
|
| 90 |
typedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE; |
| 91 |
|
| 92 |
/* |
| 93 |
* !!! |
| 94 |
* The following flags are included in the dbopen(3) call as part of the |
| 95 |
* open(2) flags. In order to avoid conflicts with the open flags, start |
| 96 |
* at the top of the 16 or 32-bit number space and work our way down. If |
| 97 |
* the open flags were significantly expanded in the future, it could be |
| 98 |
* a problem. Wish I'd left another flags word in the dbopen call. |
| 99 |
* |
| 100 |
* !!! |
| 101 |
* None of this stuff is implemented yet. The only reason that it's here |
| 102 |
* is so that the access methods can skip copying the key/data pair when |
| 103 |
* the DB_LOCK flag isn't set. |
| 104 |
*/ |
| 105 |
#if UINT_MAX > 65535 |
| 106 |
#define DB_LOCK 0x20000000 /* Do locking. */ |
| 107 |
#define DB_SHMEM 0x40000000 /* Use shared memory. */ |
| 108 |
#define DB_TXN 0x80000000 /* Do transactions. */ |
| 109 |
#else |
| 110 |
#define DB_LOCK 0x2000 /* Do locking. */ |
| 111 |
#define DB_SHMEM 0x4000 /* Use shared memory. */ |
| 112 |
#define DB_TXN 0x8000 /* Do transactions. */ |
| 113 |
#endif |
| 114 |
|
| 115 |
/* Access method description structure. */ |
| 116 |
typedef struct __db { |
| 117 |
DBTYPE type; /* Underlying db type. */ |
| 118 |
int (*close) (struct __db *); |
| 119 |
int (*del) (const struct __db *, const DBT *, unsigned int); |
| 120 |
int (*get) (const struct __db *, const DBT *, DBT *, unsigned int); |
| 121 |
int (*put) (const struct __db *, DBT *, const DBT *, unsigned int); |
| 122 |
int (*seq) (const struct __db *, DBT *, DBT *, unsigned int); |
| 123 |
int (*sync) (const struct __db *, unsigned int); |
| 124 |
void *internal; /* Access method private. */ |
| 125 |
int (*fd) (const struct __db *); |
| 126 |
} DB; |
| 127 |
|
| 128 |
#define BTREEMAGIC 0x053162 |
| 129 |
#define BTREEVERSION 3 |
| 130 |
|
| 131 |
/* Structure used to pass parameters to the btree routines. */ |
| 132 |
typedef struct { |
| 133 |
#define R_DUP 0x01 /* duplicate keys */ |
| 134 |
unsigned long flags; |
| 135 |
unsigned int cachesize; /* bytes to cache */ |
| 136 |
int maxkeypage; /* maximum keys per page */ |
| 137 |
int minkeypage; /* minimum keys per page */ |
| 138 |
unsigned int psize; /* page size */ |
| 139 |
int (*compare) /* comparison function */ |
| 140 |
(const DBT *, const DBT *); |
| 141 |
size_t (*prefix) /* prefix function */ |
| 142 |
(const DBT *, const DBT *); |
| 143 |
int lorder; /* byte order */ |
| 144 |
} BTREEINFO; |
| 145 |
|
| 146 |
#define HASHMAGIC 0x061561 |
| 147 |
#define HASHVERSION 2 |
| 148 |
|
| 149 |
/* Structure used to pass parameters to the hashing routines. */ |
| 150 |
typedef struct { |
| 151 |
unsigned int bsize; /* bucket size */ |
| 152 |
unsigned int ffactor; /* fill factor */ |
| 153 |
unsigned int nelem; /* number of elements */ |
| 154 |
unsigned int cachesize; /* bytes to cache */ |
| 155 |
uint32_t /* hash function */ |
| 156 |
(*hash)(const void *, size_t); |
| 157 |
int lorder; /* byte order */ |
| 158 |
} HASHINFO; |
| 159 |
|
| 160 |
/* Structure used to pass parameters to the record routines. */ |
| 161 |
typedef struct { |
| 162 |
#define R_FIXEDLEN 0x01 /* fixed-length records */ |
| 163 |
#define R_NOKEY 0x02 /* key not required */ |
| 164 |
#define R_SNAPSHOT 0x04 /* snapshot the input */ |
| 165 |
unsigned long flags; |
| 166 |
unsigned int cachesize; /* bytes to cache */ |
| 167 |
unsigned int psize; /* page size */ |
| 168 |
int lorder; /* byte order */ |
| 169 |
size_t reclen; /* record length (fixed-length records) */ |
| 170 |
uint8_t bval; /* delimiting byte (variable-length records */ |
| 171 |
char *bfname; /* btree file name */ |
| 172 |
} RECNOINFO; |
| 173 |
|
| 174 |
#ifdef __DBINTERFACE_PRIVATE |
| 175 |
/* |
| 176 |
* Little endian <==> big endian 32-bit swap macros. |
| 177 |
* M_32_SWAP swap a memory location |
| 178 |
* P_32_SWAP swap a referenced memory location |
| 179 |
* P_32_COPY swap from one location to another |
| 180 |
*/ |
| 181 |
#define M_32_SWAP(a) { \ |
| 182 |
uint32_t _tmp = a; \ |
| 183 |
((char *)(void *)&a)[0] = ((char *)(void *)&_tmp)[3]; \ |
| 184 |
((char *)(void *)&a)[1] = ((char *)(void *)&_tmp)[2]; \ |
| 185 |
((char *)(void *)&a)[2] = ((char *)(void *)&_tmp)[1]; \ |
| 186 |
((char *)(void *)&a)[3] = ((char *)(void *)&_tmp)[0]; \ |
| 187 |
} |
| 188 |
#define P_32_SWAP(a) { \ |
| 189 |
char _tmp[4]; \ |
| 190 |
_tmp[0] = ((char *)(void *)a)[0]; \ |
| 191 |
_tmp[1] = ((char *)(void *)a)[1]; \ |
| 192 |
_tmp[2] = ((char *)(void *)a)[2]; \ |
| 193 |
_tmp[3] = ((char *)(void *)a)[3]; \ |
| 194 |
((char *)(void *)a)[0] = _tmp[3]; \ |
| 195 |
((char *)(void *)a)[1] = _tmp[2]; \ |
| 196 |
((char *)(void *)a)[2] = _tmp[1]; \ |
| 197 |
((char *)(void *)a)[3] = _tmp[0]; \ |
| 198 |
} |
| 199 |
#define P_32_COPY(a, b) { \ |
| 200 |
((char *)(void *)&(b))[0] = ((char *)(void *)&(a))[3]; \ |
| 201 |
((char *)(void *)&(b))[1] = ((char *)(void *)&(a))[2]; \ |
| 202 |
((char *)(void *)&(b))[2] = ((char *)(void *)&(a))[1]; \ |
| 203 |
((char *)(void *)&(b))[3] = ((char *)(void *)&(a))[0]; \ |
| 204 |
} |
| 205 |
|
| 206 |
/* |
| 207 |
* Little endian <==> big endian 16-bit swap macros. |
| 208 |
* M_16_SWAP swap a memory location |
| 209 |
* P_16_SWAP swap a referenced memory location |
| 210 |
* P_16_COPY swap from one location to another |
| 211 |
*/ |
| 212 |
#define M_16_SWAP(a) { \ |
| 213 |
uint16_t _tmp = a; \ |
| 214 |
((char *)(void *)&a)[0] = ((char *)(void *)&_tmp)[1]; \ |
| 215 |
((char *)(void *)&a)[1] = ((char *)(void *)&_tmp)[0]; \ |
| 216 |
} |
| 217 |
#define P_16_SWAP(a) { \ |
| 218 |
char _tmp[2]; \ |
| 219 |
_tmp[0] = ((char *)(void *)a)[0]; \ |
| 220 |
_tmp[1] = ((char *)(void *)a)[1]; \ |
| 221 |
((char *)(void *)a)[0] = _tmp[1]; \ |
| 222 |
((char *)(void *)a)[1] = _tmp[0]; \ |
| 223 |
} |
| 224 |
#define P_16_COPY(a, b) { \ |
| 225 |
((char *)(void *)&(b))[0] = ((char *)(void *)&(a))[1]; \ |
| 226 |
((char *)(void *)&(b))[1] = ((char *)(void *)&(a))[0]; \ |
| 227 |
} |
| 228 |
#endif |
| 229 |
|
| 230 |
__BEGIN_DECLS |
| 231 |
DB *dbopen(const char *, int, mode_t, DBTYPE, const void *); |
| 232 |
|
| 233 |
#ifdef __DBINTERFACE_PRIVATE |
| 234 |
|
| 235 |
#ifndef NBBY |
| 236 |
#define NBBY 8 |
| 237 |
#endif |
| 238 |
|
| 239 |
#define _DBMASK(a) (~((1ULL << (sizeof(a) * NBBY)) - 1)) |
| 240 |
#define _DBFIT(a, t) _DIAGASSERT(((a) & _DBMASK(t)) == 0) |
| 241 |
|
| 242 |
DB *__bt_open(const char *, int, mode_t, const BTREEINFO *, int); |
| 243 |
DB *__hash_open(const char *, int, mode_t, const HASHINFO *, int); |
| 244 |
DB *__rec_open(const char *, int, mode_t, const RECNOINFO *, int); |
| 245 |
void __dbpanic(DB *); |
| 246 |
#endif |
| 247 |
__END_DECLS |
| 248 |
#endif /* !_DB_H_ */ |