sdskv-rpc-types.h 12 KB
Newer Older
Matthieu Dorier's avatar
Matthieu Dorier committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#ifndef SDSKV_RPC_TYPE_H
#define SDSKV_RPC_TYPE_H

#include <stdint.h>
#include <assert.h>

#include <mercury.h>
#include <mercury_macros.h>
#include <mercury_proc_string.h>

#include <margo.h>

#include "sdskv-common.h"

// setup to support opaque type handling
typedef char* kv_data_t;

typedef struct {
  uint64_t  db_id;
  kv_data_t key;
  hg_size_t ksize;
  kv_data_t value;
  hg_size_t vsize;
} put_in_t;

typedef struct {
  uint64_t  db_id;
  kv_data_t key;
  hg_size_t ksize;
  hg_size_t vsize;
} get_in_t;

typedef struct {
  kv_data_t value;
  hg_size_t vsize;
  int32_t ret;
} get_out_t;

typedef struct {
    uint64_t  db_id;
    kv_data_t key;
    hg_size_t ksize;
} length_in_t;

static inline hg_return_t hg_proc_put_in_t(hg_proc_t proc, void *data)
{
  hg_return_t ret;
  put_in_t *in = (put_in_t*)data;

  ret = hg_proc_uint64_t(proc, &in->db_id);
  if(ret != HG_SUCCESS) return ret;

  ret = hg_proc_hg_size_t(proc, &in->ksize);
  if(ret != HG_SUCCESS) return ret;
  if (in->ksize) {
    switch (hg_proc_get_op(proc)) {
    case HG_ENCODE:
      ret = hg_proc_raw(proc, in->key, in->ksize);
      if(ret != HG_SUCCESS) return ret;
      break;
    case HG_DECODE:
      in->key = (kv_data_t)malloc(in->ksize);
      ret = hg_proc_raw(proc, in->key, in->ksize);
      if(ret != HG_SUCCESS) return ret;
      break;
    case HG_FREE:
      free(in->key);
      break;
    default:
      break;
    }
  }
  ret = hg_proc_hg_size_t(proc, &in->vsize);
  if(ret != HG_SUCCESS) return ret;
  if (in->vsize) {
    switch (hg_proc_get_op(proc)) {
    case HG_ENCODE:
      ret = hg_proc_raw(proc, in->value, in->vsize);
      if(ret != HG_SUCCESS) return ret;
      break;
    case HG_DECODE:
      in->value = (kv_data_t)malloc(in->vsize);
      ret = hg_proc_raw(proc, in->value, in->vsize);
      if(ret != HG_SUCCESS) return ret;
      break;
    case HG_FREE:
      free(in->value);
      break;
    default:
      break;
    }
  }

  return HG_SUCCESS;
}

static inline hg_return_t hg_proc_length_in_t(hg_proc_t proc, void *data)
{
  hg_return_t ret;
  length_in_t *in = (length_in_t*)data;

  ret = hg_proc_uint64_t(proc, &in->db_id);
  if(ret != HG_SUCCESS) return ret;

  ret = hg_proc_hg_size_t(proc, &in->ksize);
  if(ret != HG_SUCCESS) return ret;
  if (in->ksize) {
    switch (hg_proc_get_op(proc)) {
    case HG_ENCODE:
      ret = hg_proc_raw(proc, in->key, in->ksize);
      if(ret != HG_SUCCESS) return ret;
      break;
    case HG_DECODE:
      in->key = (kv_data_t)malloc(in->ksize);
      ret = hg_proc_raw(proc, in->key, in->ksize);
      if(ret != HG_SUCCESS) return ret;
      break;
    case HG_FREE:
      free(in->key);
      break;
    default:
      break;
    }
  }
  return HG_SUCCESS;
}

static inline hg_return_t hg_proc_get_in_t(hg_proc_t proc, void *data)
{
  hg_return_t ret;
  get_in_t *in = (get_in_t*)data;

  ret = hg_proc_uint64_t(proc, &in->db_id);
  if(ret != HG_SUCCESS) return ret;

  ret = hg_proc_hg_size_t(proc, &in->ksize);
  if(ret != HG_SUCCESS) return ret;
  if (in->ksize) {
    switch (hg_proc_get_op(proc)) {
    case HG_ENCODE:
      ret = hg_proc_raw(proc, in->key, in->ksize);
      if(ret != HG_SUCCESS) return ret;
      break;
    case HG_DECODE:
      in->key = (kv_data_t)malloc(in->ksize);
      ret = hg_proc_raw(proc, in->key, in->ksize);
      if(ret != HG_SUCCESS) return ret;
      break;
    case HG_FREE:
      free(in->key);
      break;
    default:
      break;
    }
  }
  ret = hg_proc_hg_size_t(proc, &in->vsize);
  if(ret != HG_SUCCESS) return ret;

  return HG_SUCCESS;
}

