sdskv-server.cc 34.9 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 20 21 22 23 24 25 26 27
#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;
};

DECLARE_MARGO_RPC_HANDLER(sdskv_put_ult)
DECLARE_MARGO_RPC_HANDLER(sdskv_length_ult)
DECLARE_MARGO_RPC_HANDLER(sdskv_get_ult)
DECLARE_MARGO_RPC_HANDLER(sdskv_open_ult)
DECLARE_MARGO_RPC_HANDLER(sdskv_bulk_put_ult)
DECLARE_MARGO_RPC_HANDLER(sdskv_bulk_get_ult)
28
DECLARE_MARGO_RPC_HANDLER(sdskv_list_keys_ult)
29
DECLARE_MARGO_RPC_HANDLER(sdskv_list_keyvals_ult)
Matthieu Dorier's avatar
Matthieu Dorier committed
30
DECLARE_MARGO_RPC_HANDLER(sdskv_erase_ult)
31
DECLARE_MARGO_RPC_HANDLER(sdskv_exists_ult)
Matthieu Dorier's avatar
Matthieu Dorier committed
32 33 34 35 36

static void sdskv_server_finalize_cb(void *data);

extern "C" int sdskv_provider_register(
        margo_instance_id mid,
37
        uint16_t provider_id,
Matthieu Dorier's avatar
Matthieu Dorier committed
38 39 40 41 42 43 44 45 46
        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;
47
        margo_provider_registered_name(mid, "sdskv_put_rpc", provider_id, &id, &flag);
Matthieu Dorier's avatar
Matthieu Dorier committed
48
        if(flag == HG_TRUE) {
49
            fprintf(stderr, "sdskv_provider_register(): a provider with the same provider id (%d) already exists\n", provider_id);
50
            return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
51 52 53 54
        }
    }

    /* allocate the resulting structure */    
Matthieu Dorier's avatar
Matthieu Dorier committed
55
    tmp_svr_ctx = new sdskv_server_context_t;
Matthieu Dorier's avatar
Matthieu Dorier committed
56
    if(!tmp_svr_ctx)
57
        return SDSKV_ERR_ALLOCATION;
Matthieu Dorier's avatar
Matthieu Dorier committed
58 59 60

    /* register RPCs */
    hg_id_t rpc_id;
61
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_put_rpc",
Matthieu Dorier's avatar
Matthieu Dorier committed
62
            put_in_t, put_out_t,
63 64 65
            sdskv_put_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_bulk_put_rpc",
Matthieu Dorier's avatar
Matthieu Dorier committed
66
            bulk_put_in_t, bulk_put_out_t,
67 68 69
            sdskv_bulk_put_ult, provider_id, abt_pool);
    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
70
            get_in_t, get_out_t,
71 72 73
            sdskv_get_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_length_rpc",
Matthieu Dorier's avatar
Matthieu Dorier committed
74
            length_in_t, length_out_t,
75 76
            sdskv_length_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
77 78 79 80
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_exists_rpc",
            exists_in_t, exists_out_t,
            sdskv_exists_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
81
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_bulk_get_rpc",
Matthieu Dorier's avatar
Matthieu Dorier committed
82
            bulk_get_in_t, bulk_get_out_t,
83 84 85
            sdskv_bulk_get_ult, provider_id, abt_pool);
    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
86
            open_in_t, open_out_t,
87 88 89
            sdskv_open_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_list_keys_rpc",
90
            list_keys_in_t, list_keys_out_t,
91 92 93
            sdskv_list_keys_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_list_keyvals_rpc",
94
            list_keyvals_in_t, list_keyvals_out_t,
95 96 97
            sdskv_list_keyvals_ult, provider_id, abt_pool);
    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
98
            erase_in_t, erase_out_t,
99 100
            sdskv_erase_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
