/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1990, 1993, 19944* The Regents of the University of California. All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. Neither the name of the University nor the names of its contributors15* may be used to endorse or promote products derived from this software16* without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND19* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE22* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL23* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS24* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY27* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF28* SUCH DAMAGE.29*/3031#ifndef _DB_H_32#define _DB_H_3334#include <sys/types.h>35#include <sys/cdefs.h>3637#include <limits.h>3839#define RET_ERROR -1 /* Return values. */40#define RET_SUCCESS 041#define RET_SPECIAL 14243#define MAX_PAGE_NUMBER 0xffffffff /* >= # of pages in a file */44typedef uint32_t pgno_t;45#define MAX_PAGE_OFFSET 65535 /* >= # of bytes in a page */46typedef uint16_t indx_t;47#define MAX_REC_NUMBER 0xffffffff /* >= # of records in a tree */48typedef uint32_t recno_t;4950/* Key/data structure -- a Data-Base Thang. */51typedef struct {52void *data; /* data */53size_t size; /* data length */54} DBT;5556/* Routine flags. */57#define R_CURSOR 1 /* del, put, seq */58#define __R_UNUSED 2 /* UNUSED */59#define R_FIRST 3 /* seq */60#define R_IAFTER 4 /* put (RECNO) */61#define R_IBEFORE 5 /* put (RECNO) */62#define R_LAST 6 /* seq (BTREE, RECNO) */63#define R_NEXT 7 /* seq */64#define R_NOOVERWRITE 8 /* put */65#define R_PREV 9 /* seq (BTREE, RECNO) */66#define R_SETCURSOR 10 /* put (RECNO) */67#define R_RECNOSYNC 11 /* sync (RECNO) */6869typedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE;7071/*72* !!!73* The following flags are included in the dbopen(3) call as part of the74* open(2) flags. In order to avoid conflicts with the open flags, start75* at the top of the 16 or 32-bit number space and work our way down. If76* the open flags were significantly expanded in the future, it could be77* a problem. Wish I'd left another flags word in the dbopen call.78*79* !!!80* None of this stuff is implemented yet. The only reason that it's here81* is so that the access methods can skip copying the key/data pair when82* the DB_LOCK flag isn't set.83*/84#if UINT_MAX > 6553585#define DB_LOCK 0x20000000 /* Do locking. */86#define DB_SHMEM 0x40000000 /* Use shared memory. */87#define DB_TXN 0x80000000 /* Do transactions. */88#else89#define DB_LOCK 0x2000 /* Do locking. */90#define DB_SHMEM 0x4000 /* Use shared memory. */91#define DB_TXN 0x8000 /* Do transactions. */92#endif9394/* Access method description structure. */95typedef struct __db {96DBTYPE type; /* Underlying db type. */97int (*close)(struct __db *);98int (*del)(const struct __db *, const DBT *, unsigned int);99int (*get)(const struct __db *, const DBT *, DBT *, unsigned int);100int (*put)(const struct __db *, DBT *, const DBT *, unsigned int);101int (*seq)(const struct __db *, DBT *, DBT *, unsigned int);102int (*sync)(const struct __db *, unsigned int);103void *internal; /* Access method private. */104int (*fd)(const struct __db *);105} DB;106107#define BTREEMAGIC 0x053162108#define BTREEVERSION 3109110/* Structure used to pass parameters to the btree routines. */111typedef struct {112#define R_DUP 0x01 /* duplicate keys */113unsigned long flags;114unsigned int cachesize; /* bytes to cache */115int maxkeypage; /* maximum keys per page */116int minkeypage; /* minimum keys per page */117unsigned int psize; /* page size */118int (*compare) /* comparison function */119(const DBT *, const DBT *);120size_t (*prefix) /* prefix function */121(const DBT *, const DBT *);122int lorder; /* byte order */123} BTREEINFO;124125#define HASHMAGIC 0x061561126#define HASHVERSION 2127128/* Structure used to pass parameters to the hashing routines. */129typedef struct {130unsigned int bsize; /* bucket size */131unsigned int ffactor; /* fill factor */132unsigned int nelem; /* number of elements */133unsigned int cachesize; /* bytes to cache */134uint32_t /* hash function */135(*hash)(const void *, size_t);136int lorder; /* byte order */137} HASHINFO;138139/* Structure used to pass parameters to the record routines. */140typedef struct {141#define R_FIXEDLEN 0x01 /* fixed-length records */142#define R_NOKEY 0x02 /* key not required */143#define R_SNAPSHOT 0x04 /* snapshot the input */144unsigned long flags;145unsigned int cachesize; /* bytes to cache */146unsigned int psize; /* page size */147int lorder; /* byte order */148size_t reclen; /* record length (fixed-length records) */149unsigned char bval; /* delimiting byte (variable-length records */150char *bfname; /* btree file name */151} RECNOINFO;152153#ifdef __DBINTERFACE_PRIVATE154/*155* Little endian <==> big endian 32-bit swap macros.156* M_32_SWAP swap a memory location157* P_32_SWAP swap a referenced memory location158* P_32_COPY swap from one location to another159*/160#define M_32_SWAP(a) { \161uint32_t _tmp = a; \162((char *)&a)[0] = ((char *)&_tmp)[3]; \163((char *)&a)[1] = ((char *)&_tmp)[2]; \164((char *)&a)[2] = ((char *)&_tmp)[1]; \165((char *)&a)[3] = ((char *)&_tmp)[0]; \166}167#define P_32_SWAP(a) { \168uint32_t _tmp = *(uint32_t *)a; \169((char *)a)[0] = ((char *)&_tmp)[3]; \170((char *)a)[1] = ((char *)&_tmp)[2]; \171((char *)a)[2] = ((char *)&_tmp)[1]; \172((char *)a)[3] = ((char *)&_tmp)[0]; \173}174#define P_32_COPY(a, b) { \175((char *)&(b))[0] = ((char *)&(a))[3]; \176((char *)&(b))[1] = ((char *)&(a))[2]; \177((char *)&(b))[2] = ((char *)&(a))[1]; \178((char *)&(b))[3] = ((char *)&(a))[0]; \179}180181/*182* Little endian <==> big endian 16-bit swap macros.183* M_16_SWAP swap a memory location184* P_16_SWAP swap a referenced memory location185* P_16_COPY swap from one location to another186*/187#define M_16_SWAP(a) { \188uint16_t _tmp = a; \189((char *)&a)[0] = ((char *)&_tmp)[1]; \190((char *)&a)[1] = ((char *)&_tmp)[0]; \191}192#define P_16_SWAP(a) { \193uint16_t _tmp = *(uint16_t *)a; \194((char *)a)[0] = ((char *)&_tmp)[1]; \195((char *)a)[1] = ((char *)&_tmp)[0]; \196}197#define P_16_COPY(a, b) { \198((char *)&(b))[0] = ((char *)&(a))[1]; \199((char *)&(b))[1] = ((char *)&(a))[0]; \200}201#endif202203__BEGIN_DECLS204#if __BSD_VISIBLE205DB *dbopen(const char *, int, int, DBTYPE, const void *);206#endif207208#ifdef __DBINTERFACE_PRIVATE209DB *__bt_open(const char *, int, int, const BTREEINFO *, int);210DB *__hash_open(const char *, int, int, const HASHINFO *, int);211DB *__rec_open(const char *, int, int, const RECNOINFO *, int);212void __dbpanic(DB *dbp);213#endif214__END_DECLS215#endif /* !_DB_H_ */216217218