static inline hg_return_t hg_proc_get_out_t(hg_proc_t proc, void *data)
{
  hg_return_t ret;
  get_out_t *out = (get_out_t*)data;

  ret = hg_proc_hg_size_t(proc, &out->vsize);
  if(ret != HG_SUCCESS) return ret;
  if (out->vsize) {
    switch (hg_proc_get_op(proc)) {
    case HG_ENCODE:
      ret = hg_proc_raw(proc, out->value, out->vsize);
      if(ret != HG_SUCCESS) return ret;
      break;
    case HG_DECODE:
      out->value = (kv_data_t)malloc(out->vsize);
      ret = hg_proc_raw(proc, out->value, out->vsize);
      if(ret != HG_SUCCESS) return ret;
      break;
    case HG_FREE:
      free(out->value);
      break;
    default:
      break;
    }
  }
  ret = hg_proc_int32_t(proc, &out->ret);
  if(ret != HG_SUCCESS) return ret;

  return HG_SUCCESS;
}

Matthieu Dorier's avatar
Matthieu Dorier committed
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
typedef struct {
  uint64_t  db_id;
  kv_data_t key;
  hg_size_t ksize;
} erase_in_t;

static inline hg_return_t hg_proc_erase_in_t(hg_proc_t proc, void *data)
{
  hg_return_t ret;
  erase_in_t *in = (erase_in_t*)data;

  ret = hg_proc_uint64_t(proc, &in->db_id);
  if(ret != HG_SUCCESS) return ret;

  ret = hg_proc_hg_size_t(proc, &in->ksize);
  if(ret != HG_SUCCESS) return ret;
  if (in->ksize) {
    switch (hg_proc_get_op(proc)) {
    case HG_ENCODE:
      ret = hg_proc_raw(proc, in->key, in->ksize);
      if(ret != HG_SUCCESS) return ret;
      break;
    case HG_DECODE:
      in->key = (kv_data_t)malloc(in->ksize);
      ret = hg_proc_raw(proc, in->key, in->ksize);
      if(ret != HG_SUCCESS) return ret;
      break;
    case HG_FREE:
      free(in->key);
      break;
    default:
      break;
    }
  }

  return HG_SUCCESS;
}

MERCURY_GEN_PROC(erase_out_t, ((int32_t)(ret)))

Matthieu Dorier's avatar
Matthieu Dorier committed
233
234
235
236
237
typedef struct {
    uint64_t  db_id;
    kv_data_t start_key;
    hg_size_t start_ksize;
    hg_size_t max_keys;
238
} list_keys_in_t;
Matthieu Dorier's avatar
Matthieu Dorier committed
239
240
241
242
243

typedef struct {
    hg_size_t nkeys;
    kv_data_t *keys;
    hg_size_t *ksizes;
Matthieu Dorier's avatar
Matthieu Dorier committed
244
245
246
    hg_size_t nvalues;
    kv_data_t *values;
    hg_size_t *vsizes;
Matthieu Dorier's avatar
Matthieu Dorier committed
247
    int32_t ret;
248
} list_keys_out_t;
Matthieu Dorier's avatar
Matthieu Dorier committed
249

250
static inline hg_return_t hg_proc_list_keys_in_t(hg_proc_t proc, void *data)
Matthieu Dorier's avatar
Matthieu Dorier committed
251
252
{
    hg_return_t ret;
253
    list_keys_in_t *in = (list_keys_in_t*)data;
Matthieu Dorier's avatar
Matthieu Dorier committed
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

    ret = hg_proc_uint64_t(proc, &in->db_id);
    if(ret != HG_SUCCESS) return ret;

    ret = hg_proc_hg_size_t(proc, &in->start_ksize);
    if(ret != HG_SUCCESS) return ret;
    if (in->start_ksize) {
	switch(hg_proc_get_op(proc)) {
	    case HG_ENCODE:
		ret = hg_proc_raw(proc, in->start_key, in->start_ksize);
		if(ret != HG_SUCCESS) return ret;
		break;
	    case HG_DECODE:
		in->start_key = (kv_data_t)malloc(in->start_ksize);
		ret = hg_proc_raw(proc, in->start_key, in->start_ksize);
		if(ret != HG_SUCCESS) return ret;
		break;
	    case HG_FREE:
		free(in->start_key);
	    default:
		break;
	}
    }
    ret = hg_proc_hg_size_t(proc, &in->max_keys);
    return ret;
}

