From cb2a50691fb0cddb64e1b5a9ed242a6a0b42d503 Mon Sep 17 00:00:00 2001 From: Hristo Venev Date: Mon, 22 Jun 2020 12:32:11 +0300 Subject: Initial commit. --- lib/iter.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 lib/iter.c (limited to 'lib/iter.c') diff --git a/lib/iter.c b/lib/iter.c new file mode 100644 index 0000000..ff27a68 --- /dev/null +++ b/lib/iter.c @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: LGPL-3.0-or-later + +#include "impl.h" + +void hkvs_iter_open(hkvs *c, hkvs_table *t, hkvs_iter *it) { + (void)c; + + it->priv1 = 0; + it->priv2 = hkvs_slab_size(&t->slab); +} + +void hkvs_iter_seek(hkvs *c, hkvs_table *t, hkvs_iter *it, hkvs_rid id) { + (void)c; + + size_t n = hkvs_slab_size(&t->slab); + it->priv1 = id.id >= n ? SIZE_MAX : (size_t)id.id; + it->priv2 = n; +} + +hkvs_rid hkvs_iter_next(hkvs *c, hkvs_table *t, hkvs_iter *it, const char **pkey) { + (void)c; + + size_t id = it->priv1; + size_t n = it->priv2; + if(id >= n) return HKVS_INVALID_RID; + while(true) { + uint8_t mark = hkvs_slab_mark(&t->slab, id); + if(mark != 0) { + it->priv1 = id + 1; + if(pkey) *pkey = hkvs_slab_at(&t->slab, id); + return (hkvs_rid){id}; + } + id++; + if(id == n) { + it->priv1 = SIZE_MAX; + return HKVS_INVALID_RID; + } + } +} + +void hkvs_iter_delete(hkvs *c, hkvs_table *t, hkvs_iter *it) { + hkvs_record_delete(c, t, (hkvs_rid){it->priv1 - 1}); +} -- cgit