aboutsummaryrefslogtreecommitdiff
path: root/src/peerstore
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2017-03-14 00:31:02 +0100
committerChristian Grothoff <christian@grothoff.org>2017-03-14 00:31:02 +0100
commit8d71f909cb22fbf6774e4042309a8eb133af3bfc (patch)
tree0efc19ac24a6521f927772364f1379154d2d0cad /src/peerstore
parent6e01dc4d91d072e8e58ba0140fb7f4ddd5109358 (diff)
downloadgnunet-8d71f909cb22fbf6774e4042309a8eb133af3bfc.tar.gz
gnunet-8d71f909cb22fbf6774e4042309a8eb133af3bfc.zip
convert sqlite peerstore to using libgnunetsq
Diffstat (limited to 'src/peerstore')
-rw-r--r--src/peerstore/gnunet-service-peerstore.c28
-rw-r--r--src/peerstore/peerstore.h2
-rw-r--r--src/peerstore/peerstore_api.c27
-rw-r--r--src/peerstore/peerstore_common.c20
-rw-r--r--src/peerstore/peerstore_common.h2
-rw-r--r--src/peerstore/plugin_peerstore_sqlite.c357
-rw-r--r--src/peerstore/test_peerstore_api_iterate.c73
-rw-r--r--src/peerstore/test_peerstore_api_store.c55
-rw-r--r--src/peerstore/test_plugin_peerstore.c127
9 files changed, 424 insertions, 267 deletions
diff --git a/src/peerstore/gnunet-service-peerstore.c b/src/peerstore/gnunet-service-peerstore.c
index 8200c2366..92d020799 100644
--- a/src/peerstore/gnunet-service-peerstore.c
+++ b/src/peerstore/gnunet-service-peerstore.c
@@ -260,15 +260,23 @@ record_iterator (void *cls,
260 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (cls_record->client), 260 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (cls_record->client),
261 env); 261 env);
262 if (NULL == emsg) 262 if (NULL == emsg)
263 {
263 GNUNET_SERVICE_client_continue (cls_record->client); 264 GNUNET_SERVICE_client_continue (cls_record->client);
265 }
264 else 266 else
267 {
268 GNUNET_break (0);
269 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
270 "Failed to iterate: %s\n",
271 emsg);
265 GNUNET_SERVICE_client_drop (cls_record->client); 272 GNUNET_SERVICE_client_drop (cls_record->client);
273 }
266 PEERSTORE_destroy_record (cls_record); 274 PEERSTORE_destroy_record (cls_record);
267 return; 275 return;
268 } 276 }
269 277
270 env = PEERSTORE_create_record_mq_envelope (record->sub_system, 278 env = PEERSTORE_create_record_mq_envelope (record->sub_system,
271 record->peer, 279 &record->peer,
272 record->key, 280 record->key,
273 record->value, 281 record->value,
274 record->value_size, 282 record->value_size,
@@ -301,7 +309,7 @@ watch_notifier_it (void *cls,
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
302 "Found a watcher to update.\n"); 310 "Found a watcher to update.\n");
303 env = PEERSTORE_create_record_mq_envelope (record->sub_system, 311 env = PEERSTORE_create_record_mq_envelope (record->sub_system,
304 record->peer, 312 &record->peer,
305 record->key, 313 record->key,
306 record->value, 314 record->value,
307 record->value_size, 315 record->value_size,
@@ -325,7 +333,7 @@ watch_notifier (struct GNUNET_PEERSTORE_Record *record)
325 struct GNUNET_HashCode keyhash; 333 struct GNUNET_HashCode keyhash;
326 334
327 PEERSTORE_hash_key (record->sub_system, 335 PEERSTORE_hash_key (record->sub_system,
328 record->peer, 336 &record->peer,
329 record->key, 337 record->key,
330 &keyhash); 338 &keyhash);
331 GNUNET_CONTAINER_multihashmap_get_multiple (watchers, 339 GNUNET_CONTAINER_multihashmap_get_multiple (watchers,
@@ -434,17 +442,18 @@ handle_iterate (void *cls,
434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
435 "Iterate request: ss `%s', peer `%s', key `%s'\n", 443 "Iterate request: ss `%s', peer `%s', key `%s'\n",
436 record->sub_system, 444 record->sub_system,
437 (NULL == record->peer) ? "NULL" : GNUNET_i2s (record->peer), 445 GNUNET_i2s (&record->peer),
438 (NULL == record->key) ? "NULL" : record->key); 446 (NULL == record->key) ? "NULL" : record->key);
439 record->client = client; 447 record->client = client;
440 if (GNUNET_OK != 448 if (GNUNET_OK !=
441 db->iterate_records (db->cls, 449 db->iterate_records (db->cls,
442 record->sub_system, 450 record->sub_system,
443 record->peer, 451 (ntohs (srm->peer_set)) ? &record->peer : NULL,
444 record->key, 452 record->key,
445 &record_iterator, 453 &record_iterator,
446 record)) 454 record))
447 { 455 {
456 GNUNET_break (0);
448 GNUNET_SERVICE_client_drop (client); 457 GNUNET_SERVICE_client_drop (client);
449 PEERSTORE_destroy_record (record); 458 PEERSTORE_destroy_record (record);
450 } 459 }
@@ -470,6 +479,7 @@ store_record_continuation (void *cls,
470 } 479 }
471 else 480 else
472 { 481 {
482 GNUNET_break (0);
473 GNUNET_SERVICE_client_drop (record->client); 483 GNUNET_SERVICE_client_drop (record->client);
474 } 484 }
475 PEERSTORE_destroy_record (record); 485 PEERSTORE_destroy_record (record);
@@ -496,7 +506,6 @@ check_store (void *cls,
496 return GNUNET_SYSERR; 506 return GNUNET_SYSERR;
497 } 507 }
498 if ( (NULL == record->sub_system) || 508 if ( (NULL == record->sub_system) ||
499 (NULL == record->peer) ||
500 (NULL == record->key) ) 509 (NULL == record->key) )
501 { 510 {
502 GNUNET_break (0); 511 GNUNET_break (0);
@@ -525,22 +534,23 @@ handle_store (void *cls,
525 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 534 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
526 "Received a store request. Sub system `%s' Peer `%s Key `%s' Options: %u.\n", 535 "Received a store request. Sub system `%s' Peer `%s Key `%s' Options: %u.\n",
527 record->sub_system, 536 record->sub_system,
528 GNUNET_i2s (record->peer), 537 GNUNET_i2s (&record->peer),
529 record->key, 538 record->key,
530 (uint32_t) ntohl (srm->options)); 539 (uint32_t) ntohl (srm->options));
531 record->client = client; 540 record->client = client;
532 if (GNUNET_OK != 541 if (GNUNET_OK !=
533 db->store_record (db->cls, 542 db->store_record (db->cls,
534 record->sub_system, 543 record->sub_system,
535 record->peer, 544 &record->peer,
536 record->key, 545 record->key,
537 record->value, 546 record->value,
538 record->value_size, 547 record->value_size,
539 *record->expiry, 548 record->expiry,
540 ntohl (srm->options), 549 ntohl (srm->options),
541 &store_record_continuation, 550 &store_record_continuation,
542 record)) 551 record))
543 { 552 {
553 GNUNET_break (0);
544 PEERSTORE_destroy_record (record); 554 PEERSTORE_destroy_record (record);
545 GNUNET_SERVICE_client_drop (client); 555 GNUNET_SERVICE_client_drop (client);
546 return; 556 return;
diff --git a/src/peerstore/peerstore.h b/src/peerstore/peerstore.h
index 8b3c4dd92..c6b954676 100644
--- a/src/peerstore/peerstore.h
+++ b/src/peerstore/peerstore.h
@@ -60,7 +60,7 @@ struct StoreRecordMessage
60 /** 60 /**
61 * Expiry time of entry 61 * Expiry time of entry
62 */ 62 */
63 struct GNUNET_TIME_Absolute expiry GNUNET_PACKED; 63 struct GNUNET_TIME_AbsoluteNBO expiry;
64 64
65 /** 65 /**
66 * Size of the key string 66 * Size of the key string
diff --git a/src/peerstore/peerstore_api.c b/src/peerstore/peerstore_api.c
index c9fcd17ab..df182fe10 100644
--- a/src/peerstore/peerstore_api.c
+++ b/src/peerstore/peerstore_api.c
@@ -286,6 +286,10 @@ store_request_sent (void *cls)
286/******************* CONNECTION FUNCTIONS *********************/ 286/******************* CONNECTION FUNCTIONS *********************/
287/******************************************************************************/ 287/******************************************************************************/
288 288
289
290/**
291 * Function called when we had trouble talking to the service.
292 */
289static void 293static void
290handle_client_error (void *cls, 294handle_client_error (void *cls,
291 enum GNUNET_MQ_Error error) 295 enum GNUNET_MQ_Error error)
@@ -293,7 +297,7 @@ handle_client_error (void *cls,
293 struct GNUNET_PEERSTORE_Handle *h = cls; 297 struct GNUNET_PEERSTORE_Handle *h = cls;
294 298
295 LOG (GNUNET_ERROR_TYPE_ERROR, 299 LOG (GNUNET_ERROR_TYPE_ERROR,
296 _("Received an error notification from MQ of type: %d\n"), 300 "Received an error notification from MQ of type: %d\n",
297 error); 301 error);
298 reconnect (h); 302 reconnect (h);
299} 303}
@@ -341,7 +345,9 @@ iterate_timeout (void *cls)
341 callback_cls = ic->callback_cls; 345 callback_cls = ic->callback_cls;
342 GNUNET_PEERSTORE_iterate_cancel (ic); 346 GNUNET_PEERSTORE_iterate_cancel (ic);
343 if (NULL != callback) 347 if (NULL != callback)
344 callback (callback_cls, NULL, _("timeout")); 348 callback (callback_cls,
349 NULL,
350 _("timeout"));
345} 351}
346 352
347 353
@@ -510,7 +516,7 @@ GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h,
510 "Storing value (size: %lu) for subsytem `%s', peer `%s', key `%s'\n", 516 "Storing value (size: %lu) for subsytem `%s', peer `%s', key `%s'\n",
511 size, sub_system, GNUNET_i2s (peer), key); 517 size, sub_system, GNUNET_i2s (peer), key);
512 ev = PEERSTORE_create_record_mq_envelope (sub_system, peer, key, value, size, 518 ev = PEERSTORE_create_record_mq_envelope (sub_system, peer, key, value, size,
513 &expiry, options, 519 expiry, options,
514 GNUNET_MESSAGE_TYPE_PEERSTORE_STORE); 520 GNUNET_MESSAGE_TYPE_PEERSTORE_STORE);
515 sc = GNUNET_new (struct GNUNET_PEERSTORE_StoreContext); 521 sc = GNUNET_new (struct GNUNET_PEERSTORE_StoreContext);
516 522
@@ -684,8 +690,12 @@ GNUNET_PEERSTORE_iterate (struct GNUNET_PEERSTORE_Handle *h,
684 struct GNUNET_MQ_Envelope *ev; 690 struct GNUNET_MQ_Envelope *ev;
685 struct GNUNET_PEERSTORE_IterateContext *ic; 691 struct GNUNET_PEERSTORE_IterateContext *ic;
686 692
687 ev = PEERSTORE_create_record_mq_envelope (sub_system, peer, key, NULL, 0, 693 ev = PEERSTORE_create_record_mq_envelope (sub_system,
694 peer,
695 key,
688 NULL, 0, 696 NULL, 0,
697 GNUNET_TIME_UNIT_FOREVER_ABS,
698 0,
689 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE); 699 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE);
690 ic = GNUNET_new (struct GNUNET_PEERSTORE_IterateContext); 700 ic = GNUNET_new (struct GNUNET_PEERSTORE_IterateContext);
691 701
@@ -756,7 +766,7 @@ handle_watch_record (void *cls,
756 return; 766 return;
757 } 767 }
758 PEERSTORE_hash_key (record->sub_system, 768 PEERSTORE_hash_key (record->sub_system,
759 record->peer, 769 &record->peer,
760 record->key, 770 record->key,
761 &keyhash); 771 &keyhash);
762 // FIXME: what if there are multiple watches for the same key? 772 // FIXME: what if there are multiple watches for the same key?
@@ -848,9 +858,12 @@ reconnect (struct GNUNET_PEERSTORE_Handle *h)
848 &ic->peer, 858 &ic->peer,
849 ic->key, 859 ic->key,
850 NULL, 0, 860 NULL, 0,
851 NULL, 0, 861 GNUNET_TIME_UNIT_FOREVER_ABS,
862 0,
852 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE); 863 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE);
853 GNUNET_MQ_send (h->mq, ev); 864 GNUNET_MQ_send (h->mq, ev);
865 if (NULL != ic->timeout_task)
866 GNUNET_SCHEDULER_cancel (ic->timeout_task);
854 ic->timeout_task 867 ic->timeout_task
855 = GNUNET_SCHEDULER_add_delayed (ic->timeout, 868 = GNUNET_SCHEDULER_add_delayed (ic->timeout,
856 &iterate_timeout, 869 &iterate_timeout,
@@ -863,7 +876,7 @@ reconnect (struct GNUNET_PEERSTORE_Handle *h)
863 sc->key, 876 sc->key,
864 sc->value, 877 sc->value,
865 sc->size, 878 sc->size,
866 &sc->expiry, 879 sc->expiry,
867 sc->options, 880 sc->options,
868 GNUNET_MESSAGE_TYPE_PEERSTORE_STORE); 881 GNUNET_MESSAGE_TYPE_PEERSTORE_STORE);
869 GNUNET_MQ_notify_sent (ev, 882 GNUNET_MQ_notify_sent (ev,
diff --git a/src/peerstore/peerstore_common.c b/src/peerstore/peerstore_common.c
index d12c4e21e..e0ab778fa 100644
--- a/src/peerstore/peerstore_common.c
+++ b/src/peerstore/peerstore_common.c
@@ -77,7 +77,7 @@ PEERSTORE_create_record_mq_envelope (const char *sub_system,
77 const char *key, 77 const char *key,
78 const void *value, 78 const void *value,
79 size_t value_size, 79 size_t value_size,
80 struct GNUNET_TIME_Absolute *expiry, 80 struct GNUNET_TIME_Absolute expiry,
81 enum GNUNET_PEERSTORE_StoreOption options, 81 enum GNUNET_PEERSTORE_StoreOption options,
82 uint16_t msg_type) 82 uint16_t msg_type)
83{ 83{
@@ -97,8 +97,7 @@ PEERSTORE_create_record_mq_envelope (const char *sub_system,
97 msg_size = ss_size + key_size + value_size; 97 msg_size = ss_size + key_size + value_size;
98 ev = GNUNET_MQ_msg_extra (srm, msg_size, msg_type); 98 ev = GNUNET_MQ_msg_extra (srm, msg_size, msg_type);
99 srm->key_size = htons (key_size); 99 srm->key_size = htons (key_size);
100 if (NULL != expiry) 100 srm->expiry = GNUNET_TIME_absolute_hton (expiry);
101 srm->expiry = *expiry;
102 if (NULL == peer) 101 if (NULL == peer)
103 srm->peer_set = htons (GNUNET_NO); 102 srm->peer_set = htons (GNUNET_NO);
104 else 103 else
@@ -147,12 +146,9 @@ PEERSTORE_parse_record_message (const struct StoreRecordMessage *srm)
147 record = GNUNET_new (struct GNUNET_PEERSTORE_Record); 146 record = GNUNET_new (struct GNUNET_PEERSTORE_Record);
148 if (GNUNET_YES == ntohs (srm->peer_set)) 147 if (GNUNET_YES == ntohs (srm->peer_set))
149 { 148 {
150 record->peer = GNUNET_new (struct GNUNET_PeerIdentity); 149 record->peer = srm->peer;
151 *record->peer = srm->peer;
152 } 150 }
153 record->expiry = GNUNET_new (struct GNUNET_TIME_Absolute); 151 record->expiry = GNUNET_TIME_absolute_ntoh (srm->expiry);
154
155 *(record->expiry) = srm->expiry;
156 dummy = (char *) &srm[1]; 152 dummy = (char *) &srm[1];
157 if (ss_size > 0) 153 if (ss_size > 0)
158 { 154 {
@@ -167,7 +163,9 @@ PEERSTORE_parse_record_message (const struct StoreRecordMessage *srm)
167 if (value_size > 0) 163 if (value_size > 0)
168 { 164 {
169 record->value = GNUNET_malloc (value_size); 165 record->value = GNUNET_malloc (value_size);
170 GNUNET_memcpy (record->value, dummy, value_size); 166 GNUNET_memcpy (record->value,
167 dummy,
168 value_size);
171 } 169 }
172 record->value_size = value_size; 170 record->value_size = value_size;
173 return record; 171 return record;
@@ -184,8 +182,6 @@ PEERSTORE_destroy_record (struct GNUNET_PEERSTORE_Record *record)
184{ 182{
185 if (NULL != record->sub_system) 183 if (NULL != record->sub_system)
186 GNUNET_free (record->sub_system); 184 GNUNET_free (record->sub_system);
187 if (NULL != record->peer)
188 GNUNET_free (record->peer);
189 if (NULL != record->key) 185 if (NULL != record->key)
190 GNUNET_free (record->key); 186 GNUNET_free (record->key);
191 if (NULL != record->value) 187 if (NULL != record->value)
@@ -193,7 +189,5 @@ PEERSTORE_destroy_record (struct GNUNET_PEERSTORE_Record *record)
193 GNUNET_free (record->value); 189 GNUNET_free (record->value);
194 record->value = 0; 190 record->value = 0;
195 } 191 }
196 if (NULL != record->expiry)
197 GNUNET_free (record->expiry);
198 GNUNET_free (record); 192 GNUNET_free (record);
199} 193}
diff --git a/src/peerstore/peerstore_common.h b/src/peerstore/peerstore_common.h
index 3d938b5da..0fc14d9b4 100644
--- a/src/peerstore/peerstore_common.h
+++ b/src/peerstore/peerstore_common.h
@@ -56,7 +56,7 @@ PEERSTORE_create_record_mq_envelope (const char *sub_system,
56 const char *key, 56 const char *key,
57 const void *value, 57 const void *value,
58 size_t value_size, 58 size_t value_size,
59 struct GNUNET_TIME_Absolute *expiry, 59 struct GNUNET_TIME_Absolute expiry,
60 enum GNUNET_PEERSTORE_StoreOption options, 60 enum GNUNET_PEERSTORE_StoreOption options,
61 uint16_t msg_type); 61 uint16_t msg_type);
62 62
diff --git a/src/peerstore/plugin_peerstore_sqlite.c b/src/peerstore/plugin_peerstore_sqlite.c
index 236be129e..440263d44 100644
--- a/src/peerstore/plugin_peerstore_sqlite.c
+++ b/src/peerstore/plugin_peerstore_sqlite.c
@@ -1,6 +1,6 @@
1/* 1/*
2 * This file is part of GNUnet 2 * This file is part of GNUnet
3 * Copyright (C) 2013 GNUnet e.V. 3 * Copyright (C) 2013, 2017 GNUnet e.V.
4 * 4 *
5 * GNUnet is free software; you can redistribute it and/or modify 5 * GNUnet is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published 6 * it under the terms of the GNU General Public License as published
@@ -22,11 +22,13 @@
22 * @file peerstore/plugin_peerstore_sqlite.c 22 * @file peerstore/plugin_peerstore_sqlite.c
23 * @brief sqlite-based peerstore backend 23 * @brief sqlite-based peerstore backend
24 * @author Omar Tarabai 24 * @author Omar Tarabai
25 * @author Christian Grothoff
25 */ 26 */
26 27
27#include "platform.h" 28#include "platform.h"
28#include "gnunet_peerstore_plugin.h" 29#include "gnunet_peerstore_plugin.h"
29#include "gnunet_peerstore_service.h" 30#include "gnunet_peerstore_service.h"
31#include "gnunet_sq_lib.h"
30#include "peerstore.h" 32#include "peerstore.h"
31#include <sqlite3.h> 33#include <sqlite3.h>
32 34
@@ -111,6 +113,7 @@ struct Plugin
111 113
112}; 114};
113 115
116
114/** 117/**
115 * Delete records with the given key 118 * Delete records with the given key
116 * 119 *
@@ -118,40 +121,50 @@ struct Plugin
118 * @param sub_system name of sub system 121 * @param sub_system name of sub system
119 * @param peer Peer identity (can be NULL) 122 * @param peer Peer identity (can be NULL)
120 * @param key entry key string (can be NULL) 123 * @param key entry key string (can be NULL)
121 * @return number of deleted records 124 * @return number of deleted records, #GNUNE_SYSERR on error
122 */ 125 */
123static int 126static int
124peerstore_sqlite_delete_records (void *cls, const char *sub_system, 127peerstore_sqlite_delete_records (void *cls,
128 const char *sub_system,
125 const struct GNUNET_PeerIdentity *peer, 129 const struct GNUNET_PeerIdentity *peer,
126 const char *key) 130 const char *key)
127{ 131{
128 struct Plugin *plugin = cls; 132 struct Plugin *plugin = cls;
129 sqlite3_stmt *stmt = plugin->delete_peerstoredata; 133 sqlite3_stmt *stmt = plugin->delete_peerstoredata;
134 struct GNUNET_SQ_QueryParam params[] = {
135 GNUNET_SQ_query_param_string (sub_system),
136 GNUNET_SQ_query_param_auto_from_type (peer),
137 GNUNET_SQ_query_param_string (key),
138 GNUNET_SQ_query_param_end
139 };
140 int ret;
130 141
131 if ((SQLITE_OK != 142 if (GNUNET_OK !=
132 sqlite3_bind_text (stmt, 1, sub_system, strlen (sub_system) + 1, 143 GNUNET_SQ_bind (stmt,
133 SQLITE_STATIC)) || 144 params))
134 (SQLITE_OK !=
135 sqlite3_bind_blob (stmt, 2, peer, sizeof (struct GNUNET_PeerIdentity),
136 SQLITE_STATIC)) ||
137 (SQLITE_OK !=
138 sqlite3_bind_text (stmt, 3, key, strlen (key) + 1, SQLITE_STATIC)))
139 { 145 {
140 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 146 LOG_SQLITE (plugin,
147 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
141 "sqlite3_bind"); 148 "sqlite3_bind");
149 GNUNET_SQ_reset (plugin->dbh,
150 stmt);
151 return GNUNET_SYSERR;
142 } 152 }
143 else if (SQLITE_DONE != sqlite3_step (stmt)) 153 if (SQLITE_DONE !=
154 sqlite3_step (stmt))
144 { 155 {
145 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 156 LOG_SQLITE (plugin,
157 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
146 "sqlite3_step"); 158 "sqlite3_step");
159 ret = GNUNET_SYSERR;
147 } 160 }
148 if (SQLITE_OK != sqlite3_reset (stmt)) 161 else
149 { 162 {
150 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 163 ret = sqlite3_changes (plugin->dbh);
151 "sqlite3_reset");
152 return 0;
153 } 164 }
154 return sqlite3_changes (plugin->dbh); 165 GNUNET_SQ_reset (plugin->dbh,
166 stmt);
167 return ret;
155} 168}
156 169
157 170
@@ -172,28 +185,36 @@ peerstore_sqlite_expire_records (void *cls, struct GNUNET_TIME_Absolute now,
172{ 185{
173 struct Plugin *plugin = cls; 186 struct Plugin *plugin = cls;
174 sqlite3_stmt *stmt = plugin->expire_peerstoredata; 187 sqlite3_stmt *stmt = plugin->expire_peerstoredata;
188 struct GNUNET_SQ_QueryParam params[] = {
189 GNUNET_SQ_query_param_absolute_time (&now),
190 GNUNET_SQ_query_param_end
191 };
175 192
176 if (SQLITE_OK != 193 if (GNUNET_OK !=
177 sqlite3_bind_int64 (stmt, 1, (sqlite3_uint64) now.abs_value_us)) 194 GNUNET_SQ_bind (stmt,
195 params))
178 { 196 {
179 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 197 LOG_SQLITE (plugin,
198 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
180 "sqlite3_bind"); 199 "sqlite3_bind");
200 GNUNET_SQ_reset (plugin->dbh,
201 stmt);
202 return GNUNET_SYSERR;
181 } 203 }
182 else if (SQLITE_DONE != sqlite3_step (stmt)) 204 if (SQLITE_DONE != sqlite3_step (stmt))
183 { 205 {
184 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 206 LOG_SQLITE (plugin,
207 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
185 "sqlite3_step"); 208 "sqlite3_step");
186 } 209 GNUNET_SQ_reset (plugin->dbh,
187 if (SQLITE_OK != sqlite3_reset (stmt)) 210 stmt);
188 {
189 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
190 "sqlite3_reset");
191 return GNUNET_SYSERR; 211 return GNUNET_SYSERR;
192 } 212 }
193 if (NULL != cont) 213 if (NULL != cont)
194 { 214 cont (cont_cls,
195 cont (cont_cls, sqlite3_changes (plugin->dbh)); 215 sqlite3_changes (plugin->dbh));
196 } 216 GNUNET_SQ_reset (plugin->dbh,
217 stmt);
197 return GNUNET_OK; 218 return GNUNET_OK;
198} 219}
199 220
@@ -224,94 +245,115 @@ peerstore_sqlite_iterate_records (void *cls,
224 sqlite3_stmt *stmt; 245 sqlite3_stmt *stmt;
225 int err = 0; 246 int err = 0;
226 int sret; 247 int sret;
227 struct GNUNET_PEERSTORE_Record *ret; 248 struct GNUNET_PEERSTORE_Record rec;
228 249
229 LOG (GNUNET_ERROR_TYPE_DEBUG, "Executing iterate request on sqlite db.\n"); 250 LOG (GNUNET_ERROR_TYPE_DEBUG,
230 if (NULL == peer && NULL == key) 251 "Executing iterate request on sqlite db.\n");
231 { 252 if (NULL == peer)
232 stmt = plugin->select_peerstoredata;
233 err =
234 (SQLITE_OK !=
235 sqlite3_bind_text (stmt, 1, sub_system, strlen (sub_system) + 1,
236 SQLITE_STATIC));
237 }
238 else if (NULL == key)
239 {
240 stmt = plugin->select_peerstoredata_by_pid;
241 err =
242 (SQLITE_OK !=
243 sqlite3_bind_text (stmt, 1, sub_system, strlen (sub_system) + 1,
244 SQLITE_STATIC)) ||
245 (SQLITE_OK !=
246 sqlite3_bind_blob (stmt, 2, peer, sizeof (struct GNUNET_PeerIdentity),
247 SQLITE_STATIC));
248 }
249 else if (NULL == peer)
250 { 253 {
251 stmt = plugin->select_peerstoredata_by_key; 254 if (NULL == key)
252 err = 255 {
253 (SQLITE_OK != 256 struct GNUNET_SQ_QueryParam params[] = {
254 sqlite3_bind_text (stmt, 1, sub_system, strlen (sub_system) + 1, 257 GNUNET_SQ_query_param_string (sub_system),
255 SQLITE_STATIC)) || 258 GNUNET_SQ_query_param_end
256 (SQLITE_OK != 259 };
257 sqlite3_bind_text (stmt, 2, key, strlen (key) + 1, SQLITE_STATIC)); 260
261 stmt = plugin->select_peerstoredata;
262 err = GNUNET_SQ_bind (stmt,
263 params);
264 }
265 else
266 {
267 struct GNUNET_SQ_QueryParam params[] = {
268 GNUNET_SQ_query_param_string (sub_system),
269 GNUNET_SQ_query_param_string (key),
270 GNUNET_SQ_query_param_end
271 };
272
273 stmt = plugin->select_peerstoredata_by_key;
274 err = GNUNET_SQ_bind (stmt,
275 params);
276 }
258 } 277 }
259 else 278 else
260 { 279 {
261 stmt = plugin->select_peerstoredata_by_all; 280 if (NULL == key)
262 err = 281 {
263 (SQLITE_OK != 282 struct GNUNET_SQ_QueryParam params[] = {
264 sqlite3_bind_text (stmt, 1, sub_system, strlen (sub_system) + 1, 283 GNUNET_SQ_query_param_string (sub_system),
265 SQLITE_STATIC)) || 284 GNUNET_SQ_query_param_auto_from_type (peer),
266 (SQLITE_OK != 285 GNUNET_SQ_query_param_end
267 sqlite3_bind_blob (stmt, 2, peer, sizeof (struct GNUNET_PeerIdentity), 286 };
268 SQLITE_STATIC)) || 287
269 (SQLITE_OK != 288 stmt = plugin->select_peerstoredata_by_pid;
270 sqlite3_bind_text (stmt, 3, key, strlen (key) + 1, SQLITE_STATIC)); 289 err = GNUNET_SQ_bind (stmt,
290 params);
291 }
292 else
293 {
294 struct GNUNET_SQ_QueryParam params[] = {
295 GNUNET_SQ_query_param_string (sub_system),
296 GNUNET_SQ_query_param_auto_from_type (peer),
297 GNUNET_SQ_query_param_string (key),
298 GNUNET_SQ_query_param_end
299 };
300
301 stmt = plugin->select_peerstoredata_by_all;
302 err = GNUNET_SQ_bind (stmt,
303 params);
304 }
271 } 305 }
272 306
273 if (err) 307 if (GNUNET_OK != err)
274 { 308 {
275 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 309 LOG_SQLITE (plugin,
310 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
276 "sqlite3_bind_XXXX"); 311 "sqlite3_bind_XXXX");
277 if (SQLITE_OK != sqlite3_reset (stmt)) 312 GNUNET_SQ_reset (plugin->dbh,
278 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 313 stmt);
279 "sqlite3_reset");
280 return GNUNET_SYSERR; 314 return GNUNET_SYSERR;
281 } 315 }
316
317 err = 0;
282 while (SQLITE_ROW == (sret = sqlite3_step (stmt))) 318 while (SQLITE_ROW == (sret = sqlite3_step (stmt)))
283 { 319 {
284 LOG (GNUNET_ERROR_TYPE_DEBUG, "Returning a matched record.\n"); 320 LOG (GNUNET_ERROR_TYPE_DEBUG,
285 ret = GNUNET_new (struct GNUNET_PEERSTORE_Record); 321 "Returning a matched record.\n");
286 322 struct GNUNET_SQ_ResultSpec rs[] = {
287 ret->sub_system = (char *) sqlite3_column_text (stmt, 0); 323 GNUNET_SQ_result_spec_string (&rec.sub_system),
288 ret->peer = (struct GNUNET_PeerIdentity *) sqlite3_column_blob (stmt, 1); 324 GNUNET_SQ_result_spec_auto_from_type (&rec.peer),
289 ret->key = (char *) sqlite3_column_text (stmt, 2); 325 GNUNET_SQ_result_spec_string (&rec.key),
290 ret->value = (void *) sqlite3_column_blob (stmt, 3); 326 GNUNET_SQ_result_spec_variable_size (&rec.value, &rec.value_size),
291 ret->value_size = sqlite3_column_bytes (stmt, 3); 327 GNUNET_SQ_result_spec_absolute_time (&rec.expiry),
292 ret->expiry = GNUNET_new (struct GNUNET_TIME_Absolute); 328 GNUNET_SQ_result_spec_end
293 329 };
294 ret->expiry->abs_value_us = (uint64_t) sqlite3_column_int64 (stmt, 4); 330
331 if (GNUNET_OK !=
332 GNUNET_SQ_extract_result (stmt,
333 rs))
334 {
335 GNUNET_break (0);
336 break;
337 }
295 if (NULL != iter) 338 if (NULL != iter)
296 iter (iter_cls, ret, NULL); 339 iter (iter_cls,
297 GNUNET_free (ret->expiry); 340 &rec,
298 GNUNET_free (ret); 341 NULL);
342 GNUNET_SQ_cleanup_result (rs);
299 } 343 }
300 if (SQLITE_DONE != sret) 344 if (SQLITE_DONE != sret)
301 { 345 {
302 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite_step"); 346 LOG_SQLITE (plugin,
303 err = 1; 347 GNUNET_ERROR_TYPE_ERROR,
304 } 348 "sqlite_step");
305 if (SQLITE_OK != sqlite3_reset (stmt))
306 {
307 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
308 "sqlite3_reset");
309 err = 1; 349 err = 1;
310 } 350 }
351 GNUNET_SQ_reset (plugin->dbh,
352 stmt);
311 if (NULL != iter) 353 if (NULL != iter)
312 { 354 iter (iter_cls,
313 iter (iter_cls, NULL, err ? "sqlite error" : NULL); 355 NULL,
314 } 356 err ? "sqlite error" : NULL);
315 return GNUNET_OK; 357 return GNUNET_OK;
316} 358}
317 359
@@ -347,6 +389,14 @@ peerstore_sqlite_store_record (void *cls,
347{ 389{
348 struct Plugin *plugin = cls; 390 struct Plugin *plugin = cls;
349 sqlite3_stmt *stmt = plugin->insert_peerstoredata; 391 sqlite3_stmt *stmt = plugin->insert_peerstoredata;
392 struct GNUNET_SQ_QueryParam params[] = {
393 GNUNET_SQ_query_param_string (sub_system),
394 GNUNET_SQ_query_param_auto_from_type (peer),
395 GNUNET_SQ_query_param_string (key),
396 GNUNET_SQ_query_param_fixed_size (value, size),
397 GNUNET_SQ_query_param_absolute_time (&expiry),
398 GNUNET_SQ_query_param_end
399 };
350 400
351 if (GNUNET_PEERSTORE_STOREOPTION_REPLACE == options) 401 if (GNUNET_PEERSTORE_STOREOPTION_REPLACE == options)
352 { 402 {
@@ -355,34 +405,23 @@ peerstore_sqlite_store_record (void *cls,
355 peer, 405 peer,
356 key); 406 key);
357 } 407 }
358 if (SQLITE_OK != 408 if (GNUNET_OK !=
359 sqlite3_bind_text (stmt, 1, sub_system, strlen (sub_system) + 1, 409 GNUNET_SQ_bind (stmt,
360 SQLITE_STATIC) || 410 params))
361 SQLITE_OK != sqlite3_bind_blob (stmt, 2, peer, 411 LOG_SQLITE (plugin,
362 sizeof (struct GNUNET_PeerIdentity), 412 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
363 SQLITE_STATIC) ||
364 SQLITE_OK != sqlite3_bind_text (stmt, 3, key, strlen (key) + 1,
365 SQLITE_STATIC) ||
366 SQLITE_OK != sqlite3_bind_blob (stmt, 4, value, size, SQLITE_STATIC) ||
367 SQLITE_OK != sqlite3_bind_int64 (stmt, 5,
368 (sqlite3_uint64) expiry.abs_value_us))
369 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
370 "sqlite3_bind"); 413 "sqlite3_bind");
371 else if (SQLITE_DONE != sqlite3_step (stmt)) 414 else if (SQLITE_DONE != sqlite3_step (stmt))
372 { 415 {
373 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 416 LOG_SQLITE (plugin,
417 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
374 "sqlite3_step"); 418 "sqlite3_step");
375 } 419 }
376 if (SQLITE_OK != sqlite3_reset (stmt)) 420 GNUNET_SQ_reset (plugin->dbh,
377 { 421 stmt);
378 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
379 "sqlite3_reset");
380 return GNUNET_SYSERR;
381 }
382 if (NULL != cont) 422 if (NULL != cont)
383 { 423 cont (cont_cls,
384 cont (cont_cls, GNUNET_OK); 424 GNUNET_OK);
385 }
386 return GNUNET_OK; 425 return GNUNET_OK;
387} 426}
388 427
@@ -454,24 +493,6 @@ sql_prepare (sqlite3 *dbh,
454 493
455 494
456/** 495/**
457 * sqlite3 custom function for comparison of uint64_t values
458 * since it is not supported by default
459 */
460static void
461sqlite3_lessthan (sqlite3_context *ctx,
462 int dummy,
463 sqlite3_value **values)
464{
465 uint64_t v1;
466 uint64_t v2;
467
468 v1 = (uint64_t) sqlite3_value_int64 (values[0]);
469 v2 = (uint64_t) sqlite3_value_int64 (values[1]);
470 sqlite3_result_int (ctx, v1 < v2);
471}
472
473
474/**
475 * Initialize the database connections and associated 496 * Initialize the database connections and associated
476 * data structures (create tables and indices 497 * data structures (create tables and indices
477 * as needed as well). 498 * as needed as well).
@@ -532,17 +553,11 @@ database_setup (struct Plugin *plugin)
532 /* Create tables */ 553 /* Create tables */
533 sql_exec (plugin->dbh, 554 sql_exec (plugin->dbh,
534 "CREATE TABLE IF NOT EXISTS peerstoredata (\n" 555 "CREATE TABLE IF NOT EXISTS peerstoredata (\n"
535 " sub_system TEXT NOT NULL,\n" " peer_id BLOB NOT NULL,\n" 556 " sub_system TEXT NOT NULL,\n"
536 " key TEXT NOT NULL,\n" " value BLOB NULL,\n" 557 " peer_id BLOB NOT NULL,\n"
537 " expiry sqlite3_uint64 NOT NULL" ");"); 558 " key TEXT NOT NULL,\n"
538 sqlite3_create_function (plugin->dbh, 559 " value BLOB NULL,\n"
539 "UINT64_LT", 560 " expiry INT8 NOT NULL" ");");
540 2,
541 SQLITE_UTF8,
542 NULL,
543 &sqlite3_lessthan,
544 NULL,
545 NULL);
546 /* Create Indices */ 561 /* Create Indices */
547 if (SQLITE_OK != 562 if (SQLITE_OK !=
548 sqlite3_exec (plugin->dbh, 563 sqlite3_exec (plugin->dbh,
@@ -559,26 +574,35 @@ database_setup (struct Plugin *plugin)
559 /* Prepare statements */ 574 /* Prepare statements */
560 575
561 sql_prepare (plugin->dbh, 576 sql_prepare (plugin->dbh,
562 "INSERT INTO peerstoredata (sub_system, peer_id, key, value, expiry) VALUES (?,?,?,?,?);", 577 "INSERT INTO peerstoredata (sub_system, peer_id, key, value, expiry)"
578 " VALUES (?,?,?,?,?);",
563 &plugin->insert_peerstoredata); 579 &plugin->insert_peerstoredata);
564 sql_prepare (plugin->dbh, 580 sql_prepare (plugin->dbh,
565 "SELECT * FROM peerstoredata" " WHERE sub_system = ?", 581 "SELECT sub_system,peer_id,key,value,expiry FROM peerstoredata"
582 " WHERE sub_system = ?",
566 &plugin->select_peerstoredata); 583 &plugin->select_peerstoredata);
567 sql_prepare (plugin->dbh, 584 sql_prepare (plugin->dbh,
568 "SELECT * FROM peerstoredata" " WHERE sub_system = ?" 585 "SELECT sub_system,peer_id,key,value,expiry FROM peerstoredata"
569 " AND peer_id = ?", &plugin->select_peerstoredata_by_pid); 586 " WHERE sub_system = ?"
587 " AND peer_id = ?",
588 &plugin->select_peerstoredata_by_pid);
570 sql_prepare (plugin->dbh, 589 sql_prepare (plugin->dbh,
571 "SELECT * FROM peerstoredata" " WHERE sub_system = ?" 590 "SELECT sub_system,peer_id,key,value,expiry FROM peerstoredata"
572 " AND key = ?", &plugin->select_peerstoredata_by_key); 591 " WHERE sub_system = ?"
592 " AND key = ?",
593 &plugin->select_peerstoredata_by_key);
573 sql_prepare (plugin->dbh, 594 sql_prepare (plugin->dbh,
574 "SELECT * FROM peerstoredata" " WHERE sub_system = ?" 595 "SELECT sub_system,peer_id,key,value,expiry FROM peerstoredata"
596 " WHERE sub_system = ?"
575 " AND peer_id = ?" " AND key = ?", 597 " AND peer_id = ?" " AND key = ?",
576 &plugin->select_peerstoredata_by_all); 598 &plugin->select_peerstoredata_by_all);
577 sql_prepare (plugin->dbh, 599 sql_prepare (plugin->dbh,
578 "DELETE FROM peerstoredata" " WHERE UINT64_LT(expiry, ?)", 600 "DELETE FROM peerstoredata"
601 " WHERE expiry < ?",
579 &plugin->expire_peerstoredata); 602 &plugin->expire_peerstoredata);
580 sql_prepare (plugin->dbh, 603 sql_prepare (plugin->dbh,
581 "DELETE FROM peerstoredata" " WHERE sub_system = ?" 604 "DELETE FROM peerstoredata"
605 " WHERE sub_system = ?"
582 " AND peer_id = ?" " AND key = ?", 606 " AND peer_id = ?" " AND key = ?",
583 &plugin->delete_peerstoredata); 607 &plugin->delete_peerstoredata);
584 return GNUNET_OK; 608 return GNUNET_OK;
@@ -596,15 +620,20 @@ database_shutdown (struct Plugin *plugin)
596 int result; 620 int result;
597 sqlite3_stmt *stmt; 621 sqlite3_stmt *stmt;
598 622
599 while (NULL != (stmt = sqlite3_next_stmt (plugin->dbh, NULL))) 623 while (NULL != (stmt = sqlite3_next_stmt (plugin->dbh,
624 NULL)))
600 { 625 {
601 result = sqlite3_finalize (stmt); 626 result = sqlite3_finalize (stmt);
602 if (SQLITE_OK != result) 627 if (SQLITE_OK != result)
603 LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to close statement %p: %d\n", 628 LOG (GNUNET_ERROR_TYPE_WARNING,
604 stmt, result); 629 "Failed to close statement %p: %d\n",
630 stmt,
631 result);
605 } 632 }
606 if (SQLITE_OK != sqlite3_close (plugin->dbh)) 633 if (SQLITE_OK != sqlite3_close (plugin->dbh))
607 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_close"); 634 LOG_SQLITE (plugin,
635 GNUNET_ERROR_TYPE_ERROR,
636 "sqlite3_close");
608 GNUNET_free_non_null (plugin->fn); 637 GNUNET_free_non_null (plugin->fn);
609} 638}
610 639
diff --git a/src/peerstore/test_peerstore_api_iterate.c b/src/peerstore/test_peerstore_api_iterate.c
index 83a6bf7b7..c607d9fb3 100644
--- a/src/peerstore/test_peerstore_api_iterate.c
+++ b/src/peerstore/test_peerstore_api_iterate.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 3 Copyright (C) 2013-2017 GNUnet e.V.
4 4
5 GNUnet is free software; you can redistribute it and/or modify 5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published 6 it under the terms of the GNU General Public License as published
@@ -42,7 +42,8 @@ static int count = 0;
42 42
43 43
44static void 44static void
45iter3_cb (void *cls, const struct GNUNET_PEERSTORE_Record *record, 45iter3_cb (void *cls,
46 const struct GNUNET_PEERSTORE_Record *record,
46 const char *emsg) 47 const char *emsg)
47{ 48{
48 if (NULL != emsg) 49 if (NULL != emsg)
@@ -63,7 +64,8 @@ iter3_cb (void *cls, const struct GNUNET_PEERSTORE_Record *record,
63 64
64 65
65static void 66static void
66iter2_cb (void *cls, const struct GNUNET_PEERSTORE_Record *record, 67iter2_cb (void *cls,
68 const struct GNUNET_PEERSTORE_Record *record,
67 const char *emsg) 69 const char *emsg)
68{ 70{
69 if (NULL != emsg) 71 if (NULL != emsg)
@@ -78,13 +80,19 @@ iter2_cb (void *cls, const struct GNUNET_PEERSTORE_Record *record,
78 } 80 }
79 GNUNET_assert (count == 2); 81 GNUNET_assert (count == 2);
80 count = 0; 82 count = 0;
81 ic = GNUNET_PEERSTORE_iterate (h, ss, NULL, NULL, GNUNET_TIME_UNIT_FOREVER_REL, 83 ic = GNUNET_PEERSTORE_iterate (h,
82 iter3_cb, NULL); 84 ss,
85 NULL,
86 NULL,
87 GNUNET_TIME_UNIT_FOREVER_REL,
88 &iter3_cb,
89 NULL);
83} 90}
84 91
85 92
86static void 93static void
87iter1_cb (void *cls, const struct GNUNET_PEERSTORE_Record *record, 94iter1_cb (void *cls,
95 const struct GNUNET_PEERSTORE_Record *record,
88 const char *emsg) 96 const char *emsg)
89{ 97{
90 if (NULL != emsg) 98 if (NULL != emsg)
@@ -99,30 +107,61 @@ iter1_cb (void *cls, const struct GNUNET_PEERSTORE_Record *record,
99 } 107 }
100 GNUNET_assert (count == 1); 108 GNUNET_assert (count == 1);
101 count = 0; 109 count = 0;
102 ic = GNUNET_PEERSTORE_iterate (h, ss, &p1, NULL, GNUNET_TIME_UNIT_FOREVER_REL, 110 ic = GNUNET_PEERSTORE_iterate (h,
103 iter2_cb, NULL); 111 ss,
112 &p1,
113 NULL,
114 GNUNET_TIME_UNIT_FOREVER_REL,
115 iter2_cb,
116 NULL);
104} 117}
105 118
106 119
107static void 120static void
108run (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg, 121run (void *cls,
122 const struct GNUNET_CONFIGURATION_Handle *cfg,
109 struct GNUNET_TESTING_Peer *peer) 123 struct GNUNET_TESTING_Peer *peer)
110{ 124{
111 h = GNUNET_PEERSTORE_connect (cfg); 125 h = GNUNET_PEERSTORE_connect (cfg);
112 GNUNET_assert (NULL != h); 126 GNUNET_assert (NULL != h);
113 memset (&p1, 1, sizeof (p1)); 127 memset (&p1, 1, sizeof (p1));
114 memset (&p2, 2, sizeof (p2)); 128 memset (&p2, 2, sizeof (p2));
115 GNUNET_PEERSTORE_store (h, ss, &p1, k1, val, strlen (val) + 1, 129 GNUNET_PEERSTORE_store (h,
130 ss,
131 &p1,
132 k1,
133 val,
134 strlen (val) + 1,
116 GNUNET_TIME_UNIT_FOREVER_ABS, 135 GNUNET_TIME_UNIT_FOREVER_ABS,
117 GNUNET_PEERSTORE_STOREOPTION_REPLACE, NULL, NULL); 136 GNUNET_PEERSTORE_STOREOPTION_REPLACE,
118 GNUNET_PEERSTORE_store (h, ss, &p1, k2, val, strlen (val) + 1, 137 NULL,
138 NULL);
139 GNUNET_PEERSTORE_store (h,
140 ss,
141 &p1,
142 k2,
143 val,
144 strlen (val) + 1,
119 GNUNET_TIME_UNIT_FOREVER_ABS, 145 GNUNET_TIME_UNIT_FOREVER_ABS,
120 GNUNET_PEERSTORE_STOREOPTION_REPLACE, NULL, NULL); 146 GNUNET_PEERSTORE_STOREOPTION_REPLACE,
121 GNUNET_PEERSTORE_store (h, ss, &p2, k3, val, strlen (val) + 1, 147 NULL,
148 NULL);
149 GNUNET_PEERSTORE_store (h,
150 ss,
151 &p2,
152 k3,
153 val,
154 strlen (val) + 1,
122 GNUNET_TIME_UNIT_FOREVER_ABS, 155 GNUNET_TIME_UNIT_FOREVER_ABS,
123 GNUNET_PEERSTORE_STOREOPTION_REPLACE, NULL, NULL); 156 GNUNET_PEERSTORE_STOREOPTION_REPLACE,
124 ic = GNUNET_PEERSTORE_iterate (h, ss, &p1, k1, GNUNET_TIME_UNIT_FOREVER_REL, 157 NULL,
125 iter1_cb, NULL); 158 NULL);
159 ic = GNUNET_PEERSTORE_iterate (h,
160 ss,
161 &p1,
162 k1,
163 GNUNET_TIME_UNIT_FOREVER_REL,
164 &iter1_cb, NULL);
126} 165}
127 166
128 167
diff --git a/src/peerstore/test_peerstore_api_store.c b/src/peerstore/test_peerstore_api_store.c
index bfe1b5b55..978009dbb 100644
--- a/src/peerstore/test_peerstore_api_store.c
+++ b/src/peerstore/test_peerstore_api_store.c
@@ -39,6 +39,7 @@ static char *val3 = "test_peerstore_api_store_val3--";
39 39
40static int count = 0; 40static int count = 0;
41 41
42
42static void 43static void
43test3_cont2 (void *cls, 44test3_cont2 (void *cls,
44 const struct GNUNET_PEERSTORE_Record *record, 45 const struct GNUNET_PEERSTORE_Record *record,
@@ -49,7 +50,8 @@ test3_cont2 (void *cls,
49 if (NULL != record) 50 if (NULL != record)
50 { 51 {
51 GNUNET_assert ((strlen (val3) + 1) == record->value_size); 52 GNUNET_assert ((strlen (val3) + 1) == record->value_size);
52 GNUNET_assert (0 == strcmp ((char *) val3, (char *) record->value)); 53 GNUNET_assert (0 == strcmp ((char *) val3,
54 (char *) record->value));
53 count++; 55 count++;
54 return; 56 return;
55 } 57 }
@@ -61,7 +63,8 @@ test3_cont2 (void *cls,
61 63
62 64
63static void 65static void
64test3_cont (void *cls, int success) 66test3_cont (void *cls,
67 int success)
65{ 68{
66 if (GNUNET_YES != success) 69 if (GNUNET_YES != success)
67 return; 70 return;
@@ -71,7 +74,8 @@ test3_cont (void *cls, int success)
71 &pid, 74 &pid,
72 key, 75 key,
73 GNUNET_TIME_UNIT_SECONDS, 76 GNUNET_TIME_UNIT_SECONDS,
74 &test3_cont2, NULL); 77 &test3_cont2,
78 NULL);
75} 79}
76 80
77 81
@@ -81,9 +85,15 @@ test3_cont (void *cls, int success)
81static void 85static void
82test3 () 86test3 ()
83{ 87{
84 GNUNET_PEERSTORE_store (h, subsystem, &pid, key, val3, strlen (val3) + 1, 88 GNUNET_PEERSTORE_store (h,
89 subsystem,
90 &pid,
91 key,
92 val3,
93 strlen (val3) + 1,
85 GNUNET_TIME_UNIT_FOREVER_ABS, 94 GNUNET_TIME_UNIT_FOREVER_ABS,
86 GNUNET_PEERSTORE_STOREOPTION_REPLACE, &test3_cont, 95 GNUNET_PEERSTORE_STOREOPTION_REPLACE,
96 &test3_cont,
87 NULL); 97 NULL);
88} 98}
89 99
@@ -130,9 +140,15 @@ test2_cont (void *cls, int success)
130void 140void
131test2 () 141test2 ()
132{ 142{
133 GNUNET_PEERSTORE_store (h, subsystem, &pid, key, val2, strlen (val2) + 1, 143 GNUNET_PEERSTORE_store (h,
144 subsystem,
145 &pid,
146 key,
147 val2,
148 strlen (val2) + 1,
134 GNUNET_TIME_UNIT_FOREVER_ABS, 149 GNUNET_TIME_UNIT_FOREVER_ABS,
135 GNUNET_PEERSTORE_STOREOPTION_MULTIPLE, &test2_cont, 150 GNUNET_PEERSTORE_STOREOPTION_MULTIPLE,
151 &test2_cont,
136 NULL); 152 NULL);
137} 153}
138 154
@@ -163,8 +179,13 @@ test1_cont (void *cls, int success)
163 if (GNUNET_YES != success) 179 if (GNUNET_YES != success)
164 return; 180 return;
165 count = 0; 181 count = 0;
166 GNUNET_PEERSTORE_iterate (h, subsystem, &pid, key, GNUNET_TIME_UNIT_SECONDS, 182 GNUNET_PEERSTORE_iterate (h,
167 &test1_cont2, NULL); 183 subsystem,
184 &pid,
185 key,
186 GNUNET_TIME_UNIT_SECONDS,
187 &test1_cont2,
188 NULL);
168} 189}
169 190
170 191
@@ -174,9 +195,15 @@ test1_cont (void *cls, int success)
174static void 195static void
175test1 () 196test1 ()
176{ 197{
177 GNUNET_PEERSTORE_store (h, subsystem, &pid, key, val1, strlen (val1) + 1, 198 GNUNET_PEERSTORE_store (h,
199 subsystem,
200 &pid,
201 key,
202 val1,
203 strlen (val1) + 1,
178 GNUNET_TIME_UNIT_FOREVER_ABS, 204 GNUNET_TIME_UNIT_FOREVER_ABS,
179 GNUNET_PEERSTORE_STOREOPTION_REPLACE, &test1_cont, 205 GNUNET_PEERSTORE_STOREOPTION_REPLACE,
206 &test1_cont,
180 NULL); 207 NULL);
181} 208}
182 209
@@ -196,8 +223,10 @@ int
196main (int argc, char *argv[]) 223main (int argc, char *argv[])
197{ 224{
198 if (0 != 225 if (0 !=
199 GNUNET_TESTING_service_run ("test-gnunet-peerstore", "peerstore", 226 GNUNET_TESTING_service_run ("test-gnunet-peerstore",
200 "test_peerstore_api_data.conf", &run, NULL)) 227 "peerstore",
228 "test_peerstore_api_data.conf",
229 &run, NULL))
201 return 1; 230 return 1;
202 return ok; 231 return ok;
203} 232}
diff --git a/src/peerstore/test_plugin_peerstore.c b/src/peerstore/test_plugin_peerstore.c
index 179e32b52..62c06be8e 100644
--- a/src/peerstore/test_plugin_peerstore.c
+++ b/src/peerstore/test_plugin_peerstore.c
@@ -36,6 +36,11 @@ static int ok;
36static const char *plugin_name; 36static const char *plugin_name;
37 37
38 38
39static struct GNUNET_PEERSTORE_PluginFunctions *psp;
40
41static struct GNUNET_PeerIdentity p1;
42
43
39/** 44/**
40 * Function called when the service shuts down. Unloads our namestore 45 * Function called when the service shuts down. Unloads our namestore
41 * plugin. 46 * plugin.
@@ -47,8 +52,12 @@ unload_plugin (struct GNUNET_PEERSTORE_PluginFunctions *api)
47{ 52{
48 char *libname; 53 char *libname;
49 54
50 GNUNET_asprintf (&libname, "libgnunet_plugin_peer_%s", plugin_name); 55 GNUNET_asprintf (&libname,
51 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api)); 56 "libgnunet_plugin_peer_%s",
57 plugin_name);
58 GNUNET_break (NULL ==
59 GNUNET_PLUGIN_unload (libname,
60 api));
52 GNUNET_free (libname); 61 GNUNET_free (libname);
53} 62}
54 63
@@ -65,12 +74,18 @@ load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
65 struct GNUNET_PEERSTORE_PluginFunctions *ret; 74 struct GNUNET_PEERSTORE_PluginFunctions *ret;
66 char *libname; 75 char *libname;
67 76
68 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' peer plugin\n"), 77 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
78 _("Loading `%s' peer plugin\n"),
69 plugin_name); 79 plugin_name);
70 GNUNET_asprintf (&libname, "libgnunet_plugin_peerstore_%s", plugin_name); 80 GNUNET_asprintf (&libname,
71 if (NULL == (ret = GNUNET_PLUGIN_load (libname, (void*) cfg))) 81 "libgnunet_plugin_peerstore_%s",
82 plugin_name);
83 if (NULL == (ret = GNUNET_PLUGIN_load (libname,
84 (void*) cfg)))
72 { 85 {
73 FPRINTF (stderr, "Failed to load plugin `%s'!\n", plugin_name); 86 FPRINTF (stderr,
87 "Failed to load plugin `%s'!\n",
88 plugin_name);
74 GNUNET_free (libname); 89 GNUNET_free (libname);
75 return NULL; 90 return NULL;
76 } 91 }
@@ -81,19 +96,28 @@ load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
81 96
82static void 97static void
83test_record (void *cls, 98test_record (void *cls,
84 const struct GNUNET_PEERSTORE_Record *record, 99 const struct GNUNET_PEERSTORE_Record *record,
85 const char *error) 100 const char *error)
86{ 101{
87 struct GNUNET_PeerIdentity *id = cls; 102 const struct GNUNET_PeerIdentity *id = cls;
88 char* testval = "test_val"; 103 const char* testval = "test_val";
89 104
90 if (NULL == record) 105 if (NULL == record)
106 {
107 unload_plugin (psp);
91 return; 108 return;
92 109 }
93 GNUNET_assert (0 == memcmp (record->peer, id, sizeof (struct GNUNET_PeerIdentity))); 110 GNUNET_assert (0 == memcmp (&record->peer,
94 GNUNET_assert (0 == strcmp ("subsys", record->sub_system)); 111 id,
95 GNUNET_assert (0 == strcmp ("key", record->key)); 112 sizeof (struct GNUNET_PeerIdentity)));
96 GNUNET_assert (0 == memcmp (testval, record->value, strlen (testval))); 113 GNUNET_assert (0 == strcmp ("subsys",
114 record->sub_system));
115 GNUNET_assert (0 == strcmp ("key",
116 record->key));
117 GNUNET_assert (0 == memcmp (testval,
118 record->value,
119 strlen (testval)));
120 ok = 0;
97} 121}
98 122
99 123
@@ -101,38 +125,52 @@ static void
101get_record (struct GNUNET_PEERSTORE_PluginFunctions *psp, 125get_record (struct GNUNET_PEERSTORE_PluginFunctions *psp,
102 const struct GNUNET_PeerIdentity *identity) 126 const struct GNUNET_PeerIdentity *identity)
103{ 127{
104 GNUNET_assert (GNUNET_OK == psp->iterate_records (psp->cls, 128 GNUNET_assert (GNUNET_OK ==
105 "subsys", identity, "key", &test_record, (void*)identity)); 129 psp->iterate_records (psp->cls,
130 "subsys",
131 identity,
132 "key",
133 &test_record,
134 (void*)identity));
106} 135}
107 136
137
108static void 138static void
109store_cont (void *cls, int status) 139store_cont (void *cls,
140 int status)
110{ 141{
111 GNUNET_assert (GNUNET_OK == status); 142 GNUNET_assert (GNUNET_OK == status);
143 get_record (psp,
144 &p1);
112} 145}
113 146
147
114static void 148static void
115put_record (struct GNUNET_PEERSTORE_PluginFunctions *psp, struct GNUNET_PeerIdentity *identity) 149put_record (struct GNUNET_PEERSTORE_PluginFunctions *psp,
150 const struct GNUNET_PeerIdentity *identity)
116{ 151{
117 GNUNET_assert (GNUNET_OK == psp->store_record (psp->cls, 152 GNUNET_assert (GNUNET_OK ==
118 "subsys", 153 psp->store_record (psp->cls,
119 identity, 154 "subsys",
120 "key", "test_value", strlen ("test_value"), 155 identity,
121 GNUNET_TIME_absolute_get (), 156 "key",
122 GNUNET_PEERSTORE_STOREOPTION_REPLACE, 157 "test_value",
123 &store_cont, 158 strlen ("test_value"),
124 identity)); 159 GNUNET_TIME_absolute_get (),
160 GNUNET_PEERSTORE_STOREOPTION_REPLACE,
161 &store_cont,
162 NULL));
125} 163}
126 164
127 165
128static void 166static void
129run (void *cls, char *const *args, const char *cfgfile, 167run (void *cls,
168 char *const *args,
169 const char *cfgfile,
130 const struct GNUNET_CONFIGURATION_Handle *cfg) 170 const struct GNUNET_CONFIGURATION_Handle *cfg)
131{ 171{
132 struct GNUNET_PEERSTORE_PluginFunctions *psp;
133 struct GNUNET_PeerIdentity p1;
134 172
135 ok = 0; 173 ok = 1;
136 psp = load_plugin (cfg); 174 psp = load_plugin (cfg);
137 if (NULL == psp) 175 if (NULL == psp)
138 { 176 {
@@ -142,10 +180,8 @@ run (void *cls, char *const *args, const char *cfgfile,
142 return; 180 return;
143 } 181 }
144 memset (&p1, 1, sizeof (p1)); 182 memset (&p1, 1, sizeof (p1));
145 put_record (psp, &p1); 183 put_record (psp,
146 get_record (psp, &p1); 184 &p1);
147
148 unload_plugin (psp);
149} 185}
150 186
151 187
@@ -163,19 +199,26 @@ main (int argc, char *argv[])
163 GNUNET_GETOPT_OPTION_END 199 GNUNET_GETOPT_OPTION_END
164 }; 200 };
165 201
166 //GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-namestore-sqlite");
167 GNUNET_log_setup ("test-plugin-peerstore", 202 GNUNET_log_setup ("test-plugin-peerstore",
168 "WARNING", 203 "WARNING",
169 NULL); 204 NULL);
170 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]); 205 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
171 GNUNET_snprintf (cfg_name, sizeof (cfg_name), "test_plugin_peerstore_%s.conf", 206 GNUNET_snprintf (cfg_name,
207 sizeof (cfg_name),
208 "test_plugin_peerstore_%s.conf",
172 plugin_name); 209 plugin_name);
173 GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv, 210 GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1,
174 "test-plugin-peerstore", "nohelp", options, &run, NULL); 211 xargv,
212 "test-plugin-peerstore",
213 "nohelp",
214 options,
215 &run,
216 NULL);
175 if (ok != 0) 217 if (ok != 0)
176 FPRINTF (stderr, "Missed some testcases: %d\n", ok); 218 FPRINTF (stderr,
177 //GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-namestore-sqlite"); 219 "Missed some testcases: %d\n",
220 ok);
178 return ok; 221 return ok;
179} 222}
180 223
181/* end of test_plugin_namestore.c */ 224/* end of test_plugin_peerstore.c */