Commit 60ddb02c authored by Matthieu Dorier's avatar Matthieu Dorier

added write_op visitor pattern

parent 91f00a69
......@@ -6,7 +6,8 @@ src_libmobject_store_la_SOURCES = \
src/proc-write-actions.c \
src/proc-read-actions.c \
src/prepare-write-op.c \
src/prepare-read-op.c
src/prepare-read-op.c \
src/write-op-visitor.c
noinst_HEADERS += \
src/log.h \
......@@ -19,4 +20,6 @@ noinst_HEADERS += \
src/proc-write-actions.h \
src/proc-read-actions.h \
src/prepare-write-op.h \
src/prepare-read-op.h
src/prepare-read-op.h \
src/write-op-visitor.h \
src/buffer-union.h
/*
* (C) 2017 The University of Chicago
*
* See COPYRIGHT in top-level directory.
*/
#ifndef __MOBJECT_BUFFER_UNION_H
#define __MOBJECT_BUFFER_UNION_H
typedef union {
const char* as_pointer;
uint64_t as_offset;
} buffer_u;
#endif
......@@ -7,6 +7,7 @@
#define __MOBJECT_WRITE_OPCODES_H
#include "mobject-store-config.h"
#include "buffer-union.h"
typedef enum {
WRITE_OPCODE_BASE = 0,
......@@ -45,29 +46,20 @@ typedef struct wr_action_CREATE {
typedef struct wr_action_WRITE {
struct wr_action_BASE base;
union {
const char* as_pointer;
uint64_t as_offset;
} buffer;
buffer_u buffer;
size_t len;
uint64_t offset;
}* wr_action_write_t;
typedef struct wr_action_WRITE_FULL {
struct wr_action_BASE base;
union {
const char* as_pointer;
uint64_t as_offset;
} buffer;
buffer_u buffer;
size_t len;
}* wr_action_write_full_t;
typedef struct wr_action_WRITE_SAME {
struct wr_action_BASE base;
union {
const char* as_pointer;
uint64_t as_offset;
} buffer;
buffer_u buffer;
size_t data_len;
size_t write_len;
uint64_t offset;
......@@ -75,10 +67,7 @@ typedef struct wr_action_WRITE_SAME {
typedef struct wr_action_APPEND {
struct wr_action_BASE base;
union {
const char* as_pointer;
uint64_t as_offset;
} buffer;
buffer_u buffer;
size_t len;
}* wr_action_append_t;
......
/*
* (C) 2017 The University of Chicago
*
* See COPYRIGHT in top-level directory.
*/
#include "libmobject-store.h"
#include "write-op-visitor.h"
#include "write-op-impl.h"
#include "utlist.h"
static void execute_write_op_visitor_on_create(write_op_visitor_t visitor, wr_action_create_t a);
static void execute_write_op_visitor_on_write(write_op_visitor_t visitor, wr_action_write_t a);
static void execute_write_op_visitor_on_write_full(write_op_visitor_t visitor, wr_action_write_full_t a);
static void execute_write_op_visitor_on_write_same(write_op_visitor_t visitor, wr_action_write_same_t a);
static void execute_write_op_visitor_on_append(write_op_visitor_t visitor, wr_action_append_t a);
static void execute_write_op_visitor_on_remove(write_op_visitor_t visitor, wr_action_remove_t a);
static void execute_write_op_visitor_on_truncate(write_op_visitor_t visitor, wr_action_truncate_t a);
static void execute_write_op_visitor_on_zero(write_op_visitor_t visitor, wr_action_zero_t a);
static void execute_write_op_visitor_on_omap_set(write_op_visitor_t visitor, wr_action_omap_set_t a);
static void execute_write_op_visitor_on_omap_rm_keys(write_op_visitor_t visitor, wr_action_omap_rm_keys_t a);
typedef void (*dispatch_fn)(write_op_visitor_t, wr_action_base_t);
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
};
void execute_write_op_visitor(write_op_visitor_t visitor, mobject_store_write_op_t write_op)
{
void* uargs = visitor->uargs;
wr_action_base_t a;
visitor->visit_begin(uargs);
DL_FOREACH((write_op->actions), a) {
visitor_dispatch[a->type](visitor, a);
}
visitor->visit_end(uargs);
}
////////////////////////////////////////////////////////////////////////////////
// STATIC FUNCTIONS BELOW //
////////////////////////////////////////////////////////////////////////////////
static void execute_write_op_visitor_on_create(write_op_visitor_t visitor, wr_action_create_t a)
{
visitor->visit_create(visitor->uargs, a->exclusive);
}
static void execute_write_op_visitor_on_write(write_op_visitor_t visitor, wr_action_write_t a)
{
visitor->visit_write(visitor->uargs, a->buffer, a->len, a->offset);
}
static void execute_write_op_visitor_on_write_full(write_op_visitor_t visitor, wr_action_write_full_t a)
{
visitor->visit_write_full(visitor->uargs, a->buffer, a->len);
}
static void execute_write_op_visitor_on_write_same(write_op_visitor_t visitor, wr_action_write_same_t a)
{
visitor->visit_writesame(visitor->uargs, a->buffer, a->data_len, a->write_len, a->offset);
}
static void execute_write_op_visitor_on_append(write_op_visitor_t visitor, wr_action_append_t a)
{
visitor->visit_append(visitor->uargs, a->buffer, a->len);
}
static void execute_write_op_visitor_on_remove(write_op_visitor_t visitor, wr_action_remove_t a)
{
visitor->visit_remove(visitor->uargs);
}
static void execute_write_op_visitor_on_truncate(write_op_visitor_t visitor, wr_action_truncate_t a)
{
visitor->visit_truncate(visitor->uargs, a->offset);
}
static void execute_write_op_visitor_on_zero(write_op_visitor_t visitor, wr_action_zero_t a)
{
visitor->visit_zero(visitor->uargs, a->offset, a->len);
}
static void execute_write_op_visitor_on_omap_set(write_op_visitor_t visitor, wr_action_omap_set_t a)
{
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];
}
visitor->visit_omap_set(visitor->uargs, keys, vals, lens, num);
}
static void execute_write_op_visitor_on_omap_rm_keys(write_op_visitor_t visitor, wr_action_omap_rm_keys_t a)
{
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;
}
visitor->visit_omap_rm_keys(visitor->uargs, keys, num_keys);
}
/*
* (C) 2017 The University of Chicago
*
* See COPYRIGHT in top-level directory.
*/
#ifndef __MOBJECT_WRITE_OP_VISITOR_H
#define __MOBJECT_WRITE_OP_VISITOR_H
#include "libmobject-store.h"
#include "buffer-union.h"
typedef struct write_op_visitor {
void* uargs;
void (*visit_begin)(void*);
void (*visit_create)(void*, int);
void (*visit_write)(void*, buffer_u, size_t, uint64_t);
void (*visit_write_full)(void*, buffer_u, size_t);
void (*visit_writesame)(void*, buffer_u, size_t, size_t, uint64_t);
void (*visit_append)(void*, buffer_u, size_t);
void (*visit_remove)(void*);
void (*visit_truncate)(void*, uint64_t);
void (*visit_zero)(void*, uint64_t, uint64_t);
void (*visit_omap_set)(void*, char const* const*, char const* const*, const size_t*, size_t);
void (*visit_omap_rm_keys)(void*, char const* const*, size_t);
void (*visit_end)(void*);
}* write_op_visitor_t;
void execute_write_op_visitor(write_op_visitor_t visitor, mobject_store_write_op_t write_op);
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment