sdskv-client.c 52.3 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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

int sdskv_provider_handle_get_info(
        sdskv_provider_handle_t ph,
        sdskv_client_t* client,
        hg_addr_t* addr,
        uint16_t* provider_id) {
    if(ph) {
        if(client) *client = ph->client;
        if(addr) *addr = ph->addr;
        if(provider_id) *provider_id = ph->provider_id;
    } else {
        if(client) *client = SDSKV_CLIENT_NULL;
        if(addr) *addr = HG_ADDR_NULL;
        if(provider_id) *provider_id = 0;
    }
    return SDSKV_SUCCESS;
}

Matthieu Dorier's avatar
Matthieu Dorier committed
201 202 203
int sdskv_provider_handle_ref_incr(
        sdskv_provider_handle_t handle)
{
204
    if(handle == SDSKV_PROVIDER_HANDLE_NULL) return SDSKV_ERR_INVALID_ARG;
Matthieu Dorier's avatar
Matthieu Dorier committed
205
    handle->refcount += 1;
206
    return SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
207 208
}

Matthieu Dorier's avatar
Matthieu Dorier committed
209
int sdskv_provider_handle_release(sdskv_provider_handle_t handle)
Matthieu Dorier's avatar
Matthieu Dorier committed
210 211 212 213 214 215 216 217
{
    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);
    }
218
    return SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
219 220 221 222 223 224 225
}

int sdskv_open(
        sdskv_provider_handle_t provider,
        const char* db_name,
        sdskv_database_id_t* db_id)
{
226
    hg_return_t hret;
Matthieu Dorier's avatar
Matthieu Dorier committed
227 228 229 230 231 232 233 234 235 236 237
    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);
238
    if(hret != HG_SUCCESS) return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
239

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

242
    hret = margo_provider_forward(provider->provider_id, handle, &in);
Matthieu Dorier's avatar
Matthieu Dorier committed
243 244
    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
245
        return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
246 247 248 249 250
    }

    hret = margo_get_output(handle, &out);
    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
251
        return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
252 253 254 255 256 257
    }

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

    margo_free_output(handle, &out);
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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
    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
347 348 349 350 351 352 353 354 355 356 357
    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
358
    int ret = SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
359 360 361 362 363 364 365 366 367 368
    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;
369 370 371 372
        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
373 374 375 376 377 378 379

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

385
        hret = margo_provider_forward(provider->provider_id, handle, &in);
