sdskv-server.cc 65.5 KB
Newer Older
Matthieu Dorier's avatar
Matthieu Dorier committed
1 2 3 4 5 6 7
/*
 * (C) 2015 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */

#include <map>
Matthieu Dorier's avatar
Matthieu Dorier committed
8
#include <iostream>
Matthieu Dorier's avatar
Matthieu Dorier committed
9 10 11 12 13 14 15 16 17 18 19
#include <unordered_map>
#define SDSKV
#include "datastore/datastore_factory.h"
#include "sdskv-rpc-types.h"
#include "sdskv-server.h"

struct sdskv_server_context_t
{
    std::unordered_map<sdskv_database_id_t, AbstractDataStore*> databases;
    std::map<std::string, sdskv_database_id_t> name2id;
    std::map<sdskv_database_id_t, std::string> id2name;
20 21

    hg_id_t sdskv_put_id;
Matthieu Dorier's avatar
Matthieu Dorier committed
22
    hg_id_t sdskv_put_multi_id;
23 24
    hg_id_t sdskv_bulk_put_id;
    hg_id_t sdskv_get_id;
Matthieu Dorier's avatar
Matthieu Dorier committed
25
    hg_id_t sdskv_get_multi_id;
26 27
    hg_id_t sdskv_exists_id;
    hg_id_t sdskv_erase_id;
Matthieu Dorier's avatar
Matthieu Dorier committed
28
    hg_id_t sdskv_erase_multi_id;
29
    hg_id_t sdskv_length_id;
Matthieu Dorier's avatar
Matthieu Dorier committed
30
    hg_id_t sdskv_length_multi_id;
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
    hg_id_t sdskv_bulk_get_id;
    hg_id_t sdskv_open_id;
    hg_id_t sdskv_list_keys_id;
    hg_id_t sdskv_list_keyvals_id;
    /* migration */
    hg_id_t sdskv_migrate_keys_id;
    hg_id_t sdskv_migrate_key_range_id;
    hg_id_t sdskv_migrate_keys_prefixed_id;
    hg_id_t sdskv_migrate_all_keys_id;
    hg_id_t sdskv_migrate_database_id;
};

template<typename F>
struct scoped_call {
    F _f;
    scoped_call(const F& f) : _f(f) {}
    ~scoped_call() { _f(); }
Matthieu Dorier's avatar
Matthieu Dorier committed
48 49
};

50 51 52 53 54
template<typename F>
inline scoped_call<F> at_exit(F&& f) {
    return scoped_call<F>(std::forward<F>(f));
}

Matthieu Dorier's avatar
Matthieu Dorier committed
55
DECLARE_MARGO_RPC_HANDLER(sdskv_put_ult)
Matthieu Dorier's avatar
Matthieu Dorier committed
56
DECLARE_MARGO_RPC_HANDLER(sdskv_put_multi_ult)
Matthieu Dorier's avatar
Matthieu Dorier committed
57
DECLARE_MARGO_RPC_HANDLER(sdskv_length_ult)
Matthieu Dorier's avatar
Matthieu Dorier committed
58
DECLARE_MARGO_RPC_HANDLER(sdskv_length_multi_ult)
Matthieu Dorier's avatar
Matthieu Dorier committed
59
DECLARE_MARGO_RPC_HANDLER(sdskv_get_ult)
Matthieu Dorier's avatar
Matthieu Dorier committed
60
DECLARE_MARGO_RPC_HANDLER(sdskv_get_multi_ult)
Matthieu Dorier's avatar
Matthieu Dorier committed
61 62 63
DECLARE_MARGO_RPC_HANDLER(sdskv_open_ult)
DECLARE_MARGO_RPC_HANDLER(sdskv_bulk_put_ult)
DECLARE_MARGO_RPC_HANDLER(sdskv_bulk_get_ult)
64
DECLARE_MARGO_RPC_HANDLER(sdskv_list_keys_ult)
65
DECLARE_MARGO_RPC_HANDLER(sdskv_list_keyvals_ult)
Matthieu Dorier's avatar
Matthieu Dorier committed
66
DECLARE_MARGO_RPC_HANDLER(sdskv_erase_ult)
Matthieu Dorier's avatar
Matthieu Dorier committed
67
DECLARE_MARGO_RPC_HANDLER(sdskv_erase_multi_ult)
68
DECLARE_MARGO_RPC_HANDLER(sdskv_exists_ult)
69 70 71 72
DECLARE_MARGO_RPC_HANDLER(sdskv_migrate_keys_ult)
DECLARE_MARGO_RPC_HANDLER(sdskv_migrate_key_range_ult)
DECLARE_MARGO_RPC_HANDLER(sdskv_migrate_keys_prefixed_ult)
DECLARE_MARGO_RPC_HANDLER(sdskv_migrate_all_keys_ult)
Matthieu Dorier's avatar
Matthieu Dorier committed
73

Matthieu Dorier's avatar
Matthieu Dorier committed
74 75 76 77
static void sdskv_server_finalize_cb(void *data);

extern "C" int sdskv_provider_register(
        margo_instance_id mid,
78
        uint16_t provider_id,
Matthieu Dorier's avatar
Matthieu Dorier committed
79 80 81 82 83 84 85 86 87
        ABT_pool abt_pool,
        sdskv_provider_t* provider)
{
    sdskv_server_context_t *tmp_svr_ctx;

    /* check if a provider with the same multiplex id already exists */
    {
        hg_id_t id;
        hg_bool_t flag;
88
        margo_provider_registered_name(mid, "sdskv_put_rpc", provider_id, &id, &flag);
Matthieu Dorier's avatar
Matthieu Dorier committed
89
        if(flag == HG_TRUE) {
90
            fprintf(stderr, "sdskv_provider_register(): a provider with the same provider id (%d) already exists\n", provider_id);
91
            return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
92 93 94 95
        }
    }

    /* allocate the resulting structure */    
Matthieu Dorier's avatar
Matthieu Dorier committed
96
    tmp_svr_ctx = new sdskv_server_context_t;
Matthieu Dorier's avatar
Matthieu Dorier committed
97
    if(!tmp_svr_ctx)
98
        return SDSKV_ERR_ALLOCATION;
Matthieu Dorier's avatar
Matthieu Dorier committed
99 100 101

    /* register RPCs */
    hg_id_t rpc_id;
102
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_put_rpc",
Matthieu Dorier's avatar
Matthieu Dorier committed
103
            put_in_t, put_out_t,
104
            sdskv_put_ult, provider_id, abt_pool);
105
    tmp_svr_ctx->sdskv_put_id = rpc_id;
106
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
Matthieu Dorier's avatar
Matthieu Dorier committed
107 108 109 110 111
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_put_multi_rpc",
            put_multi_in_t, put_multi_out_t,
            sdskv_put_multi_ult, provider_id, abt_pool);
    tmp_svr_ctx->sdskv_put_multi_id = rpc_id;
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
112
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_bulk_put_rpc",
Matthieu Dorier's avatar
Matthieu Dorier committed
113
            bulk_put_in_t, bulk_put_out_t,
114
            sdskv_bulk_put_ult, provider_id, abt_pool);
115
    tmp_svr_ctx->sdskv_bulk_put_id = rpc_id;
116 117
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_get_rpc",
Matthieu Dorier's avatar
Matthieu Dorier committed
118
            get_in_t, get_out_t,
119
            sdskv_get_ult, provider_id, abt_pool);
120
    tmp_svr_ctx->sdskv_get_id = rpc_id;
121
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
Matthieu Dorier's avatar
Matthieu Dorier committed
122 123 124 125 126
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_get_multi_rpc",
            get_multi_in_t, get_multi_out_t,
            sdskv_get_multi_ult, provider_id, abt_pool);
    tmp_svr_ctx->sdskv_get_multi_id = rpc_id;
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
127
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_length_rpc",
Matthieu Dorier's avatar
Matthieu Dorier committed
128
            length_in_t, length_out_t,
129
            sdskv_length_ult, provider_id, abt_pool);
130
    tmp_svr_ctx->sdskv_length_id = rpc_id;
