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