aboutsummaryrefslogblamecommitdiff
path: root/lib/iter.c
blob: ff27a68263494ab3583da22971d56911f1976253 (plain) (tree)










































                                                                                   
// 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});
}