1/*- 2 * SPDX-License-Identifier: BSD-3-Clause 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 32#ifndef _DB_H_ 33#define _DB_H_ 34 35#include <sys/types.h> 36#include <sys/cdefs.h> 37 38#include <limits.h> 39 40#define RET_ERROR -1 /* Return values. */ 41#define RET_SUCCESS 0 42#define RET_SPECIAL 1 43 44#define MAX_PAGE_NUMBER 0xffffffff /* >= # of pages in a file */ 45typedef uint32_t pgno_t; 46#define MAX_PAGE_OFFSET 65535 /* >= # of bytes in a page */ 47typedef uint16_t indx_t; 48#define MAX_REC_NUMBER 0xffffffff /* >= # of records in a tree */ 49typedef uint32_t recno_t; 50 51/* Key/data structure -- a Data-Base Thang. */ 52typedef struct { 53 void *data; /* data */ 54 size_t size; /* data length */ 55} DBT; 56 57/* Routine flags. */ 58#define R_CURSOR 1 /* del, put, seq */ 59#define __R_UNUSED 2 /* UNUSED */ 60#define R_FIRST 3 /* seq */ 61#define R_IAFTER 4 /* put (RECNO) */ 62#define R_IBEFORE 5 /* put (RECNO) */ 63#define R_LAST 6 /* seq (BTREE, RECNO) */ 64#define R_NEXT 7 /* seq */ 65#define R_NOOVERWRITE 8 /* put */ 66#define R_PREV 9 /* seq (BTREE, RECNO) */ 67#define R_SETCURSOR 10 /* put (RECNO) */ 68#define R_RECNOSYNC 11 /* sync (RECNO) */ 69 70typedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE; 71 72/* 73 * !!! 74 * The following flags are included in the dbopen(3) call as part of the 75 * open(2) flags. In order to avoid conflicts with the open flags, start 76 * at the top of the 16 or 32-bit number space and work our way down. If 77 * the open flags were significantly expanded in the future, it could be 78 * a problem. Wish I'd left another flags word in the dbopen call. 79 * 80 * !!! 81 * None of this stuff is implemented yet. The only reason that it's here 82 * is so that the access methods can skip copying the key/data pair when 83 * the DB_LOCK flag isn't set. 84 */ 85#if UINT_MAX > 65535 86#define DB_LOCK 0x20000000 /* Do locking. */ 87#define DB_SHMEM 0x40000000 /* Use shared memory. */ 88#define DB_TXN 0x80000000 /* Do transactions. */ 89#else 90#define DB_LOCK 0x2000 /* Do locking. */ 91#define DB_SHMEM 0x4000 /* Use shared memory. */ 92#define DB_TXN 0x8000 /* Do transactions. */ 93#endif 94 95/* Access method description structure. */ 96typedef struct __db { 97 DBTYPE type; /* Underlying db type. */ 98 int (*close)(struct __db *); 99 int (*del)(const struct __db *, const DBT *, unsigned int); 100 int (*get)(const struct __db *, const DBT *, DBT *, unsigned int); 101 int (*put)(const struct __db *, DBT *, const DBT *, unsigned int); 102 int (*seq)(const struct __db *, DBT *, DBT *, unsigned int); 103 int (*sync)(const struct __db *, unsigned int); 104 void *internal; /* Access method private. */ 105 int (*fd)(const struct __db *); 106} DB; 107 108#define BTREEMAGIC 0x053162 109#define BTREEVERSION 3 110 111/* Structure used to pass parameters to the btree routines. */ 112typedef struct { 113#define R_DUP 0x01 /* duplicate keys */ 114 unsigned long flags; 115 unsigned int cachesize; /* bytes to cache */ 116 int maxkeypage; /* maximum keys per page */ 117 int minkeypage; /* minimum keys per page */ 118 unsigned int psize; /* page size */ 119 int (*compare) /* comparison function */ 120 (const DBT *, const DBT *); 121 size_t (*prefix) /* prefix function */ 122 (const DBT *, const DBT *); 123 int lorder; /* byte order */ 124} BTREEINFO; 125 126#define HASHMAGIC 0x061561 127#define HASHVERSION 2 128 129/* Structure used to pass parameters to the hashing routines. */ 130typedef struct { 131 unsigned int bsize; /* bucket size */ 132 unsigned int ffactor; /* fill factor */ 133 unsigned int nelem; /* number of elements */ 134 unsigned int cachesize; /* bytes to cache */ 135 uint32_t /* hash function */ 136 (*hash)(const void *, size_t); 137 int lorder; /* byte order */ 138} HASHINFO; 139 140/* Structure used to pass parameters to the record routines. */ 141typedef struct { 142#define R_FIXEDLEN 0x01 /* fixed-length records */ 143#define R_NOKEY 0x02 /* key not required */ 144#define R_SNAPSHOT 0x04 /* snapshot the input */ 145 unsigned long flags; 146 unsigned int cachesize; /* bytes to cache */ 147 unsigned int psize; /* page size */ 148 int lorder; /* byte order */ 149 size_t reclen; /* record length (fixed-length records) */ 150 unsigned char bval; /* delimiting byte (variable-length records */ 151 char *bfname; /* btree file name */ 152} RECNOINFO; 153 154#ifdef __DBINTERFACE_PRIVATE 155/* 156 * Little endian <==> big endian 32-bit swap macros. 157 * M_32_SWAP swap a memory location 158 * P_32_SWAP swap a referenced memory location 159 * P_32_COPY swap from one location to another 160 */ 161#define M_32_SWAP(a) { \ 162 uint32_t _tmp = a; \ 163 ((char *)&a)[0] = ((char *)&_tmp)[3]; \ 164 ((char *)&a)[1] = ((char *)&_tmp)[2]; \ 165 ((char *)&a)[2] = ((char *)&_tmp)[1]; \ 166 ((char *)&a)[3] = ((char *)&_tmp)[0]; \ 167} 168#define P_32_SWAP(a) { \ 169 uint32_t _tmp = *(uint32_t *)a; \ 170 ((char *)a)[0] = ((char *)&_tmp)[3]; \ 171 ((char *)a)[1] = ((char *)&_tmp)[2]; \ 172 ((char *)a)[2] = ((char *)&_tmp)[1]; \ 173 ((char *)a)[3] = ((char *)&_tmp)[0]; \ 174} 175#define P_32_COPY(a, b) { \ 176 ((char *)&(b))[0] = ((char *)&(a))[3]; \ 177 ((char *)&(b))[1] = ((char *)&(a))[2]; \ 178 ((char *)&(b))[2] = ((char *)&(a))[1]; \ 179 ((char *)&(b))[3] = ((char *)&(a))[0]; \ 180} 181 182/* 183 * Little endian <==> big endian 16-bit swap macros. 184 * M_16_SWAP swap a memory location 185 * P_16_SWAP swap a referenced memory location 186 * P_16_COPY swap from one location to another 187 */ 188#define M_16_SWAP(a) { \ 189 uint16_t _tmp = a; \ 190 ((char *)&a)[0] = ((char *)&_tmp)[1]; \ 191 ((char *)&a)[1] = ((char *)&_tmp)[0]; \ 192} 193#define P_16_SWAP(a) { \ 194 uint16_t _tmp = *(uint16_t *)a; \ 195 ((char *)a)[0] = ((char *)&_tmp)[1]; \ 196 ((char *)a)[1] = ((char *)&_tmp)[0]; \ 197} 198#define P_16_COPY(a, b) { \ 199 ((char *)&(b))[0] = ((char *)&(a))[1]; \ 200 ((char *)&(b))[1] = ((char *)&(a))[0]; \ 201} 202#endif 203 204__BEGIN_DECLS 205#if __BSD_VISIBLE 206DB *dbopen(const char *, int, int, DBTYPE, const void *); 207#endif 208 209#ifdef __DBINTERFACE_PRIVATE 210DB *__bt_open(const char *, int, int, const BTREEINFO *, int); 211DB *__hash_open(const char *, int, int, const HASHINFO *, int); 212DB *__rec_open(const char *, int, int, const RECNOINFO *, int); 213void __dbpanic(DB *dbp); 214#endif 215__END_DECLS 216#endif /* !_DB_H_ */ 217