sdskv-client.c 23.3 KB
Newer Older
Matthieu Dorier's avatar
Matthieu Dorier committed
1 2 3 4 5 6 7 8 9 10 11
#include "sdskv-client.h"
#include "sdskv-rpc-types.h"

#define MAX_RPC_MESSAGE_SIZE 4000 // in bytes

struct sdskv_client {
    margo_instance_id mid;

    hg_id_t sdskv_put_id;
    hg_id_t sdskv_bulk_put_id;
    hg_id_t sdskv_get_id;
Matthieu Dorier's avatar
Matthieu Dorier committed
12
    hg_id_t sdskv_erase_id;
Matthieu Dorier's avatar
Matthieu Dorier committed
13 14 15
    hg_id_t sdskv_length_id;
    hg_id_t sdskv_bulk_get_id;
    hg_id_t sdskv_open_id;
Matthieu Dorier's avatar
Matthieu Dorier committed
16
    hg_id_t sdskv_list_keys_id;
17
    hg_id_t sdskv_list_keyvals_id;
Matthieu Dorier's avatar
Matthieu Dorier committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

    uint64_t num_provider_handles;
};

struct sdskv_provider_handle {
    sdskv_client_t client;
    hg_addr_t      addr;
    uint8_t        mplex_id;
    uint64_t       refcount;
};

static int sdskv_client_register(sdskv_client_t client, margo_instance_id mid)
{
    client->mid = mid;

    /* check if RPCs have already been registered */
    hg_bool_t flag;
    hg_id_t id;
    margo_registered_name(mid, "sdskv_put_rpc", &id, &flag);

    if(flag == HG_TRUE) { /* RPCs already registered */

        margo_registered_name(mid, "sdskv_put_rpc",      &client->sdskv_put_id,      &flag);
        margo_registered_name(mid, "sdskv_bulk_put_rpc", &client->sdskv_bulk_put_id, &flag);
        margo_registered_name(mid, "sdskv_get_rpc",      &client->sdskv_get_id,      &flag);
Matthieu Dorier's avatar
Matthieu Dorier committed
43
        margo_registered_name(mid, "sdskv_erase_rpc",    &client->sdskv_erase_id,    &flag);
Matthieu Dorier's avatar
Matthieu Dorier committed
44 45 46
        margo_registered_name(mid, "sdskv_length_rpc",   &client->sdskv_length_id,   &flag);
        margo_registered_name(mid, "sdskv_bulk_get_rpc", &client->sdskv_bulk_get_id, &flag);
        margo_registered_name(mid, "sdskv_open_rpc",     &client->sdskv_open_id,     &flag);
Matthieu Dorier's avatar
Matthieu Dorier committed
47
        margo_registered_name(mid, "sdskv_list_keys_rpc", &client->sdskv_list_keys_id, &flag);
48
        margo_registered_name(mid, "sdskv_list_keyvals_rpc", &client->sdskv_list_keyvals_id, &flag);
Matthieu Dorier's avatar
Matthieu Dorier committed
49 50 51 52 53 54 55 56 57

    } else {

        client->sdskv_put_id =
            MARGO_REGISTER(mid, "sdskv_put_rpc", put_in_t, put_out_t, NULL);
        client->sdskv_bulk_put_id =
            MARGO_REGISTER(mid, "sdskv_bulk_put_rpc", bulk_put_in_t, bulk_put_out_t, NULL);
        client->sdskv_get_id =
            MARGO_REGISTER(mid, "sdskv_get_rpc", get_in_t, get_out_t, NULL);
Matthieu Dorier's avatar
Matthieu Dorier committed
58 59
        client->sdskv_erase_id =
            MARGO_REGISTER(mid, "sdskv_erase_rpc", erase_in_t, erase_out_t, NULL);
Matthieu Dorier's avatar
Matthieu Dorier committed
60 61 62 63 64 65
        client->sdskv_length_id =
            MARGO_REGISTER(mid, "sdskv_length_rpc", length_in_t, length_out_t, NULL);
        client->sdskv_bulk_get_id =
            MARGO_REGISTER(mid, "sdskv_bulk_get_rpc", bulk_get_in_t, bulk_get_out_t, NULL);
        client->sdskv_open_id =
            MARGO_REGISTER(mid, "sdskv_open_rpc", open_in_t, open_out_t, NULL);
Matthieu Dorier's avatar
Matthieu Dorier committed
66
        client->sdskv_list_keys_id =
67
            MARGO_REGISTER(mid, "sdskv_list_keys_rpc", list_keys_in_t, list_keys_out_t, NULL);
68 69
        client->sdskv_list_keyvals_id =
            MARGO_REGISTER(mid, "sdskv_list_keyvals_rpc", list_keyvals_in_t, list_keyvals_out_t, NULL);
Matthieu Dorier's avatar
Matthieu Dorier committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    }

    return 0;
}

