fehQlibs 28
Qlibs
Loading...
Searching...
No Matches
cdbmake.c
Go to the documentation of this file.
1#include <unistd.h>
2#include "seek.h"
3#include "error.h"
4#include "alloc.h"
5#include "buffer.h"
6#include "cdbread.h"
7#include "cdbmake.h"
8
15
16int cdb_make_start(struct cdb_make *c,int fd)
17{
18 c->head = 0;
19 c->split = 0;
20 c->hash = 0;
21 c->numentries = 0;
22 c->fd = fd;
23 c->pos = sizeof(c->final);
24 buffer_init(&c->b,buffer_unixwrite,fd,c->bspace,sizeof(c->bspace));
25 return seek_set(fd,c->pos);
26}
27
28static int posplus(struct cdb_make *c,uint32 len)
29{
30 uint32 newpos = c->pos + len;
31 if (newpos < len) { errno = ENOMEM; return -1; }
32 c->pos = newpos;
33 return 0;
34}
35
36int cdb_make_addend(struct cdb_make *c,unsigned int keylen,unsigned int datalen,uint32 h)
37{
38 struct cdb_hplist *head;
39
40 head = c->head;
41 if (!head || (head->num >= CDB_HPLIST)) {
42 head = (struct cdb_hplist *) alloc(sizeof(struct cdb_hplist));
43 if (!head) return -1;
44 head->num = 0;
45 head->next = c->head;
46 c->head = head;
47 }
48 head->hp[head->num].h = h;
49 head->hp[head->num].p = c->pos;
50 ++head->num;
51 ++c->numentries;
52 if (posplus(c,8) == -1) return -1;
53 if (posplus(c,keylen) == -1) return -1;
54 if (posplus(c,datalen) == -1) return -1;
55 return 0;
56}
57
58int cdb_make_addbegin(struct cdb_make *c,unsigned int keylen,unsigned int datalen)
59{
60 char buf[8];
61
62 if (keylen > 0xffffffff) { errno = ENOMEM; return -1; }
63 if (datalen > 0xffffffff) { errno = ENOMEM; return -1; }
64
65 uint32_pack(buf,keylen);
66 uint32_pack(buf + 4,datalen);
67 if (buffer_putalign(&c->b,buf,8) == -1) return -1;
68 return 0;
69}
70
71int cdb_make_add(struct cdb_make *c,char *key,unsigned int keylen,char *data,unsigned int datalen)
72{
73 if (cdb_make_addbegin(c,keylen,datalen) == -1) return -1;
74 if (buffer_putalign(&c->b,key,keylen) == -1) return -1;
75 if (buffer_putalign(&c->b,data,datalen) == -1) return -1;
76 return cdb_make_addend(c,keylen,datalen,cdb_hash(key,keylen));
77}
78
80{
81 char buf[8];
82 int i;
83 uint32 len;
84 uint32 u;
85 uint32 memsize;
86 uint32 count;
87 uint32 where;
88 struct cdb_hplist *x;
89 struct cdb_hp *hp;
90
91 for (i = 0; i < 256; ++i)
92 c->count[i] = 0;
93
94 for (x = c->head; x; x = x->next) {
95 i = x->num;
96 while (i--)
97 ++c->count[255 & x->hp[i].h];
98 }
99
100 memsize = 1;
101 for (i = 0; i < 256; ++i) {
102 u = c->count[i] * 2;
103 if (u > memsize)
104 memsize = u;
105 }
106
107 memsize += c->numentries; /* no overflow possible up to now */
108 u = (uint32) 0 - (uint32) 1;
109 u /= sizeof(struct cdb_hp);
110 if (memsize > u) { errno = ENOMEM; return -1; }
111
112 c->split = (struct cdb_hp *) alloc(memsize * sizeof(struct cdb_hp));
113 if (!c->split) return -1;
114
115 c->hash = c->split + c->numentries;
116
117 u = 0;
118 for (i = 0; i < 256; ++i) {
119 u += c->count[i]; /* bounded by numentries, so no overflow */
120 c->start[i] = u;
121 }
122
123 for (x = c->head; x; x = x->next) {
124 i = x->num;
125 while (i--)
126 c->split[--c->start[255 & x->hp[i].h]] = x->hp[i];
127 }
128
129 for (i = 0; i < 256; ++i) {
130 count = c->count[i];
131
132 len = count + count; /* no overflow possible */
133 uint32_pack(c->final + 8 * i,c->pos);
134 uint32_pack(c->final + 8 * i + 4,len);
135
136 for (u = 0; u < len; ++u)
137 c->hash[u].h = c->hash[u].p = 0;
138
139 hp = c->split + c->start[i];
140 for (u = 0; u < count; ++u) {
141 where = (hp->h >> 8) % len;
142 while (c->hash[where].p)
143 if (++where == len)
144 where = 0;
145 c->hash[where] = *hp++;
146 }
147
148 for (u = 0; u < len; ++u) {
149 uint32_pack(buf,c->hash[u].h);
150 uint32_pack(buf + 4,c->hash[u].p);
151 if (buffer_putalign(&c->b,buf,8) == -1) return -1;
152 if (posplus(c,8) == -1) return -1;
153 }
154 }
155
156 if (buffer_flush(&c->b) == -1) return -1;
157 if (seek_begin(c->fd) == -1) return -1;
158 return buffer_putflush(&c->b,c->final,sizeof(c->final));
159}
ssize_t buffer_unixwrite(int, char *, size_t)
Definition buffer.c:27
void buffer_init(buffer *, ssize_t(*op)(int, char *, size_t), int, char *, size_t)
Definition buffer.c:13
int buffer_putflush(buffer *, const char *, size_t)
Definition buffer.c:201
int buffer_putalign(buffer *, const char *, size_t)
Definition buffer.c:165
int buffer_flush(buffer *)
Definition buffer.c:155
void uint32_pack(char *, uint32)
uint32_t uint32
Definition uint_t.h:73
#define CDB_HPLIST
Definition cdbmake.h:9
int seek_set(int, seek_pos)
Definition seek.c:31
#define seek_begin(fd)
Definition seek.h:13
uint32 cdb_hash(char *, unsigned int)
Definition cdbread.c:159
void * alloc(unsigned int)
Definition alloc.c:27
int cdb_make_start(struct cdb_make *c, int fd)
Definition cdbmake.c:16
int cdb_make_finish(struct cdb_make *c)
Definition cdbmake.c:79
int cdb_make_addbegin(struct cdb_make *c, unsigned int keylen, unsigned int datalen)
Definition cdbmake.c:58
int cdb_make_addend(struct cdb_make *c, unsigned int keylen, unsigned int datalen, uint32 h)
Definition cdbmake.c:36
int cdb_make_add(struct cdb_make *c, char *key, unsigned int keylen, char *data, unsigned int datalen)
Definition cdbmake.c:71
uint32 p
Definition cdbmake.h:13
uint32 h
Definition cdbmake.h:12
struct cdb_hplist * next
Definition cdbmake.h:18
int num
Definition cdbmake.h:19
struct cdb_hp hp[CDB_HPLIST]
Definition cdbmake.h:17
struct cdb_hplist * head
Definition cdbmake.h:27
struct cdb_hp * hash
Definition cdbmake.h:29
struct cdb_hp * split
Definition cdbmake.h:28
uint32 pos
Definition cdbmake.h:32
uint32 start[256]
Definition cdbmake.h:26
buffer b
Definition cdbmake.h:31
int fd
Definition cdbmake.h:33
char final[2048]
Definition cdbmake.h:24
uint32 numentries
Definition cdbmake.h:30
uint32 count[256]
Definition cdbmake.h:25
char bspace[8192]
Definition cdbmake.h:23