1 /* $NetBSD: citrus_db.c,v 1.5 2008/02/09 14:56:20 junyoung Exp $ */
2 /* $DragonFly: src/lib/libc/citrus/citrus_db.c,v 1.3 2008/04/10 10:21:01 hasso Exp $ */
6 * Copyright (c)2003 Citrus Project,
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/types.h>
32 #include <sys/endian.h>
40 #include "citrus_namespace.h"
41 #include "citrus_bcs.h"
42 #include "citrus_region.h"
43 #include "citrus_memstream.h"
44 #include "citrus_mmap.h"
45 #include "citrus_db.h"
46 #include "citrus_db_file.h"
50 struct _region db_region;
51 uint32_t (*db_hashfunc)(void *, struct _citrus_region *);
52 void *db_hashfunc_closure;
56 _citrus_db_open(struct _citrus_db **rdb, struct _region *r, const char *magic,
57 uint32_t (*hashfunc)(void *, struct _citrus_region *),
58 void *hashfunc_closure)
61 struct _citrus_db *db;
62 struct _citrus_db_header_x *dhx;
64 _memstream_bind(&ms, r);
67 dhx = _memstream_getregion(&ms, NULL, sizeof(*dhx));
70 if (strncmp(dhx->dhx_magic, magic, _CITRUS_DB_MAGIC_SIZE) != 0)
72 if (_memstream_seek(&ms, be32toh(dhx->dhx_entry_offset), SEEK_SET))
75 if (be32toh(dhx->dhx_num_entries)*_CITRUS_DB_ENTRY_SIZE >
76 _memstream_remainder(&ms))
79 db = malloc(sizeof(*db));
83 db->db_hashfunc = hashfunc;
84 db->db_hashfunc_closure = hashfunc_closure;
91 _citrus_db_close(struct _citrus_db *db)
97 _citrus_db_lookup(struct _citrus_db *db, struct _citrus_region *key,
98 struct _citrus_region *data, struct _citrus_db_locator *dl)
100 uint32_t hashval, num_entries;
102 struct _memstream ms;
103 struct _citrus_db_header_x *dhx;
104 struct _citrus_db_entry_x *dex;
105 struct _citrus_region r;
107 _memstream_bind(&ms, &db->db_region);
109 dhx = _memstream_getregion(&ms, NULL, sizeof(*dhx));
111 num_entries = be32toh(dhx->dhx_num_entries);
112 if (num_entries == 0)
115 if (dl != NULL && dl->dl_offset>0) {
116 hashval = dl->dl_hashval;
117 offset = dl->dl_offset;
118 if (offset >= _region_size(&db->db_region))
122 db->db_hashfunc(db->db_hashfunc_closure, key)%num_entries;
124 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,
185 struct _citrus_db_locator *dl)
189 _region_init(&r, __DECONST(char *, key), strlen(key));
191 return _citrus_db_lookup(db, &r, data, dl);
195 _citrus_db_lookup8_by_string(struct _citrus_db *db, const char *key,
196 uint8_t *rval, struct _citrus_db_locator *dl)
201 ret = _citrus_db_lookup_by_string(db, key, &r, dl);
205 if (_region_size(&r) != 1)
209 memcpy(rval, _region_head(&r), 1);
215 _citrus_db_lookup16_by_string(struct _citrus_db *db, const char *key,
216 uint16_t *rval, struct _citrus_db_locator *dl)
222 ret = _citrus_db_lookup_by_string(db, key, &r, dl);
226 if (_region_size(&r) != 2)
230 memcpy(&val, _region_head(&r), 2);
231 *rval = be16toh(val);
238 _citrus_db_lookup32_by_string(struct _citrus_db *db, const char *key,
239 uint32_t *rval, struct _citrus_db_locator *dl)
245 ret = _citrus_db_lookup_by_string(db, key, &r, dl);
249 if (_region_size(&r) != 4)
253 memcpy(&val, _region_head(&r), 4);
254 *rval = be32toh(val);
261 _citrus_db_lookup_string_by_string(struct _citrus_db *db, const char *key,
263 struct _citrus_db_locator *dl)
268 ret = _citrus_db_lookup_by_string(db, key, &r, dl);
272 /* check whether the string is null terminated */
273 if (_region_size(&r) == 0)
275 if (*((const char*)_region_head(&r)+_region_size(&r)-1) != '\0')
279 *rdata = _region_head(&r);
285 _citrus_db_get_number_of_entries(struct _citrus_db *db)
287 struct _memstream ms;
288 struct _citrus_db_header_x *dhx;
290 _memstream_bind(&ms, &db->db_region);
292 dhx = _memstream_getregion(&ms, NULL, sizeof(*dhx));
294 return (int)be32toh(dhx->dhx_num_entries);
298 _citrus_db_get_entry(struct _citrus_db *db, int idx,
299 struct _region *key, struct _region *data)
301 uint32_t num_entries;
303 struct _memstream ms;
304 struct _citrus_db_header_x *dhx;
305 struct _citrus_db_entry_x *dex;
307 _memstream_bind(&ms, &db->db_region);
309 dhx = _memstream_getregion(&ms, NULL, sizeof(*dhx));
311 num_entries = be32toh(dhx->dhx_num_entries);
312 if (idx < 0 || (uint32_t)idx >= num_entries)
315 /* seek to the next entry */
316 offset = be32toh(dhx->dhx_entry_offset) + idx * _CITRUS_DB_ENTRY_SIZE;
317 if (_citrus_memory_stream_seek(&ms, offset, SEEK_SET))
319 /* get the entry record */
320 dex = _memstream_getregion(&ms, NULL, _CITRUS_DB_ENTRY_SIZE);
323 /* seek to the head of the key. */
324 if (_memstream_seek(&ms, be32toh(dex->dex_key_offset), SEEK_SET))
326 /* get the region of the key. */
327 if (_memstream_getregion(&ms, key, be32toh(dex->dex_key_size))==NULL)
329 /* seek to the head of the data. */
330 if (_memstream_seek(&ms, be32toh(dex->dex_data_offset), SEEK_SET))
332 /* get the region of the data. */
333 if (_memstream_getregion(&ms, data, be32toh(dex->dex_data_size))==NULL)