write-op-visitor.c 5.23 KB
Newer Older
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 10
#include "src/io-chain/write-op-visitor.h"
#include "src/io-chain/write-op-impl.h"
#include "src/util/utlist.h"
#include "src/util/log.h"
11

12 13 14 15 16 17 18 19 20 21
static void execute_write_op_visitor_on_create(write_op_visitor_t visitor, wr_action_create_t a, void* uargs);
static void execute_write_op_visitor_on_write(write_op_visitor_t visitor, wr_action_write_t a, void* uargs);
static void execute_write_op_visitor_on_write_full(write_op_visitor_t visitor, wr_action_write_full_t a, void* uargs);
static void execute_write_op_visitor_on_write_same(write_op_visitor_t visitor, wr_action_write_same_t a, void* uargs);
static void execute_write_op_visitor_on_append(write_op_visitor_t visitor, wr_action_append_t a, void* uargs);
static void execute_write_op_visitor_on_remove(write_op_visitor_t visitor, wr_action_remove_t a, void* uargs);
static void execute_write_op_visitor_on_truncate(write_op_visitor_t visitor, wr_action_truncate_t a, void* uargs);
static void execute_write_op_visitor_on_zero(write_op_visitor_t visitor, wr_action_zero_t a, void* uargs);
static void execute_write_op_visitor_on_omap_set(write_op_visitor_t visitor, wr_action_omap_set_t a, void* uargs);
static void execute_write_op_visitor_on_omap_rm_keys(write_op_visitor_t visitor, wr_action_omap_rm_keys_t a, void* uargs);
22

23
typedef void (*dispatch_fn)(write_op_visitor_t, wr_action_base_t, void* uargs);
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

static dispatch_fn visitor_dispatch[_WRITE_OPCODE_END_ENUM_] = {
	NULL,
	(dispatch_fn)execute_write_op_visitor_on_create,
	(dispatch_fn)execute_write_op_visitor_on_write,
	(dispatch_fn)execute_write_op_visitor_on_write_full,
	(dispatch_fn)execute_write_op_visitor_on_write_same,
	(dispatch_fn)execute_write_op_visitor_on_append,
	(dispatch_fn)execute_write_op_visitor_on_remove,
	(dispatch_fn)execute_write_op_visitor_on_truncate,
	(dispatch_fn)execute_write_op_visitor_on_zero,
	(dispatch_fn)execute_write_op_visitor_on_omap_set,
	(dispatch_fn)execute_write_op_visitor_on_omap_rm_keys
};

39
void execute_write_op_visitor(write_op_visitor_t visitor, mobject_store_write_op_t write_op, void* uargs)
40 41 42 43 44 45
{
	wr_action_base_t a;

	visitor->visit_begin(uargs);
	
	DL_FOREACH((write_op->actions), a) {
46 47 48
		MOBJECT_ASSERT(a->type > 0 && a->type < _WRITE_OPCODE_END_ENUM_,
		"Invalid action type");
		visitor_dispatch[a->type](visitor, a, uargs);
49 50 51 52 53 54 55 56 57
	}

	visitor->visit_end(uargs);
}

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

58
static void execute_write_op_visitor_on_create(write_op_visitor_t visitor, wr_action_create_t a, void* uargs)
59
{
60
	if(visitor->visit_create)
61
		visitor->visit_create(uargs, a->exclusive);
62 63
}

64
static void execute_write_op_visitor_on_write(write_op_visitor_t visitor, wr_action_write_t a, void* uargs)
65
{
66
	if(visitor->visit_write)
67
		visitor->visit_write(uargs, a->buffer, a->len, a->offset);
68 69
}

70
static void execute_write_op_visitor_on_write_full(write_op_visitor_t visitor, wr_action_write_full_t a, void* uargs)
71
{
72
	if(visitor->visit_write_full)
73
		visitor->visit_write_full(uargs, a->buffer, a->len);
74 75
}

76
static void execute_write_op_visitor_on_write_same(write_op_visitor_t visitor, wr_action_write_same_t a, void* uargs)
77
{
78
	if(visitor->visit_writesame)
79
		visitor->visit_writesame(uargs, a->buffer, a->data_len, a->write_len, a->offset);
80 81
}

82
static void execute_write_op_visitor_on_append(write_op_visitor_t visitor, wr_action_append_t a, void* uargs)
83
{
84
	if(visitor->visit_append)
85
		visitor->visit_append(uargs, a->buffer, a->len);
86 87
}

88
static void execute_write_op_visitor_on_remove(write_op_visitor_t visitor, wr_action_remove_t a, void* uargs)
89
{
90
	if(visitor->visit_remove)
91
		visitor->visit_remove(uargs);
92 93
}

94
static void execute_write_op_visitor_on_truncate(write_op_visitor_t visitor, wr_action_truncate_t a, void* uargs)
95
{
96
	if(visitor->visit_truncate)
97
		visitor->visit_truncate(uargs, a->offset);
98 99
}

100
static void execute_write_op_visitor_on_zero(write_op_visitor_t visitor, wr_action_zero_t a, void* uargs)
101
{
102
	if(visitor->visit_zero)
103
		visitor->visit_zero(uargs, a->offset, a->len);
104 105
}

106
static void execute_write_op_visitor_on_omap_set(write_op_visitor_t visitor, wr_action_omap_set_t a, void* uargs)
107
{
108 109
	if(visitor->visit_omap_set == NULL) return;

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
	size_t num = a->num;
	size_t lens[num];
	const char* keys[num];
	const char* vals[num];

	unsigned i;
	const char* ptr = a->data;
	for(i=0; i < num; i++) {
		// ptr is pointing to a key now
		keys[i] = ptr;
		// get size of key
		size_t s = strlen(keys[i])+1;
		// jump pointer right after key
		ptr += s;
		// ptr is pointing to a length of data, now
		memcpy(lens+i, ptr, sizeof(size_t));
		ptr += sizeof(size_t);
		vals[i] = ptr;
		ptr += lens[i];
	}

131
	visitor->visit_omap_set(uargs, keys, vals, lens, num);
132 133
}

134
static void execute_write_op_visitor_on_omap_rm_keys(write_op_visitor_t visitor, wr_action_omap_rm_keys_t a, void* uargs)
135
{
136
	if(visitor->visit_omap_rm_keys == NULL) return;
137

138 139 140 141 142 143 144 145 146 147
	size_t num_keys = a->num_keys;
	const char* keys[num_keys];

	unsigned i;
	const char* ptr = a->data;
	for(i=0; i < num_keys; i++) {
		keys[i] = ptr;
		ptr += strlen(keys[i])+1;
	}

148
	visitor->visit_omap_rm_keys(uargs, keys, num_keys);
149
}