fake-write-op.cpp 5.93 KB
Newer Older
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
#include <map>
#include <string>
#include <iostream>
#include "src/server/visitor-args.h"
#include "src/server/fake/fake-object.hpp"
#include "src/io-chain/write-op-visitor.h"

extern std::map< std::string, fake_object > fake_db;

static void write_op_exec_begin(void*);
static void write_op_exec_end(void*);
static void write_op_exec_create(void*, int);
static void write_op_exec_write(void*, buffer_u, size_t, uint64_t);
static void write_op_exec_write_full(void*, buffer_u, size_t);
static void write_op_exec_writesame(void*, buffer_u, size_t, size_t, uint64_t);
static void write_op_exec_append(void*, buffer_u, size_t);
static void write_op_exec_remove(void*);
static void write_op_exec_truncate(void*, uint64_t);
static void write_op_exec_zero(void*, uint64_t, uint64_t);
static void write_op_exec_omap_set(void*, char const* const*, char const* const*, const size_t*, size_t);
static void write_op_exec_omap_rm_keys(void*, char const* const*, size_t);

struct write_op_visitor write_op_exec = {
	.visit_begin        = write_op_exec_begin,
	.visit_create       = write_op_exec_create,
	.visit_write        = write_op_exec_write,
	.visit_write_full   = write_op_exec_write_full,
	.visit_writesame    = write_op_exec_writesame,
	.visit_append       = write_op_exec_append,
	.visit_remove       = write_op_exec_remove,
	.visit_truncate     = write_op_exec_truncate,
	.visit_zero         = write_op_exec_zero,
	.visit_omap_set     = write_op_exec_omap_set,
	.visit_omap_rm_keys = write_op_exec_omap_rm_keys,
	.visit_end          = write_op_exec_end
};

extern "C" void fake_write_op(mobject_store_write_op_t write_op, server_visitor_args_t vargs)
{
	/* Execute the operation chain */
	execute_write_op_visitor(&write_op_exec, write_op, (void*)vargs);
}

void write_op_exec_begin(void* u)
{
	auto vargs = static_cast<server_visitor_args_t>(u);
}

void write_op_exec_end(void* u)
{
	auto vargs = static_cast<server_visitor_args_t>(u);
}

void write_op_exec_create(void* u, int exclusive)
{
	auto vargs = static_cast<server_visitor_args_t>(u);
	std::string name(vargs->object_name);
	if(fake_db.count(name) != 0) {
		std::cerr << "[FAKE-BACKEND-WARNING] (create) Object " << name << " already exists" << std::endl;
		return;
	}
	fake_db[name] = fake_object();
}

void write_op_exec_write(void* u, buffer_u buf, size_t len, uint64_t offset)
{
	auto vargs = static_cast<server_visitor_args_t>(u);
	std::string name(vargs->object_name);
	if(fake_db.count(name) == 0) {
		std::cerr << "[FAKE-BACKEND-WARNING] (write) Object " << name << " does not exist, it will be created" << std::endl;
	}
	fake_db[name].write(vargs->mid, vargs->client_addr, vargs->bulk_handle, buf.as_offset, offset, len);
}

void write_op_exec_write_full(void* u, buffer_u buf, size_t len)
{
	auto vargs = static_cast<server_visitor_args_t>(u);
	std::string name(vargs->object_name);
	if(fake_db.count(name) == 0) {
		std::cerr << "[FAKE-BACKEND-WARNING] (write_full) Object " << name << " does not exist, it will be created" << std::endl;
	}
    fake_db[name].write_full(vargs->mid, vargs->client_addr, vargs->bulk_handle, buf.as_offset, len);
}

void write_op_exec_writesame(void* u, buffer_u buf, size_t data_len, size_t write_len, uint64_t offset)
{
	auto vargs = static_cast<server_visitor_args_t>(u);
	std::string name(vargs->object_name);
	if(fake_db.count(name) == 0) {
		std::cerr << "[FAKE-BACKEND-WARNING] (writesame) Object " << name << " does not exist, it will be created" << std::endl;
	}
    fake_db[name].writesame(vargs->mid, vargs->client_addr, vargs->bulk_handle, buf.as_offset, offset, data_len, write_len);
}

void write_op_exec_append(void* u, buffer_u buf, size_t len)
{
	auto vargs = static_cast<server_visitor_args_t>(u);
	std::string name(vargs->object_name);
	if(fake_db.count(name) == 0) {
		std::cerr << "[FAKE-BACKEND-WARNING] (append) Object " << name << " does not exist, it will be created" << std::endl;
	}
	fake_db[name].append(vargs->mid, vargs->client_addr, vargs->bulk_handle, buf.as_offset, len);
}

void write_op_exec_remove(void* u)
{
	auto vargs = static_cast<server_visitor_args_t>(u);
	std::string name(vargs->object_name);
	if(fake_db.count(name) == 0) {
		std::cerr << "[FAKE-BACKEND-WARNING] (remove) Object " << name << " did not exist" << std::endl;
        return;
	}
    fake_db.erase(name);
}

void write_op_exec_truncate(void* u, uint64_t offset)
{
	auto vargs = static_cast<server_visitor_args_t>(u);
	std::string name(vargs->object_name);
	if(fake_db.count(name) == 0) {
		std::cerr << "[FAKE-BACKEND-WARNING] (truncate) Object " << name << " does not exist, it will be created" << std::endl;
	}
	fake_db[name].truncate(offset);
}

void write_op_exec_zero(void* u, uint64_t offset, uint64_t len)
{
	auto vargs = static_cast<server_visitor_args_t>(u);
	std::string name(vargs->object_name);
	if(fake_db.count(name) == 0) {
		std::cerr << "[FAKE-BACKEND-WARNING] (zero) Object " << name << " does not exist, it will be created" << std::endl;
	}
    fake_db[name].zero(offset, len);
}

void write_op_exec_omap_set(void* u, char const* const* keys,
                                     char const* const* vals,
                                     const size_t *lens,
                                     size_t num)
{
	auto vargs = static_cast<server_visitor_args_t>(u);
	std::string name(vargs->object_name);
	if(fake_db.count(name) == 0) {
		std::cerr << "[FAKE-BACKEND-WARNING] (omap_set) Object " << name << " does not exist, it will be created" << std::endl;
	}
	unsigned i;
    auto& entry = fake_db[name];
	for(i=0; i<num; i++) {
            entry.omap_set(keys[i], lens[i], vals[i]);
	}
}

void write_op_exec_omap_rm_keys(void* u, char const* const* keys, size_t num_keys)
{
	auto vargs = static_cast<server_visitor_args_t>(u);
	std::string name(vargs->object_name);
	if(fake_db.count(name) == 0) {
        std::cerr << "[FAKE-BACKEND-WARNING] (omap_rm_keys) Object " << name << " did not exist" << std::endl;
        return;
	}
	unsigned i;
    auto& entry = fake_db[name];
	for(i=0; i<num_keys; i++) {
            entry.omap_rm(keys[i]);
	}
}