omap-iter-impl.c 1.84 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * (C) 2017 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */
#include <stdlib.h>
#include <string.h>
#include "utlist.h"
#include "libmobject-store.h"
#include "omap-iter-impl.h"
11
#include "log.h"
12 13 14 15

void omap_iter_create(mobject_store_omap_iter_t* iter)
{
	*iter = (mobject_store_omap_iter_t)calloc(1, sizeof(**iter));
16
	(*iter)->ref_count = 1;
17 18 19 20
}

void omap_iter_free(mobject_store_omap_iter_t iter)
{
21
	if(!iter) return;
22 23
	iter->ref_count -= 1;
	if(iter->ref_count > 0) return;
24

25 26 27 28 29 30 31
	omap_iter_node_t elt, tmp;

	DL_FOREACH_SAFE(iter->head, elt, tmp) {
		DL_DELETE(iter->head, elt);
		free((void*)(elt->key));
		free((void*)(elt->value));
		free((void*)(elt));
32
	}
33 34 35 36

	free(iter);
}

37 38 39 40 41 42
void omap_iter_incr_ref(mobject_store_omap_iter_t iter)
{
	if(!iter) return;
	iter->ref_count += 1;
}

43 44 45 46
void omap_iter_append(mobject_store_omap_iter_t iter, 
                      const char* key, const char* val, 
                      size_t val_size)
{
47 48
	MOBJECT_ASSERT(iter, "trying to append to a NULL iterator");

49
	omap_iter_node_t item = (omap_iter_node_t)calloc(1, sizeof(*item));
50 51 52 53
	item->key             = strdup(key);
	item->key_size        = strlen(key)+1;
	item->value_size      = val_size;
	item->value           = (char*)malloc(val_size);
54 55 56 57 58 59
	memcpy(item->value, val, val_size);

	DL_APPEND(iter->head, item);

	if(iter->current == NULL)
		iter->current = iter->head;
60 61

	iter->num_items += 1;
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
}

int mobject_store_omap_get_next(mobject_store_omap_iter_t iter,
                                char **key,
                                char **val,
                                size_t *len)
{
	if(iter->current == NULL) return -1;

	*key = iter->current->key;
	*val = iter->current->value;
	*len = iter->current->value_size;

	iter->current = iter->current->next;

	return 0;
}

void mobject_store_omap_get_end(mobject_store_omap_iter_t iter)
{
	omap_iter_free(iter);	
}