diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2024-09-13 09:38:07 +0000 |
---|---|---|
committer | Laurent Bercot <ska@appnovation.com> | 2024-09-13 09:38:07 +0000 |
commit | fb355e4200fcf98f36a4f3e1fbeb1e16b4761ee6 (patch) | |
tree | bf78792e9206e121607ad786432c0b60bbdcdc72 /src/libdcache | |
parent | 45c04a53fb9fbefe4dd86514c7563b4af1961dfb (diff) | |
download | shibari-fb355e4200fcf98f36a4f3e1fbeb1e16b4761ee6.tar.xz |
Push what I have for now
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/libdcache')
-rw-r--r-- | src/libdcache/dcache-internal.h | 5 | ||||
-rw-r--r-- | src/libdcache/dcache_add.c | 27 | ||||
-rw-r--r-- | src/libdcache/dcache_add_data.c | 2 | ||||
-rw-r--r-- | src/libdcache/dcache_clean_expired.c | 2 | ||||
-rw-r--r-- | src/libdcache/dcache_free.c | 8 | ||||
-rw-r--r-- | src/libdcache/dcache_get_data.c | 1 | ||||
-rw-r--r-- | src/libdcache/dcache_load.c | 39 | ||||
-rw-r--r-- | src/libdcache/dcache_node_add.c | 21 | ||||
-rw-r--r-- | src/libdcache/dcache_node_new.c | 22 | ||||
-rw-r--r-- | src/libdcache/dcache_save.c | 16 | ||||
-rw-r--r-- | src/libdcache/dcache_search.c (renamed from src/libdcache/dcache_searchnode.c) | 6 | ||||
-rw-r--r-- | src/libdcache/deps-lib/dcache | 4 |
12 files changed, 113 insertions, 40 deletions
diff --git a/src/libdcache/dcache-internal.h b/src/libdcache/dcache-internal.h index 49ec504..9986456 100644 --- a/src/libdcache/dcache-internal.h +++ b/src/libdcache/dcache-internal.h @@ -6,6 +6,7 @@ #include <stdint.h> #include <skalibs/tai.h> +#include <skalibs/stralloc.h> #include <skalibs/avlnode.h> #include <skalibs/gensetdyn.h> @@ -14,6 +15,10 @@ #define DNODE(z, i) GENSETDYN_P(dcache_node, &(z)->storage, i) #define DCACHE_NODE_OVERHEAD (32 + sizeof(dcache_node) + 3 * sizeof(avlnode)) +extern int dcache_node_new (dcache *, uint32_t *, char const *, uint16_t, uint16_t, uint16_t) ; +extern int dcache_node_add (dcache *, uint32_t) ; +#define dcache_node_free(node) stralloc_free(&(node)->sa) + extern void dcache_delete (dcache *, uint32_t) ; #endif diff --git a/src/libdcache/dcache_add.c b/src/libdcache/dcache_add.c new file mode 100644 index 0000000..82f1ef4 --- /dev/null +++ b/src/libdcache/dcache_add.c @@ -0,0 +1,27 @@ +/* ISC license. */ + +#include <stdint.h> +#include <string.h> + +#include <shibari/dcache.h> +#include "dcache-internal.h" + +#include <skalibs/posixishard.h> + +int dcache_add (dcache *z, char const *q, uint16_t qlen, uint16_t qtype, char const *data, uint16_t datalen, tai const *entry, tai const *expire) +{ + uint32_t i ; + dcache_node *node ; + if (!dcache_node_new(z, &i, q, qlen, qtype, datalen)) return 0 ; + node = DNODE(z, i) ; + node->entry = *entry ; + node->expire = *expire ; + memcpy(node->sa.s + node->sa.len, data, datalen) ; + node->sa.len += datalen ; + if (!dcache_node_add(z, i)) + { + dcache_node_free(node) ; + return 0 ; + } + return 1 ; +} diff --git a/src/libdcache/dcache_add_data.c b/src/libdcache/dcache_add_data.c index aa1a110..06d20b7 100644 --- a/src/libdcache/dcache_add_data.c +++ b/src/libdcache/dcache_add_data.c @@ -56,7 +56,7 @@ int dcache_add_data (dcache *z, char const *q, uint16_t qlen, uint16_t qtype, ch if (z->size > z->max - size) dcache_clean_expired(z, stamp) ; if (z->size > z->max - size) dcache_gc_by_entry(z, z->max - size) ; if (z->size > z->max - size) return (errno = ENOBUFS, 0) ; - if (!dcache_searchnode(z, &i, q, qlen, qtype, stamp)) return 0 ; + if (!dcache_search(z, &i, q, qlen, qtype, stamp)) return 0 ; if (!dcache_add_data_to_node(z, i, qlen, data, datalen, expire, stamp)) return 0 ; z->size += size ; z->motion += size ; diff --git a/src/libdcache/dcache_clean_expired.c b/src/libdcache/dcache_clean_expired.c index d81bec8..7109e5c 100644 --- a/src/libdcache/dcache_clean_expired.c +++ b/src/libdcache/dcache_clean_expired.c @@ -14,7 +14,7 @@ void dcache_clean_expired (dcache *z, tai const *stamp) { uint32_t i ; if (!avltree_min(&z->by_expire, &i)) break ; - if (tai_less(stamp, &DNODE(z, i)->expire)) break ; + if (!tai_less(&DNODE(z, i)->expire, stamp)) break ; dcache_delete(z, i) ; } } diff --git a/src/libdcache/dcache_free.c b/src/libdcache/dcache_free.c index 07d6982..58dec5f 100644 --- a/src/libdcache/dcache_free.c +++ b/src/libdcache/dcache_free.c @@ -1,15 +1,15 @@ /* ISC license. */ -#include <skalibs/stralloc.h> #include <skalibs/gensetdyn.h> #include <skalibs/avltree.h> #include <shibari/dcache.h> +#include "dcache-internal.h" -static void dcache_node_free (void *p) +static void dnode_free (void *p) { dcache_node *node = p ; - stralloc_free(&node->sa) ; + dcache_node_free(node) ; } void dcache_free (dcache *z) @@ -18,6 +18,6 @@ void dcache_free (dcache *z) avltree_free(&z->by_expire) ; avltree_free(&z->by_entry) ; avltree_free(&z->by_key) ; - gensetdyn_deepfree(&z->storage, &dcache_node_free) ; + gensetdyn_deepfree(&z->storage, &dnode_free) ; *z = dcache_zero ; } diff --git a/src/libdcache/dcache_get_data.c b/src/libdcache/dcache_get_data.c index f14977c..36d16c9 100644 --- a/src/libdcache/dcache_get_data.c +++ b/src/libdcache/dcache_get_data.c @@ -5,6 +5,7 @@ #include <skalibs/uint16.h> #include <shibari/dcache.h> +#include "dcache-internal.h" void dcache_get_data (dcache *z, uint32_t nid, dcache_string *data) { diff --git a/src/libdcache/dcache_load.c b/src/libdcache/dcache_load.c index 0693b21..f195b28 100644 --- a/src/libdcache/dcache_load.c +++ b/src/libdcache/dcache_load.c @@ -9,32 +9,47 @@ #include <skalibs/buffer.h> #include <skalibs/tai.h> #include <skalibs/djbunix.h> +#include <skalibs/avltree.h> #include <shibari/dcache.h> +#include "dcache-internal.h" #include <skalibs/posixishard.h> +static inline int dcache_adjust_node (dcache *z, uint32_t i, char const *data, uint16_t datalen, tai const *entry, tai const *expire) +{ + /* can't happen. Complete if it ever can. */ + return (errno = EDOM, 0) ; +} + +static inline int dcache_add_keydata (dcache *z, char const *q, uint16_t qlen, uint16_t qtype, char const *data, uint16_t datalen, tai const *entry, tai const *expire) +{ + uint32_t i ; + if (dcache_search_g(z, &i, q, qlen, qtype)) return dcache_adjust_node(z, i, data, datalen, entry, expire) ; + return dcache_add(z, q, qlen, qtype, data, datalen, entry, expire) ; +} + static inline int dcache_load_node (dcache *z, buffer *b) { tai entry, expire ; - uint16_t keylen ; - uint16_t datalen ; - char pack[TAI_PACK * 2 + 4] ; - ssize_t r = buffer_get(b, pack, TAI_PACK * 2 + 4) ; + uint16_t qtype, qlen, datalen ; + char pack[TAI_PACK * 2 + 6] ; + ssize_t r = buffer_get(b, pack, TAI_PACK * 2 + 6) ; if (!r) return 0 ; - if (r < TAI_PACK * 2 + 4) return -1 ; + if (r < TAI_PACK * 2 + 6) return -1 ; tai_unpack(pack, &entry) ; tai_unpack(pack + TAI_PACK, &expire) ; - uint16_unpack_big(pack + TAI_PACK * 2, &keylen) ; - uint16_unpack_big(pack + TAI_PACK * 2 + 2, &datalen) ; + uint16_unpack_big(pack + TAI_PACK * 2, &datalen) ; + uint16_unpack_big(pack + TAI_PACK * 2 + 2, &qtype) ; + uint16_unpack_big(pack + TAI_PACK * 2 + 4, &qlen) ; { - uint32_t len = (uint32_t)keylen + (uint32_t)datalen ; - char blob[len+1] ; /* 128 kB max, it's ok */ - r = buffer_get(b, blob, len+1) ; + uint32_t len = qlen + datalen ; + char blob[len+1] ; /* 128 kB max */ + r = buffer_get(b, blob, len + 1) ; if (!r) return (errno = EPIPE, -1) ; - if (r < len) return -1 ; + if (r <= len) return -1 ; if (blob[len]) return (errno = EPROTO, -1) ; -// if (!dcache_add(z, blob, keylen, blob + keylen, datalen, &expire, &entry)) return -1 ; + if (!dcache_add_keydata(z, blob, qlen, qtype, blob + qlen, datalen, &entry, &expire)) return -1 ; } return 1 ; } diff --git a/src/libdcache/dcache_node_add.c b/src/libdcache/dcache_node_add.c new file mode 100644 index 0000000..a9cc539 --- /dev/null +++ b/src/libdcache/dcache_node_add.c @@ -0,0 +1,21 @@ +/* ISC license. */ + +#include <skalibs/avltree.h> + +#include "dcache-internal.h" + +int dcache_node_add (dcache *z, uint32_t i) +{ + dcache_node *node = DNODE(z, i) ; + if (!avltree_insert(&z->by_key, i)) return 0 ; + if (node->sa.len < node->sa.a) return 1 ; + if (!avltree_insert(&z->by_entry, i)) goto err0 ; + if (!avltree_insert(&z->by_expire, i)) goto err1 ; + return 1 ; + + err1: + avltree_delete(&z->by_entry, &node->entry) ; + err0: + avltree_delete(&z->by_key, node->sa.s) ; + return 0 ; +} diff --git a/src/libdcache/dcache_node_new.c b/src/libdcache/dcache_node_new.c index 1d13eaf..01e2278 100644 --- a/src/libdcache/dcache_node_new.c +++ b/src/libdcache/dcache_node_new.c @@ -6,28 +6,24 @@ #include <skalibs/uint16.h> #include <skalibs/stralloc.h> #include <skalibs/gensetdyn.h> -#include <skalibs/avltree.h> #include <shibari/dcache.h> #include "dcache-internal.h" -dcache_node *dcache_node_new (dcache *z, char const *key, uint16_t keylen) +int dcache_node_new (dcache *z, uint32_t *idx, char const *q, uint16_t qlen, uint16_t qtype, uint16_t extra) { - static tai const tai_infinite = TAI_INFINITE ; dcache_node *node ; uint32_t i ; - if (!gensetdyn_new(&z->storage, i)) return 0 ; + if (!gensetdyn_new(&z->storage, &i)) return 0 ; node = DNODE(z, i) ; - if (!stralloc_ready(&node->sa, 6 + keylen)) goto err0 ; - uint16_pack_big(node->sa.s, keylen) ; - memcpy(node->sa.s + 2, key, keylen) ; - node->sa.len = 2 + keylen ; - node->entry = tai_infinite ; - node->expire = tai_infinite ; - return node ; + if (!stralloc_ready_tuned(&node->sa, 4 + qlen + extra, 0, 0, 1)) goto err0 ; + uint16_pack_big(node->sa.s, qtype) ; + uint16_pack_big(node->sa.s + 2, qlen) ; + memcpy(node->sa.s + 4, q, qlen) ; + node->sa.len = 4 + qlen ; + *idx = i ; + return 1 ; - err1: - node->sa.len = 0 ; err0: gensetdyn_delete(&z->storage, i) ; return 0 ; diff --git a/src/libdcache/dcache_save.c b/src/libdcache/dcache_save.c index 62c55a2..3cff3c5 100644 --- a/src/libdcache/dcache_save.c +++ b/src/libdcache/dcache_save.c @@ -12,7 +12,6 @@ #include <skalibs/buffer.h> #include <skalibs/tai.h> #include <skalibs/djbunix.h> -#include <skalibs/skamisc.h> #include <skalibs/gensetdyn.h> #include <shibari/dcache.h> @@ -21,13 +20,18 @@ static int write_node_iter (void *data, void *aux) { dcache_node *y = data ; buffer *b = aux ; - char pack[TAI_PACK * 2 + 4] ; + char pack[TAI_PACK] ; + uint16_t len ; + if (y->sa.a != y->sa.len) return 1 ; tai_pack(pack, &y->entry) ; + if (buffer_put(b, pack, TAI_PACK) == -1) return 0 ; tai_pack(pack + TAI_PACK, &y->expire) ; -// uint16_pack(pack + TAI_PACK * 2, y->key.len) ; -// uint16_pack(pack + TAI_PACK * 2 + 2, y->datalen) ; - if (buffer_put(b, pack, TAI_PACK * 2 + 4) == -1) return 0 ; -// if (buffer_put(b, y->key.s, y->key.len + y->datalen) == -1) return 0 ; + if (buffer_put(b, pack, TAI_PACK) == -1) return 0 ; + uint16_unpack_big(y->sa.s + 2, &len) ; + len = y->sa.len - len ; + uint16_pack_big(pack, len) ; + if (buffer_put(b, pack, 2) == -1) return 0 ; + if (buffer_put(b, y->sa.s, y->sa.len) == -1) return 0 ; if (buffer_put(b, "", 1) == -1) return 0 ; return 1 ; } diff --git a/src/libdcache/dcache_searchnode.c b/src/libdcache/dcache_search.c index ef7341c..d01faa0 100644 --- a/src/libdcache/dcache_searchnode.c +++ b/src/libdcache/dcache_search.c @@ -8,7 +8,7 @@ #include <shibari/dcache.h> #include "dcache-internal.h" -int dcache_searchnode (dcache *z, uint32_t *idx, char const *q, uint16_t qlen, uint16_t qtype, tai const *stamp) +int dcache_search (dcache *z, uint32_t *idx, char const *q, uint16_t qlen, uint16_t qtype, tai const *stamp) { dcache_node *node ; uint32_t i ; @@ -16,9 +16,9 @@ int dcache_searchnode (dcache *z, uint32_t *idx, char const *q, uint16_t qlen, u uint16_pack_big(key, qtype) ; uint16_pack_big(key+2, qlen) ; memcpy(key+4, q, qlen) ; - if (!avltree_search(&z->by_key, &k, &i)) return -1 ; + if (!avltree_search(&z->by_key, &key, &i)) return -1 ; node = DNODE(z, i) ; - if (node->sa.len == node->sa.a && !tai_less(stamp, &node->expire)) + if (node->sa.len == node->sa.a && tai_less(&node->expire, stamp)) { dcache_delete(z, i) ; return -1 ; diff --git a/src/libdcache/deps-lib/dcache b/src/libdcache/deps-lib/dcache index 07f9f51..4e411c4 100644 --- a/src/libdcache/deps-lib/dcache +++ b/src/libdcache/deps-lib/dcache @@ -2,6 +2,10 @@ dcache_add_data.o dcache_clean_expired.o dcache_delete.o dcache_free.o +dcache_get_data.o dcache_init.o dcache_load.o +dcache_node_add.o +dcache_node_new.o dcache_save.o +dcache_search.o |