fake-read-op.cpp 4.28 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <map>
#include <string>
#include <iostream>
#include "src/server/fake/fake-object.hpp"
#include "src/server/visitor-args.h"
#include "src/io-chain/read-op-visitor.h"
#include "src/io-chain/read-resp-impl.h"
#include "src/omap-iter/omap-iter-impl.h"

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

static void read_op_exec_begin(void*);
static void read_op_exec_stat(void*, uint64_t*, time_t*, int*);
static void read_op_exec_read(void*, uint64_t, size_t, buffer_u, size_t*, int*);
static void read_op_exec_omap_get_keys(void*, const char*, uint64_t, mobject_store_omap_iter_t*, int*);
static void read_op_exec_omap_get_vals(void*, const char*, const char*, uint64_t, mobject_store_omap_iter_t*, int*);
static void read_op_exec_omap_get_vals_by_keys(void*, char const* const*, size_t, mobject_store_omap_iter_t*, int*);
static void read_op_exec_end(void*);

20
static struct read_op_visitor read_op_exec = {
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
	.visit_begin                 = read_op_exec_begin,
	.visit_stat                  = read_op_exec_stat,
	.visit_read                  = read_op_exec_read,
	.visit_omap_get_keys         = read_op_exec_omap_get_keys,
	.visit_omap_get_vals         = read_op_exec_omap_get_vals,
	.visit_omap_get_vals_by_keys = read_op_exec_omap_get_vals_by_keys,
	.visit_end                   = read_op_exec_end
};

extern "C" void fake_read_op(mobject_store_read_op_t read_op, server_visitor_args_t vargs)
{
	execute_read_op_visitor(&read_op_exec, read_op, (void*)vargs);
}

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

void read_op_exec_stat(void* u, uint64_t* psize, time_t* pmtime, int* prval)
{
    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] (stat) Object " << name << " does not exist" << std::endl;
        *prval = -1;
        return;
    }
    fake_db[name].stat(psize, pmtime);
	*prval = 0;
}

void read_op_exec_read(void* u, uint64_t offset, size_t len, buffer_u buf, size_t* bytes_read, int* prval)
{
    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] (read) Object " << name << " does not exist" << std::endl;
        *prval = -1;
        return;
    }
62
    margo_instance_id mid = vargs->srv_ctx->mid;
Matthieu Dorier's avatar
Matthieu Dorier committed
63
    fake_db[name].read(mid, vargs->client_addr.as_handle, vargs->bulk_handle, 
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
                        buf.as_offset, offset, len, bytes_read);
	*prval = 0;
}

void read_op_exec_omap_get_keys(void* u, const char* start_after, uint64_t max_return, 
				mobject_store_omap_iter_t* iter, int* prval)
{
    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_get_keys) Object " << name << " does not exist" << std::endl;
        *prval = -1;
        *iter  = NULL;
        return;
    }

	omap_iter_create(iter);
    fake_db[name].omap_get_keys(start_after, max_return, *iter);
	*prval = 0;
}

void read_op_exec_omap_get_vals(void* u, const char* start_after, const char* filter_prefix, uint64_t max_return, mobject_store_omap_iter_t* iter, int* prval)
{
    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_get_vals) Object " << name << " does not exist" << std::endl;
        *prval = -1;
        *iter = NULL;
        return;
    }

	omap_iter_create(iter);
    fake_db[name].omap_get_vals(start_after, filter_prefix, max_return, *iter);
	*prval = 0;
}

void read_op_exec_omap_get_vals_by_keys(void* u, char const* const* keys, size_t num_keys, mobject_store_omap_iter_t* iter, int* prval)
{
    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_get_vals_by_keys) Object " << name << " does not exist" << std::endl;
        *prval = -1;
        *iter = NULL;
        return;
    }

	omap_iter_create(iter);
    std::vector<std::string> keys_vec(keys, keys+num_keys);
    fake_db[name].omap_get_vals_by_keys(keys_vec, *iter);
	*prval = 0;
}

void read_op_exec_end(void* u)
{
}