131
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
Matthieu Dorier's avatar
Matthieu Dorier committed
132 133 134 135 136
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_length_multi_rpc",
            length_multi_in_t, length_multi_out_t,
            sdskv_length_multi_ult, provider_id, abt_pool);
    tmp_svr_ctx->sdskv_length_multi_id = rpc_id;
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
137 138 139
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_exists_rpc",
            exists_in_t, exists_out_t,
            sdskv_exists_ult, provider_id, abt_pool);
140
    tmp_svr_ctx->sdskv_exists_id = rpc_id;
141
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
142
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_bulk_get_rpc",
Matthieu Dorier's avatar
Matthieu Dorier committed
143
            bulk_get_in_t, bulk_get_out_t,
144
            sdskv_bulk_get_ult, provider_id, abt_pool);
145
    tmp_svr_ctx->sdskv_bulk_get_id = rpc_id;
146 147
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_open_rpc",
Matthieu Dorier's avatar
Matthieu Dorier committed
148
            open_in_t, open_out_t,
149
            sdskv_open_ult, provider_id, abt_pool);
150
    tmp_svr_ctx->sdskv_open_id = rpc_id;
151 152
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_list_keys_rpc",
153
            list_keys_in_t, list_keys_out_t,
154
            sdskv_list_keys_ult, provider_id, abt_pool);
155
    tmp_svr_ctx->sdskv_list_keys_id = rpc_id;
156 157
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_list_keyvals_rpc",
158
            list_keyvals_in_t, list_keyvals_out_t,
159
            sdskv_list_keyvals_ult, provider_id, abt_pool);
160
    tmp_svr_ctx->sdskv_list_keyvals_id = rpc_id;
161 162
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_erase_rpc",
Matthieu Dorier's avatar
Matthieu Dorier committed
163
            erase_in_t, erase_out_t,
164
            sdskv_erase_ult, provider_id, abt_pool);
165
    tmp_svr_ctx->sdskv_erase_id = rpc_id;
166
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
Matthieu Dorier's avatar
Matthieu Dorier committed
167 168 169 170 171
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_erase_multi_rpc",
            erase_multi_in_t, erase_multi_out_t,
            sdskv_erase_multi_ult, provider_id, abt_pool);
    tmp_svr_ctx->sdskv_erase_multi_id = rpc_id;
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    /* migration RPC */
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_migrate_keys_rpc",
            migrate_keys_in_t, migrate_keys_out_t,
            sdskv_migrate_keys_ult, provider_id, abt_pool);
    tmp_svr_ctx->sdskv_migrate_keys_id = rpc_id;
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_migrate_key_range_rpc",
            migrate_key_range_in_t, migrate_keys_out_t,
            sdskv_migrate_key_range_ult, provider_id, abt_pool);
    tmp_svr_ctx->sdskv_migrate_key_range_id = rpc_id;
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_migrate_keys_prefixed_rpc",
            migrate_keys_prefixed_in_t, migrate_keys_out_t,
            sdskv_migrate_keys_prefixed_ult, provider_id, abt_pool);
    tmp_svr_ctx->sdskv_migrate_keys_prefixed_id = rpc_id;
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_migrate_all_keys_rpc",
            migrate_all_keys_in_t, migrate_keys_out_t,
            sdskv_migrate_all_keys_ult, provider_id, abt_pool);
    tmp_svr_ctx->sdskv_migrate_all_keys_id = rpc_id;
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
Matthieu Dorier's avatar
Matthieu Dorier committed
193 194 195 196 197 198
    /* install the bake server finalize callback */
    margo_push_finalize_callback(mid, &sdskv_server_finalize_cb, tmp_svr_ctx);

    if(provider != SDSKV_PROVIDER_IGNORE)
        *provider = tmp_svr_ctx;

199
    return SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
200 201
}

