sdskv-client.c 50.5 KB
Newer Older
Matthieu Dorier's avatar
Matthieu Dorier committed
1 2 3 4 5
#include "sdskv-client.h"
#include "sdskv-rpc-types.h"

#define MAX_RPC_MESSAGE_SIZE 4000 // in bytes

6 7
int32_t sdskv_remi_errno;

Matthieu Dorier's avatar
Matthieu Dorier committed
8 9
struct sdskv_client {
    margo_instance_id mid;
10 11 12 13 14
    /* opening and querying databases */
    hg_id_t sdskv_open_id;
    hg_id_t sdskv_count_databases_id;
    hg_id_t sdskv_list_databases_id;
    /* accessing database */
Matthieu Dorier's avatar
Matthieu Dorier committed
15
    hg_id_t sdskv_put_id;
Matthieu Dorier's avatar
Matthieu Dorier committed
16
    hg_id_t sdskv_put_multi_id;
Matthieu Dorier's avatar
Matthieu Dorier committed
17 18
    hg_id_t sdskv_bulk_put_id;
    hg_id_t sdskv_get_id;
Matthieu Dorier's avatar
Matthieu Dorier committed
19
    hg_id_t sdskv_get_multi_id;
20
    hg_id_t sdskv_exists_id;
Matthieu Dorier's avatar
Matthieu Dorier committed
21
    hg_id_t sdskv_erase_id;
Matthieu Dorier's avatar
Matthieu Dorier committed
22
    hg_id_t sdskv_erase_multi_id;
Matthieu Dorier's avatar
Matthieu Dorier committed
23
    hg_id_t sdskv_length_id;
Matthieu Dorier's avatar
Matthieu Dorier committed
24
    hg_id_t sdskv_length_multi_id;
Matthieu Dorier's avatar
Matthieu Dorier committed
25
    hg_id_t sdskv_bulk_get_id;
Matthieu Dorier's avatar
Matthieu Dorier committed
26
    hg_id_t sdskv_list_keys_id;
27
    hg_id_t sdskv_list_keyvals_id;
28 29 30 31 32
    /* 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;
33
    hg_id_t sdskv_migrate_database_id;
Matthieu Dorier's avatar
Matthieu Dorier committed
34 35 36 37 38 39 40

    uint64_t num_provider_handles;
};

struct sdskv_provider_handle {
    sdskv_client_t client;
    hg_addr_t      addr;
41
    uint16_t       provider_id;
Matthieu Dorier's avatar
Matthieu Dorier committed
42 43 44 45 46 47
    uint64_t       refcount;
};

static int sdskv_client_register(sdskv_client_t client, margo_instance_id mid)
{
    client->mid = mid;
48
    sdskv_remi_errno = 0;
Matthieu Dorier's avatar
Matthieu Dorier committed
49 50 51 52 53 54 55 56

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

57 58 59
        margo_registered_name(mid, "sdskv_open_rpc",                  &client->sdskv_open_id,                  &flag);
        margo_registered_name(mid, "sdskv_count_databases_rpc",       &client->sdskv_count_databases_id,       &flag);
        margo_registered_name(mid, "sdskv_list_databases_rpc",        &client->sdskv_list_databases_id,        &flag);
60
        margo_registered_name(mid, "sdskv_put_rpc",                   &client->sdskv_put_id,                   &flag);
Matthieu Dorier's avatar
Matthieu Dorier committed
61
        margo_registered_name(mid, "sdskv_put_multi_rpc",             &client->sdskv_put_multi_id,             &flag);
62 63
        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
64
        margo_registered_name(mid, "sdskv_get_multi_rpc",             &client->sdskv_get_multi_id,             &flag);
65
        margo_registered_name(mid, "sdskv_erase_rpc",                 &client->sdskv_erase_id,                 &flag);
Matthieu Dorier's avatar
Matthieu Dorier committed
66
        margo_registered_name(mid, "sdskv_erase_multi_rpc",           &client->sdskv_erase_multi_id,           &flag);
67 68
        margo_registered_name(mid, "sdskv_exists_rpc",                &client->sdskv_exists_id,                &flag);
        margo_registered_name(mid, "sdskv_length_rpc",                &client->sdskv_length_id,                &flag);
Matthieu Dorier's avatar
Matthieu Dorier committed
69
        margo_registered_name(mid, "sdskv_length_multi_rpc",          &client->sdskv_length_multi_id,          &flag);
70 71 72 73
        margo_registered_name(mid, "sdskv_bulk_get_rpc",              &client->sdskv_bulk_get_id,              &flag);
        margo_registered_name(mid, "sdskv_list_keys_rpc",             &client->sdskv_list_keys_id,             &flag);
        margo_registered_name(mid, "sdskv_list_keyvals_rpc",          &client->sdskv_list_keyvals_id,          &flag);
        margo_registered_name(mid, "sdskv_migrate_keys_rpc",          &client->sdskv_migrate_keys_id,          &flag);
Matthieu Dorier's avatar
Matthieu Dorier committed
74
        margo_registered_name(mid, "sdskv_migrate_key_range_rpc",     &client->sdskv_migrate_key_range_id,     &flag);
75 76
        margo_registered_name(mid, "sdskv_migrate_keys_prefixed_rpc", &client->sdskv_migrate_keys_prefixed_id, &flag);
        margo_registered_name(mid, "sdskv_migrate_all_keys_rpc",      &client->sdskv_migrate_all_keys_id,      &flag);
77
        margo_registered_name(mid, "sdskv_migrate_database_rpc",      &client->sdskv_migrate_database_id,      &flag);
Matthieu Dorier's avatar
Matthieu Dorier committed
78 79 80

    } else {

81 82 83 84 85 86
        client->sdskv_open_id =
            MARGO_REGISTER(mid, "sdskv_open_rpc", open_in_t, open_out_t, NULL);
        client->sdskv_count_databases_id =
            MARGO_REGISTER(mid, "sdskv_count_databases_rpc", void, count_db_out_t, NULL);
        client->sdskv_list_databases_id =
            MARGO_REGISTER(mid, "sdskv_list_databases_rpc", list_db_in_t, list_db_out_t, NULL);
Matthieu Dorier's avatar
Matthieu Dorier committed
87 88
        client->sdskv_put_id =
            MARGO_REGISTER(mid, "sdskv_put_rpc", put_in_t, put_out_t, NULL);
Matthieu Dorier's avatar
Matthieu Dorier committed
89 90
        client->sdskv_put_multi_id =
            MARGO_REGISTER(mid, "sdskv_put_multi_rpc", put_multi_in_t, put_multi_out_t, NULL);
Matthieu Dorier's avatar
Matthieu Dorier committed
91 92 93 94
        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
95 96
        client->sdskv_get_multi_id =
            MARGO_REGISTER(mid, "sdskv_get_multi_rpc", get_multi_in_t, get_multi_out_t, NULL);
Matthieu Dorier's avatar
Matthieu Dorier committed
97 98
        client->sdskv_erase_id =
            MARGO_REGISTER(mid, "sdskv_erase_rpc", erase_in_t, erase_out_t, NULL);
Matthieu Dorier's avatar
Matthieu Dorier committed
99 100
        client->sdskv_erase_multi_id =
            MARGO_REGISTER(mid, "sdskv_erase_multi_rpc", erase_multi_in_t, erase_multi_out_t, NULL);
101 102
        client->sdskv_exists_id =
            MARGO_REGISTER(mid, "sdskv_exists_rpc", exists_in_t, exists_out_t, NULL);
Matthieu Dorier's avatar
Matthieu Dorier committed
103 104
        client->sdskv_length_id =
            MARGO_REGISTER(mid, "sdskv_length_rpc", length_in_t, length_out_t, NULL);
Matthieu Dorier's avatar
Matthieu Dorier committed
105 106
        client->sdskv_length_multi_id = 
            MARGO_REGISTER(mid, "sdskv_length_multi_rpc", length_multi_in_t, length_multi_out_t, NULL);
Matthieu Dorier's avatar
Matthieu Dorier committed
107 108
        client->sdskv_bulk_get_id =
            MARGO_REGISTER(mid, "sdskv_bulk_get_rpc", bulk_get_in_t, bulk_get_out_t, NULL);
Matthieu Dorier's avatar
Matthieu Dorier committed
109
        client->sdskv_list_keys_id =
110
            MARGO_REGISTER(mid, "sdskv_list_keys_rpc", list_keys_in_t, list_keys_out_t, NULL);
111 112
        client->sdskv_list_keyvals_id =
            MARGO_REGISTER(mid, "sdskv_list_keyvals_rpc", list_keyvals_in_t, list_keyvals_out_t, NULL);
113 114 115 116 117 118 119 120
        client->sdskv_migrate_keys_id =
            MARGO_REGISTER(mid, "sdskv_migrate_keys_rpc", migrate_keys_in_t, migrate_keys_out_t, NULL);
        client->sdskv_migrate_key_range_id = 
            MARGO_REGISTER(mid, "sdskv_migrate_key_range_rpc", migrate_key_range_in_t, migrate_keys_out_t, NULL);
        client->sdskv_migrate_keys_prefixed_id = 
            MARGO_REGISTER(mid, "sdskv_migrate_keys_prefixed_rpc", migrate_keys_prefixed_in_t, migrate_keys_out_t, NULL);
        client->sdskv_migrate_all_keys_id = 
            MARGO_REGISTER(mid, "sdskv_migrate_all_keys_rpc", migrate_all_keys_in_t, migrate_keys_out_t, NULL);
121 122
        client->sdskv_migrate_database_id =
            MARGO_REGISTER(mid, "sdskv_migrate_database_rpc", migrate_database_in_t, migrate_database_out_t, NULL);
Matthieu Dorier's avatar
Matthieu Dorier committed
123 124
    }

125
    return SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
126 127 128 129 130
}

int sdskv_client_init(margo_instance_id mid, sdskv_client_t* client)
{
    sdskv_client_t c = (sdskv_client_t)calloc(1, sizeof(*c));
131
    if(!c) return SDSKV_ERR_ALLOCATION;
Matthieu Dorier's avatar
Matthieu Dorier committed
132 133 134 135 136 137 138

    c->num_provider_handles = 0;

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

    *client = c;
139
    return SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
140 141 142 143 144 145 146 147 148 149
}

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);
150
    return SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
151 152 153 154 155
}

int sdskv_provider_handle_create(
        sdskv_client_t client,
        hg_addr_t addr,
156
        uint16_t provider_id,
Matthieu Dorier's avatar
Matthieu Dorier committed
157 158
        sdskv_provider_handle_t* handle)
{
159 160
    if(client == SDSKV_CLIENT_NULL) 
        return SDSKV_ERR_INVALID_ARG;
Matthieu Dorier's avatar
Matthieu Dorier committed
161 162 163 164

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

165
    if(!provider) return SDSKV_ERR_ALLOCATION;
Matthieu Dorier's avatar
Matthieu Dorier committed
166 167 168 169

    hg_return_t ret = margo_addr_dup(client->mid, addr, &(provider->addr));
    if(ret != HG_SUCCESS) {
        free(provider);
170
        return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
171 172
    }

173 174 175
    provider->client      = client;
    provider->provider_id = provider_id;
    provider->refcount    = 1;
Matthieu Dorier's avatar
Matthieu Dorier committed
176 177 178 179

    client->num_provider_handles += 1;

    *handle = provider;
180
    return SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
181 182 183 184 185
}

int sdskv_provider_handle_ref_incr(
        sdskv_provider_handle_t handle)
{
186
    if(handle == SDSKV_PROVIDER_HANDLE_NULL) return SDSKV_ERR_INVALID_ARG;
Matthieu Dorier's avatar
Matthieu Dorier committed
187
    handle->refcount += 1;
188
    return SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
189 190
}

Matthieu Dorier's avatar
Matthieu Dorier committed
191
int sdskv_provider_handle_release(sdskv_provider_handle_t handle)
Matthieu Dorier's avatar
Matthieu Dorier committed
192 193 194 195 196 197 198 199
{
    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);
    }
200
    return SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
201 202 203 204 205 206 207
}

int sdskv_open(
        sdskv_provider_handle_t provider,
        const char* db_name,
        sdskv_database_id_t* db_id)
{
208
    hg_return_t hret;
Matthieu Dorier's avatar
Matthieu Dorier committed
209 210 211 212 213 214 215 216 217 218 219
    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);
220
    if(hret != HG_SUCCESS) return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
221

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

224
    hret = margo_provider_forward(provider->provider_id, handle, &in);
Matthieu Dorier's avatar
Matthieu Dorier committed
225 226
    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
227
        return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
228 229 230 231 232
    }

    hret = margo_get_output(handle, &out);
    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
233
        return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
234 235 236 237 238 239
    }

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

    margo_free_output(handle, &out);
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
    margo_destroy(handle);

    return ret;
}

int sdskv_count_databases(
        sdskv_provider_handle_t provider,
        size_t* num)
{
    hg_return_t hret;
    int ret;
    count_db_out_t out;
    hg_handle_t handle;

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

    hret = margo_provider_forward(provider->provider_id, handle, NULL);
    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
        return SDSKV_ERR_MERCURY;
    }

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

    ret = out.ret;
    *num = out.count;

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

    return ret;
}

int sdskv_list_databases(
        sdskv_provider_handle_t provider,
        size_t* count,
        char** db_names,
        sdskv_database_id_t* db_ids)
{
    hg_return_t hret;
    int ret;
    list_db_in_t in;
    in.count = *count;
    list_db_out_t out;
    hg_handle_t handle;

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

    hret = margo_provider_forward(provider->provider_id, handle, &in);
    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
        return SDSKV_ERR_MERCURY;
    }

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

    ret = out.ret;
    if(ret == SDSKV_SUCCESS) {
        *count = out.count;
        unsigned i;
        for(i = 0; i < out.count; i++) {
            if(db_names != NULL) db_names[i] = strdup(out.db_names[i]);
            if(db_ids != NULL)   db_ids[i]   = out.db_ids[i];
        }
    } else {
        *count = 0;
    }

    margo_free_output(handle, &out);
Matthieu Dorier's avatar
Matthieu Dorier committed
329 330 331 332 333 334 335 336 337 338 339
    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;
Matthieu Dorier's avatar
Matthieu Dorier committed
340
    int ret = SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
341 342 343 344 345 346 347 348 349 350
    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;
351 352 353 354
        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
355 356 357 358 359 360 361

        /* create handle */
        hret = margo_create(
                provider->client->mid,
                provider->addr,
                provider->client->sdskv_put_id,
                &handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
362 363
        if(hret != HG_SUCCESS) {
            fprintf(stderr,"[SDSKV] margo_create() failed in sdskv_put()\n");
364
            return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
365
        }
Matthieu Dorier's avatar
Matthieu Dorier committed
366

367
        hret = margo_provider_forward(provider->provider_id, handle, &in);
Matthieu Dorier's avatar
Matthieu Dorier committed
368
        if(hret != HG_SUCCESS) {
Matthieu Dorier's avatar
Matthieu Dorier committed
369
            fprintf(stderr,"[SDSKV] margo_forward() failed in sdskv_put()\n");
Matthieu Dorier's avatar
Matthieu Dorier committed
370
            margo_destroy(handle);
371
            return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
372 373 374 375
        }

        hret = margo_get_output(handle, &out);
        if(hret != HG_SUCCESS) {
Matthieu Dorier's avatar
Matthieu Dorier committed
376
            fprintf(stderr,"[SDSKV] margo_get_output() failed in sdskv_put()\n");
Matthieu Dorier's avatar
Matthieu Dorier committed
377
            margo_destroy(handle);
378
            return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
379 380 381 382 383 384 385 386 387 388 389
        }

        ret = out.ret;

        margo_free_output(handle, &out);

    } else {

        bulk_put_in_t in;
        bulk_put_out_t out;

390 391 392 393
        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
394

395 396
        hret = margo_bulk_create(provider->client->mid, 1, (void**)(&value), &in.vsize,
                                HG_BULK_READ_ONLY, &in.handle);
397
        if(hret != HG_SUCCESS) {
Matthieu Dorier's avatar
Matthieu Dorier committed
398
            fprintf(stderr,"[SDSKV] margo_bulk_create() failed in sdskv_put()\n");
399
            return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
400
        }
Matthieu Dorier's avatar
Matthieu Dorier committed
401 402 403 404 405 406 407 408

        /* 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
409
            fprintf(stderr,"[SDSKV] margo_create() failed in sdskv_put()\n");
410
            margo_bulk_free(in.handle);
411
            return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
412 413
        }

414
        hret = margo_provider_forward(provider->provider_id, handle, &in);
Matthieu Dorier's avatar
Matthieu Dorier committed
415
        if(hret != HG_SUCCESS) {
Matthieu Dorier's avatar
Matthieu Dorier committed
416
            fprintf(stderr,"[SDSKV] margo_forward() failed in sdskv_put()\n");
417
            margo_bulk_free(in.handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
418
            margo_destroy(handle);
419
            return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
420 421 422 423
        }

        hret = margo_get_output(handle, &out);
        if(hret != HG_SUCCESS) {
Matthieu Dorier's avatar
Matthieu Dorier committed
424
            fprintf(stderr,"[SDSKV] margo_get_output() failed in sdskv_put()\n");
425
            margo_bulk_free(in.handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
426
            margo_destroy(handle);
427
            return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
428 429 430 431
        }

        ret = out.ret;
        margo_free_output(handle, &out);
432
        margo_bulk_free(in.handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
433 434 435
    }

    margo_destroy(handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
436 437 438 439 440
    return ret;
}

int sdskv_put_multi(sdskv_provider_handle_t provider, 
        sdskv_database_id_t db_id,
Matthieu Dorier's avatar
Matthieu Dorier committed
441 442
        size_t num, const void* const* keys, const hg_size_t* ksizes,
        const void* const* values, const hg_size_t *vsizes)
Matthieu Dorier's avatar
Matthieu Dorier committed
443 444 445 446 447 448 449 450 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 486 487 488 489 490 491 492 493 494 495 496 497 498 499 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 534 535 536 537 538 539 540
{
    hg_return_t     hret;
    int             ret;
    hg_handle_t     handle = HG_HANDLE_NULL;
    put_multi_in_t  in;
    put_multi_out_t out;
    void**          key_seg_ptrs  = NULL;
    hg_size_t*      key_seg_sizes = NULL;
    void**          val_seg_ptrs  = NULL;
    hg_size_t*      val_seg_sizes = NULL;

    in.db_id    = db_id;
    in.num_keys = num;
    in.keys_bulk_handle = HG_BULK_NULL;
    in.keys_bulk_size   = 0;
    in.vals_bulk_handle = HG_BULK_NULL;
    in.vals_bulk_size   = 0;

    /* create an array of key sizes and key pointers */
    key_seg_sizes       = malloc(sizeof(hg_size_t)*(num+1));
    key_seg_sizes[0]    = num*sizeof(hg_size_t);
    memcpy(key_seg_sizes+1, ksizes, num*sizeof(hg_size_t));
    key_seg_ptrs        = malloc(sizeof(void*)*(num+1));
    key_seg_ptrs[0]     = (void*)ksizes;
    memcpy(key_seg_ptrs+1, keys, num*sizeof(void*));
    int i;
    for(i=0; i < num+1; i++) {
        in.keys_bulk_size += key_seg_sizes[i];
    }
    /* create an array of val sizes and val pointers */
    val_seg_sizes       = malloc(sizeof(hg_size_t)*(num+1));
    val_seg_sizes[0]    = num*sizeof(hg_size_t);
    memcpy(val_seg_sizes+1, vsizes, num*sizeof(hg_size_t));
    val_seg_ptrs        = malloc(sizeof(void*)*(num+1));
    val_seg_ptrs[0]     = (void*)vsizes;
    memcpy(val_seg_ptrs+1, values, num*sizeof(void*));
    for(i=0; i < num+1; i++) {
        in.vals_bulk_size += val_seg_sizes[i];
    }

    /* create the bulk handle to access the keys */
    hret = margo_bulk_create(provider->client->mid, num+1, key_seg_ptrs, key_seg_sizes,
            HG_BULK_READ_ONLY, &in.keys_bulk_handle);
    if(hret != HG_SUCCESS) {
        fprintf(stderr,"[SDSKV] margo_bulk_create() failed in sdskv_put_multi()\n");
        out.ret = SDSKV_ERR_MERCURY;
        goto finish;
    }

    /* create the bulk handle to access the values */
    hret = margo_bulk_create(provider->client->mid, num+1, val_seg_ptrs, val_seg_sizes,
            HG_BULK_READ_ONLY, &in.vals_bulk_handle);
    if(hret != HG_SUCCESS) {
        fprintf(stderr,"[SDSKV] margo_bulk_create() failed in sdskv_put_multi()\n");
        out.ret = SDSKV_ERR_MERCURY;
        goto finish;
    }

    /* create a RPC handle */
    hret = margo_create(
            provider->client->mid,
            provider->addr,
            provider->client->sdskv_put_multi_id,
            &handle);
    if(hret != HG_SUCCESS) {
        fprintf(stderr,"[SDSKV] margo_create() failed in sdskv_put_multi()\n");
        out.ret = SDSKV_ERR_MERCURY;
        goto finish;
    }

    /* forward the RPC */
    hret = margo_provider_forward(provider->provider_id, handle, &in);
    if(hret != HG_SUCCESS) {
        fprintf(stderr,"[SDSKV] margo_forward() failed in sdskv_put_multi()\n");
        out.ret = SDSKV_ERR_MERCURY;
        goto finish;
    }

    /* get the output */
    hret = margo_get_output(handle, &out);
    if(hret != HG_SUCCESS) {
        fprintf(stderr,"[SDSKV] margo_get_output() failed in sdskv_put_multi()\n");
        out.ret = SDSKV_ERR_MERCURY;
        goto finish;
    }

    ret = out.ret;

finish:
    margo_free_output(handle, &out);
    margo_bulk_free(in.keys_bulk_handle);
    margo_bulk_free(in.vals_bulk_handle);
    free(key_seg_sizes);
    free(key_seg_ptrs);
    free(val_seg_sizes);
    free(val_seg_ptrs);
    margo_destroy(handle);
    return ret;
Matthieu Dorier's avatar
Matthieu Dorier committed
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
}

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;
563 564
        in.key.data = (kv_ptr_t)key;
        in.key.size = ksize;
Matthieu Dorier's avatar
Matthieu Dorier committed
565 566 567 568 569 570 571 572
        in.vsize = size;

