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; } SdbKv;
typedef struct sdb_t { typedef struct sdb_t {
char *dir; // path+file char *dir; // path+name
char *path;
char *name; char *name;
int fd; int fd;
int lock; int lock;

View File

@ -52,7 +52,7 @@ Let's create a database!
Using arrays (>=0.6): 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 1
foo foo
2 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); ut32_unpack (buf, &c->hpos);
c->khash = u; c->khash = u;
u >>= 8; u = ((u>>8)%c->hslots)<<3;
u %= c->hslots;
u <<= 3;
c->kpos = c->hpos + u; 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? // TODO: must be in sdb_sync() or wat?
Sdb *n; Sdb *n;
sdb_sync (s); sdb_sync (s);
n = sdb_new (s->dir, s->name, s->lock); n = sdb_new (s->path, s->name, s->lock);
sdb_free (s); sdb_free (s);
s = n; s = n;
} }
@ -52,7 +52,7 @@ static int sdb_dump (const char *db) {
static void createdb(const char *f) { static void createdb(const char *f) {
char *line, *eq; char *line, *eq;
s = sdb_new (f, f, 0); s = sdb_new (NULL, f, 0);
if (!sdb_create (s)) { if (!sdb_create (s)) {
printf ("Cannot create database\n"); printf ("Cannot create database\n");
exit (1); 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)); if (!len || !buf) buf = malloc ((len=64));
ask = strchr (cmd, '?'); ask = strchr (cmd, '?');
if (*cmd == '(') { if (*cmd == '[') {
char *tp = strchr (cmd, ')'); char *tp = strchr (cmd, ']');
if (!tp) { if (!tp) {
fprintf (stderr, "Missing ')'.\n"); fprintf (stderr, "Missing ']'.\n");
return NULL; return NULL;
} }
*tp++ = 0; *tp++ = 0;
@ -97,7 +97,8 @@ SDB_VISIBLE char *sdb_querys (Sdb *s, char *buf, size_t len, const char *cmd) {
} }
return buf; return buf;
} }
} else if (*cmd == '(') { } else if (*cmd == '[') {
// [?] - count elements of array
if (cmd[1]=='?') { if (cmd[1]=='?') {
// if (!eq) { ... // if (!eq) { ...
alength = sdb_alength (s, p); 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; return buf;
} }
if (cmd[1]=='+'||cmd[1]=='-') { if (cmd[1]=='+'||cmd[1]=='-') {
/* (+)foo=bla (-)foo=bla */ // [+]foo remove first element */
if (!cmd[2] || cmd[2] ==')') { // [+]foo=bar PUSH */
// [-]foo POP */
// [-]foo=xx POP (=xx ignored) */
if (!cmd[2] || cmd[2] ==']') {
// insert // insert
if (eq) { if (eq) {
if (cmd[1]=='+') { if (cmd[1]=='+') {
if (sdb_agetv (s, p, val, 0)== -1) if (sdb_agetv (s, p, val, 0)== -1)
sdb_aset (s, p, -1, val, 0); sdb_aset (s, p, -1, val, 0);
} else sdb_adels (s, p, val, 0); } else {
sdb_adels (s, p, val, 0);
}
return NULL; return NULL;
} else { } else {
char *ret; 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); ret = sdb_aget (s, p, 0, 0);
// (+)foo :: remove first element // (+)foo :: remove first element
sdb_adel (s, p, 0, 0); sdb_adel (s, p, 0, 0);
} else { } else { // POP
ret = sdb_aget (s, p, -1, 0); ret = sdb_aget (s, p, -1, 0);
// (-)foo :: remove last element // (-)foo :: remove last element
sdb_adel (s, p, -1, 0); 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); s->fd = open (s->dir, O_RDONLY|O_BINARY);
// if (s->fd == -1) // must fail if we cant open for write in sync // if (s->fd == -1) // must fail if we cant open for write in sync
s->name = strdup (name); s->name = strdup (name);
s->path = path? strdup (path): NULL;
} else { } else {
s->dir = NULL; s->dir = NULL;
s->name = NULL; s->name = NULL;
s->path = NULL;
s->fd = -1; s->fd = -1;
} }
s->fdump = -1; s->fdump = -1;

View File

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

View File

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