Skip to content

Commit

Permalink
Upgrade sdb
Browse files Browse the repository at this point in the history
  • Loading branch information
radare committed Feb 16, 2019
1 parent a795cd6 commit 936efa6
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 8 deletions.
2 changes: 2 additions & 0 deletions libr/include/sdb/ht_uu.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
#define HT_TYPE 0
#include "ht_inc.h"

SDB_API HtName_(Ht)* Ht_(new0)(void);

#endif
1 change: 1 addition & 0 deletions libr/include/sdb/sdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ SDB_API ut8 sdb_hash_byte(const char *s);
// SDB_API int sdb_js0n(const unsigned char *js, RangstrType len, RangstrType *out);
SDB_API bool sdb_isjson(const char *k);
SDB_API char *sdb_json_get_str (const char *json, const char *path);
SDB_API bool sdb_json_get_bool(const char *json, const char *path);

SDB_API char *sdb_json_get(Sdb* s, const char *key, const char *p, ut32 *cas);
SDB_API bool sdb_json_set(Sdb* s, const char *k, const char *p, const char *v, ut32 cas);
Expand Down
16 changes: 14 additions & 2 deletions shlr/sdb/src/ht_inc.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,17 @@ static inline void freefn(HtName_(Ht) *ht, HT_(Kv) *kv) {
}
}

static inline ut32 next_idx(ut32 idx) {
if (idx != UT32_MAX && idx < S_ARRAY_SIZE (ht_primes_sizes) - 1) {
return idx + 1;
}
return UT32_MAX;
}

static inline ut32 compute_size(ut32 idx, ut32 sz) {
// when possible, use the precomputed prime numbers which help with
// collisions, otherwise, at least make the number odd with |1
return idx != UT32_MAX ? ht_primes_sizes[idx] : (sz | 1);
return idx != UT32_MAX && idx < S_ARRAY_SIZE(ht_primes_sizes) ? ht_primes_sizes[idx] : (sz | 1);
}

static inline bool is_kv_equal(HtName_(Ht) *ht, const KEY_TYPE key, const ut32 key_len, const HT_(Kv) *kv) {
Expand Down Expand Up @@ -144,11 +151,16 @@ SDB_API void Ht_(free)(HtName_(Ht)* ht) {
static void internal_ht_grow(HtName_(Ht)* ht) {
HtName_(Ht)* ht2;
HtName_(Ht) swap;
ut32 idx = ht->prime_idx != UT32_MAX ? ht->prime_idx + 1 : UT32_MAX;
ut32 idx = next_idx (ht->prime_idx);
ut32 sz = compute_size (idx, ht->size * 2);
ut32 i;

ht2 = internal_ht_new (sz, idx, &ht->opt);
if (!ht2) {
// we can't grow the ht anymore. Never mind, we'll be slower,
// but everything can continue to work
return;
}

for (i = 0; i < ht->size; i++) {
HT_(Bucket) *bt = &ht->table[i];
Expand Down
13 changes: 13 additions & 0 deletions shlr/sdb/src/ht_uu.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
#include "ht_uu.h"

#include "ht_inc.c"

SDB_API HtName_(Ht)* Ht_(new0)(void) {
HT_(Options) opt = {
.cmp = NULL,
.hashfn = NULL,
.dupkey = NULL,
.dupvalue = NULL,
.calcsizeK = NULL,
.calcsizeV = NULL,
.freefn = NULL
};
return Ht_(new_opt) (&opt);
}
2 changes: 2 additions & 0 deletions shlr/sdb/src/ht_uu.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
#define HT_TYPE 0
#include "ht_inc.h"

SDB_API HtName_(Ht)* Ht_(new0)(void);

#endif
8 changes: 7 additions & 1 deletion shlr/sdb/src/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ SDB_API char *sdb_json_get_str (const char *json, const char *path) {
return rangstr_dup (&rs);
}

SDB_API char *sdb_json_get (Sdb *s, const char *k, const char *p, ut32 *cas) {
SDB_API bool sdb_json_get_bool(const char *json, const char *path) {
Rangstr rs = json_get (json, path);
const char *p = rs->p + rs->f;
return (rangstr_length (&rs) == 4 && !strncmp (p, "true", 4));
}

SDB_API char *sdb_json_get(Sdb *s, const char *k, const char *p, ut32 *cas) {
Rangstr rs;
char *u, *v = sdb_get (s, k, cas);
if (!v) {
Expand Down
8 changes: 3 additions & 5 deletions shlr/sdb/src/json/rangstr.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ SDB_IPI int rangstr_int (Rangstr *s) {
}
if (s->p[s->f]=='-') {
mul = -1;
i += s->f+1;
i += s->f + 1;
} else {
i += s->f;
}
Expand All @@ -71,13 +71,11 @@ SDB_IPI int rangstr_int (Rangstr *s) {
}

SDB_IPI char *rangstr_dup (Rangstr *rs) {
int len;
char *p;
if (!rs->p) {
return NULL;
}
len = rangstr_length (rs);
p = malloc (len + 1);
int len = rangstr_length (rs);
char *p = malloc (len + 1);
if (p) {
memcpy (p, rs->p + rs->f, len);
p[len] = 0;
Expand Down
1 change: 1 addition & 0 deletions shlr/sdb/src/sdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ SDB_API ut8 sdb_hash_byte(const char *s);
// SDB_API int sdb_js0n(const unsigned char *js, RangstrType len, RangstrType *out);
SDB_API bool sdb_isjson(const char *k);
SDB_API char *sdb_json_get_str (const char *json, const char *path);
SDB_API bool sdb_json_get_bool(const char *json, const char *path);

SDB_API char *sdb_json_get(Sdb* s, const char *key, const char *p, ut32 *cas);
SDB_API bool sdb_json_set(Sdb* s, const char *k, const char *p, const char *v, ut32 cas);
Expand Down

0 comments on commit 936efa6

Please sign in to comment.