1 | /* gdbm.h - The include file for dbm users. */
|
---|
2 |
|
---|
3 | /* This file is part of GDBM, the GNU data base manager, by Philip A. Nelson.
|
---|
4 | Copyright (C) 1990, 1991, 1993 Free Software Foundation, Inc.
|
---|
5 |
|
---|
6 | GDBM is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 2, or (at your option)
|
---|
9 | any later version.
|
---|
10 |
|
---|
11 | GDBM is distributed in the hope that it will be useful,
|
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | GNU General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with GDBM; see the file COPYING. If not, write to
|
---|
18 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
19 |
|
---|
20 | You may contact the author by:
|
---|
21 | e-mail: [email protected]
|
---|
22 | us-mail: Philip A. Nelson
|
---|
23 | Computer Science Department
|
---|
24 | Western Washington University
|
---|
25 | Bellingham, WA 98226
|
---|
26 |
|
---|
27 | *************************************************************************/
|
---|
28 |
|
---|
29 |
|
---|
30 | /* Protection for multiple includes. */
|
---|
31 | #ifndef _GDBM_H_
|
---|
32 | #define _GDBM_H_
|
---|
33 |
|
---|
34 | /* Parameters to gdbm_open for READERS, WRITERS, and WRITERS who
|
---|
35 | can create the database. */
|
---|
36 | #define GDBM_READER 0 /* A reader. */
|
---|
37 | #define GDBM_WRITER 1 /* A writer. */
|
---|
38 | #define GDBM_WRCREAT 2 /* A writer. Create the db if needed. */
|
---|
39 | #define GDBM_NEWDB 3 /* A writer. Always create a new db. */
|
---|
40 | #define GDBM_FAST 16 /* Write fast! => No fsyncs. */
|
---|
41 |
|
---|
42 | /* Parameters to gdbm_store for simple insertion or replacement in the
|
---|
43 | case that the key is already in the database. */
|
---|
44 | #define GDBM_INSERT 0 /* Never replace old data with new. */
|
---|
45 | #define GDBM_REPLACE 1 /* Always replace old data with new. */
|
---|
46 |
|
---|
47 | /* Parameters to gdbm_setopt, specifing the type of operation to perform. */
|
---|
48 | #define GDBM_CACHESIZE 1 /* Set the cache size. */
|
---|
49 | #define GDBM_FASTMODE 2 /* Toggle fast mode. */
|
---|
50 |
|
---|
51 | /* The data and key structure. This structure is defined for compatibility. */
|
---|
52 | typedef struct {
|
---|
53 | char *dptr;
|
---|
54 | int dsize;
|
---|
55 | } datum;
|
---|
56 |
|
---|
57 |
|
---|
58 | /* The available file space is stored in an "avail" table. The one with
|
---|
59 | most activity is contained in the file header. (See below.) When that
|
---|
60 | one filles up, it is split in half and half is pushed on an "avail
|
---|
61 | stack." When the active avail table is empty and the "avail stack" is
|
---|
62 | not empty, the top of the stack is popped into the active avail table. */
|
---|
63 |
|
---|
64 | /* The following structure is the element of the avaliable table. */
|
---|
65 | typedef struct {
|
---|
66 | int av_size; /* The size of the available block. */
|
---|
67 | off_t av_adr; /* The file address of the available block. */
|
---|
68 | } avail_elem;
|
---|
69 |
|
---|
70 | /* This is the actual table. The in-memory images of the avail blocks are
|
---|
71 | allocated by malloc using a calculated size. */
|
---|
72 | typedef struct {
|
---|
73 | int size; /* The number of avail elements in the table.*/
|
---|
74 | int count; /* The number of entries in the table. */
|
---|
75 | off_t next_block; /* The file address of the next avail block. */
|
---|
76 | avail_elem av_table[1]; /* The table. Make it look like an array. */
|
---|
77 | } avail_block;
|
---|
78 |
|
---|
79 | /* The dbm file header keeps track of the current location of the hash
|
---|
80 | directory and the free space in the file. */
|
---|
81 |
|
---|
82 | typedef struct {
|
---|
83 | word_t header_magic; /* 0x13579ace to make sure the header is good. */
|
---|
84 | int block_size; /* The optimal i/o blocksize from stat. */
|
---|
85 | off_t dir; /* File address of hash directory table. */
|
---|
86 | int dir_size; /* Size in bytes of the table. */
|
---|
87 | int dir_bits; /* The number of address bits used in the table.*/
|
---|
88 | int bucket_size; /* Size in bytes of a hash bucket struct. */
|
---|
89 | int bucket_elems; /* Number of elements in a hash bucket. */
|
---|
90 | off_t next_block; /* The next unallocated block address. */
|
---|
91 | avail_block avail; /* This must be last because of the psuedo
|
---|
92 | array in avail. This avail grows to fill
|
---|
93 | the entire block. */
|
---|
94 | } gdbm_file_header;
|
---|
95 |
|
---|
96 |
|
---|
97 | /* The dbm hash bucket element contains the full 31 bit hash value, the
|
---|
98 | "pointer" to the key and data (stored together) with their sizes. It also
|
---|
99 | has a small part of the actual key value. It is used to verify the first
|
---|
100 | part of the key has the correct value without having to read the actual
|
---|
101 | key. */
|
---|
102 |
|
---|
103 | typedef struct {
|
---|
104 | word_t hash_value; /* The complete 31 bit value. */
|
---|
105 | char key_start[SMALL]; /* Up to the first SMALL bytes of the key. */
|
---|
106 | off_t data_pointer; /* The file address of the key record. The
|
---|
107 | data record directly follows the key. */
|
---|
108 | int key_size; /* Size of key data in the file. */
|
---|
109 | int data_size; /* Size of associated data in the file. */
|
---|
110 | } bucket_element;
|
---|
111 |
|
---|
112 |
|
---|
113 | /* A bucket is a small hash table. This one consists of a number of
|
---|
114 | bucket elements plus some bookkeeping fields. The number of elements
|
---|
115 | depends on the optimum blocksize for the storage device and on a
|
---|
116 | parameter given at file creation time. This bucket takes one block.
|
---|
117 | When one of these tables gets full, it is split into two hash buckets.
|
---|
118 | The contents are split between them by the use of the first few bits
|
---|
119 | of the 31 bit hash function. The location in a bucket is the hash
|
---|
120 | value modulo the size of the bucket. The in-memory images of the
|
---|
121 | buckets are allocated by malloc using a calculated size depending of
|
---|
122 | the file system buffer size. To speed up write, each bucket will have
|
---|
123 | BUCKET_AVAIL avail elements with the bucket. */
|
---|
124 |
|
---|
125 | typedef struct {
|
---|
126 | int av_count; /* The number of bucket_avail entries. */
|
---|
127 | avail_elem bucket_avail[BUCKET_AVAIL]; /* Distributed avail. */
|
---|
128 | int bucket_bits; /* The number of bits used to get here. */
|
---|
129 | int count; /* The number of element buckets full. */
|
---|
130 | bucket_element h_table[1]; /* The table. Make it look like an array.*/
|
---|
131 | } hash_bucket;
|
---|
132 |
|
---|
133 | /* We want to keep from reading buckets as much as possible. The following is
|
---|
134 | to implement a bucket cache. When full, buckets will be dropped in a
|
---|
135 | least recently read from disk order. */
|
---|
136 |
|
---|
137 | /* To speed up fetching and "sequential" access, we need to implement a
|
---|
138 | data cache for key/data pairs read from the file. To find a key, we
|
---|
139 | must exactly match the key from the file. To reduce overhead, the
|
---|
140 | data will be read at the same time. Both key and data will be stored
|
---|
141 | in a data cache. Each bucket cached will have a one element data
|
---|
142 | cache. */
|
---|
143 |
|
---|
144 | typedef struct {
|
---|
145 | word_t hash_val;
|
---|
146 | int data_size;
|
---|
147 | int key_size;
|
---|
148 | char *dptr;
|
---|
149 | int elem_loc;
|
---|
150 | } data_cache_elem;
|
---|
151 |
|
---|
152 | typedef struct {
|
---|
153 | hash_bucket * ca_bucket;
|
---|
154 | off_t ca_adr;
|
---|
155 | char ca_changed; /* Data in the bucket changed. */
|
---|
156 | data_cache_elem ca_data;
|
---|
157 | } cache_elem;
|
---|
158 |
|
---|
159 |
|
---|
160 |
|
---|
161 | /* This final structure contains all main memory based information for
|
---|
162 | a gdbm file. This allows multiple gdbm files to be opened at the same
|
---|
163 | time by one program. */
|
---|
164 |
|
---|
165 | typedef struct {
|
---|
166 | /* Global variables and pointers to dynamic variables used by gdbm. */
|
---|
167 |
|
---|
168 | /* The file name. */
|
---|
169 | char *name;
|
---|
170 |
|
---|
171 | /* The reader/writer status. */
|
---|
172 | int read_write;
|
---|
173 |
|
---|
174 | /* Fast_write is set to 1 if no fsyncs are to be done. */
|
---|
175 | int fast_write;
|
---|
176 |
|
---|
177 | /* The fatal error handling routine. */
|
---|
178 | void (*fatal_err) ();
|
---|
179 |
|
---|
180 | /* The gdbm file descriptor which is set in gdbm_open. */
|
---|
181 | int desc;
|
---|
182 |
|
---|
183 | /* The file header holds information about the database. */
|
---|
184 | gdbm_file_header *header;
|
---|
185 |
|
---|
186 | /* The hash table directory from extendible hashing. See Fagin et al,
|
---|
187 | ACM Trans on Database Systems, Vol 4, No 3. Sept 1979, 315-344 */
|
---|
188 | off_t *dir;
|
---|
189 |
|
---|
190 | /* The bucket cache. */
|
---|
191 | cache_elem *bucket_cache;
|
---|
192 | int cache_size;
|
---|
193 | int last_read;
|
---|
194 |
|
---|
195 | /* Points to the current hash bucket in the cache. */
|
---|
196 | hash_bucket *bucket;
|
---|
197 |
|
---|
198 | /* The directory entry used to get the current hash bucket. */
|
---|
199 | word_t bucket_dir;
|
---|
200 |
|
---|
201 | /* Pointer to the current bucket's cache entry. */
|
---|
202 | cache_elem *cache_entry;
|
---|
203 |
|
---|
204 |
|
---|
205 | /* Bookkeeping of things that need to be written back at the
|
---|
206 | end of an update. */
|
---|
207 | char header_changed;
|
---|
208 | char directory_changed;
|
---|
209 | char bucket_changed;
|
---|
210 | char second_changed;
|
---|
211 |
|
---|
212 | } gdbm_file_info;
|
---|
213 |
|
---|
214 | /* The file information header. This is good enough for most applications. */
|
---|
215 | #define GDBM_FILE gdbm_file_info *
|
---|
216 | /* typedef struct {int dummy[10];} *GDBM_FILE;*/
|
---|
217 |
|
---|
218 | /* Determine if the C(++) compiler requires complete function prototype */
|
---|
219 | #if __STDC__ || defined(__cplusplus) || defined(c_plusplus)
|
---|
220 | #define GDBM_Proto(x) x
|
---|
221 | #else
|
---|
222 | #define GDBM_Proto(x) ()
|
---|
223 | #endif /* NeedFunctionPrototypes */
|
---|
224 |
|
---|
225 | /* External variable, the gdbm build release string. */
|
---|
226 | extern char *gdbm_version;
|
---|
227 |
|
---|
228 |
|
---|
229 | /* GDBM C++ support */
|
---|
230 | #if defined(__cplusplus) || defined(c_plusplus)
|
---|
231 | extern "C" {
|
---|
232 | #endif
|
---|
233 |
|
---|
234 | /* These are the routines! */
|
---|
235 |
|
---|
236 | extern GDBM_FILE gdbm_open GDBM_Proto((
|
---|
237 | char *file,
|
---|
238 | int block_size,
|
---|
239 | int flags,
|
---|
240 | int mode,
|
---|
241 | void (*fatal_func)(),
|
---|
242 | int need_filelock
|
---|
243 | ));
|
---|
244 |
|
---|
245 | extern void gdbm_close GDBM_Proto((
|
---|
246 | GDBM_FILE dbf
|
---|
247 | ));
|
---|
248 |
|
---|
249 | extern int gdbm_store GDBM_Proto((
|
---|
250 | GDBM_FILE dbf,
|
---|
251 | datum key,
|
---|
252 | datum content,
|
---|
253 | int flags
|
---|
254 | ));
|
---|
255 |
|
---|
256 | extern datum gdbm_fetch GDBM_Proto((
|
---|
257 | GDBM_FILE dbf,
|
---|
258 | datum key
|
---|
259 | ));
|
---|
260 |
|
---|
261 | extern int gdbm_delete GDBM_Proto((
|
---|
262 | GDBM_FILE dbf,
|
---|
263 | datum key
|
---|
264 | ));
|
---|
265 |
|
---|
266 | extern datum gdbm_firstkey GDBM_Proto((
|
---|
267 | GDBM_FILE dbf
|
---|
268 | ));
|
---|
269 |
|
---|
270 | extern datum gdbm_nextkey GDBM_Proto((
|
---|
271 | GDBM_FILE dbf,
|
---|
272 | datum key
|
---|
273 | ));
|
---|
274 |
|
---|
275 | extern int gdbm_reorganize GDBM_Proto((
|
---|
276 | GDBM_FILE dbf
|
---|
277 | ));
|
---|
278 |
|
---|
279 | extern void gdbm_sync GDBM_Proto((
|
---|
280 | GDBM_FILE dbf
|
---|
281 | ));
|
---|
282 |
|
---|
283 | extern int gdbm_exists GDBM_Proto((
|
---|
284 | GDBM_FILE dbf,
|
---|
285 | datum key
|
---|
286 | ));
|
---|
287 |
|
---|
288 | extern int gdbm_setopt GDBM_Proto((
|
---|
289 | GDBM_FILE dbf,
|
---|
290 | int optflag,
|
---|
291 | int *optval,
|
---|
292 | int optlen
|
---|
293 | ));
|
---|
294 |
|
---|
295 | #if defined(__cplusplus) || defined(c_plusplus)
|
---|
296 | }
|
---|
297 | #endif
|
---|
298 |
|
---|
299 | /* gdbm sends back the following error codes in the variable gdbm_errno. */
|
---|
300 | typedef enum { GDBM_NO_ERROR,
|
---|
301 | GDBM_MALLOC_ERROR,
|
---|
302 | GDBM_BLOCK_SIZE_ERROR,
|
---|
303 | GDBM_FILE_OPEN_ERROR,
|
---|
304 | GDBM_FILE_WRITE_ERROR,
|
---|
305 | GDBM_FILE_SEEK_ERROR,
|
---|
306 | GDBM_FILE_READ_ERROR,
|
---|
307 | GDBM_BAD_MAGIC_NUMBER,
|
---|
308 | GDBM_EMPTY_DATABASE,
|
---|
309 | GDBM_CANT_BE_READER,
|
---|
310 | GDBM_CANT_BE_WRITER,
|
---|
311 | GDBM_READER_CANT_DELETE,
|
---|
312 | GDBM_READER_CANT_STORE,
|
---|
313 | GDBM_READER_CANT_REORGANIZE,
|
---|
314 | GDBM_UNKNOWN_UPDATE,
|
---|
315 | GDBM_ITEM_NOT_FOUND,
|
---|
316 | GDBM_REORGANIZE_FAILED,
|
---|
317 | GDBM_CANNOT_REPLACE,
|
---|
318 | GDBM_ILLEGAL_DATA,
|
---|
319 | GDBM_OPT_ALREADY_SET,
|
---|
320 | GDBM_OPT_ILLEGAL}
|
---|
321 | gdbm_error;
|
---|
322 |
|
---|
323 | extern gdbm_error gdbm_errno;
|
---|
324 |
|
---|
325 |
|
---|
326 | /* extra prototypes */
|
---|
327 |
|
---|
328 | /* GDBM C++ support */
|
---|
329 | #if defined(__cplusplus) || defined(c_plusplus)
|
---|
330 | extern "C" {
|
---|
331 | #endif
|
---|
332 |
|
---|
333 | extern const char *gdbm_strerror GDBM_Proto((
|
---|
334 | gdbm_error error
|
---|
335 | ));
|
---|
336 |
|
---|
337 | #if defined(__cplusplus) || defined(c_plusplus)
|
---|
338 | }
|
---|
339 | #endif
|
---|
340 |
|
---|
341 | #endif
|
---|
342 |
|
---|
343 |
|
---|