Matthieu Dorier's avatar
Matthieu Dorier committed
386
        if(hret != HG_SUCCESS) {
Matthieu Dorier's avatar
Matthieu Dorier committed
387
            fprintf(stderr,"[SDSKV] margo_forward() failed in sdskv_put()\n");
Matthieu Dorier's avatar
Matthieu Dorier committed
388
            margo_destroy(handle);
389
            return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
390 391 392 393
        }

        hret = margo_get_output(handle, &out);
        if(hret != HG_SUCCESS) {
Matthieu Dorier's avatar
Matthieu Dorier committed
394
            fprintf(stderr,"[SDSKV] margo_get_output() failed in sdskv_put()\n");
Matthieu Dorier's avatar
Matthieu Dorier committed
395
            margo_destroy(handle);
396
            return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
397 398 399 400 401 402 403 404 405 406 407
        }

        ret = out.ret;

        margo_free_output(handle, &out);

    } else {

        bulk_put_in_t in;
        bulk_put_out_t out;

408 409 410 411
        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
412

413 414
        hret = margo_bulk_create(provider->client->mid, 1, (void**)(&value), &in.vsize,
                                HG_BULK_READ_ONLY, &in.handle);
415
        if(hret != HG_SUCCESS) {
Matthieu Dorier's avatar
Matthieu Dorier committed
416
            fprintf(stderr,"[SDSKV] margo_bulk_create() failed in sdskv_put()\n");
417
            return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
418
        }
Matthieu Dorier's avatar
Matthieu Dorier committed
419 420 421 422 423 424 425 426

        /* 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
427
            fprintf(stderr,"[SDSKV] margo_create() failed in sdskv_put()\n");
428
            margo_bulk_free(in.handle);
429
            return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
430 431
        }

432
        hret = margo_provider_forward(provider->provider_id, handle, &in);
Matthieu Dorier's avatar
Matthieu Dorier committed
433
        if(hret != HG_SUCCESS) {
Matthieu Dorier's avatar
Matthieu Dorier committed
434
            fprintf(stderr,"[SDSKV] margo_forward() failed in sdskv_put()\n");
435
            margo_bulk_free(in.handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
436
            margo_destroy(handle);
437
            return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
438 439 440 441
        }

        hret = margo_get_output(handle, &out);
        if(hret != HG_SUCCESS) {
Matthieu Dorier's avatar
Matthieu Dorier committed
442
            fprintf(stderr,"[SDSKV] margo_get_output() failed in sdskv_put()\n");
443
            margo_bulk_free(in.handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
444
            margo_destroy(handle);
445
            return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
446 447 448 449
        }

        ret = out.ret;
        margo_free_output(handle, &out);
450
        margo_bulk_free(in.handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
451 452 453
    }

    margo_destroy(handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
454 455 456 457 458
    return ret;
}

int sdskv_put_multi(sdskv_provider_handle_t provider, 
        sdskv_database_id_t db_id,
Matthieu Dorier's avatar
Matthieu Dorier committed
459 460
        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
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 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
{
    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
559 560 561 562 563 564 565 566 567 568 569 570 571
}

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;

572 573 574 575
    if(value == NULL) {
        return sdskv_length(provider, db_id, key, ksize, vsize);
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
576 577 578 579 580 581 582 583 584
    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;
585 586
        in.key.data = (kv_ptr_t)key;
        in.key.size = ksize;
Matthieu Dorier's avatar
Matthieu Dorier committed
587 588 589 590 591 592 593 594
        in.vsize = size;

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

597
        hret = margo_provider_forward(provider->provider_id, handle, &in);
Matthieu Dorier's avatar
Matthieu Dorier committed
598 599
        if(hret != HG_SUCCESS) {
            margo_destroy(handle);
600
            return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
601 602 603 604 605
        }

        hret = margo_get_output(handle, &out);
        if(hret != HG_SUCCESS) {
            margo_destroy(handle);
606
            return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
607 608 609
        }

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

612 613
        if (out.value.size > 0) {
            memcpy(value, out.value.data, out.value.size);
Matthieu Dorier's avatar
Matthieu Dorier committed
614 615 616 617 618 619 620 621 622
        }

        margo_free_output(handle, &out);

    } else {

        bulk_get_in_t in;
        bulk_get_out_t out;

623 624 625 626
        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
627

628 629
        hret = margo_bulk_create(provider->client->mid, 1, &value, &in.vsize,
                                HG_BULK_WRITE_ONLY, &in.handle);
630
        if(hret != HG_SUCCESS) return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
631 632 633 634 635 636 637 638

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

643
        hret = margo_provider_forward(provider->provider_id, handle, &in);
Matthieu Dorier's avatar
Matthieu Dorier committed
644
        if(hret != HG_SUCCESS) {
645
            margo_bulk_free(in.handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
646
            margo_destroy(handle);
647
            return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
648 649 650 651
        }

        hret = margo_get_output(handle, &out);
        if(hret != HG_SUCCESS) {
652
            margo_bulk_free(in.handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
653
            margo_destroy(handle);
654
            return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
655 656 657 658 659 660
        }

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

        margo_free_output(handle, &out);
661
        margo_bulk_free(in.handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
662 663 664 665 666 667
    }

    margo_destroy(handle);
    return ret;
}

Matthieu Dorier's avatar
Matthieu Dorier committed
668 669
int sdskv_get_multi(sdskv_provider_handle_t provider, 
        sdskv_database_id_t db_id,
Matthieu Dorier's avatar
Matthieu Dorier committed
670
        size_t num, const void* const* keys, const hg_size_t* ksizes,
Matthieu Dorier's avatar
Matthieu Dorier committed
671 672
        void** values, hg_size_t *vsizes)
{
673 674 675 676 677 678 679 680 681 682 683 684 685 686
    /******* NOTE ********
     * This function works as follows:
     *  - in.keys_bulk_handle will be a multi-segment bulk handle of num+1 segments.
     *    The addresses to these segments are stored in key_seg_ptrs and their size
     *    is in key_seg_sizes. Segment 0 will point to the ksizes array and will be
     *    used to communicate the size of each key. The next segments will be used
     *    to communicate the keys themselves.
     * - in.vals_bulk_handle will be a bulk handle exposing a large single segment.
     *   The beginning of this segment will be used to hold num hg_size_t values
     *   corresponding to the sizes allocated for each value, and that the server
     *   will modifiy with the actual sizes. The rest of the buffer will be used
     *   for the server to push actual values, which will be concatenated back to back
     *   and will require unpacking to be put into the values input buffers.
     */
Matthieu Dorier's avatar
Matthieu Dorier committed
687 688 689 690 691 692 693 694 695
    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;

696 697 698 699
    if(values == NULL) {
        return sdskv_length_multi(provider, db_id, num, keys, ksizes, vsizes);
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
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
    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 */
741
    hret = margo_bulk_create(provider->client->mid, 1, (void**)&vals_buffer, &in.vals_bulk_size,
Matthieu Dorier's avatar
Matthieu Dorier committed
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
            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;
}

801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
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
844 845 846 847 848 849 850 851 852 853 854
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;

855 856
    in.db_id    = db_id;
    in.key.data = (kv_ptr_t)key;
857
    in.key.size = ksize;
Matthieu Dorier's avatar
Matthieu Dorier committed
858 859 860 861 862 863 864

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

867
    hret = margo_provider_forward(provider->provider_id, handle, &in);
Matthieu Dorier's avatar
Matthieu Dorier committed
868 869
    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
870
        return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
871 872 873 874 875
    }

    hret = margo_get_output(handle, &out);
    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
876
        return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
877 878 879 880 881 882 883 884 885 886
    }

    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
