Update sdb

This commit is contained in:
pancake 2014-06-05 23:59:58 +02:00
parent b564af7d09
commit 8ddc053be9
7 changed files with 43 additions and 14 deletions

View File

@ -126,7 +126,7 @@ ut64 sdb_num_dec (Sdb* s, const char *key, ut64 n, ut32 cas);
/* locking */
int sdb_lock(const char *s);
const char *sdb_lockfile(const char *f);
const char *sdb_lock_file(const char *f);
void sdb_unlock(const char *s);
SDB_API int sdb_unlink (Sdb* s);
SDB_API int sdb_lock_wait(const char *s UNUSED);
@ -159,7 +159,8 @@ const char *sdb_json_format(SdbJsonString* s, const char *fmt, ...);
#define sdb_json_format_free(x) free ((x)->buf)
// namespace
Sdb* sdb_ns (Sdb *s, const char *name);
Sdb* sdb_ns (Sdb *s, const char *name, int create);
Sdb *sdb_ns_path(Sdb *s, const char *path, int create);
void sdb_ns_init (Sdb* s);
void sdb_ns_free (Sdb* s);
void sdb_ns_lock(Sdb *s, int lock, int depth);

View File

@ -32,7 +32,7 @@ endif
CFLAGS+=-Wall
#CFLAGS+=-O3
#CFLAGS+=-ggdb -g -Wall -O0
CFLAGS+=-O2
CFLAGS+=-g
HAVE_VALA=#$(shell valac --version 2> /dev/null)
# This is hacky

View File

@ -1,4 +1,4 @@
/* sdb - LGPLv3 - Copyright 2012-2013 - pancake */
/* sdb - LGPLv3 - Copyright 2012-2014 - pancake */
#include <stdio.h>
#include <string.h>
@ -9,7 +9,7 @@
#include <windows.h>
#endif
SDB_API const char *sdb_lockfile(const char *f) {
SDB_API const char *sdb_lock_file(const char *f) {
static char buf[128];
size_t len;
if (!f || !*f)

View File

@ -135,7 +135,7 @@ SDB_API int sdb_ns_set (Sdb *s, const char *name, Sdb *r) {
return 1;
}
SDB_API Sdb *sdb_ns(Sdb *s, const char *name) {
SDB_API Sdb *sdb_ns(Sdb *s, const char *name, int create) {
SdbListIter *it;
SdbNs *ns;
ut32 hash;
@ -146,6 +146,8 @@ SDB_API Sdb *sdb_ns(Sdb *s, const char *name) {
if (ns->hash == hash)
return ns->sdb;
}
if (!create)
return NULL;
if (s->ns_lock)
return NULL;
ns = sdb_ns_new (s, name, hash);
@ -154,6 +156,30 @@ SDB_API Sdb *sdb_ns(Sdb *s, const char *name) {
return ns->sdb;
}
SDB_API Sdb *sdb_ns_path(Sdb *s, const char *path, int create) {
char *ptr, *str;
char *slash;
if (!s || !path)
return NULL;
ptr = str = strdup (path);
do {
slash = strchr (ptr, '/');
if (slash)
*slash = 0;
s = sdb_ns (s, ptr, create);
if (!s) {
free (str);
return NULL;
}
//s = ns->sdb;
if (slash)
ptr = slash+1;
} while (slash);
free (str);
return s;
}
static void ns_sync (Sdb *s, SdbList *list) {
SdbNs *ns;
SdbListIter *it;

View File

@ -170,7 +170,7 @@ next_quote:
if (arroba) {
next_arroba:
*arroba = 0;
s = sdb_ns (s, cmd);
s = sdb_ns (s, cmd, eq?1:0);
if (!s) {
eprintf ("Cant find namespace %s\n", cmd);
free (out);

View File

@ -42,11 +42,11 @@ SDB_API Sdb* sdb_new (const char *path, const char *name, int lock) {
}
switch (lock) {
case 1:
if (!sdb_lock (sdb_lockfile (s->dir)))
if (!sdb_lock (sdb_lock_file (s->dir)))
goto fail;
break;
case 2:
if (!sdb_lock_wait (sdb_lockfile (s->dir)))
if (!sdb_lock_wait (sdb_lock_file (s->dir)))
goto fail;
break;
}
@ -102,11 +102,11 @@ fail:
// XXX: this is wrong. stuff not stored in memory is lost
SDB_API void sdb_file (Sdb* s, const char *dir) {
if (s->lock)
sdb_unlock (sdb_lockfile (s->dir));
sdb_unlock (sdb_lock_file (s->dir));
free (s->dir);
s->dir = (dir && *dir)? strdup (dir): NULL;
if (s->lock)
sdb_lock (sdb_lockfile (s->dir));
sdb_lock (sdb_lock_file (s->dir));
}
static void sdb_fini(Sdb* s, int donull) {
@ -114,7 +114,7 @@ static void sdb_fini(Sdb* s, int donull) {
sdb_hook_free (s);
cdb_free (&s->db);
if (s->lock)
sdb_unlock (sdb_lockfile (s->dir));
sdb_unlock (sdb_lock_file (s->dir));
sdb_ns_free (s);
s->refs = 0;
free (s->name);
@ -672,6 +672,7 @@ static int unset_cb(void *user, const char *k, const char *v) {
return 1;
}
// TODO: rename to sdb_unset_similar ?
SDB_API int sdb_unset_matching(Sdb *s, const char *k) {
UnsetCallbackData ucd = { s, k };
return sdb_foreach (s, unset_cb, &ucd);

View File

@ -126,7 +126,7 @@ ut64 sdb_num_dec (Sdb* s, const char *key, ut64 n, ut32 cas);
/* locking */
int sdb_lock(const char *s);
const char *sdb_lockfile(const char *f);
const char *sdb_lock_file(const char *f);
void sdb_unlock(const char *s);
SDB_API int sdb_unlink (Sdb* s);
SDB_API int sdb_lock_wait(const char *s UNUSED);
@ -159,7 +159,8 @@ const char *sdb_json_format(SdbJsonString* s, const char *fmt, ...);
#define sdb_json_format_free(x) free ((x)->buf)
// namespace
Sdb* sdb_ns (Sdb *s, const char *name);
Sdb* sdb_ns (Sdb *s, const char *name, int create);
Sdb *sdb_ns_path(Sdb *s, const char *path, int create);
void sdb_ns_init (Sdb* s);
void sdb_ns_free (Sdb* s);
void sdb_ns_lock(Sdb *s, int lock, int depth);