Matthieu Dorier's avatar
Matthieu Dorier committed
101 102 103 104 105 106 107

    /* 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;

108
    return SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
109 110
}

111
extern "C" int sdskv_provider_attach_database(
Matthieu Dorier's avatar
Matthieu Dorier committed
112
        sdskv_provider_t provider,
113
        const sdskv_config_t* config,
Matthieu Dorier's avatar
Matthieu Dorier committed
114 115
        sdskv_database_id_t* db_id)
{
116 117
    auto db = datastore_factory::open_datastore(config->db_type, 
            std::string(config->db_name), std::string(config->db_path));
118
    if(db == nullptr) return SDSKV_ERR_DB_CREATE;
119
    db->set_comparison_function(config->db_comparison_fn);
Matthieu Dorier's avatar
Matthieu Dorier committed
120
    sdskv_database_id_t id = (sdskv_database_id_t)(db);
121 122 123
    if(config->db_no_overwrite) {
        db->set_no_overwrite();
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
124

125 126
    provider->name2id[std::string(config->db_name)] = id;
    provider->id2name[id] = std::string(config->db_name);
Matthieu Dorier's avatar
Matthieu Dorier committed
127 128
    provider->databases[id] = db;

129 130
    *db_id = id;

131
    return SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
132 133
}

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
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
150 151 152 153 154 155 156 157 158 159 160
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);
161
        return SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
162
    } else {
163
        return SDSKV_ERR_UNKNOWN_DB;
Matthieu Dorier's avatar
Matthieu Dorier committed
164 165 166 167 168 169 170 171 172 173 174 175 176
    }
}

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();

177
    return SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
178 179 180 181 182 183 184
}

extern "C" int sdskv_provider_count_databases(
        sdskv_provider_t provider,
        uint64_t* num_db)
{
    *num_db = provider->databases.size();
185
    return SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
186 187 188 189 190 191 192 193 194 195 196
}

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++;
    }
197
    return SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
198 199 200 201 202 203 204 205 206 207 208 209
}

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 = 
210
        (sdskv_provider_t)margo_registered_data(mid, info->id);
Matthieu Dorier's avatar
Matthieu Dorier committed
211 212
    if(!svr_ctx) {
        fprintf(stderr, "Error: SDSKV could not find provider\n"); 
213
        out.ret = SDSKV_ERR_UNKNOWN_PR;
Matthieu Dorier's avatar
Matthieu Dorier committed
214 215 216 217 218 219 220
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }
    
    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
221
        out.ret = SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
222 223 224 225 226 227 228
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    auto it = svr_ctx->databases.find(in.db_id);
    if(it == svr_ctx->databases.end()) {
229
        out.ret = SDSKV_ERR_UNKNOWN_DB;
Matthieu Dorier's avatar
Matthieu Dorier committed
230 231 232 233 234 235
        margo_respond(handle, &out);
        margo_free_input(handle, &in);
        margo_destroy(handle);
        return;
    }

236 237
    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
238 239

    if(it->second->put(kdata, vdata)) {
240
        out.ret = SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
241
    } else {
242
        out.ret = SDSKV_ERR_PUT;
Matthieu Dorier's avatar
Matthieu Dorier committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
    }

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

static void sdskv_length_ult(hg_handle_t handle)
{

    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 = 
262
        (sdskv_provider_t)margo_registered_data(mid, info->id);
Matthieu Dorier's avatar
Matthieu Dorier committed
263 264
    if(!svr_ctx) {
        fprintf(stderr, "Error: SDSKV could not find provider\n"); 
265
        out.ret = SDSKV_ERR_UNKNOWN_PR;
Matthieu Dorier's avatar
Matthieu Dorier committed
266 267 268 269 270 271 272
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
273
        out.ret = SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
274 275 276 277 278 279 280
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    auto it = svr_ctx->databases.find(in.db_id);
    if(it == svr_ctx->databases.end()) {
281
        out.ret = SDSKV_ERR_UNKNOWN_DB;
Matthieu Dorier's avatar
Matthieu Dorier committed
282 283 284 285 286 287
        margo_respond(handle, &out);
        margo_free_input(handle, &in);
        margo_destroy(handle);
        return;
    }
    
288
    ds_bulk_t kdata(in.key.data, in.key.data+in.key.size);
Matthieu Dorier's avatar
Matthieu Dorier committed
289 290 291 292

    ds_bulk_t vdata;
    if(it->second->get(kdata, vdata)) {
        out.size = vdata.size();
293
        out.ret  = SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
294 295
    } else {
        out.size = 0;
296
        out.ret  = SDSKV_ERR_UNKNOWN_KEY;
Matthieu Dorier's avatar
Matthieu Dorier committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
    }

    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 = 
318
        (sdskv_provider_t)margo_registered_data(mid, info->id);
Matthieu Dorier's avatar
Matthieu Dorier committed
319 320
    if(!svr_ctx) {
        fprintf(stderr, "Error: SDSKV could not find provider\n"); 
321
        out.ret = SDSKV_ERR_UNKNOWN_PR;
Matthieu Dorier's avatar
Matthieu Dorier committed
322 323 324 325 326 327 328
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
329
        out.ret = SDSKV_ERR_MERCURY;
330 331
        out.value.data = nullptr;
        out.value.size = 0;
Matthieu Dorier's avatar
Matthieu Dorier committed
332 333 334 335 336 337 338
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    auto it = svr_ctx->databases.find(in.db_id);
    if(it == svr_ctx->databases.end()) {
339
        out.ret = SDSKV_ERR_UNKNOWN_DB;
340 341
        out.value.data = nullptr;
        out.value.size = 0;
Matthieu Dorier's avatar
Matthieu Dorier committed
342 343 344 345 346 347
        margo_respond(handle, &out);
        margo_free_input(handle, &in);
        margo_destroy(handle);
        return;
    }
    
348
    ds_bulk_t kdata(in.key.data, in.key.data+in.key.size);
Matthieu Dorier's avatar
Matthieu Dorier committed
349 350 351 352

    ds_bulk_t vdata;
    if(it->second->get(kdata, vdata)) {
        if(vdata.size() <= in.vsize) {
353 354
            out.value.size = vdata.size();
            out.value.data = vdata.data();
355
            out.ret = SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
356
        } else {
357 358
            out.value.size = 0;
            out.value.data = nullptr;
359
            out.ret = SDSKV_ERR_SIZE;
Matthieu Dorier's avatar
Matthieu Dorier committed
360 361
        }
    } else {
362 363
        out.value.size = 0;
        out.value.data = nullptr;
364
        out.ret   = SDSKV_ERR_UNKNOWN_KEY;
Matthieu Dorier's avatar
Matthieu Dorier committed
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
    }

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

    return;
}
DEFINE_MARGO_RPC_HANDLER(sdskv_get_ult)

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 = 
386
        (sdskv_provider_t)margo_registered_data(mid, info->id);
Matthieu Dorier's avatar
Matthieu Dorier committed
387 388
    if(!svr_ctx) {
        fprintf(stderr, "Error: SDSKV could not find provider\n"); 
389
        out.ret = SDSKV_ERR_UNKNOWN_PR;
Matthieu Dorier's avatar
Matthieu Dorier committed
390 391 392 393 394 395 396
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
397
        out.ret = SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
398 399 400 401 402 403 404
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    auto it = svr_ctx->name2id.find(std::string(in.name));
    if(it == svr_ctx->name2id.end()) {
405
        out.ret = SDSKV_ERR_DB_NAME;
Matthieu Dorier's avatar
Matthieu Dorier committed
406 407 408 409 410 411 412
        margo_respond(handle, &out);
        margo_free_input(handle, &in);
        margo_destroy(handle);
        return;
    }

    out.db_id = it->second;
413
    out.ret  = SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433

    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 = 
434
        (sdskv_provider_t)margo_registered_data(mid, info->id);
Matthieu Dorier's avatar
Matthieu Dorier committed
435 436
    if(!svr_ctx) {
        fprintf(stderr, "Error: SDSKV could not find provider\n"); 
437
        out.ret = SDSKV_ERR_UNKNOWN_PR;
Matthieu Dorier's avatar
Matthieu Dorier committed
438 439 440 441 442 443 444
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
445
        out.ret = SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
446 447 448 449 450
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

451
    auto it = svr_ctx->databases.find(in.db_id);
Matthieu Dorier's avatar
Matthieu Dorier committed
452
    if(it == svr_ctx->databases.end()) {
453
        out.ret = SDSKV_ERR_UNKNOWN_DB;
Matthieu Dorier's avatar
Matthieu Dorier committed
454 455 456 457 458
        margo_respond(handle, &out);
        margo_free_input(handle, &in);
        margo_destroy(handle);
        return;
    }
459

460
    ds_bulk_t vdata(in.vsize);
Matthieu Dorier's avatar
Matthieu Dorier committed
461

462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
    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
487
        margo_bulk_free(bulk_handle);
488

Matthieu Dorier's avatar
Matthieu Dorier committed
489 490
    }

491
    ds_bulk_t kdata(in.key.data, in.key.data+in.key.size);
Matthieu Dorier's avatar
Matthieu Dorier committed
492 493 494 495

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

    if(b) {
496
        out.ret = SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
497
    } else {
498
        out.ret = SDSKV_ERR_PUT;
Matthieu Dorier's avatar
Matthieu Dorier committed
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
    }

    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 = 
521
        (sdskv_provider_t)margo_registered_data(mid, info->id);
Matthieu Dorier's avatar
Matthieu Dorier committed
522 523
    if(!svr_ctx) {
        fprintf(stderr, "Error: SDSKV could not find provider\n"); 
524
        out.ret = SDSKV_ERR_UNKNOWN_PR;
Matthieu Dorier's avatar
Matthieu Dorier committed
525 526 527 528 529 530 531
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
532
        out.ret = SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
533 534 535 536 537
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

538
    auto it = svr_ctx->databases.find(in.db_id);
Matthieu Dorier's avatar
Matthieu Dorier committed
539
    if(it == svr_ctx->databases.end()) {
540
        out.ret = SDSKV_ERR_UNKNOWN_DB;
Matthieu Dorier's avatar
Matthieu Dorier committed
541 542 543 544 545 546
        margo_respond(handle, &out);
        margo_free_input(handle, &in);
        margo_destroy(handle);
        return;
    }
    
547
    ds_bulk_t kdata(in.key.data, in.key.data+in.key.size);
Matthieu Dorier's avatar
Matthieu Dorier committed
548 549 550 551

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

552
    if(!b || vdata.size() > in.vsize) {
Matthieu Dorier's avatar
Matthieu Dorier committed
553
        out.size = 0;
554
        out.ret = SDSKV_ERR_SIZE;
Matthieu Dorier's avatar
Matthieu Dorier committed
555 556 557 558 559 560 561 562
        margo_respond(handle, &out);
        margo_free_input(handle, &in);
        margo_destroy(handle);
        return;
    }

    void *buffer = (void*)vdata.data();
    hg_size_t size = vdata.size();
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
    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
586 587 588 589

        margo_bulk_free(bulk_handle);
    }

590
    out.size = size;
591
    out.ret  = SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
592 593 594 595 596 597 598 599 600

    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
601 602 603 604 605 606 607 608 609 610 611
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 = 
612
        (sdskv_provider_t)margo_registered_data(mid, info->id);
Matthieu Dorier's avatar
Matthieu Dorier committed
613 614
    if(!svr_ctx) {
        fprintf(stderr, "Error: SDSKV could not find provider\n"); 
615
        out.ret = SDSKV_ERR_UNKNOWN_PR;
Matthieu Dorier's avatar
Matthieu Dorier committed
616 617 618 619 620 621 622
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
623
        out.ret = SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
624 625 626 627 628 629 630
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    auto it = svr_ctx->databases.find(in.db_id);
    if(it == svr_ctx->databases.end()) {
631
        out.ret = SDSKV_ERR_UNKNOWN_DB;
Matthieu Dorier's avatar
Matthieu Dorier committed
632 633 634 635 636 637
        margo_respond(handle, &out);
        margo_free_input(handle, &in);
        margo_destroy(handle);
        return;
    }
    
638
    ds_bulk_t kdata(in.key.data, in.key.data+in.key.size);
Matthieu Dorier's avatar
Matthieu Dorier committed
639 640

    if(it->second->erase(kdata)) {
641
        out.ret   = SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
642
    } else {
643
        out.ret   = SDSKV_ERR_ERASE;
Matthieu Dorier's avatar
Matthieu Dorier committed
644 645 646 647 648 649 650 651 652 653
    }

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

    return;
}
DEFINE_MARGO_RPC_HANDLER(sdskv_erase_ult)

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
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) {
        fprintf(stderr, "Error: SDSKV could not find provider\n"); 
        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)

703
static void sdskv_list_keys_ult(hg_handle_t handle)
Matthieu Dorier's avatar
Matthieu Dorier committed
704 705 706
{

    hg_return_t hret;
707 708
    list_keys_in_t in;
    list_keys_out_t out;
Matthieu Dorier's avatar
Matthieu Dorier committed
709 710
    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
711

712
    out.ret     = SDSKV_SUCCESS;
713 714
    out.nkeys   = 0;

Matthieu Dorier's avatar
Matthieu Dorier committed
715
    /* get the provider handling this request */