887 888
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
889
        const void* const* keys, const hg_size_t* ksizes, hg_size_t *vsizes)
Matthieu Dorier's avatar
Matthieu Dorier committed
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
{
    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) {
922
        fprintf(stderr,"[SDSKV] margo_bulk_create() for keys failed in sdskv_length_multi()\n");
Matthieu Dorier's avatar
Matthieu Dorier committed
923 924 925 926 927 928
        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);
929
    hret = margo_bulk_create(provider->client->mid, 1, (void**)&vsizes, &vals_size_bulk_size,
Matthieu Dorier's avatar
Matthieu Dorier committed
930 931
            HG_BULK_WRITE_ONLY, &in.vals_size_bulk_handle);
    if(hret != HG_SUCCESS) {
932
        fprintf(stderr,"[SDSKV] margo_bulk_create() for vsizes failed in sdskv_length_multi()\n");
Matthieu Dorier's avatar
Matthieu Dorier committed
933 934 935 936 937 938 939 940 941 942 943
        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) {
944
        fprintf(stderr,"[SDSKV] margo_create() failed in sdskv_length_multi()\n");
Matthieu Dorier's avatar
Matthieu Dorier committed
945 946 947 948 949 950 951
        out.ret = SDSKV_ERR_MERCURY;
        goto finish;
    }

    /* forward the RPC handle */
    hret = margo_provider_forward(provider->provider_id, handle, &in);
    if(hret != HG_SUCCESS) {
952
        fprintf(stderr,"[SDSKV] margo_forward() failed in sdskv_length_multi()\n");
Matthieu Dorier's avatar
Matthieu Dorier committed
953 954 955 956 957 958 959
        out.ret = SDSKV_ERR_MERCURY;
        goto finish;
    }

    /* get the response */
    hret = margo_get_output(handle, &out);
    if(hret != HG_SUCCESS) {
960
        fprintf(stderr,"[SDSKV] margo_get_output() failed in sdskv_length_multi()\n");
Matthieu Dorier's avatar
Matthieu Dorier committed
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
        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
980 981 982 983 984 985 986 987 988 989 990 991
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;
992 993
    in.key.data   = (kv_ptr_t)key;
    in.key.size = ksize;
Matthieu Dorier's avatar
Matthieu Dorier committed
994 995 996 997 998 999 1000

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

1003
    hret = margo_provider_forward(provider->provider_id, handle, &in);
Matthieu Dorier's avatar
Matthieu Dorier committed
1004 1005
    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
1006
        return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
1007 1008 1009 1010 1011
    }

    hret = margo_get_output(handle, &out);
    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
