summaryrefslogtreecommitdiff
path: root/src/libdcache
diff options
context:
space:
mode:
Diffstat (limited to 'src/libdcache')
-rw-r--r--src/libdcache/dcache-internal.h18
-rw-r--r--src/libdcache/dcache_add.c85
-rw-r--r--src/libdcache/dcache_clean_expired.c20
-rw-r--r--src/libdcache/dcache_delete.c19
-rw-r--r--src/libdcache/dcache_free.c22
-rw-r--r--src/libdcache/dcache_init.c55
-rw-r--r--src/libdcache/dcache_load.c82
-rw-r--r--src/libdcache/dcache_save.c73
-rw-r--r--src/libdcache/dcache_search.c15
-rw-r--r--src/libdcache/deps-lib/dcache8
10 files changed, 397 insertions, 0 deletions
diff --git a/src/libdcache/dcache-internal.h b/src/libdcache/dcache-internal.h
new file mode 100644
index 0000000..7a21044
--- /dev/null
+++ b/src/libdcache/dcache-internal.h
@@ -0,0 +1,18 @@
+/* ISC license. */
+
+#ifndef DCACHE_INTERNAL_H
+#define DCACHE_INTERNAL_H
+
+#include <stdint.h>
+
+#include <skalibs/avlnode.h>
+#include <skalibs/gensetdyn.h>
+
+#include <shibari/dcache.h>
+
+#define DNODE(z, i) GENSETDYN_P(dcache_node_t, &(z)->storage, i)
+#define DCACHE_NODE_OVERHEAD (32 + sizeof(dcache_node_t) + 3 * sizeof(avlnode))
+
+extern void dcache_delete (dcache_t *, uint32_t) ;
+
+#endif
diff --git a/src/libdcache/dcache_add.c b/src/libdcache/dcache_add.c
new file mode 100644
index 0000000..7260726
--- /dev/null
+++ b/src/libdcache/dcache_add.c
@@ -0,0 +1,85 @@
+/* ISC license. */
+
+#include <stdint.h>
+#include <string.h>
+#include <errno.h>
+
+#include <skalibs/uint64.h>
+#include <skalibs/alloc.h>
+#include <skalibs/tai.h>
+#include <skalibs/gensetdyn.h>
+#include <skalibs/avlnode.h>
+#include <skalibs/avltree.h>
+
+#include <shibari/dcache.h>
+#include "dcache-internal.h"
+
+static void uniquify (avltree const *tree, tain *stamp)
+{
+ static tain const nano = { .sec = TAI_ZERO, .nano = 1 } ;
+ uint32_t dummy ;
+ while (avltree_search(tree, stamp, &dummy))
+ tain_add(stamp, stamp, &nano) ;
+}
+
+static inline void dcache_gc_by_entry (dcache_t *z, uint64_t max)
+{
+ while (z->size > max)
+ {
+ uint32_t oldest ;
+ if (!avltree_min(&z->by_entry, &oldest)) break ;
+ dcache_delete(z, oldest) ;
+ }
+}
+
+static inline int dcache_add_node (dcache_t *z, dcache_node_t const *node)
+{
+ uint32_t i ;
+ dcache_node_t *y ;
+ if (!gensetdyn_new(&z->storage, &i)) return 0 ;
+ y = DNODE(z, i) ; *y = *node ;
+ uniquify(&z->by_entry, &y->entry) ;
+ uniquify(&z->by_expire, &y->expire) ;
+ if (!avltree_insert(&z->by_key, i)) goto err1 ;
+ if (!avltree_insert(&z->by_entry, i)) goto err2 ;
+ if (!avltree_insert(&z->by_expire, i)) goto err3 ;
+ return 1 ;
+
+ err3:
+ avltree_delete(&z->by_entry, &y->entry) ;
+ err2:
+ avltree_delete(&z->by_key, &y->key) ;
+ err1:
+ gensetdyn_delete(&z->storage, i) ;
+ return 0 ;
+}
+
+static inline int dcache_add_unbounded (dcache_t *z, char const *key, uint16_t keylen, char const *data, uint16_t datalen, tain const *expire, tain const *stamp)
+{
+ uint32_t len = (uint32_t)keylen + (uint32_t)datalen ;
+ dcache_node_t y = { .key = { .s = alloc(len) } } ;
+ if (!y.key.s) return 0 ;
+ memcpy(y.key.s, key, keylen) ;
+ memcpy(y.key.s + keylen, data, datalen) ;
+ y.key.len = keylen ;
+ y.datalen = datalen ;
+ y.entry = *stamp ;
+ y.expire = *expire ;
+ if (!dcache_add_node(z, &y))
+ {
+ alloc_free(y.key.s) ;
+ return 0 ;
+ }
+ z->size += DCACHE_NODE_OVERHEAD + len ;
+ z->motion += DCACHE_NODE_OVERHEAD + len ;
+ return 1 ;
+}
+
+int dcache_add (dcache_t *z, uint64_t max, char const *key, uint16_t keylen, char const *data, uint16_t datalen, tain const *expire, tain const *stamp)
+{
+ uint64_t size = DCACHE_NODE_OVERHEAD + keylen + datalen ;
+ if (size > max) return (errno = EINVAL, 0) ;
+ if (z->size > max - size) dcache_clean_expired(z, stamp) ;
+ if (z->size > max - size) dcache_gc_by_entry(z, max - size) ;
+ return dcache_add_unbounded(z, key, keylen, data, datalen, expire, stamp) ;
+}
diff --git a/src/libdcache/dcache_clean_expired.c b/src/libdcache/dcache_clean_expired.c
new file mode 100644
index 0000000..0e23443
--- /dev/null
+++ b/src/libdcache/dcache_clean_expired.c
@@ -0,0 +1,20 @@
+/* ISC license. */
+
+#include <stdint.h>
+
+#include <skalibs/tai.h>
+#include <skalibs/avltree.h>
+
+#include <shibari/dcache.h>
+#include "dcache-internal.h"
+
+void dcache_clean_expired (dcache_t *z, tain const *stamp)
+{
+ for (;;)
+ {
+ uint32_t i ;
+ if (!avltree_min(&z->by_expire, &i)) break ;
+ if (tain_less(stamp, &DNODE(z, i)->expire)) break ;
+ dcache_delete(z, i) ;
+ }
+}
diff --git a/src/libdcache/dcache_delete.c b/src/libdcache/dcache_delete.c
new file mode 100644
index 0000000..92a5fcc
--- /dev/null
+++ b/src/libdcache/dcache_delete.c
@@ -0,0 +1,19 @@
+/* ISC license. */
+
+#include <skalibs/alloc.h>
+#include <skalibs/gensetdyn.h>
+#include <skalibs/avltree.h>
+
+#include <shibari/dcache.h>
+#include "dcache-internal.h"
+
+void dcache_delete (dcache_t *z, uint32_t i)
+{
+ dcache_node_t *y = DNODE(z, i) ;
+ avltree_delete(&z->by_expire, &y->expire) ;
+ avltree_delete(&z->by_entry, &y->entry) ;
+ avltree_delete(&z->by_key, &y->key) ;
+ alloc_free(y->key.s) ;
+ z->size -= DCACHE_NODE_OVERHEAD + y->key.len + y->datalen ;
+ gensetdyn_delete(&z->storage, i) ;
+}
diff --git a/src/libdcache/dcache_free.c b/src/libdcache/dcache_free.c
new file mode 100644
index 0000000..16c074e
--- /dev/null
+++ b/src/libdcache/dcache_free.c
@@ -0,0 +1,22 @@
+/* ISC license. */
+
+#include <skalibs/alloc.h>
+#include <skalibs/gensetdyn.h>
+#include <skalibs/avltree.h>
+
+#include <shibari/dcache.h>
+
+static void dcache_node_free (void *p)
+{
+ alloc_free(((dcache_node_t *)p)->key.s) ;
+}
+
+void dcache_free (dcache_t *z)
+{
+ static dcache_t const dcache_zero = DCACHE_ZERO ;
+ avltree_free(&z->by_expire) ;
+ avltree_free(&z->by_entry) ;
+ avltree_free(&z->by_key) ;
+ gensetdyn_deepfree(&z->storage, &dcache_node_free) ;
+ *z = dcache_zero ;
+}
diff --git a/src/libdcache/dcache_init.c b/src/libdcache/dcache_init.c
new file mode 100644
index 0000000..d42ec62
--- /dev/null
+++ b/src/libdcache/dcache_init.c
@@ -0,0 +1,55 @@
+/* ISC license. */
+
+#include <stdint.h>
+#include <string.h>
+
+#include <skalibs/uint64.h>
+#include <skalibs/tai.h>
+#include <skalibs/gensetdyn.h>
+#include <skalibs/avltree.h>
+
+#include <shibari/dcache.h>
+
+static int key_cmp (void const *a, void const *b, void *x)
+{
+ dcache_key_t const *ka = a ;
+ dcache_key_t const *kb = b ;
+ if (ka->len < kb->len) return -1 ;
+ if (kb->len < ka->len) return 1 ;
+ (void)x ;
+ return memcmp(ka->s, kb->s, ka->len) ;
+}
+
+static int tain_cmp (void const *a, void const *b, void *x)
+{
+ tain const *ta = a ;
+ tain const *tb = b ;
+ (void)x ;
+ return tain_less(ta, tb) ? -1 : tain_less(tb, ta) ;
+}
+
+static void *key_dtok (uint32_t d, void *x)
+{
+ return &GENSETDYN_P(dcache_node_t, (gensetdyn *)x, d)->key ;
+}
+
+static void *entry_dtok (uint32_t d, void *x)
+{
+ return &GENSETDYN_P(dcache_node_t, (gensetdyn *)x, d)->entry ;
+}
+
+static void *expire_dtok (uint32_t d, void *x)
+{
+ return &GENSETDYN_P(dcache_node_t, (gensetdyn *)x, d)->expire ;
+}
+
+
+void dcache_init (dcache_t *z, uint64_t max)
+{
+ gensetdyn_init(&z->storage, sizeof(dcache_node_t), max >> 9, 3, 8) ;
+ avltree_init(&z->by_key, max >> 9, 3, 8, &key_dtok, &key_cmp, &z->storage) ;
+ avltree_init(&z->by_entry, max >> 9, 3, 8, &entry_dtok, &tain_cmp, &z->storage) ;
+ avltree_init(&z->by_expire, max >> 9, 3, 8, &expire_dtok, &tain_cmp, &z->storage) ;
+ z->size = 0 ;
+ z->motion = 0 ;
+}
diff --git a/src/libdcache/dcache_load.c b/src/libdcache/dcache_load.c
new file mode 100644
index 0000000..d1b9274
--- /dev/null
+++ b/src/libdcache/dcache_load.c
@@ -0,0 +1,82 @@
+/* ISC license. */
+
+#include <stdint.h>
+#include <string.h>
+#include <errno.h>
+
+#include <skalibs/uint16.h>
+#include <skalibs/uint64.h>
+#include <skalibs/buffer.h>
+#include <skalibs/tai.h>
+#include <skalibs/djbunix.h>
+
+#include <shibari/dcache.h>
+
+#include <skalibs/posixishard.h>
+
+static inline int dcache_load_node (dcache_t *z, uint64_t max, buffer *b)
+{
+ tain entry = { .nano = 0 } ;
+ tain expire = { .nano = 0 } ;
+ uint16_t keylen ;
+ uint16_t datalen ;
+ char pack[TAI_PACK * 2 + 4] ;
+ ssize_t r = buffer_get(b, pack, TAI_PACK * 2 + 4) ;
+ if (!r) return 0 ;
+ if (r < TAI_PACK * 2 + 4) return -1 ;
+ tai_unpack(pack, tain_secp(&entry)) ;
+ tai_unpack(pack + TAI_PACK, tain_secp(&expire)) ;
+ uint16_unpack_big(pack + TAI_PACK * 2, &keylen) ;
+ uint16_unpack_big(pack + TAI_PACK * 2 + 2, &datalen) ;
+ {
+ 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) ;
+ if (!r) return (errno = EPIPE, -1) ;
+ if (r < len) return -1 ;
+ if (blob[len]) return (errno = EPROTO, -1) ;
+ if (!dcache_add(z, max, blob, keylen, blob + keylen, datalen, &expire, &entry)) return -1 ;
+ }
+ return 1 ;
+}
+
+static inline int dcache_load_from_buffer (dcache_t *z, uint64_t max, buffer *b)
+{
+ {
+ char banner[sizeof(DCACHE_MAGIC) - 1] ;
+ char pack[8] ;
+ if (buffer_get(b, banner, sizeof(DCACHE_MAGIC) - 1) < sizeof(DCACHE_MAGIC) - 1)
+ return 0 ;
+ if (memcmp(banner, DCACHE_MAGIC, sizeof(DCACHE_MAGIC) - 1)) return 0 ;
+ if (buffer_get(b, pack, 8) < 8) return 0 ;
+ uint64_unpack_big(pack, &z->size) ;
+ if (buffer_get(b, pack, 8) < 8) return 0 ;
+ uint64_unpack_big(pack, &z->motion) ;
+ }
+ for (;;)
+ {
+ int r = dcache_load_node(z, max, b) ;
+ if (r < 0) return 0 ;
+ if (!r) break ;
+ }
+ return 1 ;
+}
+
+#define N 8192
+
+int dcache_load (dcache_t *z, uint64_t max, char const *file)
+{
+ char buf[N] ;
+ buffer b ;
+ int fd = open_readb(file) ;
+ if (fd == -1) return 0 ;
+ buffer_init(&b, &buffer_read, fd, buf, N) ;
+ if (!dcache_load_from_buffer(z, max, &b)) goto err ;
+ fd_close(fd) ;
+ return 1 ;
+
+ err:
+ dcache_free(z) ;
+ fd_close(fd) ;
+ return 0 ;
+}
diff --git a/src/libdcache/dcache_save.c b/src/libdcache/dcache_save.c
new file mode 100644
index 0000000..7277771
--- /dev/null
+++ b/src/libdcache/dcache_save.c
@@ -0,0 +1,73 @@
+/* ISC license. */
+
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include <skalibs/posixplz.h>
+#include <skalibs/uint16.h>
+#include <skalibs/uint64.h>
+#include <skalibs/buffer.h>
+#include <skalibs/tai.h>
+#include <skalibs/djbunix.h>
+#include <skalibs/skamisc.h>
+#include <skalibs/gensetdyn.h>
+
+#include <shibari/dcache.h>
+
+static int write_node_iter (void *data, void *aux)
+{
+ dcache_node_t *y = data ;
+ buffer *b = aux ;
+ char pack[TAI_PACK * 2 + 4] ;
+ tai_pack(pack, tain_secp(&y->entry)) ;
+ tai_pack(pack + TAI_PACK, tain_secp(&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, "", 1) == -1) return 0 ;
+ return 1 ;
+}
+
+static inline int dcache_save_to_buffer (dcache_t const *z, buffer *b)
+{
+ char pack[16] ;
+ if (buffer_puts(b, DCACHE_MAGIC) == -1) return 0 ;
+ uint64_pack_big(pack, z->size) ;
+ uint64_pack_big(pack + 8, z->motion) ;
+ if (buffer_put(b, pack, 16) < 16) return 0 ;
+
+ /* XXX: can gensetdyn_iter blow up the stack if z->storage is huge? */
+ if (gensetdyn_iter_nocancel((gensetdyn *)&z->storage, gensetdyn_n(&z->storage), &write_node_iter, b) < gensetdyn_n(&z->storage)) return 0 ;
+
+ return buffer_flush(b) ;
+}
+
+#define N 8192
+
+int dcache_save (dcache_t const *z, char const *file)
+{
+ size_t len = strlen(file) ;
+ int fd ;
+ buffer b ;
+ char buf[N] ;
+ char tmp[len + 20] ;
+ memcpy(tmp, file, len) ;
+ memcpy(tmp + len, ":dcache_save:XXXXXX", 20) ;
+ fd = mkstemp(tmp) ;
+ if (fd == -1) return 0 ;
+ buffer_init(&b, &buffer_write, fd, buf, N) ;
+ if (!dcache_save_to_buffer(z, &b) || fsync(fd) < 0) goto err2 ;
+ fd_close(fd) ;
+ if (rename(tmp, file) == -1) goto err1 ;
+ return 1 ;
+
+ err2:
+ fd_close(fd) ;
+ err1:
+ unlink_void(tmp) ;
+ return 0 ;
+}
diff --git a/src/libdcache/dcache_search.c b/src/libdcache/dcache_search.c
new file mode 100644
index 0000000..0239b88
--- /dev/null
+++ b/src/libdcache/dcache_search.c
@@ -0,0 +1,15 @@
+/* ISC license. */
+
+#include <stdint.h>
+
+#include <skalibs/avltree.h>
+
+#include <shibari/dcache.h>
+#include "dcache-internal.h"
+
+dcache_node_t *dcache_search (dcache_t *z, char const *key, uint16_t keylen)
+{
+ uint32_t i ;
+ dcache_key_t k = { .s = (char *)key, .len = keylen } ;
+ return avltree_search(&z->by_key, &k, &i) ? DNODE(z, i) : 0 ;
+}
diff --git a/src/libdcache/deps-lib/dcache b/src/libdcache/deps-lib/dcache
new file mode 100644
index 0000000..fcc09a0
--- /dev/null
+++ b/src/libdcache/deps-lib/dcache
@@ -0,0 +1,8 @@
+dcache_add.o
+dcache_clean_expired.o
+dcache_delete.o
+dcache_free.o
+dcache_init.o
+dcache_load.o
+dcache_save.o
+dcache_search.o