read-op-visitor.c 2.78 KB
Newer Older
Matthieu Dorier's avatar
Matthieu Dorier committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
/*
 * (C) 2017 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */
#include "libmobject-store.h"
#include "read-op-visitor.h"
#include "read-op-impl.h"
#include "utlist.h"

static void execute_read_op_visitor_on_stat(read_op_visitor_t visitor, rd_action_stat_t a);
static void execute_read_op_visitor_on_read(read_op_visitor_t visitor, rd_action_read_t a);
static void execute_read_op_visitor_on_omap_get_keys(read_op_visitor_t visitor, rd_action_omap_get_keys_t a);
static void execute_read_op_visitor_on_omap_get_vals(read_op_visitor_t visitor, rd_action_omap_get_vals_t a);
static void execute_read_op_visitor_on_omap_get_vals_by_keys(read_op_visitor_t visitor, rd_action_omap_get_vals_by_keys_t a);

typedef void (*dispatch_fn)(read_op_visitor_t, rd_action_base_t);

static dispatch_fn visitor_dispatch[_READ_OPCODE_END_ENUM_] = {
	NULL,
	(dispatch_fn)execute_read_op_visitor_on_stat,
	(dispatch_fn)execute_read_op_visitor_on_read,
	(dispatch_fn)execute_read_op_visitor_on_omap_get_keys,
	(dispatch_fn)execute_read_op_visitor_on_omap_get_vals,
	(dispatch_fn)execute_read_op_visitor_on_omap_get_vals_by_keys,
};

void execute_read_op_visitor(read_op_visitor_t visitor, mobject_store_read_op_t read_op)
{
	void* uargs = visitor->uargs;
	rd_action_base_t a;

	visitor->visit_begin(uargs);
	
	DL_FOREACH((read_op->actions), a) {
		visitor_dispatch[a->type](visitor, a);
	}

	visitor->visit_end(uargs);
}

////////////////////////////////////////////////////////////////////////////////
//                          STATIC FUNCTIONS BELOW                            //
////////////////////////////////////////////////////////////////////////////////

static void execute_read_op_visitor_on_stat(read_op_visitor_t visitor, rd_action_stat_t a)
{
	visitor->visit_stat(visitor->uargs, a->psize, a->pmtime, a->prval);
}

static void execute_read_op_visitor_on_read(read_op_visitor_t visitor, rd_action_read_t a)
{
	visitor->visit_read(visitor->uargs, a->offset, a->len, a->buffer, a->bytes_read, a->prval);
}

static void execute_read_op_visitor_on_omap_get_keys(read_op_visitor_t visitor, rd_action_omap_get_keys_t a)
{
	visitor->visit_omap_get_keys(visitor->uargs, a->start_after, a->max_return, a->iter, a->prval);
}

static void execute_read_op_visitor_on_omap_get_vals(read_op_visitor_t visitor, rd_action_omap_get_vals_t a)
{
	visitor->visit_omap_get_vals(visitor->uargs, a->start_after, a->filter_prefix, a->max_return, a->iter, a->prval);
}

static void execute_read_op_visitor_on_omap_get_vals_by_keys(read_op_visitor_t visitor, rd_action_omap_get_vals_by_keys_t a)
{
	const char* keys[a->num_keys];
	unsigned i;
	const char* ptr = a->data;
	for(i = 0; i < a->num_keys; i++) {
		keys[i] = ptr;
		ptr += strlen(keys[i])+1;
	}
	visitor->visit_omap_get_vals_by_keys(visitor->uargs, keys, a->num_keys, a->iter, a->prval);
}