Revision 98b047dd src/CloudSupport/mysql_delegate_helper.c

View differences:

src/CloudSupport/mysql_delegate_helper.c
37 37
  void* (*cloud_helper_init)(struct nodeID *local, const char *config);
38 38
  int (*get_from_cloud)(void *context, const char *key, uint8_t *header_ptr,
39 39
                        int header_size, int free_header);
40
  int (*get_from_cloud_default)(void *context, const char *key,
41
                                uint8_t *header_ptr, int header_size, int free_header,
42
                                uint8_t *defval_ptr, int defval_size, int free_defval);
40 43
  int (*put_on_cloud)(void *context, const char *key, uint8_t *buffer_ptr,
41 44
                      int buffer_size, int free_buffer);
42 45
  struct nodeID* (*get_cloud_node)(void *context, uint8_t variant);
......
66 69
  int data_length;
67 70
  int free_data;
68 71

  
72
  uint8_t *default_value;
73
  int default_value_length;
74
  int free_default_value;
69 75
  struct mysql_cloud_context *helper_ctx;
70 76
};
71 77
typedef struct mysql_request mysql_request_t;
......
148 154
  req = (mysql_request_t *) req_ptr;
149 155
  if (req->free_data) free(req->data);
150 156

  
157
  if (req->free_default_value > 0)
158
    free(req->default_value);
159

  
151 160
  free(req);
152 161
  return;
153 162
}
......
220 229
      ts_str[field_len[1]] = '\0';
221 230
      rsp->last_timestamp = strtol(ts_str, NULL, 10);
222 231
      rsp->status = SUCCESS;
232
    } else {
233
      /* Since there was no value for the specified key. If the caller specified a
234
         default value, use that */
235
      if (req->default_value) {
236
        /* reserve space for value and header */
237
        rsp->data_length = req->default_value_length + req->data_length;
238
        rsp->data = calloc(rsp->data_length, sizeof(char));
239
        rsp->current_byte = rsp->data;
240

  
241
        if (req->data_length > 0)
242
          memcpy(rsp->data, req->data, req->data_length);
243

  
244
        memcpy(rsp->data + req->data_length,
245
               req->default_value, req->default_value_length);
246

  
247
        rsp->last_timestamp = 0;
248
        rsp->status = SUCCESS;
249
      }
223 250
    }
224 251
    mysql_free_result(result);
225 252
  }
......
339 366
  return ctx;
340 367
}
341 368

  
342
int get_from_cloud(void *context, const char *key, uint8_t *header_ptr,
343
                   int header_size, int free_header)
369
int get_from_cloud_default(void *context, const char *key,
370
                           uint8_t *header_ptr, int header_size, int free_header,
371
                           uint8_t *defval_ptr, int defval_size, int free_defval)
344 372
{
345 373
  struct mysql_cloud_context *ctx;
346 374
  mysql_request_t *request;
......
356 384
  request->data = header_ptr;
357 385
  request->data_length = header_size;
358 386
  request->free_data = free_header;
387
  request->default_value = defval_ptr;
388
  request->default_value_length = defval_size;
389
  request->free_default_value = free_defval;
359 390
  request->helper_ctx = ctx;
360 391

  
361 392
  err = req_handler_add_request(ctx->req_handler, &process_get_operation,
......
365 396
  return err;
366 397
}
367 398

  
399
int get_from_cloud(void *context, const char *key, uint8_t *header_ptr,
400
                   int header_size, int free_header)
401
{
402
  return get_from_cloud_default(context, key, header_ptr, header_size,
403
                                free_header, NULL, 0, 0);
404
}
405

  
368 406

  
369 407
int put_on_cloud(void *context, const char *key, uint8_t *buffer_ptr,
370 408
                 int buffer_size, int free_buffer)
......
383 421
  request->data = buffer_ptr;
384 422
  request->data_length = buffer_size;
385 423
  request->free_data = free_buffer;
386

  
424
  request->default_value = NULL;
425
  request->default_value_length = 0;
426
  request->free_default_value = 0;
387 427
  res = process_put_operation(ctx, request);
388 428
  free_request(request);
389 429
  return res;
......
472 512
struct delegate_iface delegate_impl = {
473 513
  .cloud_helper_init = &cloud_helper_init,
474 514
  .get_from_cloud = &get_from_cloud,
515
  .get_from_cloud_default = &get_from_cloud_default,
475 516
  .put_on_cloud = &put_on_cloud,
476 517
  .get_cloud_node = &get_cloud_node,
477 518
  .timestamp_cloud = &timestamp_cloud,

Also available in: Unified diff