202
extern "C" int sdskv_provider_attach_database(
Matthieu Dorier's avatar
Matthieu Dorier committed
203
        sdskv_provider_t provider,
204
        const sdskv_config_t* config,
Matthieu Dorier's avatar
Matthieu Dorier committed
205 206
        sdskv_database_id_t* db_id)
{
207 208
    auto db = datastore_factory::open_datastore(config->db_type, 
            std::string(config->db_name), std::string(config->db_path));
209
    if(db == nullptr) return SDSKV_ERR_DB_CREATE;
210
    db->set_comparison_function(config->db_comparison_fn);
Matthieu Dorier's avatar
Matthieu Dorier committed
211
    sdskv_database_id_t id = (sdskv_database_id_t)(db);
212 213 214
    if(config->db_no_overwrite) {
        db->set_no_overwrite();
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
215

216 217
    provider->name2id[std::string(config->db_name)] = id;
    provider->id2name[id] = std::string(config->db_name);
Matthieu Dorier's avatar
Matthieu Dorier committed
218 219
    provider->databases[id] = db;

220 221
    *db_id = id;

222
    return SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
223 224
}

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
extern "C" int sdskv_provider_add_database(
        sdskv_provider_t provider,
        const char* db_name,
        const char* db_path,
        sdskv_db_type_t db_type,
        sdskv_compare_fn comp_fn,
        sdskv_database_id_t* db_id) {
    sdskv_config_t config = SDSKV_CONFIG_DEFAULT;
    config.db_name = db_name;
    config.db_path = db_path;
    config.db_type = db_type;
    config.db_comparison_fn = comp_fn;
    return sdskv_provider_attach_database(
            provider, &config, db_id);
}

Matthieu Dorier's avatar
Matthieu Dorier committed
241 242 243 244 245 246 247 248 249 250 251
extern "C" int sdskv_provider_remove_database(
        sdskv_provider_t provider,
        sdskv_database_id_t db_id)
{
    if(provider->databases.count(db_id)) {
        auto dbname = provider->id2name[db_id];
        provider->id2name.erase(db_id);
        provider->name2id.erase(dbname);
        auto db = provider->databases[db_id];
        delete db;
        provider->databases.erase(db_id);
252
        return SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
253
    } else {
254
        return SDSKV_ERR_UNKNOWN_DB;
Matthieu Dorier's avatar
Matthieu Dorier committed
255 256 257 258 259 260 261 262 263 264 265 266 267
    }
}

extern "C" int sdskv_provider_remove_all_databases(
        sdskv_provider_t provider)
{
    for(auto db : provider->databases) {
        delete db.second;
    }
    provider->databases.clear();
    provider->name2id.clear();
    provider->id2name.clear();

268
    return SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
269 270 271 272 273 274 275
}

extern "C" int sdskv_provider_count_databases(
        sdskv_provider_t provider,
        uint64_t* num_db)
{
    *num_db = provider->databases.size();
276
    return SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
277 278 279 280 281 282 283 284 285 286 287
}

extern "C" int sdskv_provider_list_databases(
        sdskv_provider_t provider,
        sdskv_database_id_t* targets)
{
    unsigned i = 0;
    for(auto p : provider->name2id) {
        targets[i] = p.second;
        i++;
    }
288
    return SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
289 290 291 292 293 294 295 296 297 298 299 300
}

static void sdskv_put_ult(hg_handle_t handle)
{
    hg_return_t hret;
    put_in_t in;
    put_out_t out;

    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* info = margo_get_info(handle);
    sdskv_provider_t svr_ctx = 
301
        (sdskv_provider_t)margo_registered_data(mid, info->id);
Matthieu Dorier's avatar
Matthieu Dorier committed
302
    if(!svr_ctx) {
Matthieu Dorier's avatar
Matthieu Dorier committed
303
        fprintf(stderr, "Error (sdskv_put_ult): SDSKV could not find provider\n"); 
304
        out.ret = SDSKV_ERR_UNKNOWN_PR;
Matthieu Dorier's avatar
Matthieu Dorier committed
305 306 307 308 309 310 311
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }
    
    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
312
        out.ret = SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
313 314 315 316 317 318 319
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    auto it = svr_ctx->databases.find(in.db_id);
    if(it == svr_ctx->databases.end()) {
320
        out.ret = SDSKV_ERR_UNKNOWN_DB;
Matthieu Dorier's avatar
Matthieu Dorier committed
321 322 323 324 325 326
        margo_respond(handle, &out);
        margo_free_input(handle, &in);
        margo_destroy(handle);
        return;
    }

327 328
    ds_bulk_t kdata(in.key.data, in.key.data+in.key.size);
    ds_bulk_t vdata(in.value.data, in.value.data+in.value.size);
Matthieu Dorier's avatar
Matthieu Dorier committed
329 330

    if(it->second->put(kdata, vdata)) {
331
        out.ret = SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
332
    } else {
333
        out.ret = SDSKV_ERR_PUT;
Matthieu Dorier's avatar
Matthieu Dorier committed
334 335 336 337 338 339 340 341
    }

    margo_respond(handle, &out);
    margo_free_input(handle, &in);
    margo_destroy(handle);
}
DEFINE_MARGO_RPC_HANDLER(sdskv_put_ult)

Matthieu Dorier's avatar
Matthieu Dorier committed
342
static void sdskv_put_multi_ult(hg_handle_t handle)
Matthieu Dorier's avatar
Matthieu Dorier committed
343
{
Matthieu Dorier's avatar
Matthieu Dorier committed
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
    hg_return_t hret;
    put_multi_in_t in;
    put_multi_out_t out;
    out.ret = SDSKV_SUCCESS;
    std::vector<char> local_keys_buffer;
    std::vector<char> local_vals_buffer;
    hg_bulk_t local_keys_bulk_handle;
    hg_bulk_t local_vals_bulk_handle;

    auto r1 = at_exit([&handle]() { margo_destroy(handle); });
    auto r2 = at_exit([&handle,&out]() { margo_respond(handle, &out); });


    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    const struct hg_info* info = margo_get_info(handle);
    sdskv_provider_t svr_ctx = 
        (sdskv_provider_t)margo_registered_data(mid, info->id);
    if(!svr_ctx) {
        out.ret = SDSKV_ERR_UNKNOWN_PR;
        return;
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
365

Matthieu Dorier's avatar
Matthieu Dorier committed
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        return;
    }
    auto r3 = at_exit([&handle,&in]() { margo_free_input(handle, &in); });

    auto it = svr_ctx->databases.find(in.db_id);
    if(it == svr_ctx->databases.end()) {
        out.ret = SDSKV_ERR_UNKNOWN_DB;
        return;
    }

    // allocate a buffer to receive the keys and a buffer to receive the values
    local_keys_buffer.resize(in.keys_bulk_size);
    local_vals_buffer.resize(in.vals_bulk_size);
    std::vector<void*> keys_addr(1); keys_addr[0] = (void*)local_keys_buffer.data();
    std::vector<void*> vals_addr(1); vals_addr[0] = (void*)local_vals_buffer.data();
    
    /* create bulk handle to receive keys */
    hret = margo_bulk_create(mid, 1, keys_addr.data(), &in.keys_bulk_size,
            HG_BULK_WRITE_ONLY, &local_keys_bulk_handle);
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        return;
    }
    auto r6 = at_exit([&local_keys_bulk_handle]() { margo_bulk_free(local_keys_bulk_handle); });

    /* create bulk handle to receive values */
    hret = margo_bulk_create(mid, 1, vals_addr.data(), &in.vals_bulk_size,
            HG_BULK_WRITE_ONLY, &local_vals_bulk_handle);
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        return;
    }
    auto r7 = at_exit([&local_vals_bulk_handle]() { margo_bulk_free(local_vals_bulk_handle); });

    /* transfer keys */
    hret = margo_bulk_transfer(mid, HG_BULK_PULL, info->addr, in.keys_bulk_handle, 0,
            local_keys_bulk_handle, 0, in.keys_bulk_size);
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        return;
    }

    /* transfer values */
    hret = margo_bulk_transfer(mid, HG_BULK_PULL, info->addr, in.vals_bulk_handle, 0,
            local_vals_bulk_handle, 0, in.vals_bulk_size);
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        return;
    }

    /* interpret beginning of the key buffer as a list of key sizes */
    hg_size_t* key_sizes = (hg_size_t*)local_keys_buffer.data();
    /* interpret beginning of the value buffer as a list of value sizes */
    hg_size_t* val_sizes = (hg_size_t*)local_vals_buffer.data();

    /* go through the key/value pairs and insert them */
    uint64_t keys_offset = sizeof(hg_size_t)*in.num_keys;
    uint64_t vals_offset = sizeof(hg_size_t)*in.num_keys;
    for(unsigned i=0; i < in.num_keys; i++) {
        ds_bulk_t kdata(local_keys_buffer.data()+keys_offset, local_keys_buffer.data()+keys_offset+key_sizes[i]);
        ds_bulk_t vdata(local_vals_buffer.data()+vals_offset, local_vals_buffer.data()+vals_offset+val_sizes[i]);
        it->second->put(kdata, vdata);
        keys_offset += key_sizes[i];
        vals_offset += val_sizes[i];
    }

    return;
}
DEFINE_MARGO_RPC_HANDLER(sdskv_put_multi_ult)

