Bump sdb again

This commit is contained in:
pancake 2014-01-30 23:45:30 +01:00
parent a910c315f6
commit 27534580a1
8 changed files with 27 additions and 17 deletions

View File

@ -49,7 +49,8 @@ typedef struct sdb_kv {
} SdbKv;
typedef struct sdb_t {
char *dir; // path+file
char *dir; // path+name
char *path;
char *name;
int fd;
int lock;

View File

@ -52,7 +52,7 @@ Let's create a database!
Using arrays (>=0.6):
$ sdb - '()list=1,2' '(0)list' '(0)list=foo' '()list' '(+1)list=bar'
$ sdb - '[]list=1,2' '[0]list' '[0]list=foo' '[]list' '[+1]list=bar'
1
foo
2

View File

@ -100,9 +100,7 @@ int cdb_findnext(struct cdb *c, ut32 u, const char *key, unsigned int len) {
}
ut32_unpack (buf, &c->hpos);
c->khash = u;
u >>= 8;
u %= c->hslots;
u <<= 3;
u = ((u>>8)%c->hslots)<<3;
c->kpos = c->hpos + u;
}

View File

@ -30,7 +30,7 @@ static void syncronize(int sig UNUSED) {
// TODO: must be in sdb_sync() or wat?
Sdb *n;
sdb_sync (s);
n = sdb_new (s->dir, s->name, s->lock);
n = sdb_new (s->path, s->name, s->lock);
sdb_free (s);
s = n;
}
@ -52,7 +52,7 @@ static int sdb_dump (const char *db) {
static void createdb(const char *f) {
char *line, *eq;
s = sdb_new (f, f, 0);
s = sdb_new (NULL, f, 0);
if (!sdb_create (s)) {
printf ("Cannot create database\n");
exit (1);

View File

@ -43,10 +43,10 @@ SDB_VISIBLE char *sdb_querys (Sdb *s, char *buf, size_t len, const char *cmd) {
}
if (!len || !buf) buf = malloc ((len=64));
ask = strchr (cmd, '?');
if (*cmd == '(') {
char *tp = strchr (cmd, ')');
if (*cmd == '[') {
char *tp = strchr (cmd, ']');
if (!tp) {
fprintf (stderr, "Missing ')'.\n");
fprintf (stderr, "Missing ']'.\n");
return NULL;
}
*tp++ = 0;
@ -97,7 +97,8 @@ SDB_VISIBLE char *sdb_querys (Sdb *s, char *buf, size_t len, const char *cmd) {
}
return buf;
}
} else if (*cmd == '(') {
} else if (*cmd == '[') {
// [?] - count elements of array
if (cmd[1]=='?') {
// if (!eq) { ...
alength = sdb_alength (s, p);
@ -109,14 +110,19 @@ SDB_VISIBLE char *sdb_querys (Sdb *s, char *buf, size_t len, const char *cmd) {
return buf;
}
if (cmd[1]=='+'||cmd[1]=='-') {
/* (+)foo=bla (-)foo=bla */
if (!cmd[2] || cmd[2] ==')') {
// [+]foo remove first element */
// [+]foo=bar PUSH */
// [-]foo POP */
// [-]foo=xx POP (=xx ignored) */
if (!cmd[2] || cmd[2] ==']') {
// insert
if (eq) {
if (cmd[1]=='+') {
if (sdb_agetv (s, p, val, 0)== -1)
sdb_aset (s, p, -1, val, 0);
} else sdb_adels (s, p, val, 0);
} else {
sdb_adels (s, p, val, 0);
}
return NULL;
} else {
char *ret;
@ -125,7 +131,7 @@ SDB_VISIBLE char *sdb_querys (Sdb *s, char *buf, size_t len, const char *cmd) {
ret = sdb_aget (s, p, 0, 0);
// (+)foo :: remove first element
sdb_adel (s, p, 0, 0);
} else {
} else { // POP
ret = sdb_aget (s, p, -1, 0);
// (-)foo :: remove last element
sdb_adel (s, p, -1, 0);

View File

@ -43,9 +43,11 @@ SDB_VISIBLE Sdb* sdb_new (const char *path, const char *name, int lock) {
s->fd = open (s->dir, O_RDONLY|O_BINARY);
// if (s->fd == -1) // must fail if we cant open for write in sync
s->name = strdup (name);
s->path = path? strdup (path): NULL;
} else {
s->dir = NULL;
s->name = NULL;
s->path = NULL;
s->fd = -1;
}
s->fdump = -1;

View File

@ -49,7 +49,8 @@ typedef struct sdb_kv {
} SdbKv;
typedef struct sdb_t {
char *dir; // path+file
char *dir; // path+name
char *path;
char *name;
int fd;
int lock;

View File

@ -1,4 +1,4 @@
/* sdb - LGPLv3 - Copyright 2011-2013 - pancake */
/* sdb - LGPLv3 - Copyright 2011-2014 - pancake */
#include "sdb.h"
@ -255,6 +255,8 @@ SDB_VISIBLE int sdb_adel(Sdb *s, const char *key, int idx, ut32 cas) {
if (n) {
memmove (p, n+1, strlen (n+1)+1);
} else {
if (p != str)
p--; // remove tailing SDB_RS
*p = 0;
p[1] = 0;
}