Matthieu Dorier's avatar
Matthieu Dorier committed
716 717 718 719
    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 = 
720
        (sdskv_provider_t)margo_registered_data(mid, info->id);
Matthieu Dorier's avatar
Matthieu Dorier committed
721
    if(!svr_ctx) {
722 723
        std::cerr << "Error: SDSKV list_keys could not find provider" << std::endl;
        out.ret = SDSKV_ERR_UNKNOWN_PR;
Matthieu Dorier's avatar
Matthieu Dorier committed
724 725 726 727 728
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
729
    /* get the input */
Matthieu Dorier's avatar
Matthieu Dorier committed
730 731
    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
Matthieu Dorier's avatar
Matthieu Dorier committed
732
        std::cerr << "Error: SDSKV list_keys could not get RPC input" << std::endl;
733
        out.ret = SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
734 735 736 737 738
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
739 740 741 742 743 744
    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;
745
            throw SDSKV_ERR_UNKNOWN_DB;
Matthieu Dorier's avatar
Matthieu Dorier committed
746 747 748 749 750 751 752 753 754 755 756 757
        }
        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;
758
            throw SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
759 760
        }

761

Matthieu Dorier's avatar
Matthieu Dorier committed
762 763 764 765 766 767 768
        /* 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;
769
            throw SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
770 771
        }

772 773 774
        /* 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
775
        /* get the keys from the underlying database */    
