read-op-visitor.c 3.07 KB
Newer Older
Matthieu Dorier's avatar
Matthieu Dorier committed
1 2 3 4 5 6
/*
 * (C) 2017 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */
#include "libmobject-store.h"
Matthieu Dorier's avatar
Matthieu Dorier committed
7 8 9
#include "src/io-chain/read-op-visitor.h"
#include "src/io-chain/read-op-impl.h"
#include "src/util/utlist.h"
Matthieu Dorier's avatar
Matthieu Dorier committed
10

11 12 13 14 15
static void execute_read_op_visitor_on_stat(read_op_visitor_t visitor, rd_action_stat_t a, void* uargs);
static void execute_read_op_visitor_on_read(read_op_visitor_t visitor, rd_action_read_t a, void* uargs);
static void execute_read_op_visitor_on_omap_get_keys(read_op_visitor_t visitor, rd_action_omap_get_keys_t a, void* uargs);
static void execute_read_op_visitor_on_omap_get_vals(read_op_visitor_t visitor, rd_action_omap_get_vals_t a, void* uargs);
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, void* uargs);
Matthieu Dorier's avatar
Matthieu Dorier committed
16

17
typedef void (*dispatch_fn)(read_op_visitor_t, rd_action_base_t, void*);
Matthieu Dorier's avatar
Matthieu Dorier committed
18 19 20 21 22 23 24 25 26 27

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,
};

28
void execute_read_op_visitor(read_op_visitor_t visitor, mobject_store_read_op_t read_op, void* uargs)
Matthieu Dorier's avatar
Matthieu Dorier committed
29 30 31 32 33 34
{
	rd_action_base_t a;

	visitor->visit_begin(uargs);
	
	DL_FOREACH((read_op->actions), a) {
35
		visitor_dispatch[a->type](visitor, a, uargs);
Matthieu Dorier's avatar
Matthieu Dorier committed
36 37 38 39 40 41 42 43 44
	}

	visitor->visit_end(uargs);
}

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

45
static void execute_read_op_visitor_on_stat(read_op_visitor_t visitor, rd_action_stat_t a, void* uargs)
Matthieu Dorier's avatar
Matthieu Dorier committed
46
{
47
	if(visitor->visit_stat)
48
		visitor->visit_stat(uargs, a->psize, a->pmtime, a->prval);
Matthieu Dorier's avatar
Matthieu Dorier committed
49 50
}

51
static void execute_read_op_visitor_on_read(read_op_visitor_t visitor, rd_action_read_t a, void* uargs)
Matthieu Dorier's avatar
Matthieu Dorier committed
52
{
53
	if(visitor->visit_read)
54
		visitor->visit_read(uargs, a->offset, a->len, a->buffer, a->bytes_read, a->prval);
Matthieu Dorier's avatar
Matthieu Dorier committed
55 56
}

57
static void execute_read_op_visitor_on_omap_get_keys(read_op_visitor_t visitor, rd_action_omap_get_keys_t a, void* uargs)
Matthieu Dorier's avatar
Matthieu Dorier committed
58
{
59
	if(visitor->visit_omap_get_keys)
60
		visitor->visit_omap_get_keys(uargs, a->start_after, a->max_return, a->iter, a->prval);
Matthieu Dorier's avatar
Matthieu Dorier committed
61 62
}

63
static void execute_read_op_visitor_on_omap_get_vals(read_op_visitor_t visitor, rd_action_omap_get_vals_t a, void* uargs)
Matthieu Dorier's avatar
Matthieu Dorier committed
64
{
65
	if(visitor->visit_omap_get_vals)
66
		visitor->visit_omap_get_vals(uargs, a->start_after, a->filter_prefix, a->max_return, a->iter, a->prval);
Matthieu Dorier's avatar
Matthieu Dorier committed
67 68
}

69
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, void* uargs)
Matthieu Dorier's avatar
Matthieu Dorier committed
70
{
71 72
	if(visitor->visit_omap_get_vals_by_keys == NULL) return;

Matthieu Dorier's avatar
Matthieu Dorier committed
73 74 75 76 77 78 79
	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;
	}
80
	visitor->visit_omap_get_vals_by_keys(uargs, keys, a->num_keys, a->iter, a->prval);
Matthieu Dorier's avatar
Matthieu Dorier committed
81 82
}