1 | /* gdbmdefs.h - The include file for dbm. Defines structure and constants. */
|
---|
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 | #include "systems.h"
|
---|
29 | #include "gdbmconst.h"
|
---|
30 |
|
---|
31 | /* On MacOS X, off_t is "long long int" but on Linux (CentOS) it is "long int"
|
---|
32 | The following ensure that we use the same for both, and so lets
|
---|
33 | GDBM read the same database files */
|
---|
34 |
|
---|
35 | #undef off_t
|
---|
36 | #define off_t long
|
---|
37 |
|
---|
38 | /* The type definitions are next. */
|
---|
39 |
|
---|
40 | /* The data and key structure. This structure is defined for compatibility. */
|
---|
41 |
|
---|
42 | typedef struct {
|
---|
43 | char *dptr;
|
---|
44 | int dsize;
|
---|
45 | } datum;
|
---|
46 |
|
---|
47 |
|
---|
48 | /* The available file space is stored in an "avail" table. The one with
|
---|
49 | most activity is contained in the file header. (See below.) When that
|
---|
50 | one filles up, it is split in half and half is pushed on an "avail
|
---|
51 | stack." When the active avail table is empty and the "avail stack" is
|
---|
52 | not empty, the top of the stack is popped into the active avail table. */
|
---|
53 |
|
---|
54 | /* The following structure is the element of the avaliable table. */
|
---|
55 | typedef struct {
|
---|
56 | int av_size; /* The size of the available block. */
|
---|
57 | off_t av_adr; /* The file address of the available block. */
|
---|
58 | } avail_elem;
|
---|
59 |
|
---|
60 | /* This is the actual table. The in-memory images of the avail blocks are
|
---|
61 | allocated by malloc using a calculated size. */
|
---|
62 | typedef struct {
|
---|
63 | int size; /* The number of avail elements in the table.*/
|
---|
64 | int count; /* The number of entries in the table. */
|
---|
65 | off_t next_block; /* The file address of the next avail block. */
|
---|
66 | avail_elem av_table[1]; /* The table. Make it look like an array. */
|
---|
67 | } avail_block;
|
---|
68 |
|
---|
69 | /* The dbm file header keeps track of the current location of the hash
|
---|
70 | directory and the free space in the file. */
|
---|
71 |
|
---|
72 | typedef struct {
|
---|
73 | int header_magic; /* 0x13579ace to make sure the header is good. */
|
---|
74 | int block_size; /* The optimal i/o blocksize from stat. */
|
---|
75 | off_t dir; /* File address of hash directory table. */
|
---|
76 | int dir_size; /* Size in bytes of the table. */
|
---|
77 | int dir_bits; /* The number of address bits used in the table.*/
|
---|
78 | int bucket_size; /* Size in bytes of a hash bucket struct. */
|
---|
79 | int bucket_elems; /* Number of elements in a hash bucket. */
|
---|
80 | off_t next_block; /* The next unallocated block address. */
|
---|
81 | avail_block avail; /* This must be last because of the psuedo
|
---|
82 | array in avail. This avail grows to fill
|
---|
83 | the entire block. */
|
---|
84 | } gdbm_file_header;
|
---|
85 |
|
---|
86 |
|
---|
87 | /* The dbm hash bucket element contains the full 31 bit hash value, the
|
---|
88 | "pointer" to the key and data (stored together) with their sizes. It also
|
---|
89 | has a small part of the actual key value. It is used to verify the first
|
---|
90 | part of the key has the correct value without having to read the actual
|
---|
91 | key. */
|
---|
92 |
|
---|
93 | typedef struct {
|
---|
94 | int hash_value; /* The complete 31 bit value. */
|
---|
95 | char key_start[SMALL]; /* Up to the first SMALL bytes of the key. */
|
---|
96 | off_t data_pointer; /* The file address of the key record. The
|
---|
97 | data record directly follows the key. */
|
---|
98 | int key_size; /* Size of key data in the file. */
|
---|
99 | int data_size; /* Size of associated data in the file. */
|
---|
100 | } bucket_element;
|
---|
101 |
|
---|
102 |
|
---|
103 | /* A bucket is a small hash table. This one consists of a number of
|
---|
104 | bucket elements plus some bookkeeping fields. The number of elements
|
---|
105 | depends on the optimum blocksize for the storage device and on a
|
---|
106 | parameter given at file creation time. This bucket takes one block.
|
---|
107 | When one of these tables gets full, it is split into two hash buckets.
|
---|
108 | The contents are split between them by the use of the first few bits
|
---|
109 | of the 31 bit hash function. The location in a bucket is the hash
|
---|
110 | value modulo the size of the bucket. The in-memory images of the
|
---|
111 | buckets are allocated by malloc using a calculated size depending of
|
---|
112 | the file system buffer size. To speed up write, each bucket will have
|
---|
113 | BUCKET_AVAIL avail elements with the bucket. */
|
---|
114 |
|
---|
115 | typedef struct {
|
---|
116 | int av_count; /* The number of bucket_avail entries. */
|
---|
117 | avail_elem bucket_avail[BUCKET_AVAIL]; /* Distributed avail. */
|
---|
118 | int bucket_bits; /* The number of bits used to get here. */
|
---|
119 | int count; /* The number of element buckets full. */
|
---|
120 | bucket_element h_table[1]; /* The table. Make it look like an array.*/
|
---|
121 | } hash_bucket;
|
---|
122 |
|
---|
123 | /* We want to keep from reading buckets as much as possible. The following is
|
---|
124 | to implement a bucket cache. When full, buckets will be dropped in a
|
---|
125 | least recently read from disk order. */
|
---|
126 |
|
---|
127 | /* To speed up fetching and "sequential" access, we need to implement a
|
---|
128 | data cache for key/data pairs read from the file. To find a key, we
|
---|
129 | must exactly match the key from the file. To reduce overhead, the
|
---|
130 | data will be read at the same time. Both key and data will be stored
|
---|
131 | in a data cache. Each bucket cached will have a one element data
|
---|
132 | cache. */
|
---|
133 |
|
---|
134 | typedef struct {
|
---|
135 | int hash_val;
|
---|
136 | int data_size;
|
---|
137 | int key_size;
|
---|
138 | char *dptr;
|
---|
139 | int elem_loc;
|
---|
140 | } data_cache_elem;
|
---|
141 |
|
---|
142 | typedef struct {
|
---|
143 | hash_bucket * ca_bucket;
|
---|
144 | off_t ca_adr;
|
---|
145 | char ca_changed; /* Data in the bucket changed. */
|
---|
146 | data_cache_elem ca_data;
|
---|
147 | } cache_elem;
|
---|
148 |
|
---|
149 |
|
---|
150 |
|
---|
151 | /* This final structure contains all main memory based information for
|
---|
152 | a gdbm file. This allows multiple gdbm files to be opened at the same
|
---|
153 | time by one program. */
|
---|
154 |
|
---|
155 | typedef struct {
|
---|
156 | /* Global variables and pointers to dynamic variables used by gdbm. */
|
---|
157 |
|
---|
158 | /* The file name. */
|
---|
159 | char *name;
|
---|
160 |
|
---|
161 | /* The reader/writer status. */
|
---|
162 | int read_write;
|
---|
163 |
|
---|
164 | /* Fast_write is set to 1 if no fsyncs are to be done. */
|
---|
165 | int fast_write;
|
---|
166 |
|
---|
167 | /* Central_free is set if all free blocks are kept in the header. */
|
---|
168 | int central_free;
|
---|
169 |
|
---|
170 | /* Coalesce_blocks is set if we should try to merge free blocks. */
|
---|
171 | int coalesce_blocks;
|
---|
172 |
|
---|
173 | /* Whether or not we should do file locking ourselves. */
|
---|
174 | int file_locking;
|
---|
175 |
|
---|
176 | /* The fatal error handling routine. */
|
---|
177 | void (*fatal_err) ();
|
---|
178 |
|
---|
179 | #ifdef MSDOS
|
---|
180 | int need_filelock;
|
---|
181 | #endif
|
---|
182 |
|
---|
183 | /* The gdbm file descriptor which is set in gdbm_open. */
|
---|
184 | int desc;
|
---|
185 |
|
---|
186 | /* The file header holds information about the database. */
|
---|
187 | gdbm_file_header *header;
|
---|
188 |
|
---|
189 | /* The hash table directory from extendible hashing. See Fagin et al,
|
---|
190 | ACM Trans on Database Systems, Vol 4, No 3. Sept 1979, 315-344 */
|
---|
191 | off_t *dir;
|
---|
192 |
|
---|
193 | /* The bucket cache. */
|
---|
194 | cache_elem *bucket_cache;
|
---|
195 | int cache_size;
|
---|
196 | int last_read;
|
---|
197 |
|
---|
198 | /* Points to the current hash bucket in the cache. */
|
---|
199 | hash_bucket *bucket;
|
---|
200 |
|
---|
201 | /* The directory entry used to get the current hash bucket. */
|
---|
202 | int bucket_dir;
|
---|
203 |
|
---|
204 | /* Pointer to the current bucket's cache entry. */
|
---|
205 | cache_elem *cache_entry;
|
---|
206 |
|
---|
207 |
|
---|
208 | /* Bookkeeping of things that need to be written back at the
|
---|
209 | end of an update. */
|
---|
210 | char header_changed;
|
---|
211 | char directory_changed;
|
---|
212 | char bucket_changed;
|
---|
213 | char second_changed;
|
---|
214 |
|
---|
215 | } gdbm_file_info;
|
---|
216 |
|
---|
217 | /* Now define all the routines in use. */
|
---|
218 | #include "proto.h"
|
---|