diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2023-07-03 11:10:09 +0000 |
---|---|---|
committer | Laurent Bercot <ska@appnovation.com> | 2023-07-03 11:10:09 +0000 |
commit | 0beff2477b55728603127f21a9df604b6fcf840c (patch) | |
tree | 2d13673355974d9c74dc46b2dd84231c5b744f2e /src/libstddjb | |
parent | 109417b5defcd6d13c54b213c4f06272fcf0dc19 (diff) | |
download | skalibs-0beff2477b55728603127f21a9df604b6fcf840c.tar.xz |
Refactor cdbmake, add cdbmake_addv, cdb_hashv
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/libstddjb')
-rw-r--r-- | src/libstddjb/cdb-internal.h | 5 | ||||
-rw-r--r-- | src/libstddjb/cdb_hash.c | 8 | ||||
-rw-r--r-- | src/libstddjb/cdb_hashadd.c | 9 | ||||
-rw-r--r-- | src/libstddjb/cdb_hashv.c | 14 | ||||
-rw-r--r-- | src/libstddjb/cdbmake-internal.h | 14 | ||||
-rw-r--r-- | src/libstddjb/cdbmake_add.c | 22 | ||||
-rw-r--r-- | src/libstddjb/cdbmake_addbeginend.c | 24 | ||||
-rw-r--r-- | src/libstddjb/cdbmake_addv.c | 30 | ||||
-rw-r--r-- | src/libstddjb/cdbmake_finish.c (renamed from src/libstddjb/cdbmake.c) | 51 | ||||
-rw-r--r-- | src/libstddjb/cdbmake_posplus.c | 14 | ||||
-rw-r--r-- | src/libstddjb/cdbmake_start.c | 15 |
11 files changed, 151 insertions, 55 deletions
diff --git a/src/libstddjb/cdb-internal.h b/src/libstddjb/cdb-internal.h index 0db5a5d..d2b7324 100644 --- a/src/libstddjb/cdb-internal.h +++ b/src/libstddjb/cdb-internal.h @@ -4,11 +4,16 @@ #define SKALIBS_CDB_INTERNAL_H #include <stdint.h> +#include <sys/uio.h> #include <skalibs/gccattributes.h> #include <skalibs/cdb.h> +#define CDB_HASHSTART 5381 + +extern uint32_t cdb_hashadd (uint32_t, uint8_t) ; extern uint32_t cdb_hash (char const *, uint32_t) gccattr_pure ; +extern uint32_t cdb_hashv (struct iovec const *, unsigned int) gccattr_pure ; extern char const *cdb_p (cdb const *, uint32_t, uint32_t) gccattr_pure ; #endif diff --git a/src/libstddjb/cdb_hash.c b/src/libstddjb/cdb_hash.c index a7b626b..3849f02 100644 --- a/src/libstddjb/cdb_hash.c +++ b/src/libstddjb/cdb_hash.c @@ -4,14 +4,6 @@ #include "cdb-internal.h" -#define CDB_HASHSTART 5381 - -static inline uint32_t cdb_hashadd (uint32_t h, unsigned char c) -{ - h += (h << 5) ; - return h ^ c ; -} - uint32_t cdb_hash (char const *buf, uint32_t len) { uint32_t h = CDB_HASHSTART ; diff --git a/src/libstddjb/cdb_hashadd.c b/src/libstddjb/cdb_hashadd.c new file mode 100644 index 0000000..426fb95 --- /dev/null +++ b/src/libstddjb/cdb_hashadd.c @@ -0,0 +1,9 @@ +/* ISC license. */ + +#include "cdb-internal.h" + +uint32_t cdb_hashadd (uint32_t h, uint8_t c) +{ + h += (h << 5) ; + return h ^ c ; +} diff --git a/src/libstddjb/cdb_hashv.c b/src/libstddjb/cdb_hashv.c new file mode 100644 index 0000000..e2b80d9 --- /dev/null +++ b/src/libstddjb/cdb_hashv.c @@ -0,0 +1,14 @@ +/* ISC license. */ + +#include <stdint.h> + +#include "cdb-internal.h" + +uint32_t cdb_hashv (struct iovec const *v, unsigned int n) +{ + uint32_t h = CDB_HASHSTART ; + for (unsigned int i = 0 ; i < n ; i++) + for (size_t j = 0 ; j < v[i].iov_len ; j++) + h = cdb_hashadd(h, ((uint8_t const *)v[i].iov_base)[j]) ; + return h ; +} diff --git a/src/libstddjb/cdbmake-internal.h b/src/libstddjb/cdbmake-internal.h new file mode 100644 index 0000000..1d5bd24 --- /dev/null +++ b/src/libstddjb/cdbmake-internal.h @@ -0,0 +1,14 @@ +/* ISC license. */ + +#ifndef SKALIBS_CDBMAKE_INTERNAL_H +#define SKALIBS_CDBMAKE_INTERNAL_H + +#include <stdint.h> + +#include <skalibs/cdbmake.h> + +extern int cdbmake_posplus (cdbmaker *, uint32_t) ; +extern int cdbmake_addend (cdbmaker *, uint32_t, uint32_t, uint32_t) ; +extern int cdbmake_addbegin (cdbmaker *, uint32_t, uint32_t) ; + +#endif diff --git a/src/libstddjb/cdbmake_add.c b/src/libstddjb/cdbmake_add.c new file mode 100644 index 0000000..f7fd096 --- /dev/null +++ b/src/libstddjb/cdbmake_add.c @@ -0,0 +1,22 @@ +/* ISC license. */ + +#include <skalibs/uint32.h> +#include <skalibs/diuint32.h> +#include <skalibs/buffer.h> +#include <skalibs/genalloc.h> +#include <skalibs/cdbmake.h> +#include "cdb-internal.h" +#include "cdbmake-internal.h" + +int cdbmake_add (cdbmaker *c, char const *key, uint32_t keylen, char const *data, uint32_t datalen) +{ + if (!cdbmake_addbegin(c, keylen, datalen) + || buffer_put(&c->b, key, keylen) < keylen + || buffer_put(&c->b, data, datalen) < datalen + || !cdbmake_addend(c, keylen, datalen, cdb_hash(key, keylen))) + { + genalloc_free(diuint32, &c->hplist) ; + return 0 ; + } + return 1 ; +} diff --git a/src/libstddjb/cdbmake_addbeginend.c b/src/libstddjb/cdbmake_addbeginend.c new file mode 100644 index 0000000..37d35c8 --- /dev/null +++ b/src/libstddjb/cdbmake_addbeginend.c @@ -0,0 +1,24 @@ +/* ISC license. */ + +#include <stdint.h> +#include <errno.h> + +#include <skalibs/uint32.h> +#include <skalibs/diuint32.h> +#include <skalibs/buffer.h> +#include <skalibs/genalloc.h> +#include "cdbmake-internal.h" + +int cdbmake_addend (cdbmaker *c, uint32_t keylen, uint32_t datalen, uint32_t h) +{ + diuint32 blah = { .left = h, .right = c->pos } ; + return genalloc_append(diuint32, &c->hplist, &blah) && cdbmake_posplus(c, 8) && cdbmake_posplus(c, keylen) && cdbmake_posplus(c, datalen) ; +} + +int cdbmake_addbegin (cdbmaker *c, uint32_t keylen, uint32_t datalen) +{ + char buf[8] ; + uint32_pack(buf, keylen) ; + uint32_pack(buf + 4, datalen) ; + return buffer_put(&c->b, buf, 8) == 8 ; +} diff --git a/src/libstddjb/cdbmake_addv.c b/src/libstddjb/cdbmake_addv.c new file mode 100644 index 0000000..d8719a4 --- /dev/null +++ b/src/libstddjb/cdbmake_addv.c @@ -0,0 +1,30 @@ +/* ISC license. */ + +#include <errno.h> +#include <sys/uio.h> + +#include <skalibs/uint32.h> +#include <skalibs/diuint32.h> +#include <skalibs/buffer.h> +#include <skalibs/genalloc.h> +#include <skalibs/siovec.h> +#include <skalibs/cdbmake.h> +#include "cdb-internal.h" +#include "cdbmake-internal.h" + +int cdbmake_addv (cdbmaker *c, struct iovec const *kv, unsigned int kn, struct iovec const *dv, unsigned int dn) +{ + size_t keylen = siovec_len(kv, kn) ; + size_t datalen = siovec_len(dv, dn) ; + if (keylen > UINT32_MAX || datalen > UINT32_MAX) return (errno = EOVERFLOW, 0) ; + + if (!cdbmake_addbegin(c, keylen, datalen) + || buffer_putv(&c->b, kv, kn) < keylen + || buffer_putv(&c->b, dv, dn) < datalen + || !cdbmake_addend(c, keylen, datalen, cdb_hashv(kv, kn))) + { + genalloc_free(diuint32, &c->hplist) ; + return 0 ; + } + return 1 ; +} diff --git a/src/libstddjb/cdbmake.c b/src/libstddjb/cdbmake_finish.c index 4cc4fc8..fe0b35a 100644 --- a/src/libstddjb/cdbmake.c +++ b/src/libstddjb/cdbmake_finish.c @@ -9,50 +9,7 @@ #include <skalibs/buffer.h> #include <skalibs/genalloc.h> #include <skalibs/cdbmake.h> -#include "cdb-internal.h" - -int cdbmake_start (cdbmaker *c, int fd) -{ - c->hplist = genalloc_zero ; - c->pos = 2048 ; - buffer_init(&c->b, &buffer_write, fd, c->buf, BUFFER_OUTSIZE) ; - return lseek(fd, c->pos, SEEK_SET) >= 0 ; -} - -static int posplus (cdbmaker *c, uint32_t len) -{ - uint32_t newpos = c->pos + len ; - if (newpos < len) return (errno = ENOMEM, 0) ; - c->pos = newpos ; - return 1 ; -} - -static inline int cdbmake_addend (cdbmaker *c, uint32_t keylen, uint32_t datalen, uint32_t h) -{ - diuint32 blah = { .left = h, .right = c->pos } ; - return genalloc_append(diuint32, &c->hplist, &blah) && posplus(c, 8) && posplus(c, keylen) && posplus(c, datalen) ; -} - -static inline ssize_t cdbmake_addbegin (cdbmaker *c, uint32_t keylen, uint32_t datalen) -{ - char buf[8] ; - uint32_pack(buf, keylen) ; - uint32_pack(buf + 4, datalen) ; - return buffer_put(&c->b, buf, 8) == 8 ; -} - -int cdbmake_add (cdbmaker *c, char const *key, uint32_t keylen, char const *data, uint32_t datalen) -{ - if (!cdbmake_addbegin(c, keylen, datalen) - || buffer_put(&c->b, key, keylen) < 0 - || buffer_put(&c->b, data, datalen) < 0 - || !cdbmake_addend(c, keylen, datalen, cdb_hash(key, keylen))) - { - genalloc_free(diuint32, &c->hplist) ; - return 0 ; - } - return 1 ; -} +#include "cdbmake-internal.h" int cdbmake_finish (cdbmaker *c) { @@ -109,13 +66,13 @@ int cdbmake_finish (cdbmaker *c) uint32_pack(buf, hp[j].left) ; uint32_pack(buf + 4, hp[j].right) ; if (buffer_put(&c->b, buf, 8) < 0) return 0 ; - if (!posplus(c, 8)) return 0 ; + if (!cdbmake_posplus(c, 8)) return 0 ; } } } if (!buffer_flush(&c->b) - || lseek(buffer_fd(&c->b), 0, SEEK_SET) < 0 - || buffer_putflush(&c->b, final, 2048) < 0) return 0 ; + || lseek(buffer_fd(&c->b), 0, SEEK_SET) == -1 + || buffer_putflush(&c->b, final, 2048) < 2048) return 0 ; return 1 ; } diff --git a/src/libstddjb/cdbmake_posplus.c b/src/libstddjb/cdbmake_posplus.c new file mode 100644 index 0000000..a2f4468 --- /dev/null +++ b/src/libstddjb/cdbmake_posplus.c @@ -0,0 +1,14 @@ +/* ISC license. */ + +#include <stdint.h> +#include <errno.h> + +#include "cdbmake-internal.h" + +int cdbmake_posplus (cdbmaker *c, uint32_t len) +{ + uint32_t newpos = c->pos + len ; + if (newpos < len) return (errno = ENOMEM, 0) ; + c->pos = newpos ; + return 1 ; +} diff --git a/src/libstddjb/cdbmake_start.c b/src/libstddjb/cdbmake_start.c new file mode 100644 index 0000000..94f1635 --- /dev/null +++ b/src/libstddjb/cdbmake_start.c @@ -0,0 +1,15 @@ +/* ISC license. */ + +#include <unistd.h> + +#include <skalibs/buffer.h> +#include <skalibs/genalloc.h> +#include <skalibs/cdbmake.h> + +int cdbmake_start (cdbmaker *c, int fd) +{ + c->hplist = genalloc_zero ; + c->pos = 2048 ; + buffer_init(&c->b, &buffer_write, fd, c->buf, BUFFER_OUTSIZE) ; + return lseek(fd, c->pos, SEEK_SET) >= 0 ; +} |