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