1 /* $NetBSD: citrus_db_factory.c,v 1.9 2008/02/09 14:56:20 junyoung Exp $ */
2 /* $DragonFly: src/lib/libc/citrus/citrus_db_factory.c,v 1.2 2008/04/10 10:21:01 hasso Exp $ */
5 * Copyright (c)2003 Citrus Project,
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/types.h>
31 #include <sys/queue.h>
37 #include <netinet/in.h>
39 #include "citrus_namespace.h"
40 #include "citrus_region.h"
41 #include "citrus_db_file.h"
42 #include "citrus_db_factory.h"
44 struct _citrus_db_factory_entry {
45 STAILQ_ENTRY(_citrus_db_factory_entry) de_entry;
46 struct _citrus_db_factory_entry *de_next;
47 uint32_t de_hashvalue;
48 struct _region de_key;
50 struct _region de_data;
55 struct _citrus_db_factory {
56 size_t df_num_entries;
57 STAILQ_HEAD(, _citrus_db_factory_entry) df_entries;
58 size_t df_total_key_size;
59 size_t df_total_data_size;
60 uint32_t (*df_hashfunc)(void *, struct _citrus_region *);
61 void *df_hashfunc_closure;
67 _citrus_db_factory_create(struct _citrus_db_factory **rdf,
68 _citrus_db_hash_func_t hashfunc,
69 void *hashfunc_closure)
71 struct _citrus_db_factory *df;
73 df = malloc(sizeof(*df));
76 df->df_num_entries = 0;
77 df->df_total_key_size = df->df_total_data_size = 0;
78 STAILQ_INIT(&df->df_entries);
79 df->df_hashfunc = hashfunc;
80 df->df_hashfunc_closure = hashfunc_closure;
88 _citrus_db_factory_free(struct _citrus_db_factory *df)
90 struct _citrus_db_factory_entry *de;
92 while ((de = STAILQ_FIRST(&df->df_entries)) != NULL) {
93 STAILQ_REMOVE_HEAD(&df->df_entries, de_entry);
95 free(_region_head(&de->de_key));
97 free(_region_head(&de->de_data));
103 static __inline size_t
106 return (sz+DB_ALIGN-1) & ~(DB_ALIGN-1);
110 _citrus_db_factory_add(struct _citrus_db_factory *df,
111 struct _region *key, int keyfree,
112 struct _region *data, int datafree)
114 struct _citrus_db_factory_entry *de;
116 de = malloc(sizeof(*de));
120 de->de_hashvalue = df->df_hashfunc(df->df_hashfunc_closure, key);
122 de->de_key_free = keyfree;
124 de->de_data_free = datafree;
127 STAILQ_INSERT_TAIL(&df->df_entries, de, de_entry);
128 df->df_total_key_size += _region_size(key);
129 df->df_total_data_size += ceilto(_region_size(data));
130 df->df_num_entries++;
137 _citrus_db_factory_add_by_string(struct _citrus_db_factory *df,
139 struct _citrus_region *data, int datafree)
146 _region_init(&r, tmp, strlen(key));
147 return _citrus_db_factory_add(df, &r, 1, data, datafree);
151 _citrus_db_factory_add8_by_string(struct _citrus_db_factory *df,
152 const char *key, uint8_t val)
157 p = malloc(sizeof(*p));
161 _region_init(&r, p, 1);
162 return _citrus_db_factory_add_by_string(df, key, &r, 1);
166 _citrus_db_factory_add16_by_string(struct _citrus_db_factory *df,
167 const char *key, uint16_t val)
172 p = malloc(sizeof(*p));
176 _region_init(&r, p, 2);
177 return _citrus_db_factory_add_by_string(df, key, &r, 1);
181 _citrus_db_factory_add32_by_string(struct _citrus_db_factory *df,
182 const char *key, uint32_t val)
187 p = malloc(sizeof(*p));
191 _region_init(&r, p, 4);
192 return _citrus_db_factory_add_by_string(df, key, &r, 1);
196 _citrus_db_factory_add_string_by_string(struct _citrus_db_factory *df,
197 const char *key, const char *data)
205 _region_init(&r, p, strlen(p)+1);
206 return _citrus_db_factory_add_by_string(df, key, &r, 1);
210 _citrus_db_factory_calc_size(struct _citrus_db_factory *df)
214 sz = ceilto(_CITRUS_DB_HEADER_SIZE);
215 sz += ceilto(_CITRUS_DB_ENTRY_SIZE * df->df_num_entries);
216 sz += ceilto(df->df_total_key_size);
217 sz += df->df_total_data_size;
223 put8(struct _region *r, size_t *rofs, uint8_t val)
225 *(uint8_t *)_region_offset(r, *rofs) = val;
230 put16(struct _region *r, size_t *rofs, uint16_t val)
233 memcpy(_region_offset(r, *rofs), &val, 2);
238 put32(struct _region *r, size_t *rofs, uint32_t val)
241 memcpy(_region_offset(r, *rofs), &val, 4);
246 putpad(struct _region *r, size_t *rofs)
249 for (i = ceilto(*rofs) - *rofs; i > 0; i--)
254 dump_header(struct _region *r, const char *magic, size_t *rofs,
257 while (*rofs<_CITRUS_DB_MAGIC_SIZE)
258 put8(r, rofs, *magic++);
259 put32(r, rofs, num_entries);
260 put32(r, rofs, _CITRUS_DB_HEADER_SIZE);
264 _citrus_db_factory_serialize(struct _citrus_db_factory *df, const char *magic,
267 size_t i, ofs, keyofs, dataofs, nextofs;
268 struct _citrus_db_factory_entry *de, **depp, *det;
271 /* check whether more than 0 entries exist */
272 if (df->df_num_entries == 0) {
273 dump_header(r, magic, &ofs, 0);
276 /* allocate hash table */
277 depp = malloc(sizeof(*depp) * df->df_num_entries);
280 for (i = 0; i < df->df_num_entries; i++)
283 /* step1: store the entries which are not conflicting */
284 STAILQ_FOREACH(de, &df->df_entries, de_entry) {
285 de->de_hashvalue %= df->df_num_entries;
288 if (depp[de->de_hashvalue] == NULL) {
289 depp[de->de_hashvalue] = de;
290 de->de_idx = (int)de->de_hashvalue;
294 /* step2: resolve conflicts */
296 STAILQ_FOREACH(de, &df->df_entries, de_entry) {
297 if (de->de_idx == -1) {
298 det = depp[de->de_hashvalue];
299 while (det->de_next != NULL)
302 while (depp[i] != NULL)
310 _CITRUS_DB_HEADER_SIZE +
311 ceilto(df->df_num_entries*_CITRUS_DB_ENTRY_SIZE);
312 dataofs = keyofs + ceilto(df->df_total_key_size);
315 dump_header(r, magic, &ofs, df->df_num_entries);
318 for (i = 0; i < df->df_num_entries; i++) {
323 _CITRUS_DB_HEADER_SIZE +
324 de->de_next->de_idx * _CITRUS_DB_ENTRY_SIZE;
326 put32(r, &ofs, de->de_hashvalue);
327 put32(r, &ofs, nextofs);
328 put32(r, &ofs, keyofs);
329 put32(r, &ofs, _region_size(&de->de_key));
330 put32(r, &ofs, dataofs);
331 put32(r, &ofs, _region_size(&de->de_data));
332 memcpy(_region_offset(r, keyofs),
333 _region_head(&de->de_key), _region_size(&de->de_key));
334 keyofs += _region_size(&de->de_key);
335 memcpy(_region_offset(r, dataofs),
336 _region_head(&de->de_data), _region_size(&de->de_data));
337 dataofs += _region_size(&de->de_data);