281
static inline hg_return_t hg_proc_list_keys_out_t(hg_proc_t proc, void *data)
Matthieu Dorier's avatar
Matthieu Dorier committed
282
283
284
{
    hg_return_t ret;
    unsigned int i;
285
    list_keys_out_t *out = (list_keys_out_t*)data;
Matthieu Dorier's avatar
Matthieu Dorier committed
286
    /* encode/decode the number of keys */
Matthieu Dorier's avatar
Matthieu Dorier committed
287
288
    ret = hg_proc_hg_size_t(proc, &out->nkeys);
    if(ret != HG_SUCCESS) return ret;
Matthieu Dorier's avatar
Matthieu Dorier committed
289

290
    /* encode/decode the number of values */
Matthieu Dorier's avatar
Matthieu Dorier committed
291
292
293
    ret = hg_proc_hg_size_t(proc, &out->nvalues);
    if(ret != HG_SUCCESS) return ret;

294
	switch(hg_proc_get_op(proc)) {
Matthieu Dorier's avatar
Matthieu Dorier committed
295
	    case HG_ENCODE:
Matthieu Dorier's avatar
Matthieu Dorier committed
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
            /* encode the size of each key */
		    for (i=0; i<out->nkeys; i++) {
		        ret = hg_proc_hg_size_t(proc, &(out->ksizes[i]));
                if(ret != HG_SUCCESS) return ret;
		    }
            /* encode each key */
		    for (i=0; i<out->nkeys; i++) {
		        ret = hg_proc_raw(proc, out->keys[i], out->ksizes[i]);
		        if(ret != HG_SUCCESS) return ret;
		    }
            break;

        case HG_DECODE:
            if(out->nkeys) {
                /* decode the size of each key */
                out->ksizes = (hg_size_t*)malloc(out->nkeys*sizeof(*out->ksizes));
                for (i=0; i<out->nkeys; i++) {
                    ret = hg_proc_hg_size_t(proc, &(out->ksizes[i]));
                    if(ret != HG_SUCCESS) return ret;
                }
                /* decode each key */
                out->keys = (kv_data_t *)malloc(out->nkeys*sizeof(kv_data_t));
                for (i=0; i<out->nkeys; i++) {
                    if(out->ksizes[i] == 0) {
                        out->keys[i] = NULL;
                        continue;
                    }
                    out->keys[i] = (kv_data_t)malloc(out->ksizes[i]);
                    ret = hg_proc_raw(proc, out->keys[i], out->ksizes[i]);
                    if(ret != HG_SUCCESS) return ret;
                }
            } else {
                out->ksizes = NULL;
                out->keys = NULL;
            }
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
            break;

	    case HG_FREE:
		    for (i=0; i<out->nkeys; i++) {
		        free(out->keys[i]);
		    }
		    free(out->keys);
		    free(out->ksizes);
		    break;

	    default:
		    break;
	}

	switch(hg_proc_get_op(proc)) {
	    case HG_ENCODE:
            /* encode the size of values, if present */
            for(i=0; i <out->nvalues; i++) {
                ret = hg_proc_hg_size_t(proc, &(out->vsizes[i]));
                if(ret != HG_SUCCESS) return ret;
            }
            /* encode the values, if present */
            for(i=0; i < out->nvalues; i++) {
                ret = hg_proc_raw(proc, out->values[i], out->vsizes[i]);
                if(ret != HG_SUCCESS) return ret;
            }
            break;

        case HG_DECODE:
            if(out->nvalues != 0) {
Matthieu Dorier's avatar
Matthieu Dorier committed
361
362
                /* decode the size of each value */
                out->vsizes = (hg_size_t*)malloc(out->nvalues*sizeof(*out->vsizes));
363
                for( i=0; i < out->nvalues; i++) {
Matthieu Dorier's avatar
Matthieu Dorier committed
364
365
366
                    ret = hg_proc_hg_size_t(proc, &(out->vsizes[i]));
                    if(ret != HG_SUCCESS) return ret;
                }
367
                /* decode each value */
Matthieu Dorier's avatar
Matthieu Dorier committed
368
                out->values =  (kv_data_t *)malloc(out->nvalues*sizeof(kv_data_t));
369
                for(i=0; i < out->nvalues; i++) {
Matthieu Dorier's avatar
Matthieu Dorier committed
370
371
372
373
374
375
376
377
378
379
380
381
382
383
                    if(out->vsizes[i] == 0) {
                        out->values[i] = NULL;
                        continue;
                    }
                    out->values[i] = (kv_data_t)malloc(out->vsizes[i]);
                    ret = hg_proc_raw(proc, out->values[i], out->vsizes[i]);
                    if(ret != HG_SUCCESS) return ret;
                }
            } else {
                out->vsizes = NULL;
                out->values = NULL;
            }
            break;

Matthieu Dorier's avatar
Matthieu Dorier committed
384
	    case HG_FREE:
385
            for(i=0; i < out->nvalues; i++) {
Matthieu Dorier's avatar
Matthieu Dorier committed
386
387
388
389
390
391
                free(out->values[i]);
            }
            free(out->values);
            free(out->vsizes);
		    break;

Matthieu Dorier's avatar
Matthieu Dorier committed
392
	    default:
Matthieu Dorier's avatar
Matthieu Dorier committed
393
		    break;
394
395
	}

Matthieu Dorier's avatar
Matthieu Dorier committed
396
    /* encode/decode the return value */
Matthieu Dorier's avatar
Matthieu Dorier committed
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
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
    ret = hg_proc_int32_t(proc, &out->ret);
    return ret;
}