1012
        return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
1013 1014 1015 1016 1017 1018 1019 1020 1021
    }

    ret = out.ret;

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

Matthieu Dorier's avatar
Matthieu Dorier committed
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 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
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
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
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
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
{
    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
1138
{
1139 1140
    list_keys_in_t  in;
    list_keys_out_t out;
Matthieu Dorier's avatar
Matthieu Dorier committed
1141 1142
    hg_return_t hret      = HG_SUCCESS;
    hg_handle_t handle    = HG_HANDLE_NULL;
1143
    int ret = SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
1144 1145 1146
    int i;

    in.db_id = db_id;
1147 1148
    in.start_key.data = (kv_ptr_t) start_key;
    in.start_key.size = start_ksize;
1149 1150 1151 1152
    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
1153 1154
    in.max_keys = *max_keys;

Matthieu Dorier's avatar
Matthieu Dorier committed
1155 1156 1157 1158 1159 1160 1161 1162
    /* 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) {
1163
        ret = SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
1164 1165 1166 1167
        goto finish;
    }

    /* create bulk handle to expose where the keys should be placed */
1168 1169
    if(keys) {
        hret = margo_bulk_create(provider->client->mid,
Matthieu Dorier's avatar
Matthieu Dorier committed
1170 1171 1172
                             *max_keys, keys, ksizes,
                             HG_BULK_WRITE_ONLY,
                             &in.keys_bulk_handle);
1173 1174 1175 1176
        if(hret != HG_SUCCESS) {
            ret = SDSKV_ERR_MERCURY;
            goto finish;
        }
Matthieu Dorier's avatar
Matthieu Dorier committed
1177 1178
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
1179 1180 1181 1182
    /* create handle */
    hret = margo_create(
            provider->client->mid,
            provider->addr,
Matthieu Dorier's avatar
Matthieu Dorier committed
1183
            provider->client->sdskv_list_keys_id,
Matthieu Dorier's avatar
Matthieu Dorier committed
1184
            &handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
1185
    if(hret != HG_SUCCESS) {
1186
        ret = SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
1187 1188
        goto finish;
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
1189

Matthieu Dorier's avatar
Matthieu Dorier committed
1190
    /* forward to provider */
1191
    hret = margo_provider_forward(provider->provider_id, handle, &in);
Matthieu Dorier's avatar
Matthieu Dorier committed
1192
    if(hret != HG_SUCCESS) {
1193
        ret = SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
1194
        goto finish;
Matthieu Dorier's avatar
Matthieu Dorier committed
1195 1196
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
1197
    /* get the output from provider */
Matthieu Dorier's avatar
Matthieu Dorier committed
1198 1199
    hret = margo_get_output(handle, &out);
    if(hret != HG_SUCCESS) {
1200
        ret = SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
1201
        goto finish;
Matthieu Dorier's avatar
Matthieu Dorier committed
1202 1203
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
1204
    /* set return values */
Matthieu Dorier's avatar
Matthieu Dorier committed
1205 1206 1207
    *max_keys = out.nkeys;
    ret = out.ret;

Matthieu Dorier's avatar
Matthieu Dorier committed
1208 1209 1210 1211
finish:
    /* free everything we created */
    margo_bulk_free(in.ksizes_bulk_handle);
    margo_bulk_free(in.keys_bulk_handle);
1212
    margo_free_output(handle, &out);
Matthieu Dorier's avatar
Matthieu Dorier committed
1213
    margo_destroy(handle);
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232

    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
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254