From fb355e4200fcf98f36a4f3e1fbeb1e16b4761ee6 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Fri, 13 Sep 2024 09:38:07 +0000 Subject: Push what I have for now Signed-off-by: Laurent Bercot --- src/libdcache/dcache-internal.h | 5 +++++ src/libdcache/dcache_add.c | 27 +++++++++++++++++++++++++ src/libdcache/dcache_add_data.c | 2 +- src/libdcache/dcache_clean_expired.c | 2 +- src/libdcache/dcache_free.c | 8 ++++---- src/libdcache/dcache_get_data.c | 1 + src/libdcache/dcache_load.c | 39 +++++++++++++++++++++++++----------- src/libdcache/dcache_node_add.c | 21 +++++++++++++++++++ src/libdcache/dcache_node_new.c | 22 +++++++++----------- src/libdcache/dcache_save.c | 16 +++++++++------ src/libdcache/dcache_search.c | 28 ++++++++++++++++++++++++++ src/libdcache/dcache_searchnode.c | 28 -------------------------- src/libdcache/deps-lib/dcache | 4 ++++ 13 files changed, 138 insertions(+), 65 deletions(-) create mode 100644 src/libdcache/dcache_add.c create mode 100644 src/libdcache/dcache_node_add.c create mode 100644 src/libdcache/dcache_search.c delete mode 100644 src/libdcache/dcache_searchnode.c (limited to 'src/libdcache') 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 #include +#include #include #include @@ -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 +#include + +#include +#include "dcache-internal.h" + +#include + +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 #include #include #include +#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 #include +#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 #include #include +#include #include +#include "dcache-internal.h" #include +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 + +#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 #include #include -#include #include #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 #include #include -#include #include #include @@ -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_search.c b/src/libdcache/dcache_search.c new file mode 100644 index 0000000..d01faa0 --- /dev/null +++ b/src/libdcache/dcache_search.c @@ -0,0 +1,28 @@ +/* ISC license. */ + +#include + +#include +#include + +#include +#include "dcache-internal.h" + +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 ; + char key[4 + qlen] ; + uint16_pack_big(key, qtype) ; + uint16_pack_big(key+2, qlen) ; + memcpy(key+4, q, qlen) ; + if (!avltree_search(&z->by_key, &key, &i)) return -1 ; + node = DNODE(z, i) ; + if (node->sa.len == node->sa.a && tai_less(&node->expire, stamp)) + { + dcache_delete(z, i) ; + return -1 ; + } + *idx = i ; + return node->sa.len == node->sa.a ; +} diff --git a/src/libdcache/dcache_searchnode.c b/src/libdcache/dcache_searchnode.c deleted file mode 100644 index ef7341c..0000000 --- a/src/libdcache/dcache_searchnode.c +++ /dev/null @@ -1,28 +0,0 @@ -/* ISC license. */ - -#include - -#include -#include - -#include -#include "dcache-internal.h" - -int dcache_searchnode (dcache *z, uint32_t *idx, char const *q, uint16_t qlen, uint16_t qtype, tai const *stamp) -{ - dcache_node *node ; - uint32_t i ; - char key[4 + qlen] ; - 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 ; - node = DNODE(z, i) ; - if (node->sa.len == node->sa.a && !tai_less(stamp, &node->expire)) - { - dcache_delete(z, i) ; - return -1 ; - } - *idx = i ; - return node->sa.len == node->sa.a ; -} 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 -- cgit v1.2.3