1 /* $NetBSD: citrus_db.c,v 1.4 2005/11/29 03:11:58 christos Exp $ */
4 * Copyright (c)2003 Citrus Project,
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 #if defined(LIBC_SCCS) && !defined(lint)
31 __RCSID("$NetBSD: citrus_db.c,v 1.4 2005/11/29 03:11:58 christos Exp $");
32 #endif /* LIBC_SCCS and not lint */
34 #include "namespace.h"
41 #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_file.h"
53 struct _region db_region
;
54 uint32_t (*db_hashfunc
)(void *, struct _citrus_region
*);
55 void *db_hashfunc_closure
;
59 _citrus_db_open(struct _citrus_db
**rdb
, struct _region
*r
, const char *magic
,
60 uint32_t (*hashfunc
)(void *, struct _citrus_region
*),
61 void *hashfunc_closure
)
64 struct _citrus_db
*db
;
65 struct _citrus_db_header_x
*dhx
;
67 _memstream_bind(&ms
, r
);
70 dhx
= _memstream_getregion(&ms
, NULL
, sizeof(*dhx
));
73 if (strncmp(dhx
->dhx_magic
, magic
, _CITRUS_DB_MAGIC_SIZE
) != 0)
75 if (_memstream_seek(&ms
, be32toh(dhx
->dhx_entry_offset
), SEEK_SET
))
78 if (be32toh(dhx
->dhx_num_entries
)*_CITRUS_DB_ENTRY_SIZE
>
79 _memstream_remainder(&ms
))
82 db
= malloc(sizeof(*db
));
86 db
->db_hashfunc
= hashfunc
;
87 db
->db_hashfunc_closure
= hashfunc_closure
;
94 _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 uint32_t hashval
, num_entries
;
105 struct _memstream ms
;
106 struct _citrus_db_header_x
*dhx
;
107 struct _citrus_db_entry_x
*dex
;
108 struct _citrus_region r
;
110 _memstream_bind(&ms
, &db
->db_region
);
112 dhx
= _memstream_getregion(&ms
, NULL
, sizeof(*dhx
));
114 num_entries
= be32toh(dhx
->dhx_num_entries
);
115 if (num_entries
== 0)
118 if (dl
!= NULL
&& dl
->dl_offset
>0) {
119 hashval
= dl
->dl_hashval
;
120 offset
= dl
->dl_offset
;
121 if (offset
>= _region_size(&db
->db_region
))
125 db
->db_hashfunc(db
->db_hashfunc_closure
, key
)%num_entries
;
127 be32toh(dhx
->dhx_entry_offset
) +
128 hashval
* _CITRUS_DB_ENTRY_SIZE
;
130 dl
->dl_hashval
= hashval
;
133 /* seek to the next entry */
134 if (_citrus_memory_stream_seek(&ms
, offset
, SEEK_SET
))
136 /* get the entry record */
137 dex
= _memstream_getregion(&ms
, NULL
, _CITRUS_DB_ENTRY_SIZE
);
141 /* jump to next entry having the same hash value. */
142 offset
= be32toh(dex
->dex_next_offset
);
144 /* save the current position */
146 dl
->dl_offset
= offset
;
148 dl
->dl_offset
= _region_size(&db
->db_region
);
151 /* compare hash value. */
152 if (be32toh(dex
->dex_hash_value
) != hashval
)
155 /* compare key length */
156 if (be32toh(dex
->dex_key_size
) == _region_size(key
)) {
157 /* seek to the head of the key. */
158 if (_memstream_seek(&ms
, be32toh(dex
->dex_key_offset
),
161 /* get the region of the key */
162 if (_memstream_getregion(&ms
, &r
,
163 _region_size(key
)) == NULL
)
165 /* compare key byte stream */
166 if (memcmp(_region_head(&r
), _region_head(key
),
167 _region_size(key
)) == 0) {
170 &ms
, be32toh(dex
->dex_data_offset
),
173 if (_memstream_getregion(
175 be32toh(dex
->dex_data_size
)) == NULL
)
180 } while (offset
!= 0);
186 _citrus_db_lookup_by_string(struct _citrus_db
*db
, const char *key
,
187 struct _citrus_region
*data
,
188 struct _citrus_db_locator
*dl
)
192 _region_init(&r
, __UNCONST(key
), strlen(key
));
194 return _citrus_db_lookup(db
, &r
, data
, dl
);
198 _citrus_db_lookup8_by_string(struct _citrus_db
*db
, const char *key
,
199 uint8_t *rval
, struct _citrus_db_locator
*dl
)
204 ret
= _citrus_db_lookup_by_string(db
, key
, &r
, dl
);
208 if (_region_size(&r
) != 1)
212 memcpy(rval
, _region_head(&r
), 1);
218 _citrus_db_lookup16_by_string(struct _citrus_db
*db
, const char *key
,
219 uint16_t *rval
, struct _citrus_db_locator
*dl
)
225 ret
= _citrus_db_lookup_by_string(db
, key
, &r
, dl
);
229 if (_region_size(&r
) != 2)
233 memcpy(&val
, _region_head(&r
), 2);
234 *rval
= be16toh(val
);
241 _citrus_db_lookup32_by_string(struct _citrus_db
*db
, const char *key
,
242 uint32_t *rval
, struct _citrus_db_locator
*dl
)
248 ret
= _citrus_db_lookup_by_string(db
, key
, &r
, dl
);
252 if (_region_size(&r
) != 4)
256 memcpy(&val
, _region_head(&r
), 4);
257 *rval
= be32toh(val
);
264 _citrus_db_lookup_string_by_string(struct _citrus_db
*db
, const char *key
,
266 struct _citrus_db_locator
*dl
)
271 ret
= _citrus_db_lookup_by_string(db
, key
, &r
, dl
);
275 /* check whether the string is null terminated */
276 if (_region_size(&r
) == 0)
278 if (*((const char*)_region_head(&r
)+_region_size(&r
)-1) != '\0')
282 *rdata
= _region_head(&r
);
288 _citrus_db_get_number_of_entries(struct _citrus_db
*db
)
290 struct _memstream ms
;
291 struct _citrus_db_header_x
*dhx
;
293 _memstream_bind(&ms
, &db
->db_region
);
295 dhx
= _memstream_getregion(&ms
, NULL
, sizeof(*dhx
));
297 return (int)be32toh(dhx
->dhx_num_entries
);
301 _citrus_db_get_entry(struct _citrus_db
*db
, int idx
,
302 struct _region
*key
, struct _region
*data
)
304 uint32_t num_entries
;
306 struct _memstream ms
;
307 struct _citrus_db_header_x
*dhx
;
308 struct _citrus_db_entry_x
*dex
;
310 _memstream_bind(&ms
, &db
->db_region
);
312 dhx
= _memstream_getregion(&ms
, NULL
, sizeof(*dhx
));
314 num_entries
= be32toh(dhx
->dhx_num_entries
);
315 if (idx
< 0 || (uint32_t)idx
>= num_entries
)
318 /* seek to the next entry */
319 offset
= be32toh(dhx
->dhx_entry_offset
) + idx
* _CITRUS_DB_ENTRY_SIZE
;
320 if (_citrus_memory_stream_seek(&ms
, offset
, SEEK_SET
))
322 /* get the entry record */
323 dex
= _memstream_getregion(&ms
, NULL
, _CITRUS_DB_ENTRY_SIZE
);
326 /* seek to the head of the key. */
327 if (_memstream_seek(&ms
, be32toh(dex
->dex_key_offset
), SEEK_SET
))
329 /* get the region of the key. */
330 if (_memstream_getregion(&ms
, key
, be32toh(dex
->dex_key_size
))==NULL
)
332 /* seek to the head of the data. */
333 if (_memstream_seek(&ms
, be32toh(dex
->dex_data_offset
), SEEK_SET
))
335 /* get the region of the data. */
336 if (_memstream_getregion(&ms
, data
, be32toh(dex
->dex_data_size
))==NULL
)