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