root/libdb/db.h

/* [previous][next][first][last][top][bottom][index][help] */

INCLUDED FROM


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

/* [previous][next][first][last][top][bottom][index][help] */