int sdskv_client_init(margo_instance_id mid, sdskv_client_t* client)
{
    sdskv_client_t c = (sdskv_client_t)calloc(1, sizeof(*c));
    if(!c) return -1;

    c->num_provider_handles = 0;

    int ret = sdskv_client_register(c, mid);
    if(ret != 0) return ret;

    *client = c;
    return 0;
}

int sdskv_client_finalize(sdskv_client_t client)
{
    if(client->num_provider_handles != 0) {
        fprintf(stderr,
                "[SDSKV] Warning: %d provider handles not released before sdskv_client_finalize was called\n",
                client->num_provider_handles);
    }
    free(client);
    return 0;
}

int sdskv_provider_handle_create(
        sdskv_client_t client,
        hg_addr_t addr,
        uint8_t mplex_id,
        sdskv_provider_handle_t* handle)
{
    if(client == SDSKV_CLIENT_NULL) return -1;

    sdskv_provider_handle_t provider =
        (sdskv_provider_handle_t)calloc(1, sizeof(*provider));

    if(!provider) return -1;

    hg_return_t ret = margo_addr_dup(client->mid, addr, &(provider->addr));
    if(ret != HG_SUCCESS) {
        free(provider);
        return -1;
    }

    provider->client   = client;
    provider->mplex_id = mplex_id;
    provider->refcount = 1;

    client->num_provider_handles += 1;

    *handle = provider;
    return 0;
}

int sdskv_provider_handle_ref_incr(
        sdskv_provider_handle_t handle)
{
    if(handle == SDSKV_PROVIDER_HANDLE_NULL) return -1;
    handle->refcount += 1;
    return 0;
}

Matthieu Dorier's avatar
Matthieu Dorier committed
137
int sdskv_provider_handle_release(sdskv_provider_handle_t handle)
Matthieu Dorier's avatar
Matthieu Dorier committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
{
    if(handle == SDSKV_PROVIDER_HANDLE_NULL) return -1;
    handle->refcount -= 1;
    if(handle->refcount == 0) {
        margo_addr_free(handle->client->mid, handle->addr);
        handle->client->num_provider_handles -= 1;
        free(handle);
    }
    return 0;
}

int sdskv_open(
        sdskv_provider_handle_t provider,
        const char* db_name,
        sdskv_database_id_t* db_id)
{
    hg_return_t hret;
    int ret;
    open_in_t in;
    open_out_t out;
    hg_handle_t handle;

    /* create handle */
    hret = margo_create(
            provider->client->mid,
            provider->addr,
            provider->client->sdskv_open_id,
            &handle);
    if(hret != HG_SUCCESS) return -1;

    hret = margo_set_target_id(handle, provider->mplex_id);

    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
        return -1;
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
175 176
    in.name = (char*)db_name;

Matthieu Dorier's avatar
Matthieu Dorier committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    hret = margo_forward(handle, &in);
    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
        return -1;
    }

    hret = margo_get_output(handle, &out);
    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
        return -1;
    }

    ret = out.ret;
    *db_id = out.db_id;

    margo_free_output(handle, &out);
    margo_destroy(handle);

    return ret;
}