        /* create handle */
        hret = margo_create(
                provider->client->mid,
                provider->addr,
                provider->client->sdskv_get_id,
                &handle);
573
        if(hret != HG_SUCCESS) return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
574

575
        hret = margo_provider_forward(provider->provider_id, handle, &in);
Matthieu Dorier's avatar
Matthieu Dorier committed
576 577
        if(hret != HG_SUCCESS) {
            margo_destroy(handle);
578
            return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
579 580 581 582 583
        }

        hret = margo_get_output(handle, &out);
        if(hret != HG_SUCCESS) {
            margo_destroy(handle);
584
            return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
585 586 587
        }

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

590 591
        if (out.value.size > 0) {
            memcpy(value, out.value.data, out.value.size);
Matthieu Dorier's avatar
Matthieu Dorier committed
592 593 594 595 596 597 598 599 600
        }

        margo_free_output(handle, &out);

    } else {

        bulk_get_in_t in;
        bulk_get_out_t out;

601 602 603 604
        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
605

606 607
        hret = margo_bulk_create(provider->client->mid, 1, &value, &in.vsize,
                                HG_BULK_WRITE_ONLY, &in.handle);
608
        if(hret != HG_SUCCESS) return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
609 610 611 612 613 614 615 616

        /* create handle */
        hret = margo_create(
                provider->client->mid,
                provider->addr,
                provider->client->sdskv_bulk_get_id,
                &handle);
        if(hret != HG_SUCCESS) {
617
            margo_bulk_free(in.handle);
618
            return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
619 620
        }

621
        hret = margo_provider_forward(provider->provider_id, handle, &in);
Matthieu Dorier's avatar
Matthieu Dorier committed
622
        if(hret != HG_SUCCESS) {
623
            margo_bulk_free(in.handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
624
            margo_destroy(handle);
625
            return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
626 627 628 629
        }

        hret = margo_get_output(handle, &out);
        if(hret != HG_SUCCESS) {
630
            margo_bulk_free(in.handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
631
            margo_destroy(handle);
632
            return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
633 634 635 636 637 638
        }

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

        margo_free_output(handle, &out);
639
        margo_bulk_free(in.handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
640 641 642 643 644 645
    }

    margo_destroy(handle);
    return ret;
}

Matthieu Dorier's avatar
Matthieu Dorier committed
646 647
int sdskv_get_multi(sdskv_provider_handle_t provider, 
        sdskv_database_id_t db_id,
Matthieu Dorier's avatar
Matthieu Dorier committed
648
        size_t num, const void* const* keys, const hg_size_t* ksizes,
Matthieu Dorier's avatar
Matthieu Dorier committed
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
        void** values, hg_size_t *vsizes)
{
    hg_return_t     hret;
    int             ret;
    hg_handle_t     handle = HG_HANDLE_NULL;
    get_multi_in_t  in;
    get_multi_out_t out;
    void**          key_seg_ptrs  = NULL;
    hg_size_t*      key_seg_sizes = NULL;
    char*           vals_buffer   = NULL;

    in.db_id    = db_id;
    in.num_keys = num;
    in.keys_bulk_handle = HG_BULK_NULL;
    in.keys_bulk_size   = 0;
    in.vals_bulk_handle = HG_BULK_NULL;
    in.vals_bulk_size   = 0;

    /* create an array of key sizes and key pointers */
    key_seg_sizes       = malloc(sizeof(hg_size_t)*(num+1));
    key_seg_sizes[0]    = num*sizeof(hg_size_t);
    memcpy(key_seg_sizes+1, ksizes, num*sizeof(hg_size_t));
    key_seg_ptrs        = malloc(sizeof(void*)*(num+1));
    key_seg_ptrs[0]     = (void*)ksizes;
    memcpy(key_seg_ptrs+1, keys, num*sizeof(void*));
    
    int i;
    for(i=0; i<num+1; i++) {
        in.keys_bulk_size += key_seg_sizes[i];
    }

    /* create the bulk handle to access the keys */
    hret = margo_bulk_create(provider->client->mid, num+1, key_seg_ptrs, key_seg_sizes,
            HG_BULK_READ_ONLY, &in.keys_bulk_handle);
    if(hret != HG_SUCCESS) {
        fprintf(stderr,"[SDSKV] margo_bulk_create() failed in sdskv_get_multi()\n");
        out.ret = SDSKV_ERR_MERCURY;
        goto finish;
    }

    /* allocate memory to send max value sizes and receive values */
    for(i=0; i<num; i++) {
        in.vals_bulk_size += vsizes[i];
    }
    in.vals_bulk_size += sizeof(hg_size_t)*num;
    vals_buffer = malloc(in.vals_bulk_size);
    hg_size_t* value_sizes = (hg_size_t*)vals_buffer; // beginning of the buffer used to hold sizes
    for(i=0; i<num; i++) {
        value_sizes[i] = vsizes[i];
    }

    /* create the bulk handle to access the values */
    hret = margo_bulk_create(provider->client->mid, num, (void**)&vals_buffer, &in.vals_bulk_size,
            HG_BULK_READWRITE, &in.vals_bulk_handle);
    if(hret != HG_SUCCESS) {
        fprintf(stderr,"[SDSKV] margo_bulk_create() failed in sdskv_get_multi()\n");
        out.ret = SDSKV_ERR_MERCURY;
        goto finish;
    }

    /* create a RPC handle */
    hret = margo_create(
            provider->client->mid,
            provider->addr,
            provider->client->sdskv_get_multi_id,
            &handle);
    if(hret != HG_SUCCESS) {
        fprintf(stderr,"[SDSKV] margo_create() failed in sdskv_get_multi()\n");
        out.ret = SDSKV_ERR_MERCURY;
        goto finish;
    }

    /* forward the RPC handle */
    hret = margo_provider_forward(provider->provider_id, handle, &in);
    if(hret != HG_SUCCESS) {
        fprintf(stderr,"[SDSKV] margo_forward() failed in sdskv_get_multi()\n");
        out.ret = SDSKV_ERR_MERCURY;
        goto finish;
    }

    /* get the response */
    hret = margo_get_output(handle, &out);
    if(hret != HG_SUCCESS) {
        fprintf(stderr,"[SDSKV] margo_get_output() failed in sdskv_put_multi()\n");
        out.ret = SDSKV_ERR_MERCURY;
        goto finish;
    }

    ret = out.ret;
    if(out.ret != SDSKV_SUCCESS) {
        goto finish;
    }

    /* copy the values from the buffer into the user-provided buffer */
    char* value_ptr = vals_buffer + num*sizeof(hg_size_t);
    for(i=0; i<num; i++) {
        memcpy(values[i], value_ptr, value_sizes[i]);
        vsizes[i] = value_sizes[i];
        value_ptr += value_sizes[i];
    }

finish:
    margo_free_output(handle, &out);
    margo_bulk_free(in.keys_bulk_handle);
    margo_bulk_free(in.vals_bulk_handle);
    free(key_seg_sizes);
    free(key_seg_ptrs);
    free(vals_buffer);
    margo_destroy(handle);
    return ret;
}

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
int sdskv_exists(sdskv_provider_handle_t provider,
        sdskv_database_id_t db_id, const void *key,
        hg_size_t ksize, int* flag)
{
    hg_return_t hret;
    int ret;
    hg_handle_t handle;

    exists_in_t in;
    exists_out_t out;

    in.db_id    = db_id;
    in.key.data = (kv_ptr_t)key;
    in.key.size = ksize;

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

    hret = margo_provider_forward(provider->provider_id, handle, &in);
    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
        return SDSKV_ERR_MERCURY;
    }

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

    ret = out.ret;
    if(ret == 0) *flag = out.flag;

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

Matthieu Dorier's avatar
Matthieu Dorier committed
804 805 806 807 808 809 810 811 812 813 814
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;

815 816
    in.db_id    = db_id;
    in.key.data = (kv_ptr_t)key;
817
    in.key.size = ksize;
Matthieu Dorier's avatar
Matthieu Dorier committed
818 819 820 821 822 823 824

    /* create handle */
    hret = margo_create(
            provider->client->mid,
            provider->addr,
            provider->client->sdskv_length_id,
            &handle);
825
    if(hret != HG_SUCCESS) return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
826

827
    hret = margo_provider_forward(provider->provider_id, handle, &in);
Matthieu Dorier's avatar
Matthieu Dorier committed
828 829
    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
830
        return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
831 832 833 834 835
    }

    hret = margo_get_output(handle, &out);
    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
836
        return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
837 838 839 840 841 842 843 844 845 846
    }

    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