776
        ds_bulk_t start_kdata(in.start_key.data, in.start_key.data+in.start_key.size);
777 778
        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
779 780
        hg_size_t num_keys = std::min(keys.size(), in.max_keys);

781 782
        if(num_keys == 0) throw SDSKV_SUCCESS;

Matthieu Dorier's avatar
Matthieu Dorier committed
783 784 785 786 787 788 789
        /* 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
790
                throw SDSKV_ERR_SIZE;
Matthieu Dorier's avatar
Matthieu Dorier committed
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
            } 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;
808
            throw SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
809 810 811 812 813 814 815 816
        }

        /* 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;
817
            throw SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
818 819 820
        }

        /* transfer the keys to the client */
821 822 823 824
        uint64_t remote_offset = 0;
        uint64_t local_offset  = 0;
        for(unsigned i = 0; i < num_keys; i++) {

825 826 827 828 829 830 831
            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;
                }
832 833 834 835 836 837 838
            }

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

        out.nkeys = num_keys;
839
        out.ret = SDSKV_SUCCESS;
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865

    } 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;

866
    out.ret     = SDSKV_SUCCESS;
867 868 869 870 871 872 873
    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 = 
874
        (sdskv_provider_t)margo_registered_data(mid, info->id);
875
    if(!svr_ctx) {
876 877
        std::cerr << "Error: SDSKV list_keyvals could not find provider" << std::endl;
        out.ret = SDSKV_ERR_UNKNOWN_PR;
878 879 880 881 882 883 884 885 886
        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;
887
        out.ret = SDSKV_ERR_MERCURY;
888 889 890 891 892 893 894 895 896 897 898
        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;
899
            throw SDSKV_ERR_UNKNOWN_DB;
900 901 902 903 904 905 906 907 908 909 910 911
        }
        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;
