Main Page | Namespace List | Class List | File List | Class Members | File Members | Related Pages

cdb_make.c

Go to the documentation of this file.
00001 /* Public domain. */
00002 
00003 #include "readwrite.h"
00004 #include "seek.h"
00005 #include "error.h"
00006 #include "alloc.h"
00007 #include "cdb.h"
00008 #include "cdb_make.h"
00009 
00010 int cdb_make_start(struct cdb_make *c,int fd)
00011 {
00012   c->head = 0;
00013   c->split = 0;
00014   c->hash = 0;
00015   c->numentries = 0;
00016   c->fd = fd;
00017   c->pos = sizeof c->final;
00018   buffer_init(&c->b,write,fd,c->bspace,sizeof c->bspace);
00019   return seek_set(fd,c->pos);
00020 }
00021 
00022 static int posplus(struct cdb_make *c,uint32 len)
00023 {
00024   uint32 newpos = c->pos + len;
00025   if (newpos < len) { errno = error_nomem; return -1; }
00026   c->pos = newpos;
00027   return 0;
00028 }
00029 
00030 int cdb_make_addend(struct cdb_make *c,unsigned int keylen,unsigned int datalen,uint32 h)
00031 {
00032   struct cdb_hplist *head;
00033 
00034   head = c->head;
00035   if (!head || (head->num >= CDB_HPLIST)) {
00036     head = (struct cdb_hplist *) alloc(sizeof(struct cdb_hplist));
00037     if (!head) return -1;
00038     head->num = 0;
00039     head->next = c->head;
00040     c->head = head;
00041   }
00042   head->hp[head->num].h = h;
00043   head->hp[head->num].p = c->pos;
00044   ++head->num;
00045   ++c->numentries;
00046   if (posplus(c,8) == -1) return -1;
00047   if (posplus(c,keylen) == -1) return -1;
00048   if (posplus(c,datalen) == -1) return -1;
00049   return 0;
00050 }
00051 
00052 int cdb_make_addbegin(struct cdb_make *c,unsigned int keylen,unsigned int datalen)
00053 {
00054   char buf[8];
00055 
00056   if (keylen > 0xffffffff) { errno = error_nomem; return -1; }
00057   if (datalen > 0xffffffff) { errno = error_nomem; return -1; }
00058 
00059   uint32_pack(buf,keylen);
00060   uint32_pack(buf + 4,datalen);
00061   if (buffer_putalign(&c->b,buf,8) == -1) return -1;
00062   return 0;
00063 }
00064 
00065 int cdb_make_add(struct cdb_make *c,char *key,unsigned int keylen,char *data,unsigned int datalen)
00066 {
00067   if (cdb_make_addbegin(c,keylen,datalen) == -1) return -1;
00068   if (buffer_putalign(&c->b,key,keylen) == -1) return -1;
00069   if (buffer_putalign(&c->b,data,datalen) == -1) return -1;
00070   return cdb_make_addend(c,keylen,datalen,cdb_hash(key,keylen));
00071 }
00072 
00073 int cdb_make_finish(struct cdb_make *c)
00074 {
00075   char buf[8];
00076   int i;
00077   uint32 len;
00078   uint32 u;
00079   uint32 memsize;
00080   uint32 count;
00081   uint32 where;
00082   struct cdb_hplist *x;
00083   struct cdb_hp *hp;
00084 
00085   for (i = 0;i < 256;++i)
00086     c->count[i] = 0;
00087 
00088   for (x = c->head;x;x = x->next) {
00089     i = x->num;
00090     while (i--)
00091       ++c->count[255 & x->hp[i].h];
00092   }
00093 
00094   memsize = 1;
00095   for (i = 0;i < 256;++i) {
00096     u = c->count[i] * 2;
00097     if (u > memsize)
00098       memsize = u;
00099   }
00100 
00101   memsize += c->numentries; /* no overflow possible up to now */
00102   u = (uint32) 0 - (uint32) 1;
00103   u /= sizeof(struct cdb_hp);
00104   if (memsize > u) { errno = error_nomem; return -1; }
00105 
00106   c->split = (struct cdb_hp *) alloc(memsize * sizeof(struct cdb_hp));
00107   if (!c->split) return -1;
00108 
00109   c->hash = c->split + c->numentries;
00110 
00111   u = 0;
00112   for (i = 0;i < 256;++i) {
00113     u += c->count[i]; /* bounded by numentries, so no overflow */
00114     c->start[i] = u;
00115   }
00116 
00117   for (x = c->head;x;x = x->next) {
00118     i = x->num;
00119     while (i--)
00120       c->split[--c->start[255 & x->hp[i].h]] = x->hp[i];
00121   }
00122 
00123   for (i = 0;i < 256;++i) {
00124     count = c->count[i];
00125 
00126     len = count + count; /* no overflow possible */
00127     uint32_pack(c->final + 8 * i,c->pos);
00128     uint32_pack(c->final + 8 * i + 4,len);
00129 
00130     for (u = 0;u < len;++u)
00131       c->hash[u].h = c->hash[u].p = 0;
00132 
00133     hp = c->split + c->start[i];
00134     for (u = 0;u < count;++u) {
00135       where = (hp->h >> 8) % len;
00136       while (c->hash[where].p)
00137         if (++where == len)
00138           where = 0;
00139       c->hash[where] = *hp++;
00140     }
00141 
00142     for (u = 0;u < len;++u) {
00143       uint32_pack(buf,c->hash[u].h);
00144       uint32_pack(buf + 4,c->hash[u].p);
00145       if (buffer_putalign(&c->b,buf,8) == -1) return -1;
00146       if (posplus(c,8) == -1) return -1;
00147     }
00148   }
00149 
00150   if (buffer_flush(&c->b) == -1) return -1;
00151   if (seek_begin(c->fd) == -1) return -1;
00152   return buffer_putflush(&c->b,c->final,sizeof c->final);
00153 }

Generated on Mon Apr 26 09:49:22 2004 for ConstantDataStore by doxygen 1.3.6-20040222