db.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. All advertising materials mentioning features or use of this software
  14. * must display the following acknowledgement:
  15. * This product includes software developed by the University of
  16. * California, Berkeley and its contributors.
  17. * 4. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * @(#)db.h 8.7 (Berkeley) 6/16/94
  34. */
  35. #ifndef _DB_H
  36. #define _DB_H 1
  37. #include <sys/types.h>
  38. #include <sys/cdefs.h>
  39. #include <limits.h>
  40. #ifdef __DBINTERFACE_PRIVATE
  41. #include <compat.h>
  42. #endif
  43. #define RET_ERROR -1 /* Return values. */
  44. #define RET_SUCCESS 0
  45. #define RET_SPECIAL 1
  46. #ifndef __BIT_TYPES_DEFINED__
  47. #define __BIT_TYPES_DEFINED__
  48. #if (!defined(__FreeBSD__) && !defined(__APPLE__))
  49. typedef __signed char int8_t;
  50. typedef unsigned char u_int8_t;
  51. typedef short int16_t;
  52. typedef unsigned short u_int16_t;
  53. typedef int int32_t;
  54. typedef unsigned int u_int32_t;
  55. #ifdef WE_DONT_NEED_QUADS
  56. typedef long long int64_t;
  57. typedef unsigned long long u_int64_t;
  58. #endif
  59. #endif /* __FreeBSD__ */
  60. #endif
  61. #define MAX_PAGE_NUMBER 0xffffffff /* >= # of pages in a file */
  62. typedef u_int32_t pgno_t;
  63. #define MAX_PAGE_OFFSET 65535 /* >= # of bytes in a page */
  64. typedef u_int16_t indx_t;
  65. #define MAX_REC_NUMBER 0xffffffff /* >= # of records in a tree */
  66. typedef u_int32_t recno_t;
  67. /* Key/data structure -- a Data-Base Thang. */
  68. typedef struct {
  69. void *data; /* data */
  70. size_t size; /* data length */
  71. } DBT;
  72. /* Routine flags. */
  73. #define R_CURSOR 1 /* del, put, seq */
  74. #define __R_UNUSED 2 /* UNUSED */
  75. #define R_FIRST 3 /* seq */
  76. #define R_IAFTER 4 /* put (RECNO) */
  77. #define R_IBEFORE 5 /* put (RECNO) */
  78. #define R_LAST 6 /* seq (BTREE, RECNO) */
  79. #define R_NEXT 7 /* seq */
  80. #define R_NOOVERWRITE 8 /* put */
  81. #define R_PREV 9 /* seq (BTREE, RECNO) */
  82. #define R_SETCURSOR 10 /* put (RECNO) */
  83. #define R_RECNOSYNC 11 /* sync (RECNO) */
  84. typedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE;
  85. /*
  86. * !!!
  87. * The following flags are included in the dbopen(3) call as part of the
  88. * open(2) flags. In order to avoid conflicts with the open flags, start
  89. * at the top of the 16 or 32-bit number space and work our way down. If
  90. * the open flags were significantly expanded in the future, it could be
  91. * a problem. Wish I'd left another flags word in the dbopen call.
  92. *
  93. * !!!
  94. * None of this stuff is implemented yet. The only reason that it's here
  95. * is so that the access methods can skip copying the key/data pair when
  96. * the DB_LOCK flag isn't set.
  97. */
  98. #if UINT_MAX > 65535
  99. #define DB_LOCK 0x20000000 /* Do locking. */
  100. #define DB_SHMEM 0x40000000 /* Use shared memory. */
  101. #define DB_TXN 0x80000000 /* Do transactions. */
  102. #else
  103. #define DB_LOCK 0x2000 /* Do locking. */
  104. #define DB_SHMEM 0x4000 /* Use shared memory. */
  105. #define DB_TXN 0x8000 /* Do transactions. */
  106. #endif
  107. /* Access method description structure. */
  108. typedef struct __db {
  109. DBTYPE type; /* Underlying db type. */
  110. int (*close) __P((struct __db *));
  111. int (*del) __P((const struct __db *, const DBT *, u_int));
  112. int (*get) __P((const struct __db *, const DBT *, DBT *, u_int));
  113. int (*put) __P((const struct __db *, DBT *, const DBT *, u_int));
  114. int (*seq) __P((const struct __db *, DBT *, DBT *, u_int));
  115. int (*sync) __P((const struct __db *, u_int));
  116. void *internal; /* Access method private. */
  117. int (*fd) __P((const struct __db *));
  118. } DB;
  119. #define BTREEMAGIC 0x053162
  120. #define BTREEVERSION 3
  121. /* Structure used to pass parameters to the btree routines. */
  122. typedef struct {
  123. #define R_DUP 0x01 /* duplicate keys */
  124. u_long flags;
  125. u_int cachesize; /* bytes to cache */
  126. int maxkeypage; /* maximum keys per page */
  127. int minkeypage; /* minimum keys per page */
  128. u_int psize; /* page size */
  129. int (*compare) /* comparison function */
  130. __P((const DBT *, const DBT *));
  131. size_t (*prefix) /* prefix function */
  132. __P((const DBT *, const DBT *));
  133. int lorder; /* byte order */
  134. } BTREEINFO;
  135. #define HASHMAGIC 0x061561
  136. #define HASHVERSION 2
  137. /* Structure used to pass parameters to the hashing routines. */
  138. typedef struct {
  139. u_int bsize; /* bucket size */
  140. u_int ffactor; /* fill factor */
  141. u_int nelem; /* number of elements */
  142. u_int cachesize; /* bytes to cache */
  143. u_int32_t /* hash function */
  144. (*hash) __P((const void *, size_t));
  145. int lorder; /* byte order */
  146. } HASHINFO;
  147. /* Structure used to pass parameters to the record routines. */
  148. typedef struct {
  149. #define R_FIXEDLEN 0x01 /* fixed-length records */
  150. #define R_NOKEY 0x02 /* key not required */
  151. #define R_SNAPSHOT 0x04 /* snapshot the input */
  152. u_long flags;
  153. u_int cachesize; /* bytes to cache */
  154. u_int psize; /* page size */
  155. int lorder; /* byte order */
  156. size_t reclen; /* record length (fixed-length records) */
  157. u_char bval; /* delimiting byte (variable-length records */
  158. char *bfname; /* btree file name */
  159. } RECNOINFO;
  160. #ifdef __DBINTERFACE_PRIVATE
  161. /*
  162. * Little endian <==> big endian 32-bit swap macros.
  163. * M_32_SWAP swap a memory location
  164. * P_32_SWAP swap a referenced memory location
  165. * P_32_COPY swap from one location to another
  166. */
  167. #define M_32_SWAP(a) { \
  168. u_int32_t _tmp = 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_SWAP(a) { \
  175. u_int32_t _tmp = *(u_int32_t *)a; \
  176. ((char *)a)[0] = ((char *)&_tmp)[3]; \
  177. ((char *)a)[1] = ((char *)&_tmp)[2]; \
  178. ((char *)a)[2] = ((char *)&_tmp)[1]; \
  179. ((char *)a)[3] = ((char *)&_tmp)[0]; \
  180. }
  181. #define P_32_COPY(a, b) { \
  182. ((char *)&(b))[0] = ((char *)&(a))[3]; \
  183. ((char *)&(b))[1] = ((char *)&(a))[2]; \
  184. ((char *)&(b))[2] = ((char *)&(a))[1]; \
  185. ((char *)&(b))[3] = ((char *)&(a))[0]; \
  186. }
  187. /*
  188. * Little endian <==> big endian 16-bit swap macros.
  189. * M_16_SWAP swap a memory location
  190. * P_16_SWAP swap a referenced memory location
  191. * P_16_COPY swap from one location to another
  192. */
  193. #define M_16_SWAP(a) { \
  194. u_int16_t _tmp = a; \
  195. ((char *)&a)[0] = ((char *)&_tmp)[1]; \
  196. ((char *)&a)[1] = ((char *)&_tmp)[0]; \
  197. }
  198. #define P_16_SWAP(a) { \
  199. u_int16_t _tmp = *(u_int16_t *)a; \
  200. ((char *)a)[0] = ((char *)&_tmp)[1]; \
  201. ((char *)a)[1] = ((char *)&_tmp)[0]; \
  202. }
  203. #define P_16_COPY(a, b) { \
  204. ((char *)&(b))[0] = ((char *)&(a))[1]; \
  205. ((char *)&(b))[1] = ((char *)&(a))[0]; \
  206. }
  207. #endif
  208. __BEGIN_DECLS
  209. DB *__dbopen __P((const char *, int, int, DBTYPE, const void *));
  210. DB *dbopen __P((const char *, int, int, DBTYPE, const void *));
  211. #ifdef __DBINTERFACE_PRIVATE
  212. DB *__bt_open __P((const char *, int, int, const BTREEINFO *, int));
  213. DB *__hash_open __P((const char *, int, int, const HASHINFO *, int));
  214. DB *__rec_open __P((const char *, int, int, const RECNOINFO *, int));
  215. void __dbpanic __P((DB *dbp));
  216. #endif
  217. __END_DECLS
  218. #endif /* db.h */