static void sdskv_length_ult(hg_handle_t handle)
{
Matthieu Dorier's avatar
Matthieu Dorier committed
441 442 443 444 445 446 447 448
    hg_return_t hret;
    length_in_t in;
    length_out_t out;

    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* info = margo_get_info(handle);
    sdskv_provider_t svr_ctx = 
449
        (sdskv_provider_t)margo_registered_data(mid, info->id);
Matthieu Dorier's avatar
Matthieu Dorier committed
450 451
    if(!svr_ctx) {
        fprintf(stderr, "Error: SDSKV could not find provider\n"); 
452
        out.ret = SDSKV_ERR_UNKNOWN_PR;
Matthieu Dorier's avatar
Matthieu Dorier committed
453 454 455 456 457 458 459
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
460
        out.ret = SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
461 462 463 464 465 466 467
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    auto it = svr_ctx->databases.find(in.db_id);
    if(it == svr_ctx->databases.end()) {
468
        out.ret = SDSKV_ERR_UNKNOWN_DB;
Matthieu Dorier's avatar
Matthieu Dorier committed
469 470 471 472 473 474
        margo_respond(handle, &out);
        margo_free_input(handle, &in);
        margo_destroy(handle);
        return;
    }
    
475
    ds_bulk_t kdata(in.key.data, in.key.data+in.key.size);
Matthieu Dorier's avatar
Matthieu Dorier committed
476 477 478 479

    ds_bulk_t vdata;
    if(it->second->get(kdata, vdata)) {
        out.size = vdata.size();
480
        out.ret  = SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
481 482
    } else {
        out.size = 0;
483
        out.ret  = SDSKV_ERR_UNKNOWN_KEY;
Matthieu Dorier's avatar
Matthieu Dorier committed
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
    }

    margo_respond(handle, &out);
    margo_free_input(handle, &in);
    margo_destroy(handle); 

    return;
}
DEFINE_MARGO_RPC_HANDLER(sdskv_length_ult)

static void sdskv_get_ult(hg_handle_t handle)
{

    hg_return_t hret;
    get_in_t in;
    get_out_t out;

    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* info = margo_get_info(handle);
    sdskv_provider_t svr_ctx = 
505
        (sdskv_provider_t)margo_registered_data(mid, info->id);
Matthieu Dorier's avatar
Matthieu Dorier committed
506 507
    if(!svr_ctx) {
        fprintf(stderr, "Error: SDSKV could not find provider\n"); 
508
        out.ret = SDSKV_ERR_UNKNOWN_PR;
Matthieu Dorier's avatar
Matthieu Dorier committed
509 510 511 512 513 514 515
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
516
        out.ret = SDSKV_ERR_MERCURY;
517 518
        out.value.data = nullptr;
        out.value.size = 0;
Matthieu Dorier's avatar
Matthieu Dorier committed
519 520 521 522 523 524 525
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    auto it = svr_ctx->databases.find(in.db_id);
    if(it == svr_ctx->databases.end()) {
526
        out.ret = SDSKV_ERR_UNKNOWN_DB;
527 528
        out.value.data = nullptr;
        out.value.size = 0;
Matthieu Dorier's avatar
Matthieu Dorier committed
529 530 531 532 533 534
        margo_respond(handle, &out);
        margo_free_input(handle, &in);
        margo_destroy(handle);
        return;
    }
    
535
    ds_bulk_t kdata(in.key.data, in.key.data+in.key.size);
Matthieu Dorier's avatar
Matthieu Dorier committed
536 537 538 539

    ds_bulk_t vdata;
    if(it->second->get(kdata, vdata)) {
        if(vdata.size() <= in.vsize) {
540 541
            out.value.size = vdata.size();
            out.value.data = vdata.data();
542
            out.ret = SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
543
        } else {
544 545
            out.value.size = 0;
            out.value.data = nullptr;
546
            out.ret = SDSKV_ERR_SIZE;
Matthieu Dorier's avatar
Matthieu Dorier committed
547 548
        }
    } else {
549 550
        out.value.size = 0;
        out.value.data = nullptr;
551
        out.ret   = SDSKV_ERR_UNKNOWN_KEY;
Matthieu Dorier's avatar
Matthieu Dorier committed
552 553 554 555 556 557 558 559 560 561
    }

    margo_respond(handle, &out);
    margo_free_input(handle, &in);
    margo_destroy(handle); 

    return;
}
DEFINE_MARGO_RPC_HANDLER(sdskv_get_ult)

Matthieu Dorier's avatar
Matthieu Dorier committed
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
static void sdskv_get_multi_ult(hg_handle_t handle)
{

    hg_return_t hret;
    get_multi_in_t in;
    get_multi_out_t out;
    out.ret = SDSKV_SUCCESS;
    std::vector<char> local_keys_buffer;
    std::vector<char> local_vals_buffer;
    hg_bulk_t local_keys_bulk_handle;
    hg_bulk_t local_vals_bulk_handle;

    auto r1 = at_exit([&handle]() { margo_destroy(handle); });
    auto r2 = at_exit([&handle,&out]() { margo_respond(handle, &out); });

    /* get margo instance and provider */
    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    const struct hg_info* info = margo_get_info(handle);
    sdskv_provider_t svr_ctx = 
        (sdskv_provider_t)margo_registered_data(mid, info->id);
    if(!svr_ctx) {
        out.ret = SDSKV_ERR_UNKNOWN_PR;
        return;
    }

    /* deserialize input */
    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        return;
    }
    auto r3 = at_exit([&handle,&in]() { margo_free_input(handle, &in); });

    /* find the target database */
    auto it = svr_ctx->databases.find(in.db_id);
    if(it == svr_ctx->databases.end()) {
        out.ret = SDSKV_ERR_UNKNOWN_DB;
        return;
    }

    /* allocate buffers to receive the keys */
    local_keys_buffer.resize(in.keys_bulk_size);
    std::vector<void*> keys_addr(1);
    keys_addr[0] = (void*)local_keys_buffer.data();
    
    /* create bulk handle to receive key sizes and packed keys */
    hret = margo_bulk_create(mid, 1, keys_addr.data(), &in.keys_bulk_size,
            HG_BULK_WRITE_ONLY, &local_keys_bulk_handle);
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        return;
    }
    auto r6 = at_exit([&local_keys_bulk_handle]() { margo_bulk_free(local_keys_bulk_handle); });

    /* allocate buffer to send/receive the values */
    local_vals_buffer.resize(in.vals_bulk_size);
    std::vector<void*> vals_addr(1);
    vals_addr[0] = (void*)local_vals_buffer.data();

    /* create bulk handle to receive max value sizes and to send values */
    hret = margo_bulk_create(mid, 1, vals_addr.data(), &in.vals_bulk_size,
            HG_BULK_READWRITE, &local_vals_bulk_handle);
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        return;
    }
    auto r7 = at_exit([&local_vals_bulk_handle]() { margo_bulk_free(local_vals_bulk_handle); });

    /* transfer keys */
    hret = margo_bulk_transfer(mid, HG_BULK_PULL, info->addr, in.keys_bulk_handle, 0,
            local_keys_bulk_handle, 0, in.keys_bulk_size);
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        return;
    }

    /* transfer sizes allocated by user for the values (beginning of value segment) */
    hret = margo_bulk_transfer(mid, HG_BULK_PULL, info->addr, in.vals_bulk_handle, 0,
            local_vals_bulk_handle, 0, in.num_keys*sizeof(hg_size_t));
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        return;
    }

    /* interpret beginning of the key buffer as a list of key sizes */
    hg_size_t* key_sizes = (hg_size_t*)local_keys_buffer.data();
    /* find beginning of packed keys */
    char* packed_keys = local_keys_buffer.data() + in.num_keys*sizeof(hg_size_t);
    /* interpret beginning of the value buffer as a list of value sizes */
    hg_size_t* val_sizes = (hg_size_t*)local_vals_buffer.data();
    /* find beginning of region where to pack values */
    char* packed_values = local_vals_buffer.data() + in.num_keys*sizeof(hg_size_t);

    /* go through the key/value pairs and get the values from the database */
    for(unsigned i=0; i < in.num_keys; i++) {
        ds_bulk_t kdata(packed_keys, packed_keys+key_sizes[i]);
        ds_bulk_t vdata;
        size_t client_allocated_value_size = val_sizes[i];
        if(it->second->get(kdata, vdata)) {
            if(vdata.size() > val_sizes[i]) {
                val_sizes[i] = 0;
            } else {
                val_sizes[i] = vdata.size();
                memcpy(packed_values, vdata.data(), val_sizes[i]);
            }
        } else {
            val_sizes[i] = 0;
        }
        packed_keys += key_sizes[i];
        packed_values += client_allocated_value_size;
    }

    /* do a PUSH operation to push back the values to the client */
    hret = margo_bulk_transfer(mid, HG_BULK_PUSH, info->addr, in.vals_bulk_handle, 0,
            local_vals_bulk_handle, 0, in.vals_bulk_size);
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        return;
    }

    return;
}
DEFINE_MARGO_RPC_HANDLER(sdskv_get_multi_ult)

static void sdskv_length_multi_ult(hg_handle_t handle)
{

    hg_return_t hret;
    length_multi_in_t in;
    length_multi_out_t out;
    out.ret = SDSKV_SUCCESS;
    std::vector<char> local_keys_buffer;
    std::vector<hg_size_t> local_vals_size_buffer;
    hg_bulk_t local_keys_bulk_handle;
    hg_bulk_t local_vals_size_bulk_handle;

    auto r1 = at_exit([&handle]() { margo_destroy(handle); });
    auto r2 = at_exit([&handle,&out]() { margo_respond(handle, &out); });

    /* get margo instance and provider */
    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    const struct hg_info* info = margo_get_info(handle);
    sdskv_provider_t svr_ctx = 
        (sdskv_provider_t)margo_registered_data(mid, info->id);
    if(!svr_ctx) {
        out.ret = SDSKV_ERR_UNKNOWN_PR;
        return;
    }

    /* deserialize input */
    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        return;
    }
    auto r3 = at_exit([&handle,&in]() { margo_free_input(handle, &in); });

    /* find the target database */
    auto it = svr_ctx->databases.find(in.db_id);
    if(it == svr_ctx->databases.end()) {
        out.ret = SDSKV_ERR_UNKNOWN_DB;
        return;
    }

    /* allocate buffers to receive the keys */
    local_keys_buffer.resize(in.keys_bulk_size);
    std::vector<void*> keys_addr(1);
    keys_addr[0] = (void*)local_keys_buffer.data();
    
    /* create bulk handle to receive key sizes and packed keys */
    hret = margo_bulk_create(mid, 1, keys_addr.data(), &in.keys_bulk_size,
            HG_BULK_WRITE_ONLY, &local_keys_bulk_handle);
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        return;
    }
    auto r6 = at_exit([&local_keys_bulk_handle]() { margo_bulk_free(local_keys_bulk_handle); });

    /* allocate buffer to send the values */
    local_vals_size_buffer.resize(in.num_keys);
    std::vector<void*> vals_sizes_addr(1);
    hg_size_t local_vals_size_buffer_size = in.num_keys * sizeof(hg_size_t);
    vals_sizes_addr[0] = (void*)local_vals_size_buffer.data();

    /* create bulk handle to send values sizes */
    hret = margo_bulk_create(mid, 1, vals_sizes_addr.data(), &local_vals_size_buffer_size,
            HG_BULK_READ_ONLY, &local_vals_size_bulk_handle);
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        return;
    }
    auto r7 = at_exit([&local_vals_size_bulk_handle]() { margo_bulk_free(local_vals_size_bulk_handle); });

    /* transfer keys */
    hret = margo_bulk_transfer(mid, HG_BULK_PULL, info->addr, in.keys_bulk_handle, 0,
            local_keys_bulk_handle, 0, in.keys_bulk_size);
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        return;
    }

    /* interpret beginning of the key buffer as a list of key sizes */
    hg_size_t* key_sizes = (hg_size_t*)local_keys_buffer.data();
    /* find beginning of packed keys */
    char* packed_keys = local_keys_buffer.data() + in.num_keys*sizeof(hg_size_t);

    /* go through the key/value pairs and get the values from the database */
    for(unsigned i=0; i < in.num_keys; i++) {
        ds_bulk_t kdata(packed_keys, packed_keys+key_sizes[i]);
        ds_bulk_t vdata;
        if(it->second->get(kdata, vdata)) {
            local_vals_size_buffer[i] = vdata.size();
        } else {
            local_vals_size_buffer[i] = 0;
        }
        packed_keys += key_sizes[i];
    }

    /* do a PUSH operation to push back the value sizes to the client */
    hret = margo_bulk_transfer(mid, HG_BULK_PUSH, info->addr, in.vals_size_bulk_handle, 0,
            local_vals_size_bulk_handle, 0, local_vals_size_buffer_size);
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        return;
    }

    return;
}
DEFINE_MARGO_RPC_HANDLER(sdskv_length_multi_ult)