MERCURY_GEN_PROC(put_out_t, ((int32_t)(ret)))

MERCURY_GEN_PROC(length_out_t, ((hg_size_t)(size)) ((int32_t)(ret)))

MERCURY_GEN_PROC(open_in_t, 
        ((hg_string_t)(name)))
MERCURY_GEN_PROC(open_out_t, ((uint64_t)(db_id)) ((int32_t)(ret)))

// for handling bulk puts/gets (e.g. for ParSplice use case)
typedef struct {
  uint64_t  db_id;
  kv_data_t key;
  hg_size_t ksize;
  hg_size_t vsize;
  hg_bulk_t handle;
} kv_bulk_t;

static inline hg_return_t hg_proc_kv_bulk_t(hg_proc_t proc, void *data)
{
  hg_return_t ret;
  kv_bulk_t *bulk = (kv_bulk_t*)data;

  ret = hg_proc_uint64_t(proc, &bulk->db_id);
  if(ret != HG_SUCCESS) return ret;

  ret = hg_proc_hg_size_t(proc, &bulk->ksize);
  if(ret != HG_SUCCESS) return ret;
  if (bulk->ksize) {
    switch (hg_proc_get_op(proc)) {
    case HG_ENCODE:
      ret = hg_proc_raw(proc, bulk->key, bulk->ksize);
      if(ret != HG_SUCCESS) return ret;
      break;
    case HG_DECODE:
      bulk->key = (kv_data_t)malloc(bulk->ksize);
      ret = hg_proc_raw(proc, bulk->key, bulk->ksize);
      if(ret != HG_SUCCESS) return ret;
      break;
    case HG_FREE:
      free(bulk->key);
      break;
    default:
      break;
    }
  }
  ret = hg_proc_hg_size_t(proc, &bulk->vsize);
  if(ret != HG_SUCCESS) return ret;
  ret = hg_proc_hg_bulk_t(proc, &bulk->handle);
  if(ret != HG_SUCCESS) return ret;

  return HG_SUCCESS;
}

MERCURY_GEN_PROC(bulk_put_in_t, ((kv_bulk_t)(bulk)))
MERCURY_GEN_PROC(bulk_put_out_t, ((int32_t)(ret)))

MERCURY_GEN_PROC(bulk_get_in_t, ((kv_bulk_t)(bulk)))
MERCURY_GEN_PROC(bulk_get_out_t, ((hg_size_t)(size)) ((int32_t)(ret)))

static inline hg_return_t hg_proc_double(hg_proc_t proc, void *data)
{
  hg_return_t ret;
  hg_size_t size = sizeof(double);

  ret = hg_proc_raw(proc, data, size);
  assert(ret == HG_SUCCESS);

  return HG_SUCCESS;
}

#endif