847 848
int sdskv_length_multi(sdskv_provider_handle_t provider, 
        sdskv_database_id_t db_id, size_t num, 
Matthieu Dorier's avatar
Matthieu Dorier committed
849
        const void* const* keys, const hg_size_t* ksizes, hg_size_t *vsizes)
Matthieu Dorier's avatar
Matthieu Dorier committed
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 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 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
{
    hg_return_t     hret;
    int             ret;
    hg_handle_t     handle = HG_HANDLE_NULL;
    length_multi_in_t  in;
    length_multi_out_t out;
    void**          key_seg_ptrs  = NULL;
    hg_size_t*      key_seg_sizes = NULL;

    in.db_id    = db_id;
    in.num_keys = num;
    in.keys_bulk_handle = HG_BULK_NULL;
    in.keys_bulk_size   = 0;
    in.vals_size_bulk_handle = HG_BULK_NULL;

    /* create an array of key sizes and key pointers */
    key_seg_sizes       = malloc(sizeof(hg_size_t)*(num+1));
    key_seg_sizes[0]    = num*sizeof(hg_size_t);
    memcpy(key_seg_sizes+1, ksizes, num*sizeof(hg_size_t));
    key_seg_ptrs        = malloc(sizeof(void*)*(num+1));
    key_seg_ptrs[0]     = (void*)ksizes;
    memcpy(key_seg_ptrs+1, keys, num*sizeof(void*));

    int i;
    for(i=0; i<num+1; i++) {
        in.keys_bulk_size += key_seg_sizes[i];
    }

    /* create the bulk handle to access the keys */
    hret = margo_bulk_create(provider->client->mid, num+1, key_seg_ptrs, key_seg_sizes,
            HG_BULK_READ_ONLY, &in.keys_bulk_handle);
    if(hret != HG_SUCCESS) {
        fprintf(stderr,"[SDSKV] margo_bulk_create() failed in sdskv_get_multi()\n");
        out.ret = SDSKV_ERR_MERCURY;
        goto finish;
    }

    /* create the bulk handle for the server to put the values sizes */
    hg_size_t vals_size_bulk_size = num*sizeof(hg_size_t);
    hret = margo_bulk_create(provider->client->mid, num, (void**)&vsizes, &vals_size_bulk_size,
            HG_BULK_WRITE_ONLY, &in.vals_size_bulk_handle);
    if(hret != HG_SUCCESS) {
        fprintf(stderr,"[SDSKV] margo_bulk_create() failed in sdskv_get_multi()\n");
        out.ret = SDSKV_ERR_MERCURY;
        goto finish;
    }

    /* create a RPC handle */
    hret = margo_create(
            provider->client->mid,
            provider->addr,
            provider->client->sdskv_length_multi_id,
            &handle);
    if(hret != HG_SUCCESS) {
        fprintf(stderr,"[SDSKV] margo_create() failed in sdskv_get_multi()\n");
        out.ret = SDSKV_ERR_MERCURY;
        goto finish;
    }

    /* forward the RPC handle */
    hret = margo_provider_forward(provider->provider_id, handle, &in);
    if(hret != HG_SUCCESS) {
        fprintf(stderr,"[SDSKV] margo_forward() failed in sdskv_get_multi()\n");
        out.ret = SDSKV_ERR_MERCURY;
        goto finish;
    }

    /* get the response */
    hret = margo_get_output(handle, &out);
    if(hret != HG_SUCCESS) {
        fprintf(stderr,"[SDSKV] margo_get_output() failed in sdskv_put_multi()\n");
        out.ret = SDSKV_ERR_MERCURY;
        goto finish;
    }

    ret = out.ret;
    if(out.ret != SDSKV_SUCCESS) {
        goto finish;
    }

finish:
    margo_free_output(handle, &out);
    margo_bulk_free(in.keys_bulk_handle);
    margo_bulk_free(in.vals_size_bulk_handle);
    free(key_seg_sizes);
    free(key_seg_ptrs);
    margo_destroy(handle);
    return ret;
}

