aboutsummaryrefslogtreecommitdiff
path: root/src/datastore/plugin_datastore_mysql.c
diff options
context:
space:
mode:
authorDavid Barksdale <amatus@amat.us>2017-04-16 12:39:43 -0500
committerDavid Barksdale <amatus@amat.us>2017-04-16 12:42:34 -0500
commit4907330f51ffd48af1f7bac6f43c7c7f78c37818 (patch)
treea2cd65dbb24ea5caeda1fff2521f935dd7ea6256 /src/datastore/plugin_datastore_mysql.c
parentcacd64d8635201459e59bf2cd8a2ea8fd0699b84 (diff)
downloadgnunet-4907330f51ffd48af1f7bac6f43c7c7f78c37818.tar.gz
gnunet-4907330f51ffd48af1f7bac6f43c7c7f78c37818.zip
[datastore] Combine put and update plugin APIs
This resolves issue #4965.
Diffstat (limited to 'src/datastore/plugin_datastore_mysql.c')
-rw-r--r--src/datastore/plugin_datastore_mysql.c127
1 files changed, 50 insertions, 77 deletions
diff --git a/src/datastore/plugin_datastore_mysql.c b/src/datastore/plugin_datastore_mysql.c
index 6f2a76499..edc459272 100644
--- a/src/datastore/plugin_datastore_mysql.c
+++ b/src/datastore/plugin_datastore_mysql.c
@@ -198,8 +198,8 @@ struct Plugin
198#define UPDATE_ENTRY "UPDATE gn090 SET "\ 198#define UPDATE_ENTRY "UPDATE gn090 SET "\
199 "prio = prio + ?, "\ 199 "prio = prio + ?, "\
200 "repl = repl + ?, "\ 200 "repl = repl + ?, "\
201 "expire = IF(expire >= ?, expire, ?) "\ 201 "expire = GREATEST(expire, ?) "\
202 "WHERE uid = ?" 202 "WHERE hash = ? AND vhash = ?"
203 struct GNUNET_MYSQL_StatementHandle *update_entry; 203 struct GNUNET_MYSQL_StatementHandle *update_entry;
204 204
205#define DEC_REPL "UPDATE gn090 SET repl=GREATEST (1, repl) - 1 WHERE uid=?" 205#define DEC_REPL "UPDATE gn090 SET repl=GREATEST (1, repl) - 1 WHERE uid=?"
@@ -330,6 +330,7 @@ mysql_plugin_estimate_size (void *cls,
330 * 330 *
331 * @param cls closure 331 * @param cls closure
332 * @param key key for the item 332 * @param key key for the item
333 * @param absent true if the key was not found in the bloom filter
333 * @param size number of bytes in @a data 334 * @param size number of bytes in @a data
334 * @param data content stored 335 * @param data content stored
335 * @param type type of the content 336 * @param type type of the content
@@ -343,6 +344,7 @@ mysql_plugin_estimate_size (void *cls,
343static void 344static void
344mysql_plugin_put (void *cls, 345mysql_plugin_put (void *cls,
345 const struct GNUNET_HashCode *key, 346 const struct GNUNET_HashCode *key,
347 bool absent,
346 uint32_t size, 348 uint32_t size,
347 const void *data, 349 const void *data,
348 enum GNUNET_BLOCK_Type type, 350 enum GNUNET_BLOCK_Type type,
@@ -355,9 +357,54 @@ mysql_plugin_put (void *cls,
355{ 357{
356 struct Plugin *plugin = cls; 358 struct Plugin *plugin = cls;
357 uint64_t lexpiration = expiration.abs_value_us; 359 uint64_t lexpiration = expiration.abs_value_us;
360 struct GNUNET_HashCode vhash;
361
362 GNUNET_CRYPTO_hash (data,
363 size,
364 &vhash);
365 if (!absent)
366 {
367 struct GNUNET_MY_QueryParam params_update[] = {
368 GNUNET_MY_query_param_uint32 (&priority),
369 GNUNET_MY_query_param_uint32 (&replication),
370 GNUNET_MY_query_param_uint64 (&lexpiration),
371 GNUNET_MY_query_param_auto_from_type (key),
372 GNUNET_MY_query_param_auto_from_type (&vhash),
373 GNUNET_MY_query_param_end
374 };
375
376 if (GNUNET_OK !=
377 GNUNET_MY_exec_prepared (plugin->mc,
378 plugin->update_entry,
379 params_update))
380 {
381 cont (cont_cls,
382 key,
383 size,
384 GNUNET_SYSERR,
385 _("MySQL statement run failure"));
386 return;
387 }
388
389 MYSQL_STMT *stmt = GNUNET_MYSQL_statement_get_stmt (plugin->update_entry);
390 my_ulonglong rows = mysql_stmt_affected_rows (stmt);
391
392 GNUNET_break (GNUNET_NO ==
393 GNUNET_MY_extract_result (plugin->update_entry,
394 NULL));
395 if (0 != rows)
396 {
397 cont (cont_cls,
398 key,
399 size,
400 GNUNET_NO,
401 NULL);
402 return;
403 }
404 }
405
358 uint64_t lrvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, 406 uint64_t lrvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
359 UINT64_MAX); 407 UINT64_MAX);
360 struct GNUNET_HashCode vhash;
361 struct GNUNET_MY_QueryParam params_insert[] = { 408 struct GNUNET_MY_QueryParam params_insert[] = {
362 GNUNET_MY_query_param_uint32 (&replication), 409 GNUNET_MY_query_param_uint32 (&replication),
363 GNUNET_MY_query_param_uint32 (&type), 410 GNUNET_MY_query_param_uint32 (&type),
@@ -377,9 +424,6 @@ mysql_plugin_put (void *cls,
377 cont (cont_cls, key, size, GNUNET_SYSERR, _("Data too large")); 424 cont (cont_cls, key, size, GNUNET_SYSERR, _("Data too large"));
378 return; 425 return;
379 } 426 }
380 GNUNET_CRYPTO_hash (data,
381 size,
382 &vhash);
383 427
384 if (GNUNET_OK != 428 if (GNUNET_OK !=
385 GNUNET_MY_exec_prepared (plugin->mc, 429 GNUNET_MY_exec_prepared (plugin->mc,
@@ -412,76 +456,6 @@ mysql_plugin_put (void *cls,
412 456
413 457
414/** 458/**
415 * Update the priority, replication and expiration for a particular
416 * unique ID in the datastore. If the expiration time in value is
417 * different than the time found in the datastore, the higher value
418 * should be kept. The specified priority and replication is added
419 * to the existing value.
420 *
421 * @param cls our "struct Plugin*"
422 * @param uid unique identifier of the datum
423 * @param priority by how much should the priority
424 * change?
425 * @param replication by how much should the replication
426 * change?
427 * @param expire new expiration time should be the
428 * MAX of any existing expiration time and
429 * this value
430 * @param cont continuation called with success or failure status
431 * @param cons_cls continuation closure
432 */
433static void
434mysql_plugin_update (void *cls,
435 uint64_t uid,
436 uint32_t priority,
437 uint32_t replication,
438 struct GNUNET_TIME_Absolute expire,
439 PluginUpdateCont cont,
440 void *cont_cls)
441{
442 struct Plugin *plugin = cls;
443 uint64_t lexpire = expire.abs_value_us;
444 int ret;
445
446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
447 "Updating value %llu adding %d to priority %d to replication and maxing exp at %s\n",
448 (unsigned long long) uid,
449 priority,
450 replication,
451 GNUNET_STRINGS_absolute_time_to_string (expire));
452
453 struct GNUNET_MY_QueryParam params_update[] = {
454 GNUNET_MY_query_param_uint32 (&priority),
455 GNUNET_MY_query_param_uint32 (&replication),
456 GNUNET_MY_query_param_uint64 (&lexpire),
457 GNUNET_MY_query_param_uint64 (&lexpire),
458 GNUNET_MY_query_param_uint64 (&uid),
459 GNUNET_MY_query_param_end
460 };
461
462 ret = GNUNET_MY_exec_prepared (plugin->mc,
463 plugin->update_entry,
464 params_update);
465
466 if (GNUNET_OK != ret)
467 {
468 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
469 "Failed to update value %llu\n",
470 (unsigned long long) uid);
471 }
472 else
473 {
474 GNUNET_break (GNUNET_NO ==
475 GNUNET_MY_extract_result (plugin->update_entry,
476 NULL));
477 }
478 cont (cont_cls,
479 ret,
480 NULL);
481}
482
483
484/**
485 * Run the given select statement and call 'proc' on the resulting 459 * Run the given select statement and call 'proc' on the resulting
486 * values (which must be in particular positions). 460 * values (which must be in particular positions).
487 * 461 *
@@ -1197,7 +1171,6 @@ libgnunet_plugin_datastore_mysql_init (void *cls)
1197 api->cls = plugin; 1171 api->cls = plugin;
1198 api->estimate_size = &mysql_plugin_estimate_size; 1172 api->estimate_size = &mysql_plugin_estimate_size;
1199 api->put = &mysql_plugin_put; 1173 api->put = &mysql_plugin_put;
1200 api->update = &mysql_plugin_update;
1201 api->get_key = &mysql_plugin_get_key; 1174 api->get_key = &mysql_plugin_get_key;
1202 api->get_replication = &mysql_plugin_get_replication; 1175 api->get_replication = &mysql_plugin_get_replication;
1203 api->get_expiration = &mysql_plugin_get_expiration; 1176 api->get_expiration = &mysql_plugin_get_expiration;