912
            throw SDSKV_ERR_MERCURY;
913 914 915 916 917 918 919 920 921 922 923
        }

        /* 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;
924
            throw SDSKV_ERR_MERCURY;
925 926 927 928 929 930 931 932 933
        }

        /* 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;
934
            throw SDSKV_ERR_MERCURY;
935 936 937 938 939
        }

        /* 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
940
        if(hret != HG_SUCCESS) {
941 942
            std::cerr << "Error: SDSKV list_keyvals could not issue bulk transfer " 
                << "(pull from in.vsizes_bulk_handle to vsizes_local_bulk)" << std::endl;
943
            throw SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
944 945
        }

946 947 948 949 950
        /* 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 */    
951
        ds_bulk_t start_kdata(in.start_key.data, in.start_key.data+in.start_key.size);
952 953
        ds_bulk_t prefix(in.prefix.data, in.prefix.data+in.prefix.size);
        auto keyvals = db->list_keyvals(start_kdata, in.max_keys, prefix);
954 955
        hg_size_t num_keys = std::min(keyvals.size(), in.max_keys);

956 957
        if(num_keys == 0) throw SDSKV_SUCCESS;

958 959 960 961 962 963 964
        /* 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
965
                throw SDSKV_ERR_SIZE;
966 967 968 969 970 971 972 973 974 975 976 977 978
            } 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
979
                throw SDSKV_ERR_SIZE;
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
            } 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;
1003
            throw SDSKV_ERR_MERCURY;
1004 1005 1006 1007 1008 1009 1010
        }

        /* 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;
1011
            throw SDSKV_ERR_MERCURY;
1012 1013 1014 1015 1016 1017 1018 1019
        }

        /* 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;
1020
            throw SDSKV_ERR_MERCURY;
1021 1022 1023 1024 1025 1026 1027 1028
        }

        /* 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;
1029
            throw SDSKV_ERR_MERCURY;
1030 1031 1032 1033 1034 1035 1036
        }

        uint64_t remote_offset = 0;
        uint64_t local_offset  = 0;

        /* transfer the keys to the client */
        for(unsigned i=0; i < num_keys; i++) {
1037 1038 1039 1040 1041 1042 1043
            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;
                }
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
            }
            remote_offset += remote_ksizes[i];
            local_offset  += true_ksizes[i];
        }

        remote_offset = 0;
        local_offset  = 0;

        /* transfer the values to the client */
        for(unsigned i=0; i < num_keys; i++) {
1054 1055 1056 1057 1058 1059 1060
            if(true_vsizes[i] > 0) {
                hret = margo_bulk_transfer(mid, HG_BULK_PUSH, origin_addr,
                        in.vals_bulk_handle, remote_offset, vals_local_bulk, local_offset, true_vsizes[i]);
                if(hret != HG_SUCCESS) {
                    std::cerr << "Error: SDSKV list_keyvals could not issue bulk transfer (vals_local_bulk)" << std::endl;
                    throw SDSKV_ERR_MERCURY;
                }
1061 1062 1063 1064 1065
            }
            remote_offset += remote_vsizes[i];
            local_offset  += true_vsizes[i];
        }