Matthieu Dorier's avatar
Matthieu Dorier committed
940 941 942 943 944 945 946 947 948 949 950 951
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;
952 953
    in.key.data   = (kv_ptr_t)key;
    in.key.size = ksize;
Matthieu Dorier's avatar
Matthieu Dorier committed
954 955 956 957 958 959 960

    /* create handle */
    hret = margo_create(
            provider->client->mid,
            provider->addr,
            provider->client->sdskv_erase_id,
            &handle);
961
    if(hret != HG_SUCCESS) return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
962

963
    hret = margo_provider_forward(provider->provider_id, handle, &in);
Matthieu Dorier's avatar
Matthieu Dorier committed
964 965
    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
966
        return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
967 968 969 970 971
    }

    hret = margo_get_output(handle, &out);
    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
972
        return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
973 974 975 976 977 978 979 980 981
    }

    ret = out.ret;

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

Matthieu Dorier's avatar
Matthieu Dorier committed
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
int sdskv_erase_multi(sdskv_provider_handle_t provider,
        sdskv_database_id_t db_id, size_t num,
        const void* const* keys,
        const hg_size_t* ksizes)
{
    hg_return_t     hret;
    int             ret;
    hg_handle_t     handle = HG_HANDLE_NULL;
    erase_multi_in_t  in;
    erase_multi_out_t out;
    void**          key_seg_ptrs  = NULL;
    hg_size_t*      key_seg_sizes = NULL;

    in.db_id    = db_id;
    in.num_keys = num;
    in.keys_bulk_handle = HG_BULK_NULL;
    in.keys_bulk_size   = 0;

    /* create an array of key sizes and key pointers */
    key_seg_sizes       = malloc(sizeof(hg_size_t)*(num+1));
    key_seg_sizes[0]    = num*sizeof(hg_size_t);
    memcpy(key_seg_sizes+1, ksizes, num*sizeof(hg_size_t));
    key_seg_ptrs        = malloc(sizeof(void*)*(num+1));
    key_seg_ptrs[0]     = (void*)ksizes;
    memcpy(key_seg_ptrs+1, keys, num*sizeof(void*));

    int i;
    for(i=0; i<num+1; i++) {
        in.keys_bulk_size += key_seg_sizes[i];
    }

    /* create the bulk handle to access the keys */
    hret = margo_bulk_create(provider->client->mid, num+1, key_seg_ptrs, key_seg_sizes,
            HG_BULK_READ_ONLY, &in.keys_bulk_handle);
    if(hret != HG_SUCCESS) {
        fprintf(stderr,"[SDSKV] margo_bulk_create() failed in sdskv_erase_multi()\n");
        out.ret = SDSKV_ERR_MERCURY;
        goto finish;
    }

    /* create a RPC handle */
    hret = margo_create(
            provider->client->mid,
            provider->addr,
            provider->client->sdskv_erase_multi_id,
            &handle);
    if(hret != HG_SUCCESS) {
        fprintf(stderr,"[SDSKV] margo_create() failed in sdskv_erase_multi()\n");
        out.ret = SDSKV_ERR_MERCURY;
        goto finish;
    }

    /* forward the RPC handle */
    hret = margo_provider_forward(provider->provider_id, handle, &in);
    if(hret != HG_SUCCESS) {
        fprintf(stderr,"[SDSKV] margo_forward() failed in sdskv_erase_multi()\n");
        out.ret = SDSKV_ERR_MERCURY;
        goto finish;
    }

    /* get the response */
    hret = margo_get_output(handle, &out);
    if(hret != HG_SUCCESS) {
        fprintf(stderr,"[SDSKV] margo_get_output() failed in sdskv_erase_multi()\n");
        out.ret = SDSKV_ERR_MERCURY;
        goto finish;
    }

    ret = out.ret;
    if(out.ret != SDSKV_SUCCESS) {
        goto finish;
    }

finish:
    margo_free_output(handle, &out);
    margo_bulk_free(in.keys_bulk_handle);
    free(key_seg_sizes);
    free(key_seg_ptrs);
    margo_destroy(handle);
    return ret;
}