int sdskv_put(sdskv_provider_handle_t provider, 
        sdskv_database_id_t db_id,
        const void *key, hg_size_t ksize,
        const void *value, hg_size_t vsize)
{
    hg_return_t hret;
    int ret;
    hg_handle_t handle;

    hg_size_t msize = ksize + vsize + 2*sizeof(hg_size_t);

    if(msize <= MAX_RPC_MESSAGE_SIZE) {

        put_in_t in;
        put_out_t out;

        in.db_id = db_id;
215 216 217 218
        in.key.data = (kv_ptr_t)key;
        in.key.size = ksize;
        in.value.data = (kv_ptr_t)value;
        in.value.size = vsize;
Matthieu Dorier's avatar
Matthieu Dorier committed
219 220 221 222 223 224 225

        /* create handle */
        hret = margo_create(
                provider->client->mid,
                provider->addr,
                provider->client->sdskv_put_id,
                &handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
226 227 228 229
        if(hret != HG_SUCCESS) {
            fprintf(stderr,"[SDSKV] margo_create() failed in sdskv_put()\n");
            return -1;
        }
Matthieu Dorier's avatar
Matthieu Dorier committed
230 231 232 233

        hret = margo_set_target_id(handle, provider->mplex_id);

        if(hret != HG_SUCCESS) {
Matthieu Dorier's avatar
Matthieu Dorier committed
234
            fprintf(stderr,"[SDSKV] margo_set_target_id() failed in sdskv_put()\n");
Matthieu Dorier's avatar
Matthieu Dorier committed
235 236 237 238 239 240
            margo_destroy(handle);
            return -1;
        }

        hret = margo_forward(handle, &in);
        if(hret != HG_SUCCESS) {
Matthieu Dorier's avatar
Matthieu Dorier committed
241
            fprintf(stderr,"[SDSKV] margo_forward() failed in sdskv_put()\n");
Matthieu Dorier's avatar
Matthieu Dorier committed
242 243 244 245 246 247
            margo_destroy(handle);
            return -1;
        }

        hret = margo_get_output(handle, &out);
        if(hret != HG_SUCCESS) {
Matthieu Dorier's avatar
Matthieu Dorier committed
248
            fprintf(stderr,"[SDSKV] margo_get_output() failed in sdskv_put()\n");
Matthieu Dorier's avatar
Matthieu Dorier committed
249 250 251 252 253 254 255 256 257 258 259 260 261
            margo_destroy(handle);
            return -1;
        }

        ret = out.ret;

        margo_free_output(handle, &out);

    } else {

        bulk_put_in_t in;
        bulk_put_out_t out;

262 263 264 265
        in.db_id = db_id;
        in.key.data = (kv_ptr_t)key;
        in.key.size = ksize;
        in.vsize = vsize;
Matthieu Dorier's avatar
Matthieu Dorier committed
266

267 268
        hret = margo_bulk_create(provider->client->mid, 1, (void**)(&value), &in.vsize,
                                HG_BULK_READ_ONLY, &in.handle);
269
        if(hret != HG_SUCCESS) {
Matthieu Dorier's avatar
Matthieu Dorier committed
270 271 272
            fprintf(stderr,"[SDSKV] margo_bulk_create() failed in sdskv_put()\n");
            return -1;
        }
Matthieu Dorier's avatar
Matthieu Dorier committed
273 274 275 276 277 278 279 280

        /* create handle */
        hret = margo_create(
                provider->client->mid,
                provider->addr,
                provider->client->sdskv_bulk_put_id,
                &handle);
        if(hret != HG_SUCCESS) {
Matthieu Dorier's avatar
Matthieu Dorier committed
281
            fprintf(stderr,"[SDSKV] margo_create() failed in sdskv_put()\n");
282
            margo_bulk_free(in.handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
283 284 285 286 287 288
            return -1;
        }

        hret = margo_set_target_id(handle, provider->mplex_id);

        if(hret != HG_SUCCESS) {
Matthieu Dorier's avatar
Matthieu Dorier committed
289
            fprintf(stderr,"[SDSKV] margo_set_target_id() failed in sdskv_put()\n");
290
            margo_bulk_free(in.handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
291 292 293 294 295 296
            margo_destroy(handle);
            return -1;
        }

        hret = margo_forward(handle, &in);
        if(hret != HG_SUCCESS) {
Matthieu Dorier's avatar
Matthieu Dorier committed
297
            fprintf(stderr,"[SDSKV] margo_forward() failed in sdskv_put()\n");
298
            margo_bulk_free(in.handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
299 300 301 302 303 304
            margo_destroy(handle);
            return -1;
        }

        hret = margo_get_output(handle, &out);
        if(hret != HG_SUCCESS) {
Matthieu Dorier's avatar
Matthieu Dorier committed
305
            fprintf(stderr,"[SDSKV] margo_get_output() failed in sdskv_put()\n");
306
            margo_bulk_free(in.handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
307 308 309 310 311 312
            margo_destroy(handle);
            return -1;
        }

        ret = out.ret;
        margo_free_output(handle, &out);
313
        margo_bulk_free(in.handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
    }

    margo_destroy(handle);
    return ret;
}

int sdskv_get(sdskv_provider_handle_t provider,
        sdskv_database_id_t db_id, 
        const void *key, hg_size_t ksize,
        void *value, hg_size_t* vsize)
{
    hg_return_t hret;
    hg_size_t size;
    hg_size_t msize;
    int ret;
    hg_handle_t handle;

    size = *(hg_size_t*)vsize;
    msize = size + sizeof(hg_size_t) + sizeof(hg_return_t);

    if (msize <= MAX_RPC_MESSAGE_SIZE) {

        get_in_t in;
        get_out_t out;

        in.db_id = db_id;
340 341
        in.key.data = (kv_ptr_t)key;
        in.key.size = ksize;
Matthieu Dorier's avatar
Matthieu Dorier committed
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
        in.vsize = size;

        /* create handle */
        hret = margo_create(
                provider->client->mid,
                provider->addr,
                provider->client->sdskv_get_id,
                &handle);
        if(hret != HG_SUCCESS) return -1;

        hret = margo_set_target_id(handle, provider->mplex_id);

        if(hret != HG_SUCCESS) {
            margo_destroy(handle);
            return -1;
        }

        hret = margo_forward(handle, &in);
        if(hret != HG_SUCCESS) {
            margo_destroy(handle);
            return -1;
        }

        hret = margo_get_output(handle, &out);
        if(hret != HG_SUCCESS) {
            margo_destroy(handle);
            return -1;
        }

        ret = out.ret;
372
        *vsize = (hg_size_t)out.value.size;
Matthieu Dorier's avatar
Matthieu Dorier committed
373

374 375
        if (out.value.size > 0) {
            memcpy(value, out.value.data, out.value.size);
Matthieu Dorier's avatar
Matthieu Dorier committed
376 377 378 379 380 381 382 383 384
        }

        margo_free_output(handle, &out);

    } else {

        bulk_get_in_t in;
        bulk_get_out_t out;

385 386 387 388
        in.db_id = db_id;
        in.key.data = (kv_ptr_t)key;
        in.key.size = ksize;
        in.vsize = size;
Matthieu Dorier's avatar
Matthieu Dorier committed
389

390 391
        hret = margo_bulk_create(provider->client->mid, 1, &value, &in.vsize,
                                HG_BULK_WRITE_ONLY, &in.handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
392 393 394 395 396 397 398 399 400
        if(hret != HG_SUCCESS) return -1;

        /* create handle */
        hret = margo_create(
                provider->client->mid,
                provider->addr,
                provider->client->sdskv_bulk_get_id,
                &handle);
        if(hret != HG_SUCCESS) {
401
            margo_bulk_free(in.handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
402 403 404 405 406
            return -1;
        }

        hret = margo_set_target_id(handle, provider->mplex_id);
        if(hret != HG_SUCCESS) {
407
            margo_bulk_free(in.handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
408 409 410 411 412 413
            margo_destroy(handle);
            return -1;
        }

        hret = margo_forward(handle, &in);
        if(hret != HG_SUCCESS) {
414
            margo_bulk_free(in.handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
415 416 417 418 419 420
            margo_destroy(handle);
            return -1;
        }

        hret = margo_get_output(handle, &out);
        if(hret != HG_SUCCESS) {
421
            margo_bulk_free(in.handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
422 423 424 425 426 427 428 429
            margo_destroy(handle);
            return -1;
        }

        ret = out.ret;
        *vsize = (hg_size_t)out.size;

        margo_free_output(handle, &out);
430
        margo_bulk_free(in.handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
    }

    margo_destroy(handle);
    return ret;
}

int sdskv_length(sdskv_provider_handle_t provider, 
        sdskv_database_id_t db_id, const void *key, 
        hg_size_t ksize, hg_size_t* vsize)
{
    hg_return_t hret;
    int ret;
    hg_handle_t handle;

    length_in_t in;
    length_out_t out;

    in.db_id = db_id;
449 450
    in.key.data   = (kv_ptr_t)key;
    in.key.size = ksize;
Matthieu Dorier's avatar
Matthieu Dorier committed
451 452 453 454 455 456 457 458 459 460 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

    /* create handle */
    hret = margo_create(
            provider->client->mid,
            provider->addr,
            provider->client->sdskv_length_id,
            &handle);
    if(hret != HG_SUCCESS) return -1;

    hret = margo_set_target_id(handle, provider->mplex_id);
    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
        return -1;
    }

    hret = margo_forward(handle, &in);
    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
        return -1;
    }

    hret = margo_get_output(handle, &out);
    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
        return -1;
    }

    ret = out.ret;
    if(ret == 0) *vsize = out.size;

    margo_free_output(handle, &out);
    margo_destroy(handle);
    return ret;
}

Matthieu Dorier's avatar
Matthieu Dorier committed
486 487 488 489 490 491 492 493 494 495 496 497
int sdskv_erase(sdskv_provider_handle_t provider,
        sdskv_database_id_t db_id, const void *key,
        hg_size_t ksize)
{
    hg_return_t hret;
    int ret;
    hg_handle_t handle;

    erase_in_t in;
    erase_out_t out;

    in.db_id = db_id;
498 499
    in.key.data   = (kv_ptr_t)key;
    in.key.size = ksize;
Matthieu Dorier's avatar
Matthieu Dorier committed
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533

    /* create handle */
    hret = margo_create(
            provider->client->mid,
            provider->addr,
            provider->client->sdskv_erase_id,
            &handle);
    if(hret != HG_SUCCESS) return -1;

    hret = margo_set_target_id(handle, provider->mplex_id);
    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
        return -1;
    }

    hret = margo_forward(handle, &in);
    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
        return -1;
    }

    hret = margo_get_output(handle, &out);
    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
        return -1;
    }

    ret = out.ret;

    margo_free_output(handle, &out);
    margo_destroy(handle);
    return ret;
}

Matthieu Dorier's avatar
Matthieu Dorier committed
534 535 536 537 538 539 540 541 542 543
int sdskv_list_keys(sdskv_provider_handle_t provider,
        sdskv_database_id_t db_id,  // db instance
        const void *start_key,  // we want keys strictly after this start_key
        hg_size_t start_ksize,  // size of the start_key
        void **keys,            // pointer to an array of void* pointers,
        //     this array has size *max_keys
        hg_size_t* ksizes,      // pointer to an array of hg_size_t sizes
        //    representing sizes allocated in
        //     keys for each key
        hg_size_t* max_keys)   // maximum number of keys requested
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
{
    return sdskv_list_keys_with_prefix(provider,
            db_id,
            start_key,
            start_ksize,
            NULL,
            0,
            keys,
            ksizes,
            max_keys);
}

int sdskv_list_keys_with_prefix(sdskv_provider_handle_t provider,
        sdskv_database_id_t db_id,  // db instance
        const void *start_key,  // we want keys strictly after this start_key
        hg_size_t start_ksize,  // size of the start_key
        const void *prefix,
        hg_size_t prefix_size,
        void **keys,            // pointer to an array of void* pointers,
        //     this array has size *max_keys
        hg_size_t* ksizes,      // pointer to an array of hg_size_t sizes
        //    representing sizes allocated in
        //     keys for each key
        hg_size_t* max_keys)   // maximum number of keys requested
Matthieu Dorier's avatar
Matthieu Dorier committed
568
{
569 570
    list_keys_in_t  in;
    list_keys_out_t out;
Matthieu Dorier's avatar
Matthieu Dorier committed
571 572 573
    hg_return_t hret      = HG_SUCCESS;
    hg_handle_t handle    = HG_HANDLE_NULL;
    int ret = 0;
Matthieu Dorier's avatar
Matthieu Dorier committed
574 575 576
    int i;

    in.db_id = db_id;
577 578
    in.start_key.data = (kv_ptr_t) start_key;
    in.start_key.size = start_ksize;
579 580 581 582
    in.prefix.data = (char*)prefix;
    in.prefix.size = prefix_size;
    in.keys_bulk_handle   = HG_BULK_NULL;
    in.ksizes_bulk_handle = HG_BULK_NULL;
Matthieu Dorier's avatar
Matthieu Dorier committed
583 584
    in.max_keys = *max_keys;

Matthieu Dorier's avatar
Matthieu Dorier committed
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
    /* create bulk handle to expose the segments with key sizes */
    hg_size_t ksize_bulk_size = (*max_keys)*sizeof(*ksizes);
    void* buf_ptr[1] = { ksizes };
    hret = margo_bulk_create(provider->client->mid,
                             1, buf_ptr, &ksize_bulk_size,
                             HG_BULK_READWRITE,
                             &in.ksizes_bulk_handle);
    if(hret != HG_SUCCESS) {
        ret = -1;
        goto finish;
    }

    /* create bulk handle to expose where the keys should be placed */
    hret = margo_bulk_create(provider->client->mid,
                             *max_keys, keys, ksizes,
                             HG_BULK_WRITE_ONLY,
                             &in.keys_bulk_handle);
    if(hret != HG_SUCCESS) {
        ret = -1;
        goto finish;
    }


Matthieu Dorier's avatar
Matthieu Dorier committed
608 609 610 611
    /* create handle */
    hret = margo_create(
            provider->client->mid,
            provider->addr,
Matthieu Dorier's avatar
Matthieu Dorier committed
612
            provider->client->sdskv_list_keys_id,
Matthieu Dorier's avatar
Matthieu Dorier committed
613
            &handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
614 615 616 617
    if(hret != HG_SUCCESS) {
        ret = -1;
        goto finish;
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
618

Matthieu Dorier's avatar
Matthieu Dorier committed
619
    /* set target id */
Matthieu Dorier's avatar
Matthieu Dorier committed
620 621
    hret = margo_set_target_id(handle, provider->mplex_id);
    if(hret != HG_SUCCESS) {
Matthieu Dorier's avatar
Matthieu Dorier committed
622 623
        ret = -1;
        goto finish;
Matthieu Dorier's avatar
Matthieu Dorier committed
624 625
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
626
    /* forward to provider */
Matthieu Dorier's avatar
Matthieu Dorier committed
627 628
    hret = margo_forward(handle, &in);
    if(hret != HG_SUCCESS) {
Matthieu Dorier's avatar
Matthieu Dorier committed
629 630
        ret = -1;
        goto finish;
Matthieu Dorier's avatar
Matthieu Dorier committed
631 632
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
633
    /* get the output from provider */
Matthieu Dorier's avatar
Matthieu Dorier committed
634 635
    hret = margo_get_output(handle, &out);
    if(hret != HG_SUCCESS) {
Matthieu Dorier's avatar
Matthieu Dorier committed
636 637
        ret = -1;
        goto finish;
Matthieu Dorier's avatar
Matthieu Dorier committed
638 639
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
640
    /* set return values */
Matthieu Dorier's avatar
Matthieu Dorier committed
641 642 643
    *max_keys = out.nkeys;
    ret = out.ret;

Matthieu Dorier's avatar
Matthieu Dorier committed
644 645 646 647
finish:
    /* free everything we created */
    margo_bulk_free(in.ksizes_bulk_handle);
    margo_bulk_free(in.keys_bulk_handle);
648
    margo_free_output(handle, &out);
Matthieu Dorier's avatar
Matthieu Dorier committed
649
    margo_destroy(handle);
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668

    return ret;
}

int sdskv_list_keyvals(sdskv_provider_handle_t provider,
        sdskv_database_id_t db_id,  // db instance
        const void *start_key,  // we want keys strictly after this start_key
        hg_size_t start_ksize,  // size of the start_key
        void **keys,            // pointer to an array of void* pointers,
                                //     this array has size *max_keys
        hg_size_t* ksizes,      // pointer to an array of hg_size_t sizes
                                //    representing sizes allocated in
                                //     keys for each key
        void **values,          // pointer to an array of void* pointers,
                                //     this array has size *max_keys
        hg_size_t* vsizes,      // pointer to an array of hg_size_t sizes
                                //    representing sizes allocated in
                                //    values for each value
        hg_size_t* max_keys)    // maximum number of keys requested
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
{
    return sdskv_list_keyvals_with_prefix(
            provider,
            db_id,
            start_key,
            start_ksize,
            NULL,
            0,
            keys,
            ksizes,
            values,
            vsizes,
            max_keys);
}

int sdskv_list_keyvals_with_prefix(sdskv_provider_handle_t provider,
        sdskv_database_id_t db_id,  // db instance
        const void *start_key,  // we want keys strictly after this start_key
        hg_size_t start_ksize,  // size of the start_key
        const void* prefix,     // prefix of returned keys
        hg_size_t prefix_size,  // size of prefix
        void **keys,            // pointer to an array of void* pointers,
                                //     this array has size *max_keys
        hg_size_t* ksizes,      // pointer to an array of hg_size_t sizes
                                //    representing sizes allocated in
                                //     keys for each key
        void **values,          // pointer to an array of void* pointers,
                                //     this array has size *max_keys
        hg_size_t* vsizes,      // pointer to an array of hg_size_t sizes
                                //    representing sizes allocated in
                                //    values for each value
        hg_size_t* max_keys)    // maximum number of keys requested
701 702 703 704 705 706 707 708 709
{
    list_keyvals_in_t  in;
    list_keyvals_out_t out;
    hg_return_t hret      = HG_SUCCESS;
    hg_handle_t handle    = HG_HANDLE_NULL;
    int ret = 0;
    int i;

    in.db_id = db_id;
710 711
    in.start_key.data = (kv_ptr_t) start_key;
    in.start_key.size = start_ksize;
712 713
    in.prefix.data = (char*)prefix;
    in.prefix.size = prefix_size;
714
    in.max_keys = *max_keys;
715 716 717 718
    in.keys_bulk_handle   = HG_BULK_NULL;
    in.ksizes_bulk_handle = HG_BULK_NULL;
    in.vals_bulk_handle   = HG_BULK_NULL;
    in.vsizes_bulk_handle = HG_BULK_NULL;
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 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807

    /* create bulk handle to expose the segments with key sizes */
    hg_size_t ksize_bulk_size = (*max_keys)*sizeof(*ksizes);
    void* ksizes_buf_ptr[1] = { ksizes };
    hret = margo_bulk_create(provider->client->mid,
                             1, ksizes_buf_ptr, &ksize_bulk_size,
                             HG_BULK_READWRITE,
                             &in.ksizes_bulk_handle);
    if(hret != HG_SUCCESS) {
        ret = -1;
        goto finish;
    }

    /* create bulk handle to expose the segments with value sizes */
    hg_size_t vsize_bulk_size = (*max_keys)*sizeof(*vsizes);
    void* vsizes_buf_ptr[1] = { vsizes };
    hret = margo_bulk_create(provider->client->mid,
                             1, vsizes_buf_ptr, &ksize_bulk_size,
                             HG_BULK_READWRITE,
                             &in.vsizes_bulk_handle);
    if(hret != HG_SUCCESS) {
        ret = -1;
        goto finish;
    }

    /* create bulk handle to expose where the keys should be placed */
    hret = margo_bulk_create(provider->client->mid,
                             *max_keys, keys, ksizes,
                             HG_BULK_WRITE_ONLY,
                             &in.keys_bulk_handle);
    if(hret != HG_SUCCESS) {
        ret = -1;
        goto finish;
    }

    /* create bulk handle to expose where the keys should be placed */
    hret = margo_bulk_create(provider->client->mid,
                             *max_keys, values, vsizes,
                             HG_BULK_WRITE_ONLY,
                             &in.vals_bulk_handle);
    if(hret != HG_SUCCESS) {
        ret = -1;
        goto finish;
    }

    /* create handle */
    hret = margo_create(
            provider->client->mid,
            provider->addr,
            provider->client->sdskv_list_keyvals_id,
            &handle);
    if(hret != HG_SUCCESS) {
        ret = -1;
        goto finish;
    }

    /* set target id */
    hret = margo_set_target_id(handle, provider->mplex_id);
    if(hret != HG_SUCCESS) {
        ret = -1;
        goto finish;
    }

    /* forward to provider */
    hret = margo_forward(handle, &in);
    if(hret != HG_SUCCESS) {
        ret = -1;
        goto finish;
    }

    /* get the output from provider */
    hret = margo_get_output(handle, &out);
    if(hret != HG_SUCCESS) {
        ret = -1;
        goto finish;
    }

    /* set return values */
    *max_keys = out.nkeys;
    ret = out.ret;

finish:
    /* free everything we created */
    margo_bulk_free(in.ksizes_bulk_handle);
    margo_bulk_free(in.keys_bulk_handle);
    margo_bulk_free(in.vsizes_bulk_handle);
    margo_bulk_free(in.vals_bulk_handle);
    margo_free_output(handle, &out);
    margo_destroy(handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
808 809 810

    return ret;
}
811

Matthieu Dorier's avatar
Matthieu Dorier committed
812 813 814 815
int sdskv_shutdown_service(sdskv_client_t client, hg_addr_t addr)
{
    return margo_shutdown_remote_instance(client->mid, addr);
}