Matthieu Dorier's avatar
Matthieu Dorier committed
1066
        out.nkeys = num_keys;
1067
        out.ret = SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
1068 1069 1070

    } catch(int exc_no) {
        out.ret = exc_no;
Matthieu Dorier's avatar
Matthieu Dorier committed
1071 1072
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
1073 1074
    margo_bulk_free(ksizes_local_bulk);
    margo_bulk_free(keys_local_bulk);
1075 1076
    margo_bulk_free(vsizes_local_bulk);
    margo_bulk_free(vals_local_bulk);
Matthieu Dorier's avatar
Matthieu Dorier committed
1077 1078 1079 1080 1081 1082
    margo_respond(handle, &out);
    margo_free_input(handle, &in);
    margo_destroy(handle); 

    return;
}
1083
DEFINE_MARGO_RPC_HANDLER(sdskv_list_keyvals_ult)
Matthieu Dorier's avatar
Matthieu Dorier committed
1084 1085 1086 1087 1088 1089 1090 1091

static void sdskv_server_finalize_cb(void *data)
{
    sdskv_provider_t svr_ctx = (sdskv_provider_t)data;
    assert(svr_ctx);

    sdskv_provider_remove_all_databases(svr_ctx);

Matthieu Dorier's avatar
Matthieu Dorier committed
1092
    delete svr_ctx;
Matthieu Dorier's avatar
Matthieu Dorier committed
1093 1094 1095 1096

    return;
}