159deaec5SRodney W. Grimes /*- 2*2321c474SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 3*2321c474SPedro F. Giffuni * 490fd5bdfSPaul Traina * Copyright (c) 1990, 1993, 1994 559deaec5SRodney W. Grimes * The Regents of the University of California. All rights reserved. 659deaec5SRodney W. Grimes * 759deaec5SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 859deaec5SRodney W. Grimes * modification, are permitted provided that the following conditions 959deaec5SRodney W. Grimes * are met: 1059deaec5SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 1159deaec5SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 1259deaec5SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 1359deaec5SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 1459deaec5SRodney W. Grimes * documentation and/or other materials provided with the distribution. 15f2556687SWarner Losh * 3. Neither the name of the University nor the names of its contributors 1659deaec5SRodney W. Grimes * may be used to endorse or promote products derived from this software 1759deaec5SRodney W. Grimes * without specific prior written permission. 1859deaec5SRodney W. Grimes * 1959deaec5SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2059deaec5SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2159deaec5SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2259deaec5SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2359deaec5SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2459deaec5SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2559deaec5SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2659deaec5SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2759deaec5SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2859deaec5SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2959deaec5SRodney W. Grimes * SUCH DAMAGE. 3059deaec5SRodney W. Grimes */ 3159deaec5SRodney W. Grimes 3259deaec5SRodney W. Grimes #ifndef _DB_H_ 3359deaec5SRodney W. Grimes #define _DB_H_ 3459deaec5SRodney W. Grimes 3559deaec5SRodney W. Grimes #include <sys/types.h> 3659deaec5SRodney W. Grimes #include <sys/cdefs.h> 3759deaec5SRodney W. Grimes 3859deaec5SRodney W. Grimes #include <limits.h> 3959deaec5SRodney W. Grimes 4059deaec5SRodney W. Grimes #define RET_ERROR -1 /* Return values. */ 4159deaec5SRodney W. Grimes #define RET_SUCCESS 0 4259deaec5SRodney W. Grimes #define RET_SPECIAL 1 4359deaec5SRodney W. Grimes 4459deaec5SRodney W. Grimes #define MAX_PAGE_NUMBER 0xffffffff /* >= # of pages in a file */ 45ea3186bdSDavid Schultz typedef uint32_t pgno_t; 4659deaec5SRodney W. Grimes #define MAX_PAGE_OFFSET 65535 /* >= # of bytes in a page */ 47ea3186bdSDavid Schultz typedef uint16_t indx_t; 4859deaec5SRodney W. Grimes #define MAX_REC_NUMBER 0xffffffff /* >= # of records in a tree */ 49ea3186bdSDavid Schultz typedef uint32_t recno_t; 5059deaec5SRodney W. Grimes 5159deaec5SRodney W. Grimes /* Key/data structure -- a Data-Base Thang. */ 5259deaec5SRodney W. Grimes typedef struct { 5359deaec5SRodney W. Grimes void *data; /* data */ 5459deaec5SRodney W. Grimes size_t size; /* data length */ 5559deaec5SRodney W. Grimes } DBT; 5659deaec5SRodney W. Grimes 5759deaec5SRodney W. Grimes /* Routine flags. */ 5859deaec5SRodney W. Grimes #define R_CURSOR 1 /* del, put, seq */ 5959deaec5SRodney W. Grimes #define __R_UNUSED 2 /* UNUSED */ 6059deaec5SRodney W. Grimes #define R_FIRST 3 /* seq */ 6159deaec5SRodney W. Grimes #define R_IAFTER 4 /* put (RECNO) */ 6259deaec5SRodney W. Grimes #define R_IBEFORE 5 /* put (RECNO) */ 6359deaec5SRodney W. Grimes #define R_LAST 6 /* seq (BTREE, RECNO) */ 6459deaec5SRodney W. Grimes #define R_NEXT 7 /* seq */ 6559deaec5SRodney W. Grimes #define R_NOOVERWRITE 8 /* put */ 6659deaec5SRodney W. Grimes #define R_PREV 9 /* seq (BTREE, RECNO) */ 6759deaec5SRodney W. Grimes #define R_SETCURSOR 10 /* put (RECNO) */ 6859deaec5SRodney W. Grimes #define R_RECNOSYNC 11 /* sync (RECNO) */ 6959deaec5SRodney W. Grimes 7059deaec5SRodney W. Grimes typedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE; 7159deaec5SRodney W. Grimes 7259deaec5SRodney W. Grimes /* 7359deaec5SRodney W. Grimes * !!! 7459deaec5SRodney W. Grimes * The following flags are included in the dbopen(3) call as part of the 7559deaec5SRodney W. Grimes * open(2) flags. In order to avoid conflicts with the open flags, start 7659deaec5SRodney W. Grimes * at the top of the 16 or 32-bit number space and work our way down. If 7759deaec5SRodney W. Grimes * the open flags were significantly expanded in the future, it could be 7859deaec5SRodney W. Grimes * a problem. Wish I'd left another flags word in the dbopen call. 7959deaec5SRodney W. Grimes * 8059deaec5SRodney W. Grimes * !!! 8159deaec5SRodney W. Grimes * None of this stuff is implemented yet. The only reason that it's here 8259deaec5SRodney W. Grimes * is so that the access methods can skip copying the key/data pair when 8359deaec5SRodney W. Grimes * the DB_LOCK flag isn't set. 8459deaec5SRodney W. Grimes */ 8559deaec5SRodney W. Grimes #if UINT_MAX > 65535 8659deaec5SRodney W. Grimes #define DB_LOCK 0x20000000 /* Do locking. */ 8759deaec5SRodney W. Grimes #define DB_SHMEM 0x40000000 /* Use shared memory. */ 8859deaec5SRodney W. Grimes #define DB_TXN 0x80000000 /* Do transactions. */ 8959deaec5SRodney W. Grimes #else 9059deaec5SRodney W. Grimes #define DB_LOCK 0x2000 /* Do locking. */ 9159deaec5SRodney W. Grimes #define DB_SHMEM 0x4000 /* Use shared memory. */ 9259deaec5SRodney W. Grimes #define DB_TXN 0x8000 /* Do transactions. */ 9359deaec5SRodney W. Grimes #endif 9459deaec5SRodney W. Grimes 9559deaec5SRodney W. Grimes /* Access method description structure. */ 9659deaec5SRodney W. Grimes typedef struct __db { 9759deaec5SRodney W. Grimes DBTYPE type; /* Underlying db type. */ 98bb28f3c2SWarner Losh int (*close)(struct __db *); 99ea3186bdSDavid Schultz int (*del)(const struct __db *, const DBT *, unsigned int); 100ea3186bdSDavid Schultz int (*get)(const struct __db *, const DBT *, DBT *, unsigned int); 101ea3186bdSDavid Schultz int (*put)(const struct __db *, DBT *, const DBT *, unsigned int); 102ea3186bdSDavid Schultz int (*seq)(const struct __db *, DBT *, DBT *, unsigned int); 103ea3186bdSDavid Schultz int (*sync)(const struct __db *, unsigned int); 10459deaec5SRodney W. Grimes void *internal; /* Access method private. */ 105bb28f3c2SWarner Losh int (*fd)(const struct __db *); 10659deaec5SRodney W. Grimes } DB; 10759deaec5SRodney W. Grimes 10859deaec5SRodney W. Grimes #define BTREEMAGIC 0x053162 10959deaec5SRodney W. Grimes #define BTREEVERSION 3 11059deaec5SRodney W. Grimes 11159deaec5SRodney W. Grimes /* Structure used to pass parameters to the btree routines. */ 11259deaec5SRodney W. Grimes typedef struct { 11359deaec5SRodney W. Grimes #define R_DUP 0x01 /* duplicate keys */ 114ea3186bdSDavid Schultz unsigned long flags; 115ea3186bdSDavid Schultz unsigned int cachesize; /* bytes to cache */ 11659deaec5SRodney W. Grimes int maxkeypage; /* maximum keys per page */ 11759deaec5SRodney W. Grimes int minkeypage; /* minimum keys per page */ 118ea3186bdSDavid Schultz unsigned int psize; /* page size */ 11959deaec5SRodney W. Grimes int (*compare) /* comparison function */ 120bb28f3c2SWarner Losh (const DBT *, const DBT *); 12159deaec5SRodney W. Grimes size_t (*prefix) /* prefix function */ 122bb28f3c2SWarner Losh (const DBT *, const DBT *); 12359deaec5SRodney W. Grimes int lorder; /* byte order */ 12459deaec5SRodney W. Grimes } BTREEINFO; 12559deaec5SRodney W. Grimes 12659deaec5SRodney W. Grimes #define HASHMAGIC 0x061561 12759deaec5SRodney W. Grimes #define HASHVERSION 2 12859deaec5SRodney W. Grimes 12959deaec5SRodney W. Grimes /* Structure used to pass parameters to the hashing routines. */ 13059deaec5SRodney W. Grimes typedef struct { 131ea3186bdSDavid Schultz unsigned int bsize; /* bucket size */ 132ea3186bdSDavid Schultz unsigned int ffactor; /* fill factor */ 133ea3186bdSDavid Schultz unsigned int nelem; /* number of elements */ 134ea3186bdSDavid Schultz unsigned int cachesize; /* bytes to cache */ 135ea3186bdSDavid Schultz uint32_t /* hash function */ 136bb28f3c2SWarner Losh (*hash)(const void *, size_t); 13759deaec5SRodney W. Grimes int lorder; /* byte order */ 13859deaec5SRodney W. Grimes } HASHINFO; 13959deaec5SRodney W. Grimes 14059deaec5SRodney W. Grimes /* Structure used to pass parameters to the record routines. */ 14159deaec5SRodney W. Grimes typedef struct { 14259deaec5SRodney W. Grimes #define R_FIXEDLEN 0x01 /* fixed-length records */ 14359deaec5SRodney W. Grimes #define R_NOKEY 0x02 /* key not required */ 14459deaec5SRodney W. Grimes #define R_SNAPSHOT 0x04 /* snapshot the input */ 145ea3186bdSDavid Schultz unsigned long flags; 146ea3186bdSDavid Schultz unsigned int cachesize; /* bytes to cache */ 147ea3186bdSDavid Schultz unsigned int psize; /* page size */ 14859deaec5SRodney W. Grimes int lorder; /* byte order */ 14959deaec5SRodney W. Grimes size_t reclen; /* record length (fixed-length records) */ 150ea3186bdSDavid Schultz unsigned char bval; /* delimiting byte (variable-length records */ 15159deaec5SRodney W. Grimes char *bfname; /* btree file name */ 15259deaec5SRodney W. Grimes } RECNOINFO; 15359deaec5SRodney W. Grimes 15459deaec5SRodney W. Grimes #ifdef __DBINTERFACE_PRIVATE 15559deaec5SRodney W. Grimes /* 15659deaec5SRodney W. Grimes * Little endian <==> big endian 32-bit swap macros. 15759deaec5SRodney W. Grimes * M_32_SWAP swap a memory location 15859deaec5SRodney W. Grimes * P_32_SWAP swap a referenced memory location 15959deaec5SRodney W. Grimes * P_32_COPY swap from one location to another 16059deaec5SRodney W. Grimes */ 16159deaec5SRodney W. Grimes #define M_32_SWAP(a) { \ 162ea3186bdSDavid Schultz uint32_t _tmp = a; \ 16359deaec5SRodney W. Grimes ((char *)&a)[0] = ((char *)&_tmp)[3]; \ 16459deaec5SRodney W. Grimes ((char *)&a)[1] = ((char *)&_tmp)[2]; \ 16559deaec5SRodney W. Grimes ((char *)&a)[2] = ((char *)&_tmp)[1]; \ 16659deaec5SRodney W. Grimes ((char *)&a)[3] = ((char *)&_tmp)[0]; \ 16759deaec5SRodney W. Grimes } 16859deaec5SRodney W. Grimes #define P_32_SWAP(a) { \ 169ea3186bdSDavid Schultz uint32_t _tmp = *(uint32_t *)a; \ 17059deaec5SRodney W. Grimes ((char *)a)[0] = ((char *)&_tmp)[3]; \ 17159deaec5SRodney W. Grimes ((char *)a)[1] = ((char *)&_tmp)[2]; \ 17259deaec5SRodney W. Grimes ((char *)a)[2] = ((char *)&_tmp)[1]; \ 17359deaec5SRodney W. Grimes ((char *)a)[3] = ((char *)&_tmp)[0]; \ 17459deaec5SRodney W. Grimes } 17559deaec5SRodney W. Grimes #define P_32_COPY(a, b) { \ 17659deaec5SRodney W. Grimes ((char *)&(b))[0] = ((char *)&(a))[3]; \ 17759deaec5SRodney W. Grimes ((char *)&(b))[1] = ((char *)&(a))[2]; \ 17859deaec5SRodney W. Grimes ((char *)&(b))[2] = ((char *)&(a))[1]; \ 17959deaec5SRodney W. Grimes ((char *)&(b))[3] = ((char *)&(a))[0]; \ 18059deaec5SRodney W. Grimes } 18159deaec5SRodney W. Grimes 18259deaec5SRodney W. Grimes /* 18359deaec5SRodney W. Grimes * Little endian <==> big endian 16-bit swap macros. 18459deaec5SRodney W. Grimes * M_16_SWAP swap a memory location 18559deaec5SRodney W. Grimes * P_16_SWAP swap a referenced memory location 18659deaec5SRodney W. Grimes * P_16_COPY swap from one location to another 18759deaec5SRodney W. Grimes */ 18859deaec5SRodney W. Grimes #define M_16_SWAP(a) { \ 189ea3186bdSDavid Schultz uint16_t _tmp = a; \ 19059deaec5SRodney W. Grimes ((char *)&a)[0] = ((char *)&_tmp)[1]; \ 19159deaec5SRodney W. Grimes ((char *)&a)[1] = ((char *)&_tmp)[0]; \ 19259deaec5SRodney W. Grimes } 19359deaec5SRodney W. Grimes #define P_16_SWAP(a) { \ 194ea3186bdSDavid Schultz uint16_t _tmp = *(uint16_t *)a; \ 19559deaec5SRodney W. Grimes ((char *)a)[0] = ((char *)&_tmp)[1]; \ 19659deaec5SRodney W. Grimes ((char *)a)[1] = ((char *)&_tmp)[0]; \ 19759deaec5SRodney W. Grimes } 19859deaec5SRodney W. Grimes #define P_16_COPY(a, b) { \ 19959deaec5SRodney W. Grimes ((char *)&(b))[0] = ((char *)&(a))[1]; \ 20059deaec5SRodney W. Grimes ((char *)&(b))[1] = ((char *)&(a))[0]; \ 20159deaec5SRodney W. Grimes } 20259deaec5SRodney W. Grimes #endif 20359deaec5SRodney W. Grimes 20459deaec5SRodney W. Grimes __BEGIN_DECLS 205ea3186bdSDavid Schultz #if __BSD_VISIBLE 206bb28f3c2SWarner Losh DB *dbopen(const char *, int, int, DBTYPE, const void *); 207ea3186bdSDavid Schultz #endif 20859deaec5SRodney W. Grimes 20959deaec5SRodney W. Grimes #ifdef __DBINTERFACE_PRIVATE 210bb28f3c2SWarner Losh DB *__bt_open(const char *, int, int, const BTREEINFO *, int); 211bb28f3c2SWarner Losh DB *__hash_open(const char *, int, int, const HASHINFO *, int); 212bb28f3c2SWarner Losh DB *__rec_open(const char *, int, int, const RECNOINFO *, int); 213bb28f3c2SWarner Losh void __dbpanic(DB *dbp); 21459deaec5SRodney W. Grimes #endif 21559deaec5SRodney W. Grimes __END_DECLS 21659deaec5SRodney W. Grimes #endif /* !_DB_H_ */ 217