2 /* $NetBSD: citrus_db.c,v 1.5 2008/02/09 14:56:20 junyoung Exp $ */
5 * SPDX-License-Identifier: BSD-2-Clause
7 * Copyright (c)2003 Citrus Project,
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 #include <sys/endian.h>
34 #include <sys/types.h>
43 #include "citrus_namespace.h"
44 #include "citrus_bcs.h"
45 #include "citrus_region.h"
46 #include "citrus_memstream.h"
47 #include "citrus_mmap.h"
48 #include "citrus_db.h"
49 #include "citrus_db_factory.h"
50 #include "citrus_db_file.h"
53 struct _region db_region;
54 _citrus_db_hash_func_t db_hashfunc;
55 void *db_hashfunc_closure;
59 _citrus_db_open(struct _citrus_db **rdb, struct _region *r, const char *magic,
60 _citrus_db_hash_func_t hashfunc, void *hashfunc_closure)
62 struct _citrus_db *db;
63 struct _citrus_db_header_x *dhx;
66 _memstream_bind(&ms, r);
69 dhx = _memstream_getregion(&ms, NULL, sizeof(*dhx));
72 if (strncmp(dhx->dhx_magic, magic, _CITRUS_DB_MAGIC_SIZE) != 0)
74 if (_memstream_seek(&ms, be32toh(dhx->dhx_entry_offset), SEEK_SET))
77 if (be32toh(dhx->dhx_num_entries)*_CITRUS_DB_ENTRY_SIZE >
78 _memstream_remainder(&ms))
81 db = malloc(sizeof(*db));
85 db->db_hashfunc = hashfunc;
86 db->db_hashfunc_closure = hashfunc_closure;
93 _citrus_db_close(struct _citrus_db *db)
100 _citrus_db_lookup(struct _citrus_db *db, struct _citrus_region *key,
101 struct _citrus_region *data, struct _citrus_db_locator *dl)
103 struct _citrus_db_entry_x *dex;
104 struct _citrus_db_header_x *dhx;
105 struct _citrus_region r;
106 struct _memstream ms;
107 uint32_t hashval, num_entries;
110 _memstream_bind(&ms, &db->db_region);
112 dhx = _memstream_getregion(&ms, NULL, sizeof(*dhx));
113 num_entries = be32toh(dhx->dhx_num_entries);
114 if (num_entries == 0)
117 if (dl != NULL && dl->dl_offset>0) {
118 hashval = dl->dl_hashval;
119 offset = dl->dl_offset;
120 if (offset >= _region_size(&db->db_region))
123 hashval = db->db_hashfunc(key)%num_entries;
124 offset = be32toh(dhx->dhx_entry_offset) +
125 hashval * _CITRUS_DB_ENTRY_SIZE;
127 dl->dl_hashval = hashval;
130 /* seek to the next entry */
131 if (_citrus_memory_stream_seek(&ms, offset, SEEK_SET))
133 /* get the entry record */
134 dex = _memstream_getregion(&ms, NULL, _CITRUS_DB_ENTRY_SIZE);
138 /* jump to next entry having the same hash value. */
139 offset = be32toh(dex->dex_next_offset);
141 /* save the current position */
143 dl->dl_offset = offset;
145 dl->dl_offset = _region_size(&db->db_region);
148 /* compare hash value. */
149 if (be32toh(dex->dex_hash_value) != hashval)
152 /* compare key length */
153 if (be32toh(dex->dex_key_size) == _region_size(key)) {
154 /* seek to the head of the key. */
155 if (_memstream_seek(&ms, be32toh(dex->dex_key_offset),
158 /* get the region of the key */
159 if (_memstream_getregion(&ms, &r,
160 _region_size(key)) == NULL)
162 /* compare key byte stream */
163 if (memcmp(_region_head(&r), _region_head(key),
164 _region_size(key)) == 0) {
167 &ms, be32toh(dex->dex_data_offset),
170 if (_memstream_getregion(
172 be32toh(dex->dex_data_size)) == NULL)
177 } while (offset != 0);
183 _citrus_db_lookup_by_string(struct _citrus_db *db, const char *key,
184 struct _citrus_region *data, struct _citrus_db_locator *dl)
188 _region_init(&r, __DECONST(void *, key), strlen(key));
190 return (_citrus_db_lookup(db, &r, data, dl));
194 _citrus_db_lookup8_by_string(struct _citrus_db *db, const char *key,
195 uint8_t *rval, struct _citrus_db_locator *dl)
200 ret = _citrus_db_lookup_by_string(db, key, &r, dl);
204 if (_region_size(&r) != 1)
208 memcpy(rval, _region_head(&r), 1);
214 _citrus_db_lookup16_by_string(struct _citrus_db *db, const char *key,
215 uint16_t *rval, struct _citrus_db_locator *dl)
221 ret = _citrus_db_lookup_by_string(db, key, &r, dl);
225 if (_region_size(&r) != 2)
229 memcpy(&val, _region_head(&r), 2);
230 *rval = be16toh(val);
237 _citrus_db_lookup32_by_string(struct _citrus_db *db, const char *key,
238 uint32_t *rval, struct _citrus_db_locator *dl)
244 ret = _citrus_db_lookup_by_string(db, key, &r, dl);
248 if (_region_size(&r) != 4)
252 memcpy(&val, _region_head(&r), 4);
253 *rval = be32toh(val);
260 _citrus_db_lookup_string_by_string(struct _citrus_db *db, const char *key,
261 const char **rdata, struct _citrus_db_locator *dl)
266 ret = _citrus_db_lookup_by_string(db, key, &r, dl);
270 /* check whether the string is null terminated */
271 if (_region_size(&r) == 0)
273 if (*((const char*)_region_head(&r)+_region_size(&r)-1) != '\0')
277 *rdata = _region_head(&r);
283 _citrus_db_get_number_of_entries(struct _citrus_db *db)
285 struct _citrus_db_header_x *dhx;
286 struct _memstream ms;
288 _memstream_bind(&ms, &db->db_region);
290 dhx = _memstream_getregion(&ms, NULL, sizeof(*dhx));
291 return ((int)be32toh(dhx->dhx_num_entries));
295 _citrus_db_get_entry(struct _citrus_db *db, int idx, struct _region *key,
296 struct _region *data)
298 struct _citrus_db_entry_x *dex;
299 struct _citrus_db_header_x *dhx;
300 struct _memstream ms;
301 uint32_t num_entries;
304 _memstream_bind(&ms, &db->db_region);
306 dhx = _memstream_getregion(&ms, NULL, sizeof(*dhx));
307 num_entries = be32toh(dhx->dhx_num_entries);
308 if (idx < 0 || (uint32_t)idx >= num_entries)
311 /* seek to the next entry */
312 offset = be32toh(dhx->dhx_entry_offset) + idx * _CITRUS_DB_ENTRY_SIZE;
313 if (_citrus_memory_stream_seek(&ms, offset, SEEK_SET))
315 /* get the entry record */
316 dex = _memstream_getregion(&ms, NULL, _CITRUS_DB_ENTRY_SIZE);
319 /* seek to the head of the key. */
320 if (_memstream_seek(&ms, be32toh(dex->dex_key_offset), SEEK_SET))
322 /* get the region of the key. */
323 if (_memstream_getregion(&ms, key, be32toh(dex->dex_key_size))==NULL)
325 /* seek to the head of the data. */
326 if (_memstream_seek(&ms, be32toh(dex->dex_data_offset), SEEK_SET))
328 /* get the region of the data. */
329 if (_memstream_getregion(&ms, data, be32toh(dex->dex_data_size))==NULL)