Matthieu Dorier's avatar
Matthieu Dorier committed
792 793 794 795 796 797 798 799 800 801 802
static void sdskv_open_ult(hg_handle_t handle)
{

    hg_return_t hret;
    open_in_t in;
    open_out_t out;

    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* info = margo_get_info(handle);
    sdskv_provider_t svr_ctx = 
803
        (sdskv_provider_t)margo_registered_data(mid, info->id);
Matthieu Dorier's avatar
Matthieu Dorier committed
804
    if(!svr_ctx) {
Matthieu Dorier's avatar
Matthieu Dorier committed
805
        fprintf(stderr, "Error (sdskv_open_ult): SDSKV could not find provider with id\n"); 
806
        out.ret = SDSKV_ERR_UNKNOWN_PR;
Matthieu Dorier's avatar
Matthieu Dorier committed
807 808 809 810 811 812 813
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
814
        out.ret = SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
815 816 817 818 819 820 821
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    auto it = svr_ctx->name2id.find(std::string(in.name));
    if(it == svr_ctx->name2id.end()) {
822
        out.ret = SDSKV_ERR_DB_NAME;
Matthieu Dorier's avatar
Matthieu Dorier committed
823 824 825 826 827 828 829
        margo_respond(handle, &out);
        margo_free_input(handle, &in);
        margo_destroy(handle);
        return;
    }

    out.db_id = it->second;
830
    out.ret  = SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850

    margo_respond(handle, &out);
    margo_free_input(handle, &in);
    margo_destroy(handle);
    return;
}
DEFINE_MARGO_RPC_HANDLER(sdskv_open_ult)

static void sdskv_bulk_put_ult(hg_handle_t handle)
{

    hg_return_t hret;
    bulk_put_in_t in;
    bulk_put_out_t out;
    hg_bulk_t bulk_handle;

    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* info = margo_get_info(handle);
    sdskv_provider_t svr_ctx = 
851
        (sdskv_provider_t)margo_registered_data(mid, info->id);
Matthieu Dorier's avatar
Matthieu Dorier committed
852
    if(!svr_ctx) {
Matthieu Dorier's avatar
Matthieu Dorier committed
853
        fprintf(stderr, "Error (sdskv_bulk_put_ult): SDSKV could not find provider\n"); 
854
        out.ret = SDSKV_ERR_UNKNOWN_PR;
Matthieu Dorier's avatar
Matthieu Dorier committed
855 856 857 858 859 860 861
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
862
        out.ret = SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
863 864 865 866 867
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

868
    auto it = svr_ctx->databases.find(in.db_id);
Matthieu Dorier's avatar
Matthieu Dorier committed
869
    if(it == svr_ctx->databases.end()) {
870
        out.ret = SDSKV_ERR_UNKNOWN_DB;
Matthieu Dorier's avatar
Matthieu Dorier committed
871 872 873 874 875
        margo_respond(handle, &out);
        margo_free_input(handle, &in);
        margo_destroy(handle);
        return;
    }
876

877
    ds_bulk_t vdata(in.vsize);
Matthieu Dorier's avatar
Matthieu Dorier committed
878

879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
    if(in.vsize > 0) {

        void *buffer = (void*)vdata.data();
        hg_size_t size = vdata.size();
        hret = margo_bulk_create(mid, 1, (void**)&buffer, &size,
                HG_BULK_WRITE_ONLY, &bulk_handle);
        if(hret != HG_SUCCESS) {
            out.ret = SDSKV_ERR_MERCURY;
            margo_respond(handle, &out);
            margo_free_input(handle, &in);
            margo_destroy(handle);
            return;
        }

        hret = margo_bulk_transfer(mid, HG_BULK_PULL, info->addr, in.handle, 0,
                bulk_handle, 0, vdata.size());
        if(hret != HG_SUCCESS) {
            out.ret = SDSKV_ERR_MERCURY;
            margo_respond(handle, &out);
            margo_free_input(handle, &in);
            margo_bulk_free(bulk_handle);
            margo_destroy(handle);
            return;
        }

Matthieu Dorier's avatar
Matthieu Dorier committed
904
        margo_bulk_free(bulk_handle);
905

Matthieu Dorier's avatar
Matthieu Dorier committed
906 907
    }

908
    ds_bulk_t kdata(in.key.data, in.key.data+in.key.size);
Matthieu Dorier's avatar
Matthieu Dorier committed
909 910 911 912

    auto b = it->second->put(kdata, vdata);

    if(b) {
913
        out.ret = SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
914
    } else {
915
        out.ret = SDSKV_ERR_PUT;
Matthieu Dorier's avatar
Matthieu Dorier committed
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
    }

    margo_respond(handle, &out);
    margo_free_input(handle, &in);
    margo_destroy(handle);

    return;
}
DEFINE_MARGO_RPC_HANDLER(sdskv_bulk_put_ult)

static void sdskv_bulk_get_ult(hg_handle_t handle)
{

    hg_return_t hret;
    bulk_get_in_t in;
    bulk_get_out_t out;
    hg_bulk_t bulk_handle;

    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* info = margo_get_info(handle);
    sdskv_provider_t svr_ctx = 
938
        (sdskv_provider_t)margo_registered_data(mid, info->id);
Matthieu Dorier's avatar
Matthieu Dorier committed
939
    if(!svr_ctx) {
Matthieu Dorier's avatar
Matthieu Dorier committed
940
        fprintf(stderr, "Error (sdskv_bulk_get_ult): SDSKV could not find provider\n"); 
941
        out.ret = SDSKV_ERR_UNKNOWN_PR;
Matthieu Dorier's avatar
Matthieu Dorier committed
942 943 944 945 946 947 948
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
949
        out.ret = SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
950 951 952 953 954
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

955
    auto it = svr_ctx->databases.find(in.db_id);
Matthieu Dorier's avatar
Matthieu Dorier committed
956
    if(it == svr_ctx->databases.end()) {
957
        out.ret = SDSKV_ERR_UNKNOWN_DB;
Matthieu Dorier's avatar
Matthieu Dorier committed
958 959 960 961 962 963
        margo_respond(handle, &out);
        margo_free_input(handle, &in);
        margo_destroy(handle);
        return;
    }
    
964
    ds_bulk_t kdata(in.key.data, in.key.data+in.key.size);
Matthieu Dorier's avatar
Matthieu Dorier committed
965 966 967 968

    ds_bulk_t vdata;
    auto b = it->second->get(kdata, vdata);

969
    if(!b || vdata.size() > in.vsize) {
Matthieu Dorier's avatar
Matthieu Dorier committed
970
        out.size = 0;
971
        out.ret = SDSKV_ERR_SIZE;
Matthieu Dorier's avatar
Matthieu Dorier committed
972 973 974 975 976 977 978 979
        margo_respond(handle, &out);
        margo_free_input(handle, &in);
        margo_destroy(handle);
        return;
    }

    void *buffer = (void*)vdata.data();
    hg_size_t size = vdata.size();
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
    if(size > 0) {
        hret = margo_bulk_create(mid, 1, (void**)&buffer, &size,
                HG_BULK_READ_ONLY, &bulk_handle);
        if(hret != HG_SUCCESS) {
            out.size = 0;
            out.ret = SDSKV_ERR_MERCURY;
            margo_respond(handle, &out);
            margo_free_input(handle, &in);
            margo_destroy(handle);
            return;
        }

        hret = margo_bulk_transfer(mid, HG_BULK_PUSH, info->addr, in.handle, 0,
                bulk_handle, 0, vdata.size());
        if(hret != HG_SUCCESS) {
            out.size = 0;
            out.ret = SDSKV_ERR_MERCURY;
            margo_respond(handle, &out);
            margo_free_input(handle, &in);
            margo_bulk_free(bulk_handle);
            margo_destroy(handle);
            return;
        }
Matthieu Dorier's avatar
Matthieu Dorier committed
1003 1004 1005 1006

        margo_bulk_free(bulk_handle);
    }

1007
    out.size = size;
1008
    out.ret  = SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
1009 1010 1011 1012 1013 1014 1015 1016 1017

    margo_respond(handle, &out);
    margo_free_input(handle, &in);
    margo_destroy(handle);

    return;
}
DEFINE_MARGO_RPC_HANDLER(sdskv_bulk_get_ult)

Matthieu Dorier's avatar
Matthieu Dorier committed
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
static void sdskv_erase_ult(hg_handle_t handle)
{

    hg_return_t hret;
    erase_in_t in;
    erase_out_t out;

    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* info = margo_get_info(handle);
    sdskv_provider_t svr_ctx = 
1029
        (sdskv_provider_t)margo_registered_data(mid, info->id);
Matthieu Dorier's avatar
Matthieu Dorier committed
1030
    if(!svr_ctx) {
Matthieu Dorier's avatar
Matthieu Dorier committed
1031
        fprintf(stderr, "Error (sdskv_erase_ult): SDSKV could not find provider\n"); 
1032
        out.ret = SDSKV_ERR_UNKNOWN_PR;
Matthieu Dorier's avatar
Matthieu Dorier committed
1033 1034 1035 1036 1037 1038 1039
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
1040
        out.ret = SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
1041 1042 1043 1044 1045 1046 1047
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    auto it = svr_ctx->databases.find(in.db_id);
    if(it == svr_ctx->databases.end()) {
1048
        out.ret = SDSKV_ERR_UNKNOWN_DB;
Matthieu Dorier's avatar
Matthieu Dorier committed
1049 1050 1051 1052 1053 1054
        margo_respond(handle, &out);
        margo_free_input(handle, &in);
        margo_destroy(handle);
        return;
    }
    
1055
    ds_bulk_t kdata(in.key.data, in.key.data+in.key.size);
Matthieu Dorier's avatar
Matthieu Dorier committed
1056 1057

    if(it->second->erase(kdata)) {
1058
        out.ret   = SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
1059
    } else {
1060
        out.ret   = SDSKV_ERR_ERASE;
Matthieu Dorier's avatar
Matthieu Dorier committed
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
    }

    margo_respond(handle, &out);
    margo_free_input(handle, &in);
    margo_destroy(handle); 

    return;
}
DEFINE_MARGO_RPC_HANDLER(sdskv_erase_ult)

Matthieu Dorier's avatar
Matthieu Dorier committed
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
static void sdskv_erase_multi_ult(hg_handle_t handle)
{

    hg_return_t hret;
    erase_multi_in_t in;
    erase_multi_out_t out;
    out.ret = SDSKV_SUCCESS;
    std::vector<char> local_keys_buffer;
    hg_bulk_t local_keys_bulk_handle;

    auto r1 = at_exit([&handle]() { margo_destroy(handle); });
    auto r2 = at_exit([&handle,&out]() { margo_respond(handle, &out); });

    /* get margo instance and provider */
    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    const struct hg_info* info = margo_get_info(handle);
    sdskv_provider_t svr_ctx = 
        (sdskv_provider_t)margo_registered_data(mid, info->id);
    if(!svr_ctx) {
        out.ret = SDSKV_ERR_UNKNOWN_PR;
        return;
    }

    /* deserialize input */
    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        return;
    }
    auto r3 = at_exit([&handle,&in]() { margo_free_input(handle, &in); });

    /* find the target database */
    auto it = svr_ctx->databases.find(in.db_id);
    if(it == svr_ctx->databases.end()) {
        out.ret = SDSKV_ERR_UNKNOWN_DB;
        return;
    }

    /* allocate buffers to receive the keys */
    local_keys_buffer.resize(in.keys_bulk_size);
    std::vector<void*> keys_addr(1);
    keys_addr[0] = (void*)local_keys_buffer.data();
    
    /* create bulk handle to receive key sizes and packed keys */
    hret = margo_bulk_create(mid, 1, keys_addr.data(), &in.keys_bulk_size,
            HG_BULK_WRITE_ONLY, &local_keys_bulk_handle);
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        return;
    }
    auto r6 = at_exit([&local_keys_bulk_handle]() { margo_bulk_free(local_keys_bulk_handle); });

    /* interpret beginning of the key buffer as a list of key sizes */
    hg_size_t* key_sizes = (hg_size_t*)local_keys_buffer.data();
    /* find beginning of packed keys */
    char* packed_keys = local_keys_buffer.data() + in.num_keys*sizeof(hg_size_t);

    /* go through the key/value pairs and erase them */
    for(unsigned i=0; i < in.num_keys; i++) {
        ds_bulk_t kdata(packed_keys, packed_keys+key_sizes[i]);
        it->second->erase(kdata);
        packed_keys += key_sizes[i];
    }

    return;
}
DEFINE_MARGO_RPC_HANDLER(sdskv_erase_multi_ult)

1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
static void sdskv_exists_ult(hg_handle_t handle)
{

    hg_return_t hret;
    exists_in_t in;
    exists_out_t out;

    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* info = margo_get_info(handle);
    sdskv_provider_t svr_ctx = 
        (sdskv_provider_t)margo_registered_data(mid, info->id);
    if(!svr_ctx) {
Matthieu Dorier's avatar
Matthieu Dorier committed
1152
        fprintf(stderr, "Error (sdskv_exists_ult): SDSKV could not find provider\n"); 
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
        out.ret = SDSKV_ERR_UNKNOWN_PR;
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    auto it = svr_ctx->databases.find(in.db_id);
    if(it == svr_ctx->databases.end()) {
        out.ret = SDSKV_ERR_UNKNOWN_DB;
        margo_respond(handle, &out);
        margo_free_input(handle, &in);
        margo_destroy(handle);
        return;
    }
    
    ds_bulk_t kdata(in.key.data, in.key.data+in.key.size);

    out.flag = it->second->exists(kdata) ? 1 : 0;
    out.ret  = SDSKV_SUCCESS;

    margo_respond(handle, &out);
    margo_free_input(handle, &in);
    margo_destroy(handle); 
    return;
}
DEFINE_MARGO_RPC_HANDLER(sdskv_exists_ult)

1188
static void sdskv_list_keys_ult(hg_handle_t handle)
Matthieu Dorier's avatar
Matthieu Dorier committed
1189 1190 1191
{

    hg_return_t hret;
1192 1193
    list_keys_in_t in;
    list_keys_out_t out;
Matthieu Dorier's avatar
Matthieu Dorier committed
1194 1195
    hg_bulk_t ksizes_local_bulk = HG_BULK_NULL;
    hg_bulk_t keys_local_bulk   = HG_BULK_NULL;
Matthieu Dorier's avatar
Matthieu Dorier committed
1196

1197
    out.ret     = SDSKV_SUCCESS;
1198 1199
    out.nkeys   = 0;

Matthieu Dorier's avatar
Matthieu Dorier committed
1200
    /* get the provider handling this request */
Matthieu Dorier's avatar
Matthieu Dorier committed
1201 1202 1203 1204
    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* info = margo_get_info(handle);
    sdskv_provider_t svr_ctx = 
1205
        (sdskv_provider_t)margo_registered_data(mid, info->id);
Matthieu Dorier's avatar
Matthieu Dorier committed
1206
    if(!svr_ctx) {
Matthieu Dorier's avatar
Matthieu Dorier committed
1207
        std::cerr << "Error (sdskv_list_keys_ult): SDSKV list_keys could not find provider" << std::endl;
1208
        out.ret = SDSKV_ERR_UNKNOWN_PR;
Matthieu Dorier's avatar
Matthieu Dorier committed
1209 1210 1211 1212 1213
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
1214
    /* get the input */
Matthieu Dorier's avatar
Matthieu Dorier committed
1215 1216
    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
Matthieu Dorier's avatar
Matthieu Dorier committed
1217
        std::cerr << "Error: SDSKV list_keys could not get RPC input" << std::endl;
1218
        out.ret = SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
1219 1220 1221 1222 1223
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
1224 1225 1226 1227 1228 1229
    try {

        /* find the database targeted */
        auto it = svr_ctx->databases.find(in.db_id);
        if(it == svr_ctx->databases.end()) {
            std::cerr << "Error: SDSKV list_keys could not get database with id " << in.db_id << std::endl;
1230
            throw SDSKV_ERR_UNKNOWN_DB;
Matthieu Dorier's avatar
Matthieu Dorier committed
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
        }
        auto db = it->second;

        /* create a bulk handle to receive and send key sizes from client */
        std::vector<hg_size_t> ksizes(in.max_keys);
        std::vector<void*> ksizes_addr(1);
        ksizes_addr[0] = (void*)ksizes.data();
        hg_size_t ksizes_bulk_size = ksizes.size()*sizeof(hg_size_t);
        hret = margo_bulk_create(mid, 1, ksizes_addr.data(), 
                &ksizes_bulk_size, HG_BULK_READWRITE, &ksizes_local_bulk);
        if(hret != HG_SUCCESS) {
            std::cerr << "Error: SDSKV list_keys could not create bulk handle (ksizes_local_bulk)" << std::endl;
1243
            throw SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
1244 1245
        }

1246

Matthieu Dorier's avatar
Matthieu Dorier committed
1247 1248 1249 1250 1251 1252 1253
        /* receive the key sizes from the client */
        hg_addr_t origin_addr = info->addr;
        hret = margo_bulk_transfer(mid, HG_BULK_PULL, origin_addr,
                in.ksizes_bulk_handle, 0, ksizes_local_bulk, 0, ksizes_bulk_size);
        if(hret != HG_SUCCESS) {
            std::cerr << "Error: SDSKV list_keys could not issue bulk transfer " 
                << "(pull from in.ksizes_bulk_handle to ksizes_local_bulk)" << std::endl;
1254
            throw SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
1255 1256
        }

1257 1258 1259
        /* make a copy of the remote key sizes */
        std::vector<hg_size_t> remote_ksizes(ksizes.begin(), ksizes.end());

Matthieu Dorier's avatar
Matthieu Dorier committed
1260
        /* get the keys from the underlying database */    
1261
        ds_bulk_t start_kdata(in.start_key.data, in.start_key.data+in.start_key.size);
1262 1263
        ds_bulk_t prefix(in.prefix.data, in.prefix.data+in.prefix.size);
        auto keys = db->list_keys(start_kdata, in.max_keys, prefix);
Matthieu Dorier's avatar
Matthieu Dorier committed
1264 1265
        hg_size_t num_keys = std::min(keys.size(), in.max_keys);

1266 1267
        if(num_keys == 0) throw SDSKV_SUCCESS;

Matthieu Dorier's avatar
Matthieu Dorier committed
1268 1269 1270 1271 1272 1273 1274
        /* create the array of actual sizes */
        std::vector<hg_size_t> true_ksizes(num_keys);
        hg_size_t keys_bulk_size = 0;
        for(unsigned i = 0; i < num_keys; i++) {
            true_ksizes[i] = keys[i].size();
            if(true_ksizes[i] > ksizes[i]) {
                // this key has a size that exceeds the allocated size on client
1275
                throw SDSKV_ERR_SIZE;
Matthieu Dorier's avatar
Matthieu Dorier committed
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
            } else {
                ksizes[i] = true_ksizes[i];
                keys_bulk_size += ksizes[i];
            }
        }

        /* create an array of addresses pointing to keys */
        std::vector<void*> keys_addr(num_keys);
        for(unsigned i=0; i < num_keys; i++) {
            keys_addr[i] = (void*)(keys[i].data());
        }

        /* expose the keys for bulk transfer */
        hret = margo_bulk_create(mid, num_keys, keys_addr.data(),
                true_ksizes.data(), HG_BULK_READ_ONLY, &keys_local_bulk);
        if(hret != HG_SUCCESS) {
            std::cerr << "Error: SDSKV list_keys could not create bulk handle (keys_local_bulk)" << std::endl;
1293
            throw SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
1294 1295 1296 1297 1298 1299 1300 1301
        }

        /* transfer the ksizes back to the client */
        hret = margo_bulk_transfer(mid, HG_BULK_PUSH, origin_addr, 
                in.ksizes_bulk_handle, 0, ksizes_local_bulk, 0, ksizes_bulk_size);
        if(hret != HG_SUCCESS) {
            std::cerr << "Error: SDSKV list_keys could not issue bulk transfer "
                << "(push from ksizes_local_bulk to in.ksizes_bulk_handle)" << std::endl;
1302
            throw SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
1303 1304 1305
        }

        /* transfer the keys to the client */
1306 1307 1308 1309
        uint64_t remote_offset = 0;
        uint64_t local_offset  = 0;
        for(unsigned i = 0; i < num_keys; i++) {

1310 1311 1312 1313 1314 1315 1316
            if(true_ksizes[i] > 0) {
                hret = margo_bulk_transfer(mid, HG_BULK_PUSH, origin_addr,
                        in.keys_bulk_handle, remote_offset, keys_local_bulk, local_offset, true_ksizes[i]);
                if(hret != HG_SUCCESS) {
                    std::cerr << "Error: SDSKV list_keys could not issue bulk transfer (keys_local_bulk)" << std::endl;
                    throw SDSKV_ERR_MERCURY;
                }
1317 1318 1319 1320 1321 1322 1323
            }

            remote_offset += remote_ksizes[i];
            local_offset  += true_ksizes[i];
        }

        out.nkeys = num_keys;
1324
        out.ret = SDSKV_SUCCESS;
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350

    } catch(int exc_no) {
        out.ret = exc_no;
    }

    margo_bulk_free(ksizes_local_bulk);
    margo_bulk_free(keys_local_bulk);
    margo_respond(handle, &out);
    margo_free_input(handle, &in);
    margo_destroy(handle); 

    return;
}
DEFINE_MARGO_RPC_HANDLER(sdskv_list_keys_ult)

static void sdskv_list_keyvals_ult(hg_handle_t handle)
{

    hg_return_t hret;
    list_keyvals_in_t in;
    list_keyvals_out_t out;
    hg_bulk_t ksizes_local_bulk = HG_BULK_NULL;
    hg_bulk_t keys_local_bulk   = HG_BULK_NULL;
    hg_bulk_t vsizes_local_bulk = HG_BULK_NULL;
    hg_bulk_t vals_local_bulk   = HG_BULK_NULL;

1351
    out.ret     = SDSKV_SUCCESS;
1352 1353 1354 1355 1356 1357 1358
    out.nkeys   = 0;

    /* get the provider handling this request */
    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* info = margo_get_info(handle);
    sdskv_provider_t svr_ctx = 
1359
        (sdskv_provider_t)margo_registered_data(mid, info->id);
1360
    if(!svr_ctx) {
Matthieu Dorier's avatar
Matthieu Dorier committed
1361
        std::cerr << "Error (sdskv_list_keyvals_ult): SDSKV list_keyvals could not find provider" << std::endl;
1362
        out.ret = SDSKV_ERR_UNKNOWN_PR;
1363 1364 1365 1366 1367 1368 1369 1370 1371
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    /* get the input */
    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
        std::cerr << "Error: SDSKV list_keyvals could not get RPC input" << std::endl;
1372
        out.ret = SDSKV_ERR_MERCURY;
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    try {

        /* find the database targeted */
        auto it = svr_ctx->databases.find(in.db_id);
        if(it == svr_ctx->databases.end()) {
            std::cerr << "Error: SDSKV list_keyvals could not get database with id " << in.db_id << std::endl;
1384
            throw SDSKV_ERR_UNKNOWN_DB;
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
        }
        auto db = it->second;

        /* create a bulk handle to receive and send key sizes from client */
        std::vector<hg_size_t> ksizes(in.max_keys);
        std::vector<void*> ksizes_addr(1);
        ksizes_addr[0] = (void*)ksizes.data();
        hg_size_t ksizes_bulk_size = ksizes.size()*sizeof(hg_size_t);
        hret = margo_bulk_create(mid, 1, ksizes_addr.data(), 
                &ksizes_bulk_size, HG_BULK_READWRITE, &ksizes_local_bulk);
        if(hret != HG_SUCCESS) {
            std::cerr << "Error: SDSKV list_keyvals could not create bulk handle (ksizes_local_bulk)" << std::endl;
1397
            throw SDSKV_ERR_MERCURY;
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
        }

        /* create a bulk handle to receive and send value sizes from client */
        std::vector<hg_size_t> vsizes(in.max_keys);
        std::vector<void*> vsizes_addr(1);
        vsizes_addr[0] = (void*)vsizes.data();
        hg_size_t vsizes_bulk_size = vsizes.size()*sizeof(hg_size_t);
        hret = margo_bulk_create(mid, 1, vsizes_addr.data(), 
                &vsizes_bulk_size, HG_BULK_READWRITE, &vsizes_local_bulk);
        if(hret != HG_SUCCESS) {
            std::cerr << "Error: SDSKV list_keyvals could not create bulk handle (vsizes_local_bulk)" << std::endl;
1409
            throw SDSKV_ERR_MERCURY;
1410 1411 1412 1413 1414 1415 1416 1417 1418
        }

        /* receive the key sizes from the client */
        hg_addr_t origin_addr = info->addr;
        hret = margo_bulk_transfer(mid, HG_BULK_PULL, origin_addr,
                in.ksizes_bulk_handle, 0, ksizes_local_bulk, 0, ksizes_bulk_size);
        if(hret != HG_SUCCESS) {
            std::cerr << "Error: SDSKV list_keyvals could not issue bulk transfer " 
                << "(pull from in.ksizes_bulk_handle to ksizes_local_bulk)" << std::endl;
1419
            throw SDSKV_ERR_MERCURY;
1420 1421 1422 1423 1424
        }

        /* receive the values sizes from the client */
        hret = margo_bulk_transfer(mid, HG_BULK_PULL, origin_addr,
                in.vsizes_bulk_handle, 0, vsizes_local_bulk, 0, vsizes_bulk_size);
Matthieu Dorier's avatar
Matthieu Dorier committed
1425
        if(hret != HG_SUCCESS) {
1426 1427
            std::cerr << "Error: SDSKV list_keyvals could not issue bulk transfer " 
                << "(pull from in.vsizes_bulk_handle to vsizes_local_bulk)" << std::endl;
1428
            throw SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
1429 1430
        }

1431 1432 1433 1434 1435
        /* make a copy of the remote key sizes and value sizes */
        std::vector<hg_size_t> remote_ksizes(ksizes.begin(), ksizes.end());
        std::vector<hg_size_t> remote_vsizes(vsizes.begin(), vsizes.end());

        /* get the keys and values from the underlying database */    
1436
        ds_bulk_t start_kdata(in.start_key.data, in.start_key.data+in.start_key.size);
1437 1438
        ds_bulk_t prefix(in.prefix.data, in.prefix.data+in.prefix.size);
        auto keyvals = db->list_keyvals(start_kdata, in.max_keys, prefix);
1439 1440
        hg_size_t num_keys = std::min(keyvals.size(), in.max_keys);

1441 1442
        if(num_keys == 0) throw SDSKV_SUCCESS;

1443 1444 1445 1446 1447 1448 1449
        /* create the array of actual key sizes */
        std::vector<hg_size_t> true_ksizes(num_keys);
        hg_size_t keys_bulk_size = 0;
        for(unsigned i = 0; i < num_keys; i++) {
            true_ksizes[i] = keyvals[i].first.size();
            if(true_ksizes[i] > ksizes[i]) {
                // this key has a size that exceeds the allocated size on client
1450
                throw SDSKV_ERR_SIZE;
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
            } else {
                ksizes[i] = true_ksizes[i];
                keys_bulk_size += ksizes[i];
            }
        }

        /* create the array of actual value sizes */
        std::vector<hg_size_t> true_vsizes(num_keys);
        hg_size_t vals_bulk_size = 0;
        for(unsigned i = 0; i < num_keys; i++) {
            true_vsizes[i] = keyvals[i].second.size();
            if(true_vsizes[i] > vsizes[i]) {
                // this value has a size that exceeds the allocated size on client
1464
                throw SDSKV_ERR_SIZE;
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
            } else {
                vsizes[i] = true_vsizes[i];
                vals_bulk_size += vsizes[i];
            }
        }

        /* create an array of addresses pointing to keys */
        std::vector<void*> keys_addr(num_keys);
        for(unsigned i=0; i < num_keys; i++) {
            keys_addr[i] = (void*)(keyvals[i].first.data());
        }

        /* create an array of addresses pointing to values */
        std::vector<void*> vals_addr(num_keys);
        for(unsigned i=0; i < num_keys; i++) {
            vals_addr[i] = (void*)(keyvals[i].second.data());
        }

        /* expose the keys for bulk transfer */
        hret = margo_bulk_create(mid, num_keys, keys_addr.data(),
                true_ksizes.data(), HG_BULK_READ_ONLY, &keys_local_bulk);
        if(hret != HG_SUCCESS) {
            std::cerr << "Error: SDSKV list_keyvals could not create bulk handle (keys_local_bulk)" << std::endl;
1488
            throw SDSKV_ERR_MERCURY;
1489 1490 1491 1492 1493 1494 1495
        }

        /* expose the values for bulk transfer */
        hret = margo_bulk_create(mid, num_keys, vals_addr.data(),
                true_vsizes.data(), HG_BULK_READ_ONLY, &vals_local_bulk);
        if(hret != HG_SUCCESS) {
            std::cerr << "Error: SDSKV list_keyvals could not create bulk handle (vals_local_bulk)" << std::endl;
1496
            throw SDSKV_ERR_MERCURY;
1497 1498 1499 1500 1501 1502 1503 1504
        }

        /* transfer the ksizes back to the client */
        hret = margo_bulk_transfer(mid, HG_BULK_PUSH, origin_addr, 
                in.ksizes_bulk_handle, 0, ksizes_local_bulk, 0, ksizes_bulk_size);
        if(hret != HG_SUCCESS) {
            std::cerr << "Error: SDSKV list_keyvals could not issue bulk transfer "
                << "(push from ksizes_local_bulk to in.ksizes_bulk_handle)" << std::endl;
1505
            throw SDSKV_ERR_MERCURY;
1506 1507 1508 1509 1510 1511 1512 1513
        }

        /* transfer the vsizes back to the client */
        hret = margo_bulk_transfer(mid, HG_BULK_PUSH, origin_addr, 
                in.vsizes_bulk_handle, 0, vsizes_local_bulk, 0, vsizes_bulk_size);
        if(hret != HG_SUCCESS) {
            std::cerr << "Error: SDSKV list_keyvals could not issue bulk transfer "
                << "(push from vsizes_local_bulk to in.vsizes_bulk_handle)" << std::endl;
1514
            throw SDSKV_ERR_MERCURY;
1515 1516 1517 1518 1519 1520 1521
        }

        uint64_t remote_offset = 0;
        uint64_t local_offset  = 0;

        /* transfer the keys to the client */
        for(unsigned i=0; i < num_keys; i++) {
1522 1523 1524 1525 1526 1527 1528
            if(true_ksizes[i] > 0) {
                hret = margo_bulk_transfer(mid, HG_BULK_PUSH, origin_addr,
                        in.keys_bulk_handle, remote_offset, keys_local_bulk, local_offset, true_ksizes[i]);
                if(hret != HG_SUCCESS) {
                    std::cerr << "Error: SDSKV list_keyvals could not issue bulk transfer (keys_local_bulk)" << std::endl;
                    throw SDSKV_ERR_MERCURY;
                }
1529 1530