SDB_API SdbList *ls_newf(SdbListFree freefn) { SdbList *list = ls_new (); if (list) { list->free = freefn; } return list; }
// TODO: use mmap instead of read.. much faster! SDB_VISIBLE Sdb* sdb_new (const char *dir, int lock) { Sdb* s; if (lock && !sdb_lock (sdb_lockfile (dir))) return NULL; s = malloc (sizeof (Sdb)); if (dir && *dir) { s->dir = strdup (dir); s->fd = open (dir, O_RDONLY|O_BINARY); // if (s->fd == -1) // must fail if we cant open for write in sync } else { s->dir = NULL; s->fd = -1; } s->fdump = -1; s->ndump = NULL; s->ns = ls_new (); s->ht = ht_new (); s->lock = lock; s->expire = 0LL; //s->ht->list->free = (SdbListFree)sdb_kv_free; // if open fails ignore cdb_init (&s->db, s->fd); cdb_findstart (&s->db); return s; }
SdbHash* ht_new(void) { SdbHash *ht = R_NEW (SdbHash); if (!ht) return NULL; // TODO: use slices here ht->list = ls_new (); ht->size = hash_sizes[0].size; ht->table = calloc (ht->size, sizeof (*ht->table)); if (!ht->table) { free (ht); return NULL; } ht->size_index = 0; ht->entries = 0; ht->deleted_entries = 0; ht->rehash = hash_sizes[ht->size_index].rehash; ht->max_entries = hash_sizes[ht->size_index].max_entries; return ht; }
void new_insert_exists_destroy(void){ /* our simple hash set */ struct ls_set *set = 0; /* some keys */ char *key_1 = "bbbbb"; char *key_2 = "aaaaa"; char *key_3 = "ccccc"; puts("\ntesting basic functionality"); puts("testing new"); set = ls_new(); assert(set); assert( 32 == set->size ); assert( 0 == ls_nelems(set) ); assert( 0 == ls_load(set) ); puts("testing insert and get"); puts("one"); assert( ls_insert(set, key_1) ); assert( 1 == ls_nelems(set) ); assert( 0 == ls_exists(set, key_2) ); assert( 0 == ls_exists(set, key_3) ); assert(ls_exists(set, key_1) ); puts("two"); assert( ls_insert(set, key_2) ); assert( 2 == ls_nelems(set) ); assert( 0 == ls_exists(set, key_3) ); assert( ls_exists(set, key_2) ); puts("three"); assert( ls_insert(set, key_3) ); assert( 3 == ls_nelems(set) ); assert( ls_exists(set, key_3) ); assert( ls_destroy(set, 1) ); puts("success!"); }
puts("success!"); } void delete(void){ /* our simple hash set */ struct ls_set *set = 0; /* some keys */ char *key_1 = "bacon"; char *key_2 = "chicken"; char *key_3 = "pork"; puts("\ntesting delete functionality "); puts("creating a set"); set = ls_new(); assert(set); assert( 32 == set->size ); assert( 0 == ls_nelems(set) ); puts("inserting some data"); assert( ls_insert(set, key_1) ); assert( 1 == ls_nelems(set) ); assert( 0 == ls_exists(set, key_2) ); assert( 0 == ls_exists(set, key_3) ); assert( ls_exists(set, key_1) ); assert( ls_insert(set, key_2) ); assert( 2 == ls_nelems(set) );