Matthieu Dorier's avatar
Matthieu Dorier committed
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
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
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
{
    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
1098
{
1099 1100
    list_keys_in_t  in;
    list_keys_out_t out;
Matthieu Dorier's avatar
Matthieu Dorier committed
1101 1102
    hg_return_t hret      = HG_SUCCESS;
    hg_handle_t handle    = HG_HANDLE_NULL;
1103
    int ret = SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
1104 1105 1106
    int i;

    in.db_id = db_id;
1107 1108
    in.start_key.data = (kv_ptr_t) start_key;
    in.start_key.size = start_ksize;
1109 1110 1111 1112
    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
1113 1114
    in.max_keys = *max_keys;

Matthieu Dorier's avatar
Matthieu Dorier committed
1115 1116 1117 1118 1119 1120 1121 1122
    /* 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) {
1123
        ret = SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
1124 1125 1126 1127 1128 1129 1130 1131 1132
        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) {
1133
        ret = SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
1134 1135 1136 1137
        goto finish;
    }


Matthieu Dorier's avatar
Matthieu Dorier committed
1138 1139 1140 1141
    /* create handle */
    hret = margo_create(
            provider->client->mid,
            provider->addr,
Matthieu Dorier's avatar
Matthieu Dorier committed
1142
            provider->client->sdskv_list_keys_id,
Matthieu Dorier's avatar
Matthieu Dorier committed
1143
            &handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
1144
    if(hret != HG_SUCCESS) {
1145
        ret = SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
1146 1147
        goto finish;
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
1148

Matthieu Dorier's avatar
Matthieu Dorier committed
1149
    /* forward to provider */
1150
    hret = margo_provider_forward(provider->provider_id, handle, &in);
Matthieu Dorier's avatar
Matthieu Dorier committed
1151
    if(hret != HG_SUCCESS) {
1152
        ret = SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
1153
        goto finish;
Matthieu Dorier's avatar
Matthieu Dorier committed
1154 1155
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
1156
    /* get the output from provider */
Matthieu Dorier's avatar
Matthieu Dorier committed
1157 1158
    hret = margo_get_output(handle, &out);
    if(hret != HG_SUCCESS) {
1159
        ret = SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
1160
        goto finish;
Matthieu Dorier's avatar
Matthieu Dorier committed
1161 1162
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
1163
    /* set return values */
Matthieu Dorier's avatar
Matthieu Dorier committed
1164 1165 1166
    *max_keys = out.nkeys;
    ret = out.ret;

Matthieu Dorier's avatar
Matthieu Dorier committed
1167 1168 1169 1170
finish:
    /* free everything we created */
    margo_bulk_free(in.ksizes_bulk_handle);
    margo_bulk_free(in.keys_bulk_handle);
1171
    margo_free_output(handle, &out);
Matthieu Dorier's avatar
Matthieu Dorier committed
1172
    margo_destroy(handle);
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191

    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
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
{
    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
1224 1225 1226 1227 1228
{
    list_keyvals_in_t  in;
    list_keyvals_out_t out;
    hg_return_t hret      = HG_SUCCESS;
    hg_handle_t handle    = HG_HANDLE_NULL;
1229
    int ret = SDSKV_SUCCESS;
1230 1231 1232
    int i;

    in.db_id = db_id;
1233 1234
    in.start_key.data = (kv_ptr_t) start_key;
    in.start_key.size = start_ksize;
1235 1236
    in.prefix.data = (char*)prefix;
    in.prefix.size = prefix_size;
1237
    in.max_keys = *max_keys;
1238 1239 1240 1241
    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;
1242 1243 1244 1245 1246 1247 1248 1249 1250

    /* 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) {
1251
        ret = SDSKV_ERR_MERCURY;
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
        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) {
1263
        ret = SDSKV_ERR_MERCURY;
1264 1265 1266 1267 1268 1269 1270 1271 1272
        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) {
1273
        ret = SDSKV_ERR_MERCURY;
1274 1275 1276 1277