aboutsummaryrefslogtreecommitdiff
path: root/src/datacache
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
committerChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
commitc4e9ba925ffd758aaa3feee2ccfc0b76f26fe207 (patch)
treecac3ce030d77b4cbe7c7dc62ed58cfe6d24f73e1 /src/datacache
parentfbb71d527c7d6babf269a8fefce1db291b9f7068 (diff)
downloadgnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.tar.gz
gnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.zip
global reindent, now with uncrustify hook enabled
Diffstat (limited to 'src/datacache')
-rw-r--r--src/datacache/datacache.c333
-rw-r--r--src/datacache/perf_datacache.c140
-rw-r--r--src/datacache/plugin_datacache_heap.c334
-rw-r--r--src/datacache/plugin_datacache_postgres.c699
-rw-r--r--src/datacache/plugin_datacache_sqlite.c847
-rw-r--r--src/datacache/plugin_datacache_template.c77
-rw-r--r--src/datacache/test_datacache.c206
-rw-r--r--src/datacache/test_datacache_quota.c156
8 files changed, 1412 insertions, 1380 deletions
diff --git a/src/datacache/datacache.c b/src/datacache/datacache.c
index 52c755a49..1ae228b86 100644
--- a/src/datacache/datacache.c
+++ b/src/datacache/datacache.c
@@ -29,15 +29,16 @@
29#include "gnunet_datacache_plugin.h" 29#include "gnunet_datacache_plugin.h"
30 30
31 31
32#define LOG(kind, ...) GNUNET_log_from(kind, "datacache", __VA_ARGS__) 32#define LOG(kind, ...) GNUNET_log_from (kind, "datacache", __VA_ARGS__)
33 33
34#define LOG_STRERROR_FILE(kind, op, fn) \ 34#define LOG_STRERROR_FILE(kind, op, fn) \
35 GNUNET_log_from_strerror_file(kind, "datacache", op, fn) 35 GNUNET_log_from_strerror_file (kind, "datacache", op, fn)
36 36
37/** 37/**
38 * Internal state of the datacache library. 38 * Internal state of the datacache library.
39 */ 39 */
40struct GNUNET_DATACACHE_Handle { 40struct GNUNET_DATACACHE_Handle
41{
41 /** 42 /**
42 * Bloomfilter to quickly tell if we don't have the content. 43 * Bloomfilter to quickly tell if we don't have the content.
43 */ 44 */
@@ -100,24 +101,24 @@ struct GNUNET_DATACACHE_Handle {
100 * @param size number of bytes that were made available 101 * @param size number of bytes that were made available
101 */ 102 */
102static void 103static void
103env_delete_notify(void *cls, const struct GNUNET_HashCode *key, size_t size) 104env_delete_notify (void *cls, const struct GNUNET_HashCode *key, size_t size)
104{ 105{
105 struct GNUNET_DATACACHE_Handle *h = cls; 106 struct GNUNET_DATACACHE_Handle *h = cls;
106 107
107 LOG(GNUNET_ERROR_TYPE_DEBUG, 108 LOG (GNUNET_ERROR_TYPE_DEBUG,
108 "Content under key `%s' discarded\n", 109 "Content under key `%s' discarded\n",
109 GNUNET_h2s(key)); 110 GNUNET_h2s (key));
110 GNUNET_assert(h->utilization >= size); 111 GNUNET_assert (h->utilization >= size);
111 h->utilization -= size; 112 h->utilization -= size;
112 GNUNET_CONTAINER_bloomfilter_remove(h->filter, key); 113 GNUNET_CONTAINER_bloomfilter_remove (h->filter, key);
113 GNUNET_STATISTICS_update(h->stats, 114 GNUNET_STATISTICS_update (h->stats,
114 gettext_noop("# bytes stored"), 115 gettext_noop ("# bytes stored"),
115 -(long long)size, 116 -(long long) size,
116 GNUNET_NO); 117 GNUNET_NO);
117 GNUNET_STATISTICS_update(h->stats, 118 GNUNET_STATISTICS_update (h->stats,
118 gettext_noop("# items stored"), 119 gettext_noop ("# items stored"),
119 -1, 120 -1,
120 GNUNET_NO); 121 GNUNET_NO);
121} 122}
122 123
123 124
@@ -129,8 +130,8 @@ env_delete_notify(void *cls, const struct GNUNET_HashCode *key, size_t size)
129 * @return handle to use to access the service 130 * @return handle to use to access the service
130 */ 131 */
131struct GNUNET_DATACACHE_Handle * 132struct GNUNET_DATACACHE_Handle *
132GNUNET_DATACACHE_create(const struct GNUNET_CONFIGURATION_Handle *cfg, 133GNUNET_DATACACHE_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
133 const char *section) 134 const char *section)
134{ 135{
135 unsigned int bf_size; 136 unsigned int bf_size;
136 unsigned long long quota; 137 unsigned long long quota;
@@ -139,65 +140,65 @@ GNUNET_DATACACHE_create(const struct GNUNET_CONFIGURATION_Handle *cfg,
139 char *name; 140 char *name;
140 141
141 if (GNUNET_OK != 142 if (GNUNET_OK !=
142 GNUNET_CONFIGURATION_get_value_size(cfg, section, "QUOTA", &quota)) 143 GNUNET_CONFIGURATION_get_value_size (cfg, section, "QUOTA", &quota))
143 { 144 {
144 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR, section, "QUOTA"); 145 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, section, "QUOTA");
145 return NULL; 146 return NULL;
146 } 147 }
147 if (GNUNET_OK != 148 if (GNUNET_OK !=
148 GNUNET_CONFIGURATION_get_value_string(cfg, section, "DATABASE", &name)) 149 GNUNET_CONFIGURATION_get_value_string (cfg, section, "DATABASE", &name))
149 { 150 {
150 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR, section, "DATABASE"); 151 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, section, "DATABASE");
151 return NULL; 152 return NULL;
152 } 153 }
153 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */ 154 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
154 155
155 ret = GNUNET_new(struct GNUNET_DATACACHE_Handle); 156 ret = GNUNET_new (struct GNUNET_DATACACHE_Handle);
156 157
157 if (GNUNET_YES != 158 if (GNUNET_YES !=
158 GNUNET_CONFIGURATION_get_value_yesno(cfg, section, "DISABLE_BF")) 159 GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "DISABLE_BF"))
160 {
161 if (GNUNET_YES !=
162 GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "DISABLE_BF_RC"))
163 {
164 ret->bloom_name = GNUNET_DISK_mktemp ("gnunet-datacachebloom");
165 }
166 if (NULL != ret->bloom_name)
167 {
168 ret->filter = GNUNET_CONTAINER_bloomfilter_load (
169 ret->bloom_name,
170 quota / 1024, /* 8 bit per entry in DB, expect 1k entries */
171 5);
172 }
173 if (NULL == ret->filter)
159 { 174 {
160 if (GNUNET_YES != 175 ret->filter =
161 GNUNET_CONFIGURATION_get_value_yesno(cfg, section, "DISABLE_BF_RC")) 176 GNUNET_CONTAINER_bloomfilter_init (NULL,
162 { 177 bf_size,
163 ret->bloom_name = GNUNET_DISK_mktemp("gnunet-datacachebloom"); 178 5); /* approx. 3% false positives at max use */
164 }
165 if (NULL != ret->bloom_name)
166 {
167 ret->filter = GNUNET_CONTAINER_bloomfilter_load(
168 ret->bloom_name,
169 quota / 1024, /* 8 bit per entry in DB, expect 1k entries */
170 5);
171 }
172 if (NULL == ret->filter)
173 {
174 ret->filter =
175 GNUNET_CONTAINER_bloomfilter_init(NULL,
176 bf_size,
177 5); /* approx. 3% false positives at max use */
178 }
179 } 179 }
180 ret->stats = GNUNET_STATISTICS_create("datacache", cfg); 180 }
181 ret->section = GNUNET_strdup(section); 181 ret->stats = GNUNET_STATISTICS_create ("datacache", cfg);
182 ret->section = GNUNET_strdup (section);
182 ret->env.cfg = cfg; 183 ret->env.cfg = cfg;
183 ret->env.delete_notify = &env_delete_notify; 184 ret->env.delete_notify = &env_delete_notify;
184 ret->env.section = ret->section; 185 ret->env.section = ret->section;
185 ret->env.cls = ret; 186 ret->env.cls = ret;
186 ret->env.delete_notify = &env_delete_notify; 187 ret->env.delete_notify = &env_delete_notify;
187 ret->env.quota = quota; 188 ret->env.quota = quota;
188 LOG(GNUNET_ERROR_TYPE_INFO, _("Loading `%s' datacache plugin\n"), name); 189 LOG (GNUNET_ERROR_TYPE_INFO, _ ("Loading `%s' datacache plugin\n"), name);
189 GNUNET_asprintf(&libname, "libgnunet_plugin_datacache_%s", name); 190 GNUNET_asprintf (&libname, "libgnunet_plugin_datacache_%s", name);
190 ret->short_name = name; 191 ret->short_name = name;
191 ret->lib_name = libname; 192 ret->lib_name = libname;
192 ret->api = GNUNET_PLUGIN_load(libname, &ret->env); 193 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
193 if (ret->api == NULL) 194 if (ret->api == NULL)
194 { 195 {
195 LOG(GNUNET_ERROR_TYPE_ERROR, 196 LOG (GNUNET_ERROR_TYPE_ERROR,
196 _("Failed to load datacache plugin for `%s'\n"), 197 _ ("Failed to load datacache plugin for `%s'\n"),
197 name); 198 name);
198 GNUNET_DATACACHE_destroy(ret); 199 GNUNET_DATACACHE_destroy (ret);
199 return NULL; 200 return NULL;
200 } 201 }
201 return ret; 202 return ret;
202} 203}
203 204
@@ -208,26 +209,26 @@ GNUNET_DATACACHE_create(const struct GNUNET_CONFIGURATION_Handle *cfg,
208 * @param h handle to the datastore 209 * @param h handle to the datastore
209 */ 210 */
210void 211void
211GNUNET_DATACACHE_destroy(struct GNUNET_DATACACHE_Handle *h) 212GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h)
212{ 213{
213 if (NULL != h->filter) 214 if (NULL != h->filter)
214 GNUNET_CONTAINER_bloomfilter_free(h->filter); 215 GNUNET_CONTAINER_bloomfilter_free (h->filter);
215 if (NULL != h->api) 216 if (NULL != h->api)
216 GNUNET_break(NULL == GNUNET_PLUGIN_unload(h->lib_name, h->api)); 217 GNUNET_break (NULL == GNUNET_PLUGIN_unload (h->lib_name, h->api));
217 GNUNET_free(h->lib_name); 218 GNUNET_free (h->lib_name);
218 GNUNET_free(h->short_name); 219 GNUNET_free (h->short_name);
219 GNUNET_free(h->section); 220 GNUNET_free (h->section);
220 if (NULL != h->bloom_name) 221 if (NULL != h->bloom_name)
221 { 222 {
222 if (0 != unlink(h->bloom_name)) 223 if (0 != unlink (h->bloom_name))
223 GNUNET_log_from_strerror_file(GNUNET_ERROR_TYPE_WARNING, 224 GNUNET_log_from_strerror_file (GNUNET_ERROR_TYPE_WARNING,
224 "datacache", 225 "datacache",
225 "unlink", 226 "unlink",
226 h->bloom_name); 227 h->bloom_name);
227 GNUNET_free(h->bloom_name); 228 GNUNET_free (h->bloom_name);
228 } 229 }
229 GNUNET_STATISTICS_destroy(h->stats, GNUNET_NO); 230 GNUNET_STATISTICS_destroy (h->stats, GNUNET_NO);
230 GNUNET_free(h); 231 GNUNET_free (h);
231} 232}
232 233
233 234
@@ -246,52 +247,52 @@ GNUNET_DATACACHE_destroy(struct GNUNET_DATACACHE_Handle *h)
246 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error, #GNUNET_NO if duplicate 247 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error, #GNUNET_NO if duplicate
247 */ 248 */
248int 249int
249GNUNET_DATACACHE_put(struct GNUNET_DATACACHE_Handle *h, 250GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h,
250 const struct GNUNET_HashCode *key, 251 const struct GNUNET_HashCode *key,
251 uint32_t xor_distance, 252 uint32_t xor_distance,
252 size_t data_size, 253 size_t data_size,
253 const char *data, 254 const char *data,
254 enum GNUNET_BLOCK_Type type, 255 enum GNUNET_BLOCK_Type type,
255 struct GNUNET_TIME_Absolute discard_time, 256 struct GNUNET_TIME_Absolute discard_time,
256 unsigned int path_info_len, 257 unsigned int path_info_len,
257 const struct GNUNET_PeerIdentity *path_info) 258 const struct GNUNET_PeerIdentity *path_info)
258{ 259{
259 ssize_t used; 260 ssize_t used;
260 261
261 used = h->api->put(h->api->cls, 262 used = h->api->put (h->api->cls,
262 key, 263 key,
263 xor_distance, 264 xor_distance,
264 data_size, 265 data_size,
265 data, 266 data,
266 type, 267 type,
267 discard_time, 268 discard_time,
268 path_info_len, 269 path_info_len,
269 path_info); 270 path_info);
270 if (-1 == used) 271 if (-1 == used)
271 { 272 {
272 GNUNET_break(0); 273 GNUNET_break (0);
273 return GNUNET_SYSERR; 274 return GNUNET_SYSERR;
274 } 275 }
275 if (0 == used) 276 if (0 == used)
276 { 277 {
277 /* duplicate */ 278 /* duplicate */
278 return GNUNET_NO; 279 return GNUNET_NO;
279 } 280 }
280 LOG(GNUNET_ERROR_TYPE_DEBUG, 281 LOG (GNUNET_ERROR_TYPE_DEBUG,
281 "Stored data under key `%s' in cache\n", 282 "Stored data under key `%s' in cache\n",
282 GNUNET_h2s(key)); 283 GNUNET_h2s (key));
283 if (NULL != h->filter) 284 if (NULL != h->filter)
284 GNUNET_CONTAINER_bloomfilter_add(h->filter, key); 285 GNUNET_CONTAINER_bloomfilter_add (h->filter, key);
285 GNUNET_STATISTICS_update(h->stats, 286 GNUNET_STATISTICS_update (h->stats,
286 gettext_noop("# bytes stored"), 287 gettext_noop ("# bytes stored"),
287 used, 288 used,
288 GNUNET_NO); 289 GNUNET_NO);
289 GNUNET_STATISTICS_update(h->stats, 290 GNUNET_STATISTICS_update (h->stats,
290 gettext_noop("# items stored"), 291 gettext_noop ("# items stored"),
291 1, 292 1,
292 GNUNET_NO); 293 GNUNET_NO);
293 while (h->utilization + used > h->env.quota) 294 while (h->utilization + used > h->env.quota)
294 GNUNET_assert(GNUNET_OK == h->api->del(h->api->cls)); 295 GNUNET_assert (GNUNET_OK == h->api->del (h->api->cls));
295 h->utilization += used; 296 h->utilization += used;
296 return GNUNET_OK; 297 return GNUNET_OK;
297} 298}
@@ -309,33 +310,33 @@ GNUNET_DATACACHE_put(struct GNUNET_DATACACHE_Handle *h,
309 * @return the number of results found 310 * @return the number of results found
310 */ 311 */
311unsigned int 312unsigned int
312GNUNET_DATACACHE_get(struct GNUNET_DATACACHE_Handle *h, 313GNUNET_DATACACHE_get (struct GNUNET_DATACACHE_Handle *h,
313 const struct GNUNET_HashCode *key, 314 const struct GNUNET_HashCode *key,
314 enum GNUNET_BLOCK_Type type, 315 enum GNUNET_BLOCK_Type type,
315 GNUNET_DATACACHE_Iterator iter, 316 GNUNET_DATACACHE_Iterator iter,
316 void *iter_cls) 317 void *iter_cls)
317{ 318{
318 GNUNET_STATISTICS_update(h->stats, 319 GNUNET_STATISTICS_update (h->stats,
319 gettext_noop("# requests received"), 320 gettext_noop ("# requests received"),
320 1, 321 1,
321 GNUNET_NO); 322 GNUNET_NO);
322 LOG(GNUNET_ERROR_TYPE_DEBUG, 323 LOG (GNUNET_ERROR_TYPE_DEBUG,
323 "Processing request for key `%s'\n", 324 "Processing request for key `%s'\n",
324 GNUNET_h2s(key)); 325 GNUNET_h2s (key));
325 if ((NULL != h->filter) && 326 if ((NULL != h->filter) &&
326 (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_test(h->filter, key))) 327 (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_test (h->filter, key)))
327 { 328 {
328 GNUNET_STATISTICS_update(h->stats, 329 GNUNET_STATISTICS_update (h->stats,
329 gettext_noop( 330 gettext_noop (
330 "# requests filtered by bloom filter"), 331 "# requests filtered by bloom filter"),
331 1, 332 1,
332 GNUNET_NO); 333 GNUNET_NO);
333 LOG(GNUNET_ERROR_TYPE_DEBUG, 334 LOG (GNUNET_ERROR_TYPE_DEBUG,
334 "Bloomfilter filters request for key `%s'\n", 335 "Bloomfilter filters request for key `%s'\n",
335 GNUNET_h2s(key)); 336 GNUNET_h2s (key));
336 return 0; /* can not be present */ 337 return 0; /* can not be present */
337 } 338 }
338 return h->api->get(h->api->cls, key, type, iter, iter_cls); 339 return h->api->get (h->api->cls, key, type, iter, iter_cls);
339} 340}
340 341
341 342
@@ -348,17 +349,17 @@ GNUNET_DATACACHE_get(struct GNUNET_DATACACHE_Handle *h,
348 * @return the number of results found (zero or 1) 349 * @return the number of results found (zero or 1)
349 */ 350 */
350unsigned int 351unsigned int
351GNUNET_DATACACHE_get_random(struct GNUNET_DATACACHE_Handle *h, 352GNUNET_DATACACHE_get_random (struct GNUNET_DATACACHE_Handle *h,
352 GNUNET_DATACACHE_Iterator iter, 353 GNUNET_DATACACHE_Iterator iter,
353 void *iter_cls) 354 void *iter_cls)
354{ 355{
355 GNUNET_STATISTICS_update(h->stats, 356 GNUNET_STATISTICS_update (h->stats,
356 gettext_noop( 357 gettext_noop (
357 "# requests for random value received"), 358 "# requests for random value received"),
358 1, 359 1,
359 GNUNET_NO); 360 GNUNET_NO);
360 LOG(GNUNET_ERROR_TYPE_DEBUG, "Processing request for random value\n"); 361 LOG (GNUNET_ERROR_TYPE_DEBUG, "Processing request for random value\n");
361 return h->api->get_random(h->api->cls, iter, iter_cls); 362 return h->api->get_random (h->api->cls, iter, iter_cls);
362} 363}
363 364
364 365
@@ -376,21 +377,21 @@ GNUNET_DATACACHE_get_random(struct GNUNET_DATACACHE_Handle *h,
376 * @return the number of results found 377 * @return the number of results found
377 */ 378 */
378unsigned int 379unsigned int
379GNUNET_DATACACHE_get_closest(struct GNUNET_DATACACHE_Handle *h, 380GNUNET_DATACACHE_get_closest (struct GNUNET_DATACACHE_Handle *h,
380 const struct GNUNET_HashCode *key, 381 const struct GNUNET_HashCode *key,
381 unsigned int num_results, 382 unsigned int num_results,
382 GNUNET_DATACACHE_Iterator iter, 383 GNUNET_DATACACHE_Iterator iter,
383 void *iter_cls) 384 void *iter_cls)
384{ 385{
385 GNUNET_STATISTICS_update(h->stats, 386 GNUNET_STATISTICS_update (h->stats,
386 gettext_noop( 387 gettext_noop (
387 "# proximity search requests received"), 388 "# proximity search requests received"),
388 1, 389 1,
389 GNUNET_NO); 390 GNUNET_NO);
390 LOG(GNUNET_ERROR_TYPE_DEBUG, 391 LOG (GNUNET_ERROR_TYPE_DEBUG,
391 "Processing proximity search at `%s'\n", 392 "Processing proximity search at `%s'\n",
392 GNUNET_h2s(key)); 393 GNUNET_h2s (key));
393 return h->api->get_closest(h->api->cls, key, num_results, iter, iter_cls); 394 return h->api->get_closest (h->api->cls, key, num_results, iter, iter_cls);
394} 395}
395 396
396 397
diff --git a/src/datacache/perf_datacache.c b/src/datacache/perf_datacache.c
index b470ceaa6..1656ac91c 100644
--- a/src/datacache/perf_datacache.c
+++ b/src/datacache/perf_datacache.c
@@ -29,7 +29,9 @@
29#include <gauger.h> 29#include <gauger.h>
30 30
31 31
32#define ASSERT(x) do { if (!(x)) { printf("Error at %s:%d\n", __FILE__, __LINE__); goto FAILURE; } } while (0) 32#define ASSERT(x) do { if (! (x)) { printf ("Error at %s:%d\n", __FILE__, \
33 __LINE__); goto FAILURE; \
34 } } while (0)
33 35
34#define ITERATIONS 10000 36#define ITERATIONS 10000
35 37
@@ -44,22 +46,23 @@ static const char *plugin_name;
44 46
45 47
46static int 48static int
47checkIt(void *cls, 49checkIt (void *cls,
48 const struct GNUNET_HashCode * key, size_t size, const char *data, 50 const struct GNUNET_HashCode *key, size_t size, const char *data,
49 enum GNUNET_BLOCK_Type type, 51 enum GNUNET_BLOCK_Type type,
50 struct GNUNET_TIME_Absolute exp, 52 struct GNUNET_TIME_Absolute exp,
51 unsigned int path_len, 53 unsigned int path_len,
52 const struct GNUNET_PeerIdentity *path) 54 const struct GNUNET_PeerIdentity *path)
53{ 55{
54 if ((size == sizeof(struct GNUNET_HashCode)) && (0 == memcmp(data, cls, size))) 56 if ((size == sizeof(struct GNUNET_HashCode)) && (0 == memcmp (data, cls,
57 size)))
55 found++; 58 found++;
56 return GNUNET_OK; 59 return GNUNET_OK;
57} 60}
58 61
59 62
60static void 63static void
61run(void *cls, char *const *args, const char *cfgfile, 64run (void *cls, char *const *args, const char *cfgfile,
62 const struct GNUNET_CONFIGURATION_Handle *cfg) 65 const struct GNUNET_CONFIGURATION_Handle *cfg)
63{ 66{
64 struct GNUNET_DATACACHE_Handle *h; 67 struct GNUNET_DATACACHE_Handle *h;
65 struct GNUNET_HashCode k; 68 struct GNUNET_HashCode k;
@@ -70,67 +73,72 @@ run(void *cls, char *const *args, const char *cfgfile,
70 char gstr[128]; 73 char gstr[128];
71 74
72 ok = 0; 75 ok = 0;
73 h = GNUNET_DATACACHE_create(cfg, "perfcache"); 76 h = GNUNET_DATACACHE_create (cfg, "perfcache");
74 77
75 if (h == NULL) 78 if (h == NULL)
76 { 79 {
77 fprintf(stderr, "%s", "Failed to initialize datacache. Database likely not setup, skipping test.\n"); 80 fprintf (stderr, "%s",
78 ok = 77; /* mark test as skipped */ 81 "Failed to initialize datacache. Database likely not setup, skipping test.\n");
79 return; 82 ok = 77; /* mark test as skipped */
80 } 83 return;
81 exp = GNUNET_TIME_relative_to_absolute(GNUNET_TIME_UNIT_HOURS); 84 }
82 start = GNUNET_TIME_absolute_get(); 85 exp = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS);
83 memset(&k, 0, sizeof(struct GNUNET_HashCode)); 86 start = GNUNET_TIME_absolute_get ();
87 memset (&k, 0, sizeof(struct GNUNET_HashCode));
84 for (i = 0; i < ITERATIONS; i++) 88 for (i = 0; i < ITERATIONS; i++)
85 { 89 {
86 if (0 == i % (ITERATIONS / 80)) 90 if (0 == i % (ITERATIONS / 80))
87 fprintf(stderr, "%s", "."); 91 fprintf (stderr, "%s", ".");
88 GNUNET_CRYPTO_hash(&k, sizeof(struct GNUNET_HashCode), &n); 92 GNUNET_CRYPTO_hash (&k, sizeof(struct GNUNET_HashCode), &n);
89 ASSERT(GNUNET_OK == 93 ASSERT (GNUNET_OK ==
90 GNUNET_DATACACHE_put(h, &k, sizeof(struct GNUNET_HashCode), 94 GNUNET_DATACACHE_put (h, &k, sizeof(struct GNUNET_HashCode),
91 (const char *)&n, 1 + i % 16, exp, 95 (const char *) &n, 1 + i % 16, exp,
92 0, NULL)); 96 0, NULL));
93 k = n; 97 k = n;
94 } 98 }
95 fprintf(stderr, "%s", "\n"); 99 fprintf (stderr, "%s", "\n");
96 fprintf(stdout, "Stored %u items in %s\n", ITERATIONS, 100 fprintf (stdout, "Stored %u items in %s\n", ITERATIONS,
97 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start), GNUNET_YES)); 101 GNUNET_STRINGS_relative_time_to_string (
98 GNUNET_snprintf(gstr, sizeof(gstr), "DATACACHE-%s", plugin_name); 102 GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
99 GAUGER(gstr, "Time to PUT item in datacache", 103 GNUNET_snprintf (gstr, sizeof(gstr), "DATACACHE-%s", plugin_name);
100 GNUNET_TIME_absolute_get_duration(start).rel_value_us / 1000LL / ITERATIONS, 104 GAUGER (gstr, "Time to PUT item in datacache",
101 "ms/item"); 105 GNUNET_TIME_absolute_get_duration (start).rel_value_us / 1000LL
102 start = GNUNET_TIME_absolute_get(); 106 / ITERATIONS,
103 memset(&k, 0, sizeof(struct GNUNET_HashCode)); 107 "ms/item");
108 start = GNUNET_TIME_absolute_get ();
109 memset (&k, 0, sizeof(struct GNUNET_HashCode));
104 for (i = 0; i < ITERATIONS; i++) 110 for (i = 0; i < ITERATIONS; i++)
105 { 111 {
106 if (0 == i % (ITERATIONS / 80)) 112 if (0 == i % (ITERATIONS / 80))
107 fprintf(stderr, "%s", "."); 113 fprintf (stderr, "%s", ".");
108 GNUNET_CRYPTO_hash(&k, sizeof(struct GNUNET_HashCode), &n); 114 GNUNET_CRYPTO_hash (&k, sizeof(struct GNUNET_HashCode), &n);
109 GNUNET_DATACACHE_get(h, &k, 1 + i % 16, &checkIt, &n); 115 GNUNET_DATACACHE_get (h, &k, 1 + i % 16, &checkIt, &n);
110 k = n; 116 k = n;
111 } 117 }
112 fprintf(stderr, "%s", "\n"); 118 fprintf (stderr, "%s", "\n");
113 fprintf(stdout, 119 fprintf (stdout,
114 "Found %u/%u items in %s (%u were deleted during storage processing)\n", 120 "Found %u/%u items in %s (%u were deleted during storage processing)\n",
115 found, ITERATIONS, 121 found, ITERATIONS,
116 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start), GNUNET_YES), 122 GNUNET_STRINGS_relative_time_to_string (
117 ITERATIONS - found); 123 GNUNET_TIME_absolute_get_duration (start), GNUNET_YES),
124 ITERATIONS - found);
118 if (found > 0) 125 if (found > 0)
119 GAUGER(gstr, "Time to GET item from datacache", 126 GAUGER (gstr, "Time to GET item from datacache",
120 GNUNET_TIME_absolute_get_duration(start).rel_value_us / 1000LL / found, 127 GNUNET_TIME_absolute_get_duration (start).rel_value_us / 1000LL
121 "ms/item"); 128 / found,
122 GNUNET_DATACACHE_destroy(h); 129 "ms/item");
123 ASSERT(ok == 0); 130 GNUNET_DATACACHE_destroy (h);
131 ASSERT (ok == 0);
124 return; 132 return;
125FAILURE: 133FAILURE:
126 if (h != NULL) 134 if (h != NULL)
127 GNUNET_DATACACHE_destroy(h); 135 GNUNET_DATACACHE_destroy (h);
128 ok = GNUNET_SYSERR; 136 ok = GNUNET_SYSERR;
129} 137}
130 138
131 139
132int 140int
133main(int argc, char *argv[]) 141main (int argc, char *argv[])
134{ 142{
135 char cfg_name[PATH_MAX]; 143 char cfg_name[PATH_MAX];
136 char *const xargv[] = { 144 char *const xargv[] = {
@@ -143,16 +151,16 @@ main(int argc, char *argv[])
143 GNUNET_GETOPT_OPTION_END 151 GNUNET_GETOPT_OPTION_END
144 }; 152 };
145 153
146 GNUNET_log_setup("perf-datacache", 154 GNUNET_log_setup ("perf-datacache",
147 "WARNING", 155 "WARNING",
148 NULL); 156 NULL);
149 plugin_name = GNUNET_TESTING_get_testname_from_underscore(argv[0]); 157 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
150 GNUNET_snprintf(cfg_name, sizeof(cfg_name), "perf_datacache_data_%s.conf", 158 GNUNET_snprintf (cfg_name, sizeof(cfg_name), "perf_datacache_data_%s.conf",
151 plugin_name); 159 plugin_name);
152 GNUNET_PROGRAM_run((sizeof(xargv) / sizeof(char *)) - 1, xargv, 160 GNUNET_PROGRAM_run ((sizeof(xargv) / sizeof(char *)) - 1, xargv,
153 "perf-datacache", "nohelp", options, &run, NULL); 161 "perf-datacache", "nohelp", options, &run, NULL);
154 if ((0 != ok) && (77 != ok)) 162 if ((0 != ok) && (77 != ok))
155 fprintf(stderr, "Missed some perfcases: %d\n", ok); 163 fprintf (stderr, "Missed some perfcases: %d\n", ok);
156 return ok; 164 return ok;
157} 165}
158 166
diff --git a/src/datacache/plugin_datacache_heap.c b/src/datacache/plugin_datacache_heap.c
index face566a1..f176e3568 100644
--- a/src/datacache/plugin_datacache_heap.c
+++ b/src/datacache/plugin_datacache_heap.c
@@ -27,16 +27,19 @@
27#include "gnunet_util_lib.h" 27#include "gnunet_util_lib.h"
28#include "gnunet_datacache_plugin.h" 28#include "gnunet_datacache_plugin.h"
29 29
30#define LOG(kind, ...) GNUNET_log_from(kind, "datacache-heap", __VA_ARGS__) 30#define LOG(kind, ...) GNUNET_log_from (kind, "datacache-heap", __VA_ARGS__)
31 31
32#define LOG_STRERROR_FILE(kind, op, fn) GNUNET_log_from_strerror_file(kind, "datacache-heap", op, fn) 32#define LOG_STRERROR_FILE(kind, op, fn) GNUNET_log_from_strerror_file (kind, \
33 "datacache-heap", \
34 op, fn)
33 35
34#define NUM_HEAPS 24 36#define NUM_HEAPS 24
35 37
36/** 38/**
37 * Context for all functions in this plugin. 39 * Context for all functions in this plugin.
38 */ 40 */
39struct Plugin { 41struct Plugin
42{
40 /** 43 /**
41 * Our execution environment. 44 * Our execution environment.
42 */ 45 */
@@ -57,7 +60,8 @@ struct Plugin {
57/** 60/**
58 * Entry in the hash map. 61 * Entry in the hash map.
59 */ 62 */
60struct Value { 63struct Value
64{
61 /** 65 /**
62 * Key for the entry. 66 * Key for the entry.
63 */ 67 */
@@ -106,7 +110,8 @@ struct Value {
106/** 110/**
107 * Closure for #put_cb(). 111 * Closure for #put_cb().
108 */ 112 */
109struct PutContext { 113struct PutContext
114{
110 /** 115 /**
111 * Expiration time for the new value. 116 * Expiration time for the new value.
112 */ 117 */
@@ -154,39 +159,39 @@ struct PutContext {
154 * @return #GNUNET_YES if not found (to continue to iterate) 159 * @return #GNUNET_YES if not found (to continue to iterate)
155 */ 160 */
156static int 161static int
157put_cb(void *cls, 162put_cb (void *cls,
158 const struct GNUNET_HashCode *key, 163 const struct GNUNET_HashCode *key,
159 void *value) 164 void *value)
160{ 165{
161 struct PutContext *put_ctx = cls; 166 struct PutContext *put_ctx = cls;
162 struct Value *val = value; 167 struct Value *val = value;
163 168
164 if ((val->size == put_ctx->size) && 169 if ((val->size == put_ctx->size) &&
165 (val->type == put_ctx->type) && 170 (val->type == put_ctx->type) &&
166 (0 == memcmp(&val[1], 171 (0 == memcmp (&val[1],
167 put_ctx->data, 172 put_ctx->data,
168 put_ctx->size))) 173 put_ctx->size)))
169 { 174 {
170 put_ctx->found = GNUNET_YES; 175 put_ctx->found = GNUNET_YES;
171 val->discard_time = GNUNET_TIME_absolute_max(val->discard_time, 176 val->discard_time = GNUNET_TIME_absolute_max (val->discard_time,
172 put_ctx->discard_time); 177 put_ctx->discard_time);
173 /* replace old path with new path */ 178 /* replace old path with new path */
174 GNUNET_array_grow(val->path_info, 179 GNUNET_array_grow (val->path_info,
175 val->path_info_len, 180 val->path_info_len,
176 put_ctx->path_info_len); 181 put_ctx->path_info_len);
177 GNUNET_memcpy(val->path_info, 182 GNUNET_memcpy (val->path_info,
178 put_ctx->path_info, 183 put_ctx->path_info,
179 put_ctx->path_info_len * sizeof(struct GNUNET_PeerIdentity)); 184 put_ctx->path_info_len * sizeof(struct GNUNET_PeerIdentity));
180 GNUNET_CONTAINER_heap_update_cost(val->hn, 185 GNUNET_CONTAINER_heap_update_cost (val->hn,
181 val->discard_time.abs_value_us); 186 val->discard_time.abs_value_us);
182 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
183 "Got same value for key %s and type %d (size %u vs %u)\n", 188 "Got same value for key %s and type %d (size %u vs %u)\n",
184 GNUNET_h2s(key), 189 GNUNET_h2s (key),
185 val->type, 190 val->type,
186 (unsigned int)val->size, 191 (unsigned int) val->size,
187 (unsigned int)put_ctx->size); 192 (unsigned int) put_ctx->size);
188 return GNUNET_NO; 193 return GNUNET_NO;
189 } 194 }
190 return GNUNET_YES; 195 return GNUNET_YES;
191} 196}
192 197
@@ -206,15 +211,15 @@ put_cb(void *cls,
206 * @return 0 if duplicate, -1 on error, number of bytes used otherwise 211 * @return 0 if duplicate, -1 on error, number of bytes used otherwise
207 */ 212 */
208static ssize_t 213static ssize_t
209heap_plugin_put(void *cls, 214heap_plugin_put (void *cls,
210 const struct GNUNET_HashCode *key, 215 const struct GNUNET_HashCode *key,
211 uint32_t xor_distance, 216 uint32_t xor_distance,
212 size_t size, 217 size_t size,
213 const char *data, 218 const char *data,
214 enum GNUNET_BLOCK_Type type, 219 enum GNUNET_BLOCK_Type type,
215 struct GNUNET_TIME_Absolute discard_time, 220 struct GNUNET_TIME_Absolute discard_time,
216 unsigned int path_info_len, 221 unsigned int path_info_len,
217 const struct GNUNET_PeerIdentity *path_info) 222 const struct GNUNET_PeerIdentity *path_info)
218{ 223{
219 struct Plugin *plugin = cls; 224 struct Plugin *plugin = cls;
220 struct Value *val; 225 struct Value *val;
@@ -227,16 +232,16 @@ heap_plugin_put(void *cls,
227 put_ctx.path_info_len = path_info_len; 232 put_ctx.path_info_len = path_info_len;
228 put_ctx.discard_time = discard_time; 233 put_ctx.discard_time = discard_time;
229 put_ctx.type = type; 234 put_ctx.type = type;
230 GNUNET_CONTAINER_multihashmap_get_multiple(plugin->map, 235 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->map,
231 key, 236 key,
232 &put_cb, 237 &put_cb,
233 &put_ctx); 238 &put_ctx);
234 if (GNUNET_YES == put_ctx.found) 239 if (GNUNET_YES == put_ctx.found)
235 return 0; 240 return 0;
236 val = GNUNET_malloc(sizeof(struct Value) + size); 241 val = GNUNET_malloc (sizeof(struct Value) + size);
237 GNUNET_memcpy(&val[1], 242 GNUNET_memcpy (&val[1],
238 data, 243 data,
239 size); 244 size);
240 val->key = *key; 245 val->key = *key;
241 val->type = type; 246 val->type = type;
242 val->discard_time = discard_time; 247 val->discard_time = discard_time;
@@ -245,19 +250,19 @@ heap_plugin_put(void *cls,
245 val->distance = NUM_HEAPS - 1; 250 val->distance = NUM_HEAPS - 1;
246 else 251 else
247 val->distance = xor_distance; 252 val->distance = xor_distance;
248 GNUNET_array_grow(val->path_info, 253 GNUNET_array_grow (val->path_info,
249 val->path_info_len, 254 val->path_info_len,
250 path_info_len); 255 path_info_len);
251 GNUNET_memcpy(val->path_info, 256 GNUNET_memcpy (val->path_info,
252 path_info, 257 path_info,
253 path_info_len * sizeof(struct GNUNET_PeerIdentity)); 258 path_info_len * sizeof(struct GNUNET_PeerIdentity));
254 (void)GNUNET_CONTAINER_multihashmap_put(plugin->map, 259 (void) GNUNET_CONTAINER_multihashmap_put (plugin->map,
255 &val->key, 260 &val->key,
261 val,
262 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
263 val->hn = GNUNET_CONTAINER_heap_insert (plugin->heaps[val->distance],
256 val, 264 val,
257 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); 265 val->discard_time.abs_value_us);
258 val->hn = GNUNET_CONTAINER_heap_insert(plugin->heaps[val->distance],
259 val,
260 val->discard_time.abs_value_us);
261 return size + OVERHEAD; 266 return size + OVERHEAD;
262} 267}
263 268
@@ -265,7 +270,8 @@ heap_plugin_put(void *cls,
265/** 270/**
266 * Closure for #get_cb(). 271 * Closure for #get_cb().
267 */ 272 */
268struct GetContext { 273struct GetContext
274{
269 /** 275 /**
270 * Function to call for each result. 276 * Function to call for each result.
271 */ 277 */
@@ -299,9 +305,9 @@ struct GetContext {
299 * @return #GNUNET_YES to continue to iterate 305 * @return #GNUNET_YES to continue to iterate
300 */ 306 */
301static int 307static int
302get_cb(void *cls, 308get_cb (void *cls,
303 const struct GNUNET_HashCode *key, 309 const struct GNUNET_HashCode *key,
304 void *value) 310 void *value)
305{ 311{
306 struct GetContext *get_ctx = cls; 312 struct GetContext *get_ctx = cls;
307 struct Value *val = value; 313 struct Value *val = value;
@@ -311,17 +317,17 @@ get_cb(void *cls,
311 (GNUNET_BLOCK_TYPE_ANY != get_ctx->type)) 317 (GNUNET_BLOCK_TYPE_ANY != get_ctx->type))
312 return GNUNET_OK; 318 return GNUNET_OK;
313 if (0 == 319 if (0 ==
314 GNUNET_TIME_absolute_get_remaining(val->discard_time).rel_value_us) 320 GNUNET_TIME_absolute_get_remaining (val->discard_time).rel_value_us)
315 return GNUNET_OK; 321 return GNUNET_OK;
316 if (NULL != get_ctx->iter) 322 if (NULL != get_ctx->iter)
317 ret = get_ctx->iter(get_ctx->iter_cls, 323 ret = get_ctx->iter (get_ctx->iter_cls,
318 key, 324 key,
319 val->size, 325 val->size,
320 (const char *)&val[1], 326 (const char *) &val[1],
321 val->type, 327 val->type,
322 val->discard_time, 328 val->discard_time,
323 val->path_info_len, 329 val->path_info_len,
324 val->path_info); 330 val->path_info);
325 else 331 else
326 ret = GNUNET_YES; 332 ret = GNUNET_YES;
327 get_ctx->cnt++; 333 get_ctx->cnt++;
@@ -341,11 +347,11 @@ get_cb(void *cls,
341 * @return the number of results found 347 * @return the number of results found
342 */ 348 */
343static unsigned int 349static unsigned int
344heap_plugin_get(void *cls, 350heap_plugin_get (void *cls,
345 const struct GNUNET_HashCode *key, 351 const struct GNUNET_HashCode *key,
346 enum GNUNET_BLOCK_Type type, 352 enum GNUNET_BLOCK_Type type,
347 GNUNET_DATACACHE_Iterator iter, 353 GNUNET_DATACACHE_Iterator iter,
348 void *iter_cls) 354 void *iter_cls)
349{ 355{
350 struct Plugin *plugin = cls; 356 struct Plugin *plugin = cls;
351 struct GetContext get_ctx; 357 struct GetContext get_ctx;
@@ -354,10 +360,10 @@ heap_plugin_get(void *cls,
354 get_ctx.iter = iter; 360 get_ctx.iter = iter;
355 get_ctx.iter_cls = iter_cls; 361 get_ctx.iter_cls = iter_cls;
356 get_ctx.cnt = 0; 362 get_ctx.cnt = 0;
357 GNUNET_CONTAINER_multihashmap_get_multiple(plugin->map, 363 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->map,
358 key, 364 key,
359 &get_cb, 365 &get_cb,
360 &get_ctx); 366 &get_ctx);
361 return get_ctx.cnt; 367 return get_ctx.cnt;
362} 368}
363 369
@@ -370,28 +376,28 @@ heap_plugin_get(void *cls,
370 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 376 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
371 */ 377 */
372static int 378static int
373heap_plugin_del(void *cls) 379heap_plugin_del (void *cls)
374{ 380{
375 struct Plugin *plugin = cls; 381 struct Plugin *plugin = cls;
376 struct Value *val; 382 struct Value *val;
377 383
378 for (unsigned int i = 0; i < NUM_HEAPS; i++) 384 for (unsigned int i = 0; i < NUM_HEAPS; i++)
379 { 385 {
380 val = GNUNET_CONTAINER_heap_remove_root(plugin->heaps[i]); 386 val = GNUNET_CONTAINER_heap_remove_root (plugin->heaps[i]);
381 if (NULL != val) 387 if (NULL != val)
382 break; 388 break;
383 } 389 }
384 if (NULL == val) 390 if (NULL == val)
385 return GNUNET_SYSERR; 391 return GNUNET_SYSERR;
386 GNUNET_assert(GNUNET_YES == 392 GNUNET_assert (GNUNET_YES ==
387 GNUNET_CONTAINER_multihashmap_remove(plugin->map, 393 GNUNET_CONTAINER_multihashmap_remove (plugin->map,
388 &val->key, 394 &val->key,
389 val)); 395 val));
390 plugin->env->delete_notify(plugin->env->cls, 396 plugin->env->delete_notify (plugin->env->cls,
391 &val->key, 397 &val->key,
392 val->size + OVERHEAD); 398 val->size + OVERHEAD);
393 GNUNET_free_non_null(val->path_info); 399 GNUNET_free_non_null (val->path_info);
394 GNUNET_free(val); 400 GNUNET_free (val);
395 return GNUNET_OK; 401 return GNUNET_OK;
396} 402}
397 403
@@ -405,9 +411,9 @@ heap_plugin_del(void *cls)
405 * @return the number of results found 411 * @return the number of results found
406 */ 412 */
407static unsigned int 413static unsigned int
408heap_plugin_get_random(void *cls, 414heap_plugin_get_random (void *cls,
409 GNUNET_DATACACHE_Iterator iter, 415 GNUNET_DATACACHE_Iterator iter,
410 void *iter_cls) 416 void *iter_cls)
411{ 417{
412 struct Plugin *plugin = cls; 418 struct Plugin *plugin = cls;
413 struct GetContext get_ctx; 419 struct GetContext get_ctx;
@@ -416,9 +422,9 @@ heap_plugin_get_random(void *cls,
416 get_ctx.iter = iter; 422 get_ctx.iter = iter;
417 get_ctx.iter_cls = iter_cls; 423 get_ctx.iter_cls = iter_cls;
418 get_ctx.cnt = 0; 424 get_ctx.cnt = 0;
419 GNUNET_CONTAINER_multihashmap_get_random(plugin->map, 425 GNUNET_CONTAINER_multihashmap_get_random (plugin->map,
420 &get_cb, 426 &get_cb,
421 &get_ctx); 427 &get_ctx);
422 return get_ctx.cnt; 428 return get_ctx.cnt;
423} 429}
424 430
@@ -426,7 +432,8 @@ heap_plugin_get_random(void *cls,
426/** 432/**
427 * Closure for #find_closest(). 433 * Closure for #find_closest().
428 */ 434 */
429struct GetClosestContext { 435struct GetClosestContext
436{
430 struct Value **values; 437 struct Value **values;
431 438
432 unsigned int num_results; 439 unsigned int num_results;
@@ -436,32 +443,32 @@ struct GetClosestContext {
436 443
437 444
438static int 445static int
439find_closest(void *cls, 446find_closest (void *cls,
440 const struct GNUNET_HashCode *key, 447 const struct GNUNET_HashCode *key,
441 void *value) 448 void *value)
442{ 449{
443 struct GetClosestContext *gcc = cls; 450 struct GetClosestContext *gcc = cls;
444 struct Value *val = value; 451 struct Value *val = value;
445 unsigned int j; 452 unsigned int j;
446 453
447 if (1 != GNUNET_CRYPTO_hash_cmp(key, 454 if (1 != GNUNET_CRYPTO_hash_cmp (key,
448 gcc->key)) 455 gcc->key))
449 return GNUNET_OK; /* useless */ 456 return GNUNET_OK; /* useless */
450 j = gcc->num_results; 457 j = gcc->num_results;
451 for (unsigned int i = 0; i < gcc->num_results; i++) 458 for (unsigned int i = 0; i < gcc->num_results; i++)
459 {
460 if (NULL == gcc->values[i])
461 {
462 j = i;
463 break;
464 }
465 if (1 == GNUNET_CRYPTO_hash_cmp (&gcc->values[i]->key,
466 key))
452 { 467 {
453 if (NULL == gcc->values[i]) 468 j = i;
454 { 469 break;
455 j = i;
456 break;
457 }
458 if (1 == GNUNET_CRYPTO_hash_cmp(&gcc->values[i]->key,
459 key))
460 {
461 j = i;
462 break;
463 }
464 } 470 }
471 }
465 if (j == gcc->num_results) 472 if (j == gcc->num_results)
466 return GNUNET_OK; 473 return GNUNET_OK;
467 gcc->values[j] = val; 474 gcc->values[j] = val;
@@ -483,11 +490,11 @@ find_closest(void *cls,
483 * @return the number of results found 490 * @return the number of results found
484 */ 491 */
485static unsigned int 492static unsigned int
486heap_plugin_get_closest(void *cls, 493heap_plugin_get_closest (void *cls,
487 const struct GNUNET_HashCode *key, 494 const struct GNUNET_HashCode *key,
488 unsigned int num_results, 495 unsigned int num_results,
489 GNUNET_DATACACHE_Iterator iter, 496 GNUNET_DATACACHE_Iterator iter,
490 void *iter_cls) 497 void *iter_cls)
491{ 498{
492 struct Plugin *plugin = cls; 499 struct Plugin *plugin = cls;
493 struct Value *values[num_results]; 500 struct Value *values[num_results];
@@ -497,22 +504,22 @@ heap_plugin_get_closest(void *cls,
497 .key = key 504 .key = key
498 }; 505 };
499 506
500 GNUNET_CONTAINER_multihashmap_iterate(plugin->map, 507 GNUNET_CONTAINER_multihashmap_iterate (plugin->map,
501 &find_closest, 508 &find_closest,
502 &gcc); 509 &gcc);
503 for (unsigned int i = 0; i < num_results; i++) 510 for (unsigned int i = 0; i < num_results; i++)
504 { 511 {
505 if (NULL == values[i]) 512 if (NULL == values[i])
506 return i; 513 return i;
507 iter(iter_cls, 514 iter (iter_cls,
508 &values[i]->key, 515 &values[i]->key,
509 values[i]->size, 516 values[i]->size,
510 (void *)&values[i][1], 517 (void *) &values[i][1],
511 values[i]->type, 518 values[i]->type,
512 values[i]->discard_time, 519 values[i]->discard_time,
513 values[i]->path_info_len, 520 values[i]->path_info_len,
514 values[i]->path_info); 521 values[i]->path_info);
515 } 522 }
516 return num_results; 523 return num_results;
517} 524}
518 525
@@ -524,27 +531,28 @@ heap_plugin_get_closest(void *cls,
524 * @return the plugin's closure (our `struct Plugin`) 531 * @return the plugin's closure (our `struct Plugin`)
525 */ 532 */
526void * 533void *
527libgnunet_plugin_datacache_heap_init(void *cls) 534libgnunet_plugin_datacache_heap_init (void *cls)
528{ 535{
529 struct GNUNET_DATACACHE_PluginEnvironment *env = cls; 536 struct GNUNET_DATACACHE_PluginEnvironment *env = cls;
530 struct GNUNET_DATACACHE_PluginFunctions *api; 537 struct GNUNET_DATACACHE_PluginFunctions *api;
531 struct Plugin *plugin; 538 struct Plugin *plugin;
532 539
533 plugin = GNUNET_new(struct Plugin); 540 plugin = GNUNET_new (struct Plugin);
534 plugin->map = GNUNET_CONTAINER_multihashmap_create(1024, /* FIXME: base on quota! */ 541 plugin->map = GNUNET_CONTAINER_multihashmap_create (1024, /* FIXME: base on quota! */
535 GNUNET_YES); 542 GNUNET_YES);
536 for (unsigned int i = 0; i < NUM_HEAPS; i++) 543 for (unsigned int i = 0; i < NUM_HEAPS; i++)
537 plugin->heaps[i] = GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN); 544 plugin->heaps[i] = GNUNET_CONTAINER_heap_create (
545 GNUNET_CONTAINER_HEAP_ORDER_MIN);
538 plugin->env = env; 546 plugin->env = env;
539 api = GNUNET_new(struct GNUNET_DATACACHE_PluginFunctions); 547 api = GNUNET_new (struct GNUNET_DATACACHE_PluginFunctions);
540 api->cls = plugin; 548 api->cls = plugin;
541 api->get = &heap_plugin_get; 549 api->get = &heap_plugin_get;
542 api->put = &heap_plugin_put; 550 api->put = &heap_plugin_put;
543 api->del = &heap_plugin_del; 551 api->del = &heap_plugin_del;
544 api->get_random = &heap_plugin_get_random; 552 api->get_random = &heap_plugin_get_random;
545 api->get_closest = &heap_plugin_get_closest; 553 api->get_closest = &heap_plugin_get_closest;
546 LOG(GNUNET_ERROR_TYPE_INFO, 554 LOG (GNUNET_ERROR_TYPE_INFO,
547 _("Heap datacache running\n")); 555 _ ("Heap datacache running\n"));
548 return api; 556 return api;
549} 557}
550 558
@@ -556,28 +564,28 @@ libgnunet_plugin_datacache_heap_init(void *cls)
556 * @return NULL 564 * @return NULL
557 */ 565 */
558void * 566void *
559libgnunet_plugin_datacache_heap_done(void *cls) 567libgnunet_plugin_datacache_heap_done (void *cls)
560{ 568{
561 struct GNUNET_DATACACHE_PluginFunctions *api = cls; 569 struct GNUNET_DATACACHE_PluginFunctions *api = cls;
562 struct Plugin *plugin = api->cls; 570 struct Plugin *plugin = api->cls;
563 struct Value *val; 571 struct Value *val;
564 572
565 for (unsigned int i = 0; i < NUM_HEAPS; i++) 573 for (unsigned int i = 0; i < NUM_HEAPS; i++)
574 {
575 while (NULL != (val = GNUNET_CONTAINER_heap_remove_root (plugin->heaps[i])))
566 { 576 {
567 while (NULL != (val = GNUNET_CONTAINER_heap_remove_root(plugin->heaps[i]))) 577 GNUNET_assert (GNUNET_YES ==
568 { 578 GNUNET_CONTAINER_multihashmap_remove (plugin->map,
569 GNUNET_assert(GNUNET_YES == 579 &val->key,
570 GNUNET_CONTAINER_multihashmap_remove(plugin->map, 580 val));
571 &val->key, 581 GNUNET_free_non_null (val->path_info);
572 val)); 582 GNUNET_free (val);
573 GNUNET_free_non_null(val->path_info);
574 GNUNET_free(val);
575 }
576 GNUNET_CONTAINER_heap_destroy(plugin->heaps[i]);
577 } 583 }
578 GNUNET_CONTAINER_multihashmap_destroy(plugin->map); 584 GNUNET_CONTAINER_heap_destroy (plugin->heaps[i]);
579 GNUNET_free(plugin); 585 }
580 GNUNET_free(api); 586 GNUNET_CONTAINER_multihashmap_destroy (plugin->map);
587 GNUNET_free (plugin);
588 GNUNET_free (api);
581 return NULL; 589 return NULL;
582} 590}
583 591
diff --git a/src/datacache/plugin_datacache_postgres.c b/src/datacache/plugin_datacache_postgres.c
index 6666c850c..59dff9067 100644
--- a/src/datacache/plugin_datacache_postgres.c
+++ b/src/datacache/plugin_datacache_postgres.c
@@ -28,7 +28,7 @@
28#include "gnunet_pq_lib.h" 28#include "gnunet_pq_lib.h"
29#include "gnunet_datacache_plugin.h" 29#include "gnunet_datacache_plugin.h"
30 30
31#define LOG(kind, ...) GNUNET_log_from(kind, "datacache-postgres", __VA_ARGS__) 31#define LOG(kind, ...) GNUNET_log_from (kind, "datacache-postgres", __VA_ARGS__)
32 32
33/** 33/**
34 * Per-entry overhead estimate 34 * Per-entry overhead estimate
@@ -38,7 +38,8 @@
38/** 38/**
39 * Context for all functions in this plugin. 39 * Context for all functions in this plugin.
40 */ 40 */
41struct Plugin { 41struct Plugin
42{
42 /** 43 /**
43 * Our execution environment. 44 * Our execution environment.
44 */ 45 */
@@ -63,81 +64,84 @@ struct Plugin {
63 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 64 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
64 */ 65 */
65static int 66static int
66init_connection(struct Plugin *plugin) 67init_connection (struct Plugin *plugin)
67{ 68{
68 struct GNUNET_PQ_ExecuteStatement es[] = { 69 struct GNUNET_PQ_ExecuteStatement es[] = {
69 GNUNET_PQ_make_execute("CREATE TEMPORARY TABLE IF NOT EXISTS gn011dc (" 70 GNUNET_PQ_make_execute ("CREATE TEMPORARY TABLE IF NOT EXISTS gn011dc ("
70 " type INTEGER NOT NULL," 71 " type INTEGER NOT NULL,"
71 " prox INTEGER NOT NULL," 72 " prox INTEGER NOT NULL,"
72 " discard_time BIGINT NOT NULL," 73 " discard_time BIGINT NOT NULL,"
73 " key BYTEA NOT NULL," 74 " key BYTEA NOT NULL,"
74 " value BYTEA NOT NULL," 75 " value BYTEA NOT NULL,"
75 " path BYTEA DEFAULT NULL)" 76 " path BYTEA DEFAULT NULL)"
76 "WITH OIDS"), 77 "WITH OIDS"),
77 GNUNET_PQ_make_try_execute("CREATE INDEX IF NOT EXISTS idx_key ON gn011dc (key)"), 78 GNUNET_PQ_make_try_execute (
78 GNUNET_PQ_make_try_execute("CREATE INDEX IF NOT EXISTS idx_dt ON gn011dc (discard_time)"), 79 "CREATE INDEX IF NOT EXISTS idx_key ON gn011dc (key)"),
79 GNUNET_PQ_make_execute("ALTER TABLE gn011dc ALTER value SET STORAGE EXTERNAL"), 80 GNUNET_PQ_make_try_execute (
80 GNUNET_PQ_make_execute("ALTER TABLE gn011dc ALTER key SET STORAGE PLAIN"), 81 "CREATE INDEX IF NOT EXISTS idx_dt ON gn011dc (discard_time)"),
82 GNUNET_PQ_make_execute (
83 "ALTER TABLE gn011dc ALTER value SET STORAGE EXTERNAL"),
84 GNUNET_PQ_make_execute ("ALTER TABLE gn011dc ALTER key SET STORAGE PLAIN"),
81 GNUNET_PQ_EXECUTE_STATEMENT_END 85 GNUNET_PQ_EXECUTE_STATEMENT_END
82 }; 86 };
83 struct GNUNET_PQ_PreparedStatement ps[] = { 87 struct GNUNET_PQ_PreparedStatement ps[] = {
84 GNUNET_PQ_make_prepare("getkt", 88 GNUNET_PQ_make_prepare ("getkt",
85 "SELECT discard_time,type,value,path FROM gn011dc " 89 "SELECT discard_time,type,value,path FROM gn011dc "
86 "WHERE key=$1 AND type=$2 AND discard_time >= $3", 90 "WHERE key=$1 AND type=$2 AND discard_time >= $3",
87 3), 91 3),
88 GNUNET_PQ_make_prepare("getk", 92 GNUNET_PQ_make_prepare ("getk",
89 "SELECT discard_time,type,value,path FROM gn011dc " 93 "SELECT discard_time,type,value,path FROM gn011dc "
90 "WHERE key=$1 AND discard_time >= $2", 94 "WHERE key=$1 AND discard_time >= $2",
91 2), 95 2),
92 GNUNET_PQ_make_prepare("getex", 96 GNUNET_PQ_make_prepare ("getex",
93 "SELECT length(value) AS len,oid,key FROM gn011dc" 97 "SELECT length(value) AS len,oid,key FROM gn011dc"
94 " WHERE discard_time < $1" 98 " WHERE discard_time < $1"
95 " ORDER BY discard_time ASC LIMIT 1", 99 " ORDER BY discard_time ASC LIMIT 1",
96 1), 100 1),
97 GNUNET_PQ_make_prepare("getm", 101 GNUNET_PQ_make_prepare ("getm",
98 "SELECT length(value) AS len,oid,key FROM gn011dc" 102 "SELECT length(value) AS len,oid,key FROM gn011dc"
99 " ORDER BY prox ASC, discard_time ASC LIMIT 1", 103 " ORDER BY prox ASC, discard_time ASC LIMIT 1",
100 0), 104 0),
101 GNUNET_PQ_make_prepare("get_random", 105 GNUNET_PQ_make_prepare ("get_random",
102 "SELECT discard_time,type,value,path,key FROM gn011dc" 106 "SELECT discard_time,type,value,path,key FROM gn011dc"
103 " WHERE discard_time >= $1" 107 " WHERE discard_time >= $1"
104 " ORDER BY key ASC LIMIT 1 OFFSET $2", 108 " ORDER BY key ASC LIMIT 1 OFFSET $2",
105 2), 109 2),
106 GNUNET_PQ_make_prepare("get_closest", 110 GNUNET_PQ_make_prepare ("get_closest",
107 "SELECT discard_time,type,value,path,key FROM gn011dc " 111 "SELECT discard_time,type,value,path,key FROM gn011dc "
108 "WHERE key>=$1 AND discard_time >= $2 ORDER BY key ASC LIMIT $3", 112 "WHERE key>=$1 AND discard_time >= $2 ORDER BY key ASC LIMIT $3",
109 3), 113 3),
110 GNUNET_PQ_make_prepare("delrow", 114 GNUNET_PQ_make_prepare ("delrow",
111 "DELETE FROM gn011dc WHERE oid=$1", 115 "DELETE FROM gn011dc WHERE oid=$1",
112 1), 116 1),
113 GNUNET_PQ_make_prepare("put", 117 GNUNET_PQ_make_prepare ("put",
114 "INSERT INTO gn011dc (type, prox, discard_time, key, value, path) " 118 "INSERT INTO gn011dc (type, prox, discard_time, key, value, path) "
115 "VALUES ($1, $2, $3, $4, $5, $6)", 119 "VALUES ($1, $2, $3, $4, $5, $6)",
116 6), 120 6),
117 GNUNET_PQ_PREPARED_STATEMENT_END 121 GNUNET_PQ_PREPARED_STATEMENT_END
118 }; 122 };
119 123
120 plugin->dbh = GNUNET_PQ_connect_with_cfg(plugin->env->cfg, 124 plugin->dbh = GNUNET_PQ_connect_with_cfg (plugin->env->cfg,
121 "datacache-postgres"); 125 "datacache-postgres");
122 if (NULL == plugin->dbh) 126 if (NULL == plugin->dbh)
123 return GNUNET_SYSERR; 127 return GNUNET_SYSERR;
124 if (GNUNET_OK != 128 if (GNUNET_OK !=
125 GNUNET_PQ_exec_statements(plugin->dbh, 129 GNUNET_PQ_exec_statements (plugin->dbh,
126 es)) 130 es))
127 { 131 {
128 PQfinish(plugin->dbh); 132 PQfinish (plugin->dbh);
129 plugin->dbh = NULL; 133 plugin->dbh = NULL;
130 return GNUNET_SYSERR; 134 return GNUNET_SYSERR;
131 } 135 }
132 136
133 if (GNUNET_OK != 137 if (GNUNET_OK !=
134 GNUNET_PQ_prepare_statements(plugin->dbh, 138 GNUNET_PQ_prepare_statements (plugin->dbh,
135 ps)) 139 ps))
136 { 140 {
137 PQfinish(plugin->dbh); 141 PQfinish (plugin->dbh);
138 plugin->dbh = NULL; 142 plugin->dbh = NULL;
139 return GNUNET_SYSERR; 143 return GNUNET_SYSERR;
140 } 144 }
141 return GNUNET_OK; 145 return GNUNET_OK;
142} 146}
143 147
@@ -157,33 +161,34 @@ init_connection(struct Plugin *plugin)
157 * @return 0 if duplicate, -1 on error, number of bytes used otherwise 161 * @return 0 if duplicate, -1 on error, number of bytes used otherwise
158 */ 162 */
159static ssize_t 163static ssize_t
160postgres_plugin_put(void *cls, 164postgres_plugin_put (void *cls,
161 const struct GNUNET_HashCode *key, 165 const struct GNUNET_HashCode *key,
162 uint32_t prox, 166 uint32_t prox,
163 size_t data_size, 167 size_t data_size,
164 const char *data, 168 const char *data,
165 enum GNUNET_BLOCK_Type type, 169 enum GNUNET_BLOCK_Type type,
166 struct GNUNET_TIME_Absolute discard_time, 170 struct GNUNET_TIME_Absolute discard_time,
167 unsigned int path_info_len, 171 unsigned int path_info_len,
168 const struct GNUNET_PeerIdentity *path_info) 172 const struct GNUNET_PeerIdentity *path_info)
169{ 173{
170 struct Plugin *plugin = cls; 174 struct Plugin *plugin = cls;
171 uint32_t type32 = (uint32_t)type; 175 uint32_t type32 = (uint32_t) type;
172 struct GNUNET_PQ_QueryParam params[] = { 176 struct GNUNET_PQ_QueryParam params[] = {
173 GNUNET_PQ_query_param_uint32(&type32), 177 GNUNET_PQ_query_param_uint32 (&type32),
174 GNUNET_PQ_query_param_uint32(&prox), 178 GNUNET_PQ_query_param_uint32 (&prox),
175 GNUNET_PQ_query_param_absolute_time(&discard_time), 179 GNUNET_PQ_query_param_absolute_time (&discard_time),
176 GNUNET_PQ_query_param_auto_from_type(key), 180 GNUNET_PQ_query_param_auto_from_type (key),
177 GNUNET_PQ_query_param_fixed_size(data, data_size), 181 GNUNET_PQ_query_param_fixed_size (data, data_size),
178 GNUNET_PQ_query_param_fixed_size(path_info, 182 GNUNET_PQ_query_param_fixed_size (path_info,
179 path_info_len * sizeof(struct GNUNET_PeerIdentity)), 183 path_info_len * sizeof(struct
184 GNUNET_PeerIdentity)),
180 GNUNET_PQ_query_param_end 185 GNUNET_PQ_query_param_end
181 }; 186 };
182 enum GNUNET_DB_QueryStatus ret; 187 enum GNUNET_DB_QueryStatus ret;
183 188
184 ret = GNUNET_PQ_eval_prepared_non_select(plugin->dbh, 189 ret = GNUNET_PQ_eval_prepared_non_select (plugin->dbh,
185 "put", 190 "put",
186 params); 191 params);
187 if (0 > ret) 192 if (0 > ret)
188 return -1; 193 return -1;
189 plugin->num_items++; 194 plugin->num_items++;
@@ -194,7 +199,8 @@ postgres_plugin_put(void *cls,
194/** 199/**
195 * Closure for #handle_results. 200 * Closure for #handle_results.
196 */ 201 */
197struct HandleResultContext { 202struct HandleResultContext
203{
198 /** 204 /**
199 * Function to call on each result, may be NULL. 205 * Function to call on each result, may be NULL.
200 */ 206 */
@@ -222,70 +228,70 @@ struct HandleResultContext {
222 * @param num_result the number of results in @a result 228 * @param num_result the number of results in @a result
223 */ 229 */
224static void 230static void
225handle_results(void *cls, 231handle_results (void *cls,
226 PGresult *result, 232 PGresult *result,
227 unsigned int num_results) 233 unsigned int num_results)
228{ 234{
229 struct HandleResultContext *hrc = cls; 235 struct HandleResultContext *hrc = cls;
230 236
231 for (unsigned int i = 0; i < num_results; i++) 237 for (unsigned int i = 0; i < num_results; i++)
238 {
239 struct GNUNET_TIME_Absolute expiration_time;
240 uint32_t type;
241 void *data;
242 size_t data_size;
243 struct GNUNET_PeerIdentity *path;
244 size_t path_len;
245 struct GNUNET_PQ_ResultSpec rs[] = {
246 GNUNET_PQ_result_spec_absolute_time ("discard_time",
247 &expiration_time),
248 GNUNET_PQ_result_spec_uint32 ("type",
249 &type),
250 GNUNET_PQ_result_spec_variable_size ("value",
251 &data,
252 &data_size),
253 GNUNET_PQ_result_spec_variable_size ("path",
254 (void **) &path,
255 &path_len),
256 GNUNET_PQ_result_spec_end
257 };
258
259 if (GNUNET_YES !=
260 GNUNET_PQ_extract_result (result,
261 rs,
262 i))
263 {
264 GNUNET_break (0);
265 return;
266 }
267 if (0 != (path_len % sizeof(struct GNUNET_PeerIdentity)))
232 { 268 {
233 struct GNUNET_TIME_Absolute expiration_time; 269 GNUNET_break (0);
234 uint32_t type; 270 path_len = 0;
235 void *data;
236 size_t data_size;
237 struct GNUNET_PeerIdentity *path;
238 size_t path_len;
239 struct GNUNET_PQ_ResultSpec rs[] = {
240 GNUNET_PQ_result_spec_absolute_time("discard_time",
241 &expiration_time),
242 GNUNET_PQ_result_spec_uint32("type",
243 &type),
244 GNUNET_PQ_result_spec_variable_size("value",
245 &data,
246 &data_size),
247 GNUNET_PQ_result_spec_variable_size("path",
248 (void **)&path,
249 &path_len),
250 GNUNET_PQ_result_spec_end
251 };
252
253 if (GNUNET_YES !=
254 GNUNET_PQ_extract_result(result,
255 rs,
256 i))
257 {
258 GNUNET_break(0);
259 return;
260 }
261 if (0 != (path_len % sizeof(struct GNUNET_PeerIdentity)))
262 {
263 GNUNET_break(0);
264 path_len = 0;
265 }
266 path_len %= sizeof(struct GNUNET_PeerIdentity);
267 LOG(GNUNET_ERROR_TYPE_DEBUG,
268 "Found result of size %u bytes and type %u in database\n",
269 (unsigned int)data_size,
270 (unsigned int)type);
271 if ((NULL != hrc->iter) &&
272 (GNUNET_SYSERR ==
273 hrc->iter(hrc->iter_cls,
274 hrc->key,
275 data_size,
276 data,
277 (enum GNUNET_BLOCK_Type)type,
278 expiration_time,
279 path_len,
280 path)))
281 {
282 LOG(GNUNET_ERROR_TYPE_DEBUG,
283 "Ending iteration (client error)\n");
284 GNUNET_PQ_cleanup_result(rs);
285 return;
286 }
287 GNUNET_PQ_cleanup_result(rs);
288 } 271 }
272 path_len %= sizeof(struct GNUNET_PeerIdentity);
273 LOG (GNUNET_ERROR_TYPE_DEBUG,
274 "Found result of size %u bytes and type %u in database\n",
275 (unsigned int) data_size,
276 (unsigned int) type);
277 if ((NULL != hrc->iter) &&
278 (GNUNET_SYSERR ==
279 hrc->iter (hrc->iter_cls,
280 hrc->key,
281 data_size,
282 data,
283 (enum GNUNET_BLOCK_Type) type,
284 expiration_time,
285 path_len,
286 path)))
287 {
288 LOG (GNUNET_ERROR_TYPE_DEBUG,
289 "Ending iteration (client error)\n");
290 GNUNET_PQ_cleanup_result (rs);
291 return;
292 }
293 GNUNET_PQ_cleanup_result (rs);
294 }
289} 295}
290 296
291 297
@@ -301,38 +307,38 @@ handle_results(void *cls,
301 * @return the number of results found 307 * @return the number of results found
302 */ 308 */
303static unsigned int 309static unsigned int
304postgres_plugin_get(void *cls, 310postgres_plugin_get (void *cls,
305 const struct GNUNET_HashCode *key, 311 const struct GNUNET_HashCode *key,
306 enum GNUNET_BLOCK_Type type, 312 enum GNUNET_BLOCK_Type type,
307 GNUNET_DATACACHE_Iterator iter, 313 GNUNET_DATACACHE_Iterator iter,
308 void *iter_cls) 314 void *iter_cls)
309{ 315{
310 struct Plugin *plugin = cls; 316 struct Plugin *plugin = cls;
311 uint32_t type32 = (uint32_t)type; 317 uint32_t type32 = (uint32_t) type;
312 struct GNUNET_TIME_Absolute now; 318 struct GNUNET_TIME_Absolute now;
313 struct GNUNET_PQ_QueryParam paramk[] = { 319 struct GNUNET_PQ_QueryParam paramk[] = {
314 GNUNET_PQ_query_param_auto_from_type(key), 320 GNUNET_PQ_query_param_auto_from_type (key),
315 GNUNET_PQ_query_param_absolute_time(&now), 321 GNUNET_PQ_query_param_absolute_time (&now),
316 GNUNET_PQ_query_param_end 322 GNUNET_PQ_query_param_end
317 }; 323 };
318 struct GNUNET_PQ_QueryParam paramkt[] = { 324 struct GNUNET_PQ_QueryParam paramkt[] = {
319 GNUNET_PQ_query_param_auto_from_type(key), 325 GNUNET_PQ_query_param_auto_from_type (key),
320 GNUNET_PQ_query_param_uint32(&type32), 326 GNUNET_PQ_query_param_uint32 (&type32),
321 GNUNET_PQ_query_param_absolute_time(&now), 327 GNUNET_PQ_query_param_absolute_time (&now),
322 GNUNET_PQ_query_param_end 328 GNUNET_PQ_query_param_end
323 }; 329 };
324 enum GNUNET_DB_QueryStatus res; 330 enum GNUNET_DB_QueryStatus res;
325 struct HandleResultContext hr_ctx; 331 struct HandleResultContext hr_ctx;
326 332
327 now = GNUNET_TIME_absolute_get(); 333 now = GNUNET_TIME_absolute_get ();
328 hr_ctx.iter = iter; 334 hr_ctx.iter = iter;
329 hr_ctx.iter_cls = iter_cls; 335 hr_ctx.iter_cls = iter_cls;
330 hr_ctx.key = key; 336 hr_ctx.key = key;
331 res = GNUNET_PQ_eval_prepared_multi_select(plugin->dbh, 337 res = GNUNET_PQ_eval_prepared_multi_select (plugin->dbh,
332 (0 == type) ? "getk" : "getkt", 338 (0 == type) ? "getk" : "getkt",
333 (0 == type) ? paramk : paramkt, 339 (0 == type) ? paramk : paramkt,
334 &handle_results, 340 &handle_results,
335 &hr_ctx); 341 &hr_ctx);
336 if (res < 0) 342 if (res < 0)
337 return 0; 343 return 0;
338 return res; 344 return res;
@@ -347,7 +353,7 @@ postgres_plugin_get(void *cls,
347 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 353 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
348 */ 354 */
349static int 355static int
350postgres_plugin_del(void *cls) 356postgres_plugin_del (void *cls)
351{ 357{
352 struct Plugin *plugin = cls; 358 struct Plugin *plugin = cls;
353 struct GNUNET_PQ_QueryParam pempty[] = { 359 struct GNUNET_PQ_QueryParam pempty[] = {
@@ -357,57 +363,57 @@ postgres_plugin_del(void *cls)
357 uint32_t oid; 363 uint32_t oid;
358 struct GNUNET_HashCode key; 364 struct GNUNET_HashCode key;
359 struct GNUNET_PQ_ResultSpec rs[] = { 365 struct GNUNET_PQ_ResultSpec rs[] = {
360 GNUNET_PQ_result_spec_uint32("len", 366 GNUNET_PQ_result_spec_uint32 ("len",
361 &size), 367 &size),
362 GNUNET_PQ_result_spec_uint32("oid", 368 GNUNET_PQ_result_spec_uint32 ("oid",
363 &oid), 369 &oid),
364 GNUNET_PQ_result_spec_auto_from_type("key", 370 GNUNET_PQ_result_spec_auto_from_type ("key",
365 &key), 371 &key),
366 GNUNET_PQ_result_spec_end 372 GNUNET_PQ_result_spec_end
367 }; 373 };
368 enum GNUNET_DB_QueryStatus res; 374 enum GNUNET_DB_QueryStatus res;
369 struct GNUNET_PQ_QueryParam dparam[] = { 375 struct GNUNET_PQ_QueryParam dparam[] = {
370 GNUNET_PQ_query_param_uint32(&oid), 376 GNUNET_PQ_query_param_uint32 (&oid),
371 GNUNET_PQ_query_param_end 377 GNUNET_PQ_query_param_end
372 }; 378 };
373 struct GNUNET_TIME_Absolute now; 379 struct GNUNET_TIME_Absolute now;
374 struct GNUNET_PQ_QueryParam xparam[] = { 380 struct GNUNET_PQ_QueryParam xparam[] = {
375 GNUNET_PQ_query_param_absolute_time(&now), 381 GNUNET_PQ_query_param_absolute_time (&now),
376 GNUNET_PQ_query_param_end 382 GNUNET_PQ_query_param_end
377 }; 383 };
378 384
379 now = GNUNET_TIME_absolute_get(); 385 now = GNUNET_TIME_absolute_get ();
380 res = GNUNET_PQ_eval_prepared_singleton_select(plugin->dbh, 386 res = GNUNET_PQ_eval_prepared_singleton_select (plugin->dbh,
381 "getex", 387 "getex",
382 xparam, 388 xparam,
383 rs); 389 rs);
384 if (0 >= res) 390 if (0 >= res)
385 res = GNUNET_PQ_eval_prepared_singleton_select(plugin->dbh, 391 res = GNUNET_PQ_eval_prepared_singleton_select (plugin->dbh,
386 "getm", 392 "getm",
387 pempty, 393 pempty,
388 rs); 394 rs);
389 if (0 > res) 395 if (0 > res)
390 return GNUNET_SYSERR; 396 return GNUNET_SYSERR;
391 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == res) 397 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == res)
392 { 398 {
393 /* no result */ 399 /* no result */
394 LOG(GNUNET_ERROR_TYPE_DEBUG, 400 LOG (GNUNET_ERROR_TYPE_DEBUG,
395 "Ending iteration (no more results)\n"); 401 "Ending iteration (no more results)\n");
396 return 0; 402 return 0;
397 } 403 }
398 res = GNUNET_PQ_eval_prepared_non_select(plugin->dbh, 404 res = GNUNET_PQ_eval_prepared_non_select (plugin->dbh,
399 "delrow", 405 "delrow",
400 dparam); 406 dparam);
401 if (0 > res) 407 if (0 > res)
402 { 408 {
403 GNUNET_PQ_cleanup_result(rs); 409 GNUNET_PQ_cleanup_result (rs);
404 return GNUNET_SYSERR; 410 return GNUNET_SYSERR;
405 } 411 }
406 plugin->num_items--; 412 plugin->num_items--;
407 plugin->env->delete_notify(plugin->env->cls, 413 plugin->env->delete_notify (plugin->env->cls,
408 &key, 414 &key,
409 size + OVERHEAD); 415 size + OVERHEAD);
410 GNUNET_PQ_cleanup_result(rs); 416 GNUNET_PQ_cleanup_result (rs);
411 return GNUNET_OK; 417 return GNUNET_OK;
412} 418}
413 419
@@ -421,9 +427,9 @@ postgres_plugin_del(void *cls)
421 * @return the number of results found, zero (datacache empty) or one 427 * @return the number of results found, zero (datacache empty) or one
422 */ 428 */
423static unsigned int 429static unsigned int
424postgres_plugin_get_random(void *cls, 430postgres_plugin_get_random (void *cls,
425 GNUNET_DATACACHE_Iterator iter, 431 GNUNET_DATACACHE_Iterator iter,
426 void *iter_cls) 432 void *iter_cls)
427{ 433{
428 struct Plugin *plugin = cls; 434 struct Plugin *plugin = cls;
429 uint32_t off; 435 uint32_t off;
@@ -437,23 +443,23 @@ postgres_plugin_get_random(void *cls,
437 uint32_t type; 443 uint32_t type;
438 enum GNUNET_DB_QueryStatus res; 444 enum GNUNET_DB_QueryStatus res;
439 struct GNUNET_PQ_QueryParam params[] = { 445 struct GNUNET_PQ_QueryParam params[] = {
440 GNUNET_PQ_query_param_absolute_time(&now), 446 GNUNET_PQ_query_param_absolute_time (&now),
441 GNUNET_PQ_query_param_uint32(&off), 447 GNUNET_PQ_query_param_uint32 (&off),
442 GNUNET_PQ_query_param_end 448 GNUNET_PQ_query_param_end
443 }; 449 };
444 struct GNUNET_PQ_ResultSpec rs[] = { 450 struct GNUNET_PQ_ResultSpec rs[] = {
445 GNUNET_PQ_result_spec_absolute_time("discard_time", 451 GNUNET_PQ_result_spec_absolute_time ("discard_time",
446 &expiration_time), 452 &expiration_time),
447 GNUNET_PQ_result_spec_uint32("type", 453 GNUNET_PQ_result_spec_uint32 ("type",
448 &type), 454 &type),
449 GNUNET_PQ_result_spec_variable_size("value", 455 GNUNET_PQ_result_spec_variable_size ("value",
450 &data, 456 &data,
451 &data_size), 457 &data_size),
452 GNUNET_PQ_result_spec_variable_size("path", 458 GNUNET_PQ_result_spec_variable_size ("path",
453 (void **)&path, 459 (void **) &path,
454 &path_len), 460 &path_len),
455 GNUNET_PQ_result_spec_auto_from_type("key", 461 GNUNET_PQ_result_spec_auto_from_type ("key",
456 &key), 462 &key),
457 GNUNET_PQ_result_spec_end 463 GNUNET_PQ_result_spec_end
458 }; 464 };
459 465
@@ -461,43 +467,43 @@ postgres_plugin_get_random(void *cls,
461 return 0; 467 return 0;
462 if (NULL == iter) 468 if (NULL == iter)
463 return 1; 469 return 1;
464 now = GNUNET_TIME_absolute_get(); 470 now = GNUNET_TIME_absolute_get ();
465 off = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE, 471 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
466 plugin->num_items); 472 plugin->num_items);
467 res = GNUNET_PQ_eval_prepared_singleton_select(plugin->dbh, 473 res = GNUNET_PQ_eval_prepared_singleton_select (plugin->dbh,
468 "get_random", 474 "get_random",
469 params, 475 params,
470 rs); 476 rs);
471 if (0 > res) 477 if (0 > res)
472 { 478 {
473 GNUNET_break(0); 479 GNUNET_break (0);
474 return 0; 480 return 0;
475 } 481 }
476 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == res) 482 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == res)
477 { 483 {
478 GNUNET_break(0); 484 GNUNET_break (0);
479 return 0; 485 return 0;
480 } 486 }
481 if (0 != (path_len % sizeof(struct GNUNET_PeerIdentity))) 487 if (0 != (path_len % sizeof(struct GNUNET_PeerIdentity)))
482 { 488 {
483 GNUNET_break(0); 489 GNUNET_break (0);
484 path_len = 0; 490 path_len = 0;
485 } 491 }
486 path_len %= sizeof(struct GNUNET_PeerIdentity); 492 path_len %= sizeof(struct GNUNET_PeerIdentity);
487 LOG(GNUNET_ERROR_TYPE_DEBUG, 493 LOG (GNUNET_ERROR_TYPE_DEBUG,
488 "Found random value with key %s of size %u bytes and type %u in database\n", 494 "Found random value with key %s of size %u bytes and type %u in database\n",
489 GNUNET_h2s(&key), 495 GNUNET_h2s (&key),
490 (unsigned int)data_size, 496 (unsigned int) data_size,
491 (unsigned int)type); 497 (unsigned int) type);
492 (void)iter(iter_cls, 498 (void) iter (iter_cls,
493 &key, 499 &key,
494 data_size, 500 data_size,
495 data, 501 data,
496 (enum GNUNET_BLOCK_Type)type, 502 (enum GNUNET_BLOCK_Type) type,
497 expiration_time, 503 expiration_time,
498 path_len, 504 path_len,
499 path); 505 path);
500 GNUNET_PQ_cleanup_result(rs); 506 GNUNET_PQ_cleanup_result (rs);
501 return 1; 507 return 1;
502} 508}
503 509
@@ -505,7 +511,8 @@ postgres_plugin_get_random(void *cls,
505/** 511/**
506 * Closure for #extract_result_cb. 512 * Closure for #extract_result_cb.
507 */ 513 */
508struct ExtractResultContext { 514struct ExtractResultContext
515{
509 /** 516 /**
510 * Function to call for each result found. 517 * Function to call for each result found.
511 */ 518 */
@@ -528,74 +535,74 @@ struct ExtractResultContext {
528 * @param num_result the number of results in @a result 535 * @param num_result the number of results in @a result
529 */ 536 */
530static void 537static void
531extract_result_cb(void *cls, 538extract_result_cb (void *cls,
532 PGresult *result, 539 PGresult *result,
533 unsigned int num_results) 540 unsigned int num_results)
534{ 541{
535 struct ExtractResultContext *erc = cls; 542 struct ExtractResultContext *erc = cls;
536 543
537 if (NULL == erc->iter) 544 if (NULL == erc->iter)
538 return; 545 return;
539 for (unsigned int i = 0; i < num_results; i++) 546 for (unsigned int i = 0; i < num_results; i++)
547 {
548 struct GNUNET_TIME_Absolute expiration_time;
549 uint32_t type;
550 void *data;
551 size_t data_size;
552 struct GNUNET_PeerIdentity *path;
553 size_t path_len;
554 struct GNUNET_HashCode key;
555 struct GNUNET_PQ_ResultSpec rs[] = {
556 GNUNET_PQ_result_spec_absolute_time ("",
557 &expiration_time),
558 GNUNET_PQ_result_spec_uint32 ("type",
559 &type),
560 GNUNET_PQ_result_spec_variable_size ("value",
561 &data,
562 &data_size),
563 GNUNET_PQ_result_spec_variable_size ("path",
564 (void **) &path,
565 &path_len),
566 GNUNET_PQ_result_spec_auto_from_type ("key",
567 &key),
568 GNUNET_PQ_result_spec_end
569 };
570
571 if (GNUNET_YES !=
572 GNUNET_PQ_extract_result (result,
573 rs,
574 i))
540 { 575 {
541 struct GNUNET_TIME_Absolute expiration_time; 576 GNUNET_break (0);
542 uint32_t type; 577 return;
543 void *data;
544 size_t data_size;
545 struct GNUNET_PeerIdentity *path;
546 size_t path_len;
547 struct GNUNET_HashCode key;
548 struct GNUNET_PQ_ResultSpec rs[] = {
549 GNUNET_PQ_result_spec_absolute_time("",
550 &expiration_time),
551 GNUNET_PQ_result_spec_uint32("type",
552 &type),
553 GNUNET_PQ_result_spec_variable_size("value",
554 &data,
555 &data_size),
556 GNUNET_PQ_result_spec_variable_size("path",
557 (void **)&path,
558 &path_len),
559 GNUNET_PQ_result_spec_auto_from_type("key",
560 &key),
561 GNUNET_PQ_result_spec_end
562 };
563
564 if (GNUNET_YES !=
565 GNUNET_PQ_extract_result(result,
566 rs,
567 i))
568 {
569 GNUNET_break(0);
570 return;
571 }
572 if (0 != (path_len % sizeof(struct GNUNET_PeerIdentity)))
573 {
574 GNUNET_break(0);
575 path_len = 0;
576 }
577 path_len %= sizeof(struct GNUNET_PeerIdentity);
578 LOG(GNUNET_ERROR_TYPE_DEBUG,
579 "Found result of size %u bytes and type %u in database\n",
580 (unsigned int)data_size,
581 (unsigned int)type);
582 if (GNUNET_SYSERR ==
583 erc->iter(erc->iter_cls,
584 &key,
585 data_size,
586 data,
587 (enum GNUNET_BLOCK_Type)type,
588 expiration_time,
589 path_len,
590 path))
591 {
592 LOG(GNUNET_ERROR_TYPE_DEBUG,
593 "Ending iteration (client error)\n");
594 GNUNET_PQ_cleanup_result(rs);
595 break;
596 }
597 GNUNET_PQ_cleanup_result(rs);
598 } 578 }
579 if (0 != (path_len % sizeof(struct GNUNET_PeerIdentity)))
580 {
581 GNUNET_break (0);
582 path_len = 0;
583 }
584 path_len %= sizeof(struct GNUNET_PeerIdentity);
585 LOG (GNUNET_ERROR_TYPE_DEBUG,
586 "Found result of size %u bytes and type %u in database\n",
587 (unsigned int) data_size,
588 (unsigned int) type);
589 if (GNUNET_SYSERR ==
590 erc->iter (erc->iter_cls,
591 &key,
592 data_size,
593 data,
594 (enum GNUNET_BLOCK_Type) type,
595 expiration_time,
596 path_len,
597 path))
598 {
599 LOG (GNUNET_ERROR_TYPE_DEBUG,
600 "Ending iteration (client error)\n");
601 GNUNET_PQ_cleanup_result (rs);
602 break;
603 }
604 GNUNET_PQ_cleanup_result (rs);
605 }
599} 606}
600 607
601 608
@@ -613,19 +620,19 @@ extract_result_cb(void *cls,
613 * @return the number of results found 620 * @return the number of results found
614 */ 621 */
615static unsigned int 622static unsigned int
616postgres_plugin_get_closest(void *cls, 623postgres_plugin_get_closest (void *cls,
617 const struct GNUNET_HashCode *key, 624 const struct GNUNET_HashCode *key,
618 unsigned int num_results, 625 unsigned int num_results,
619 GNUNET_DATACACHE_Iterator iter, 626 GNUNET_DATACACHE_Iterator iter,
620 void *iter_cls) 627 void *iter_cls)
621{ 628{
622 struct Plugin *plugin = cls; 629 struct Plugin *plugin = cls;
623 uint32_t num_results32 = (uint32_t)num_results; 630 uint32_t num_results32 = (uint32_t) num_results;
624 struct GNUNET_TIME_Absolute now; 631 struct GNUNET_TIME_Absolute now;
625 struct GNUNET_PQ_QueryParam params[] = { 632 struct GNUNET_PQ_QueryParam params[] = {
626 GNUNET_PQ_query_param_auto_from_type(key), 633 GNUNET_PQ_query_param_auto_from_type (key),
627 GNUNET_PQ_query_param_absolute_time(&now), 634 GNUNET_PQ_query_param_absolute_time (&now),
628 GNUNET_PQ_query_param_uint32(&num_results32), 635 GNUNET_PQ_query_param_uint32 (&num_results32),
629 GNUNET_PQ_query_param_end 636 GNUNET_PQ_query_param_end
630 }; 637 };
631 enum GNUNET_DB_QueryStatus res; 638 enum GNUNET_DB_QueryStatus res;
@@ -633,25 +640,25 @@ postgres_plugin_get_closest(void *cls,
633 640
634 erc.iter = iter; 641 erc.iter = iter;
635 erc.iter_cls = iter_cls; 642 erc.iter_cls = iter_cls;
636 now = GNUNET_TIME_absolute_get(); 643 now = GNUNET_TIME_absolute_get ();
637 res = GNUNET_PQ_eval_prepared_multi_select(plugin->dbh, 644 res = GNUNET_PQ_eval_prepared_multi_select (plugin->dbh,
638 "get_closest", 645 "get_closest",
639 params, 646 params,
640 &extract_result_cb, 647 &extract_result_cb,
641 &erc); 648 &erc);
642 if (0 > res) 649 if (0 > res)
643 { 650 {
644 LOG(GNUNET_ERROR_TYPE_DEBUG, 651 LOG (GNUNET_ERROR_TYPE_DEBUG,
645 "Ending iteration (postgres error)\n"); 652 "Ending iteration (postgres error)\n");
646 return 0; 653 return 0;
647 } 654 }
648 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == res) 655 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == res)
649 { 656 {
650 /* no result */ 657 /* no result */
651 LOG(GNUNET_ERROR_TYPE_DEBUG, 658 LOG (GNUNET_ERROR_TYPE_DEBUG,
652 "Ending iteration (no more results)\n"); 659 "Ending iteration (no more results)\n");
653 return 0; 660 return 0;
654 } 661 }
655 return res; 662 return res;
656} 663}
657 664
@@ -663,30 +670,30 @@ postgres_plugin_get_closest(void *cls,
663 * @return the plugin's closure (our `struct Plugin`) 670 * @return the plugin's closure (our `struct Plugin`)
664 */ 671 */
665void * 672void *
666libgnunet_plugin_datacache_postgres_init(void *cls) 673libgnunet_plugin_datacache_postgres_init (void *cls)
667{ 674{
668 struct GNUNET_DATACACHE_PluginEnvironment *env = cls; 675 struct GNUNET_DATACACHE_PluginEnvironment *env = cls;
669 struct GNUNET_DATACACHE_PluginFunctions *api; 676 struct GNUNET_DATACACHE_PluginFunctions *api;
670 struct Plugin *plugin; 677 struct Plugin *plugin;
671 678
672 plugin = GNUNET_new(struct Plugin); 679 plugin = GNUNET_new (struct Plugin);
673 plugin->env = env; 680 plugin->env = env;
674 681
675 if (GNUNET_OK != init_connection(plugin)) 682 if (GNUNET_OK != init_connection (plugin))
676 { 683 {
677 GNUNET_free(plugin); 684 GNUNET_free (plugin);
678 return NULL; 685 return NULL;
679 } 686 }
680 687
681 api = GNUNET_new(struct GNUNET_DATACACHE_PluginFunctions); 688 api = GNUNET_new (struct GNUNET_DATACACHE_PluginFunctions);
682 api->cls = plugin; 689 api->cls = plugin;
683 api->get = &postgres_plugin_get; 690 api->get = &postgres_plugin_get;
684 api->put = &postgres_plugin_put; 691 api->put = &postgres_plugin_put;
685 api->del = &postgres_plugin_del; 692 api->del = &postgres_plugin_del;
686 api->get_random = &postgres_plugin_get_random; 693 api->get_random = &postgres_plugin_get_random;
687 api->get_closest = &postgres_plugin_get_closest; 694 api->get_closest = &postgres_plugin_get_closest;
688 LOG(GNUNET_ERROR_TYPE_INFO, 695 LOG (GNUNET_ERROR_TYPE_INFO,
689 "Postgres datacache running\n"); 696 "Postgres datacache running\n");
690 return api; 697 return api;
691} 698}
692 699
@@ -698,14 +705,14 @@ libgnunet_plugin_datacache_postgres_init(void *cls)
698 * @return NULL 705 * @return NULL
699 */ 706 */
700void * 707void *
701libgnunet_plugin_datacache_postgres_done(void *cls) 708libgnunet_plugin_datacache_postgres_done (void *cls)
702{ 709{
703 struct GNUNET_DATACACHE_PluginFunctions *api = cls; 710 struct GNUNET_DATACACHE_PluginFunctions *api = cls;
704 struct Plugin *plugin = api->cls; 711 struct Plugin *plugin = api->cls;
705 712
706 PQfinish(plugin->dbh); 713 PQfinish (plugin->dbh);
707 GNUNET_free(plugin); 714 GNUNET_free (plugin);
708 GNUNET_free(api); 715 GNUNET_free (api);
709 return NULL; 716 return NULL;
710} 717}
711 718
diff --git a/src/datacache/plugin_datacache_sqlite.c b/src/datacache/plugin_datacache_sqlite.c
index b173af2f3..55a8a7fae 100644
--- a/src/datacache/plugin_datacache_sqlite.c
+++ b/src/datacache/plugin_datacache_sqlite.c
@@ -29,10 +29,10 @@
29#include "gnunet_sq_lib.h" 29#include "gnunet_sq_lib.h"
30#include <sqlite3.h> 30#include <sqlite3.h>
31 31
32#define LOG(kind, ...) GNUNET_log_from(kind, "datacache-sqlite", __VA_ARGS__) 32#define LOG(kind, ...) GNUNET_log_from (kind, "datacache-sqlite", __VA_ARGS__)
33 33
34#define LOG_STRERROR_FILE(kind, op, fn) \ 34#define LOG_STRERROR_FILE(kind, op, fn) \
35 GNUNET_log_from_strerror_file(kind, "datacache-sqlite", op, fn) 35 GNUNET_log_from_strerror_file (kind, "datacache-sqlite", op, fn)
36 36
37 37
38/** 38/**
@@ -44,7 +44,8 @@
44/** 44/**
45 * Context for all functions in this plugin. 45 * Context for all functions in this plugin.
46 */ 46 */
47struct Plugin { 47struct Plugin
48{
48 /** 49 /**
49 * Our execution environment. 50 * Our execution environment.
50 */ 51 */
@@ -117,14 +118,14 @@ struct Plugin {
117 */ 118 */
118#define LOG_SQLITE(db, level, cmd) \ 119#define LOG_SQLITE(db, level, cmd) \
119 do \ 120 do \
120 { \ 121 { \
121 LOG(level, \ 122 LOG (level, \
122 _("`%s' failed at %s:%d with error: %s\n"), \ 123 _ ("`%s' failed at %s:%d with error: %s\n"), \
123 cmd, \ 124 cmd, \
124 __FILE__, \ 125 __FILE__, \
125 __LINE__, \ 126 __LINE__, \
126 sqlite3_errmsg(db)); \ 127 sqlite3_errmsg (db)); \
127 } while (0) 128 } while (0)
128 129
129 130
130/** 131/**
@@ -135,19 +136,19 @@ struct Plugin {
135 */ 136 */
136#define SQLITE3_EXEC(db, cmd) \ 137#define SQLITE3_EXEC(db, cmd) \
137 do \ 138 do \
138 { \ 139 { \
139 emsg = NULL; \ 140 emsg = NULL; \
140 if (SQLITE_OK != sqlite3_exec(db, cmd, NULL, NULL, &emsg)) \ 141 if (SQLITE_OK != sqlite3_exec (db, cmd, NULL, NULL, &emsg)) \
141 { \ 142 { \
142 LOG(GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, \ 143 LOG (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, \
143 _("`%s' failed at %s:%d with error: %s\n"), \ 144 _ ("`%s' failed at %s:%d with error: %s\n"), \
144 "sqlite3_exec", \ 145 "sqlite3_exec", \
145 __FILE__, \ 146 __FILE__, \
146 __LINE__, \ 147 __LINE__, \
147 emsg); \ 148 emsg); \
148 sqlite3_free(emsg); \ 149 sqlite3_free (emsg); \
149 } \ 150 } \
150 } while (0) 151 } while (0)
151 152
152 153
153/** 154/**
@@ -159,17 +160,17 @@ struct Plugin {
159 * @return 0 on success 160 * @return 0 on success
160 */ 161 */
161static int 162static int
162sq_prepare(sqlite3 *dbh, 163sq_prepare (sqlite3 *dbh,
163 const char *zSql, /* SQL statement, UTF-8 encoded */ 164 const char *zSql, /* SQL statement, UTF-8 encoded */
164 sqlite3_stmt **ppStmt) 165 sqlite3_stmt **ppStmt)
165{ /* OUT: Statement handle */ 166{ /* OUT: Statement handle */
166 char *dummy; 167 char *dummy;
167 168
168 return sqlite3_prepare(dbh, 169 return sqlite3_prepare (dbh,
169 zSql, 170 zSql,
170 strlen(zSql), 171 strlen (zSql),
171 ppStmt, 172 ppStmt,
172 (const char **)&dummy); 173 (const char **) &dummy);
173} 174}
174 175
175 176
@@ -188,54 +189,55 @@ sq_prepare(sqlite3 *dbh,
188 * @return 0 if duplicate, -1 on error, number of bytes used otherwise 189 * @return 0 if duplicate, -1 on error, number of bytes used otherwise
189 */ 190 */
190static ssize_t 191static ssize_t
191sqlite_plugin_put(void *cls, 192sqlite_plugin_put (void *cls,
192 const struct GNUNET_HashCode *key, 193 const struct GNUNET_HashCode *key,
193 uint32_t xor_distance, 194 uint32_t xor_distance,
194 size_t size, 195 size_t size,
195 const char *data, 196 const char *data,
196 enum GNUNET_BLOCK_Type type, 197 enum GNUNET_BLOCK_Type type,
197 struct GNUNET_TIME_Absolute discard_time, 198 struct GNUNET_TIME_Absolute discard_time,
198 unsigned int path_info_len, 199 unsigned int path_info_len,
199 const struct GNUNET_PeerIdentity *path_info) 200 const struct GNUNET_PeerIdentity *path_info)
200{ 201{
201 struct Plugin *plugin = cls; 202 struct Plugin *plugin = cls;
202 uint32_t type32 = type; 203 uint32_t type32 = type;
203 struct GNUNET_SQ_QueryParam params[] = 204 struct GNUNET_SQ_QueryParam params[] =
204 { GNUNET_SQ_query_param_uint32(&type32), 205 { GNUNET_SQ_query_param_uint32 (&type32),
205 GNUNET_SQ_query_param_absolute_time(&discard_time), 206 GNUNET_SQ_query_param_absolute_time (&discard_time),
206 GNUNET_SQ_query_param_auto_from_type(key), 207 GNUNET_SQ_query_param_auto_from_type (key),
207 GNUNET_SQ_query_param_uint32(&xor_distance), 208 GNUNET_SQ_query_param_uint32 (&xor_distance),
208 GNUNET_SQ_query_param_fixed_size(data, size), 209 GNUNET_SQ_query_param_fixed_size (data, size),
209 GNUNET_SQ_query_param_fixed_size(path_info, 210 GNUNET_SQ_query_param_fixed_size (path_info,
210 path_info_len * 211 path_info_len
211 sizeof(struct GNUNET_PeerIdentity)), 212 * sizeof(struct GNUNET_PeerIdentity)),
212 GNUNET_SQ_query_param_end }; 213 GNUNET_SQ_query_param_end };
213 214
214 LOG(GNUNET_ERROR_TYPE_DEBUG, 215 LOG (GNUNET_ERROR_TYPE_DEBUG,
215 "Processing PUT of %u bytes with key `%s' and expiration %s\n", 216 "Processing PUT of %u bytes with key `%s' and expiration %s\n",
216 (unsigned int)size, 217 (unsigned int) size,
217 GNUNET_h2s(key), 218 GNUNET_h2s (key),
218 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_remaining( 219 GNUNET_STRINGS_relative_time_to_string (
219 discard_time), 220 GNUNET_TIME_absolute_get_remaining (
220 GNUNET_YES)); 221 discard_time),
221 if (GNUNET_OK != GNUNET_SQ_bind(plugin->insert_stmt, params)) 222 GNUNET_YES));
222 { 223 if (GNUNET_OK != GNUNET_SQ_bind (plugin->insert_stmt, params))
223 LOG_SQLITE(plugin->dbh, 224 {
224 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 225 LOG_SQLITE (plugin->dbh,
225 "sqlite3_bind_xxx"); 226 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
226 GNUNET_SQ_reset(plugin->dbh, plugin->insert_stmt); 227 "sqlite3_bind_xxx");
227 return -1; 228 GNUNET_SQ_reset (plugin->dbh, plugin->insert_stmt);
228 } 229 return -1;
229 if (SQLITE_DONE != sqlite3_step(plugin->insert_stmt)) 230 }
230 { 231 if (SQLITE_DONE != sqlite3_step (plugin->insert_stmt))
231 LOG_SQLITE(plugin->dbh, 232 {
232 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 233 LOG_SQLITE (plugin->dbh,
233 "sqlite3_step"); 234 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
234 GNUNET_SQ_reset(plugin->dbh, plugin->insert_stmt); 235 "sqlite3_step");
235 return -1; 236 GNUNET_SQ_reset (plugin->dbh, plugin->insert_stmt);
236 } 237 return -1;
238 }
237 plugin->num_items++; 239 plugin->num_items++;
238 GNUNET_SQ_reset(plugin->dbh, plugin->insert_stmt); 240 GNUNET_SQ_reset (plugin->dbh, plugin->insert_stmt);
239 return size + OVERHEAD; 241 return size + OVERHEAD;
240} 242}
241 243
@@ -252,11 +254,11 @@ sqlite_plugin_put(void *cls,
252 * @return the number of results found 254 * @return the number of results found
253 */ 255 */
254static unsigned int 256static unsigned int
255sqlite_plugin_get(void *cls, 257sqlite_plugin_get (void *cls,
256 const struct GNUNET_HashCode *key, 258 const struct GNUNET_HashCode *key,
257 enum GNUNET_BLOCK_Type type, 259 enum GNUNET_BLOCK_Type type,
258 GNUNET_DATACACHE_Iterator iter, 260 GNUNET_DATACACHE_Iterator iter,
259 void *iter_cls) 261 void *iter_cls)
260{ 262{
261 struct Plugin *plugin = cls; 263 struct Plugin *plugin = cls;
262 uint32_t type32 = type; 264 uint32_t type32 = type;
@@ -270,100 +272,100 @@ sqlite_plugin_get(void *cls,
270 size_t psize; 272 size_t psize;
271 struct GNUNET_PeerIdentity *path; 273 struct GNUNET_PeerIdentity *path;
272 struct GNUNET_SQ_QueryParam params_count[] = 274 struct GNUNET_SQ_QueryParam params_count[] =
273 { GNUNET_SQ_query_param_auto_from_type(key), 275 { GNUNET_SQ_query_param_auto_from_type (key),
274 GNUNET_SQ_query_param_uint32(&type32), 276 GNUNET_SQ_query_param_uint32 (&type32),
275 GNUNET_SQ_query_param_absolute_time(&now), 277 GNUNET_SQ_query_param_absolute_time (&now),
276 GNUNET_SQ_query_param_end }; 278 GNUNET_SQ_query_param_end };
277 struct GNUNET_SQ_QueryParam params_select[] = 279 struct GNUNET_SQ_QueryParam params_select[] =
278 { GNUNET_SQ_query_param_auto_from_type(key), 280 { GNUNET_SQ_query_param_auto_from_type (key),
279 GNUNET_SQ_query_param_uint32(&type32), 281 GNUNET_SQ_query_param_uint32 (&type32),
280 GNUNET_SQ_query_param_absolute_time(&now), 282 GNUNET_SQ_query_param_absolute_time (&now),
281 GNUNET_SQ_query_param_uint32(&off), 283 GNUNET_SQ_query_param_uint32 (&off),
282 GNUNET_SQ_query_param_end }; 284 GNUNET_SQ_query_param_end };
283 struct GNUNET_SQ_ResultSpec rs[] = 285 struct GNUNET_SQ_ResultSpec rs[] =
284 { GNUNET_SQ_result_spec_variable_size(&dat, &size), 286 { GNUNET_SQ_result_spec_variable_size (&dat, &size),
285 GNUNET_SQ_result_spec_absolute_time(&exp), 287 GNUNET_SQ_result_spec_absolute_time (&exp),
286 GNUNET_SQ_result_spec_variable_size((void **)&path, &psize), 288 GNUNET_SQ_result_spec_variable_size ((void **) &path, &psize),
287 GNUNET_SQ_result_spec_end }; 289 GNUNET_SQ_result_spec_end };
288 290
289 now = GNUNET_TIME_absolute_get(); 291 now = GNUNET_TIME_absolute_get ();
290 LOG(GNUNET_ERROR_TYPE_DEBUG, 292 LOG (GNUNET_ERROR_TYPE_DEBUG,
291 "Processing GET for key `%s'\n", 293 "Processing GET for key `%s'\n",
292 GNUNET_h2s(key)); 294 GNUNET_h2s (key));
295
296 if (GNUNET_OK != GNUNET_SQ_bind (plugin->get_count_stmt, params_count))
297 {
298 LOG_SQLITE (plugin->dbh,
299 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
300 "sqlite3_bind_xxx");
301 GNUNET_SQ_reset (plugin->dbh, plugin->get_count_stmt);
302 return 0;
303 }
304 if (SQLITE_ROW != sqlite3_step (plugin->get_count_stmt))
305 {
306 LOG_SQLITE (plugin->dbh,
307 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
308 "sqlite_step");
309 GNUNET_SQ_reset (plugin->dbh, plugin->get_count_stmt);
310 LOG (GNUNET_ERROR_TYPE_DEBUG,
311 "No content found when processing GET for key `%s'\n",
312 GNUNET_h2s (key));
313 return 0;
314 }
315 total = sqlite3_column_int (plugin->get_count_stmt, 0);
316 GNUNET_SQ_reset (plugin->dbh, plugin->get_count_stmt);
317 if ((0 == total) || (NULL == iter))
318 {
319 if (0 == total)
320 LOG (GNUNET_ERROR_TYPE_DEBUG,
321 "No content found when processing GET for key `%s'\n",
322 GNUNET_h2s (key));
323 return total;
324 }
293 325
294 if (GNUNET_OK != GNUNET_SQ_bind(plugin->get_count_stmt, params_count)) 326 cnt = 0;
327 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, total);
328 while (cnt < total)
329 {
330 off = (off + 1) % total;
331 if (GNUNET_OK != GNUNET_SQ_bind (plugin->get_stmt, params_select))
295 { 332 {
296 LOG_SQLITE(plugin->dbh, 333 LOG_SQLITE (plugin->dbh,
297 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 334 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
298 "sqlite3_bind_xxx"); 335 "sqlite3_bind_xxx");
299 GNUNET_SQ_reset(plugin->dbh, plugin->get_count_stmt); 336 GNUNET_SQ_reset (plugin->dbh, plugin->get_stmt);
300 return 0; 337 return cnt;
301 } 338 }
302 if (SQLITE_ROW != sqlite3_step(plugin->get_count_stmt)) 339 if (SQLITE_ROW != sqlite3_step (plugin->get_stmt))
340 break;
341 if (GNUNET_OK != GNUNET_SQ_extract_result (plugin->get_stmt, rs))
303 { 342 {
304 LOG_SQLITE(plugin->dbh, 343 GNUNET_break (0);
305 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 344 GNUNET_SQ_reset (plugin->dbh, plugin->get_stmt);
306 "sqlite_step"); 345 break;
307 GNUNET_SQ_reset(plugin->dbh, plugin->get_count_stmt);
308 LOG(GNUNET_ERROR_TYPE_DEBUG,
309 "No content found when processing GET for key `%s'\n",
310 GNUNET_h2s(key));
311 return 0;
312 } 346 }
313 total = sqlite3_column_int(plugin->get_count_stmt, 0); 347 if (0 != psize % sizeof(struct GNUNET_PeerIdentity))
314 GNUNET_SQ_reset(plugin->dbh, plugin->get_count_stmt);
315 if ((0 == total) || (NULL == iter))
316 { 348 {
317 if (0 == total) 349 GNUNET_break (0);
318 LOG(GNUNET_ERROR_TYPE_DEBUG, 350 psize = 0;
319 "No content found when processing GET for key `%s'\n", 351 path = NULL;
320 GNUNET_h2s(key));
321 return total;
322 } 352 }
323 353 psize /= sizeof(struct GNUNET_PeerIdentity);
324 cnt = 0; 354 cnt++;
325 off = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, total); 355 LOG (GNUNET_ERROR_TYPE_DEBUG,
326 while (cnt < total) 356 "Found %u-byte result when processing GET for key `%s'\n",
357 (unsigned int) size,
358 GNUNET_h2s (key));
359 if (GNUNET_OK != iter (iter_cls, key, size, dat, type, exp, psize, path))
327 { 360 {
328 off = (off + 1) % total; 361 GNUNET_SQ_cleanup_result (rs);
329 if (GNUNET_OK != GNUNET_SQ_bind(plugin->get_stmt, params_select)) 362 GNUNET_SQ_reset (plugin->dbh, plugin->get_stmt);
330 { 363 break;
331 LOG_SQLITE(plugin->dbh,
332 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
333 "sqlite3_bind_xxx");
334 GNUNET_SQ_reset(plugin->dbh, plugin->get_stmt);
335 return cnt;
336 }
337 if (SQLITE_ROW != sqlite3_step(plugin->get_stmt))
338 break;
339 if (GNUNET_OK != GNUNET_SQ_extract_result(plugin->get_stmt, rs))
340 {
341 GNUNET_break(0);
342 GNUNET_SQ_reset(plugin->dbh, plugin->get_stmt);
343 break;
344 }
345 if (0 != psize % sizeof(struct GNUNET_PeerIdentity))
346 {
347 GNUNET_break(0);
348 psize = 0;
349 path = NULL;
350 }
351 psize /= sizeof(struct GNUNET_PeerIdentity);
352 cnt++;
353 LOG(GNUNET_ERROR_TYPE_DEBUG,
354 "Found %u-byte result when processing GET for key `%s'\n",
355 (unsigned int)size,
356 GNUNET_h2s(key));
357 if (GNUNET_OK != iter(iter_cls, key, size, dat, type, exp, psize, path))
358 {
359 GNUNET_SQ_cleanup_result(rs);
360 GNUNET_SQ_reset(plugin->dbh, plugin->get_stmt);
361 break;
362 }
363 GNUNET_SQ_cleanup_result(rs);
364 GNUNET_SQ_reset(plugin->dbh, plugin->get_stmt);
365 } 364 }
366 GNUNET_SQ_reset(plugin->dbh, plugin->get_stmt); 365 GNUNET_SQ_cleanup_result (rs);
366 GNUNET_SQ_reset (plugin->dbh, plugin->get_stmt);
367 }
368 GNUNET_SQ_reset (plugin->dbh, plugin->get_stmt);
367 return cnt; 369 return cnt;
368} 370}
369 371
@@ -376,7 +378,7 @@ sqlite_plugin_get(void *cls,
376 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 378 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
377 */ 379 */
378static int 380static int
379sqlite_plugin_del(void *cls) 381sqlite_plugin_del (void *cls)
380{ 382{
381 struct Plugin *plugin = cls; 383 struct Plugin *plugin = cls;
382 uint64_t rowid; 384 uint64_t rowid;
@@ -385,65 +387,66 @@ sqlite_plugin_del(void *cls)
385 struct GNUNET_HashCode hc; 387 struct GNUNET_HashCode hc;
386 struct GNUNET_TIME_Absolute now; 388 struct GNUNET_TIME_Absolute now;
387 struct GNUNET_SQ_ResultSpec rs[] = 389 struct GNUNET_SQ_ResultSpec rs[] =
388 { GNUNET_SQ_result_spec_uint64(&rowid), 390 { GNUNET_SQ_result_spec_uint64 (&rowid),
389 GNUNET_SQ_result_spec_auto_from_type(&hc), 391 GNUNET_SQ_result_spec_auto_from_type (&hc),
390 GNUNET_SQ_result_spec_variable_size((void **)&data, &dsize), 392 GNUNET_SQ_result_spec_variable_size ((void **) &data, &dsize),
391 GNUNET_SQ_result_spec_end }; 393 GNUNET_SQ_result_spec_end };
392 struct GNUNET_SQ_QueryParam params[] = { GNUNET_SQ_query_param_uint64(&rowid), 394 struct GNUNET_SQ_QueryParam params[] = { GNUNET_SQ_query_param_uint64 (
395 &rowid),
393 GNUNET_SQ_query_param_end }; 396 GNUNET_SQ_query_param_end };
394 struct GNUNET_SQ_QueryParam time_params[] = 397 struct GNUNET_SQ_QueryParam time_params[] =
395 { GNUNET_SQ_query_param_absolute_time(&now), GNUNET_SQ_query_param_end }; 398 { GNUNET_SQ_query_param_absolute_time (&now), GNUNET_SQ_query_param_end };
396 399
397 LOG(GNUNET_ERROR_TYPE_DEBUG, "Processing DEL\n"); 400 LOG (GNUNET_ERROR_TYPE_DEBUG, "Processing DEL\n");
398 now = GNUNET_TIME_absolute_get(); 401 now = GNUNET_TIME_absolute_get ();
399 if (GNUNET_OK != GNUNET_SQ_bind(plugin->del_expired_stmt, time_params)) 402 if (GNUNET_OK != GNUNET_SQ_bind (plugin->del_expired_stmt, time_params))
400 { 403 {
401 LOG_SQLITE(plugin->dbh, 404 LOG_SQLITE (plugin->dbh,
402 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 405 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
403 "sqlite3_bind"); 406 "sqlite3_bind");
404 GNUNET_SQ_reset(plugin->dbh, plugin->del_expired_stmt); 407 GNUNET_SQ_reset (plugin->dbh, plugin->del_expired_stmt);
405 return GNUNET_SYSERR; 408 return GNUNET_SYSERR;
406 } 409 }
407 if ((SQLITE_ROW != sqlite3_step(plugin->del_expired_stmt)) || 410 if ((SQLITE_ROW != sqlite3_step (plugin->del_expired_stmt)) ||
408 (GNUNET_OK != GNUNET_SQ_extract_result(plugin->del_expired_stmt, rs))) 411 (GNUNET_OK != GNUNET_SQ_extract_result (plugin->del_expired_stmt, rs)))
409 { 412 {
410 GNUNET_SQ_reset(plugin->dbh, plugin->del_expired_stmt); 413 GNUNET_SQ_reset (plugin->dbh, plugin->del_expired_stmt);
411 if (SQLITE_ROW != sqlite3_step(plugin->del_select_stmt)) 414 if (SQLITE_ROW != sqlite3_step (plugin->del_select_stmt))
412 {
413 LOG_SQLITE(plugin->dbh,
414 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
415 "sqlite3_step");
416 GNUNET_SQ_reset(plugin->dbh, plugin->del_select_stmt);
417 return GNUNET_SYSERR;
418 }
419 if (GNUNET_OK != GNUNET_SQ_extract_result(plugin->del_select_stmt, rs))
420 {
421 GNUNET_SQ_reset(plugin->dbh, plugin->del_select_stmt);
422 GNUNET_break(0);
423 return GNUNET_SYSERR;
424 }
425 }
426 GNUNET_SQ_cleanup_result(rs);
427 GNUNET_SQ_reset(plugin->dbh, plugin->del_select_stmt);
428 if (GNUNET_OK != GNUNET_SQ_bind(plugin->del_stmt, params))
429 { 415 {
430 LOG_SQLITE(plugin->dbh, 416 LOG_SQLITE (plugin->dbh,
431 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 417 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
432 "sqlite3_bind"); 418 "sqlite3_step");
433 GNUNET_SQ_reset(plugin->dbh, plugin->del_stmt); 419 GNUNET_SQ_reset (plugin->dbh, plugin->del_select_stmt);
434 return GNUNET_SYSERR; 420 return GNUNET_SYSERR;
435 } 421 }
436 if (SQLITE_DONE != sqlite3_step(plugin->del_stmt)) 422 if (GNUNET_OK != GNUNET_SQ_extract_result (plugin->del_select_stmt, rs))
437 { 423 {
438 LOG_SQLITE(plugin->dbh, 424 GNUNET_SQ_reset (plugin->dbh, plugin->del_select_stmt);
439 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 425 GNUNET_break (0);
440 "sqlite3_step");
441 GNUNET_SQ_reset(plugin->dbh, plugin->del_stmt);
442 return GNUNET_SYSERR; 426 return GNUNET_SYSERR;
443 } 427 }
428 }
429 GNUNET_SQ_cleanup_result (rs);
430 GNUNET_SQ_reset (plugin->dbh, plugin->del_select_stmt);
431 if (GNUNET_OK != GNUNET_SQ_bind (plugin->del_stmt, params))
432 {
433 LOG_SQLITE (plugin->dbh,
434 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
435 "sqlite3_bind");
436 GNUNET_SQ_reset (plugin->dbh, plugin->del_stmt);
437 return GNUNET_SYSERR;
438 }
439 if (SQLITE_DONE != sqlite3_step (plugin->del_stmt))
440 {
441 LOG_SQLITE (plugin->dbh,
442 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
443 "sqlite3_step");
444 GNUNET_SQ_reset (plugin->dbh, plugin->del_stmt);
445 return GNUNET_SYSERR;
446 }
444 plugin->num_items--; 447 plugin->num_items--;
445 plugin->env->delete_notify(plugin->env->cls, &hc, dsize + OVERHEAD); 448 plugin->env->delete_notify (plugin->env->cls, &hc, dsize + OVERHEAD);
446 GNUNET_SQ_reset(plugin->dbh, plugin->del_stmt); 449 GNUNET_SQ_reset (plugin->dbh, plugin->del_stmt);
447 return GNUNET_OK; 450 return GNUNET_OK;
448} 451}
449 452
@@ -457,9 +460,9 @@ sqlite_plugin_del(void *cls)
457 * @return the number of results found, zero (datacache empty) or one 460 * @return the number of results found, zero (datacache empty) or one
458 */ 461 */
459static unsigned int 462static unsigned int
460sqlite_plugin_get_random(void *cls, 463sqlite_plugin_get_random (void *cls,
461 GNUNET_DATACACHE_Iterator iter, 464 GNUNET_DATACACHE_Iterator iter,
462 void *iter_cls) 465 void *iter_cls)
463{ 466{
464 struct Plugin *plugin = cls; 467 struct Plugin *plugin = cls;
465 struct GNUNET_TIME_Absolute exp; 468 struct GNUNET_TIME_Absolute exp;
@@ -470,14 +473,14 @@ sqlite_plugin_get_random(void *cls,
470 uint32_t type; 473 uint32_t type;
471 struct GNUNET_PeerIdentity *path; 474 struct GNUNET_PeerIdentity *path;
472 struct GNUNET_HashCode key; 475 struct GNUNET_HashCode key;
473 struct GNUNET_SQ_QueryParam params[] = { GNUNET_SQ_query_param_uint32(&off), 476 struct GNUNET_SQ_QueryParam params[] = { GNUNET_SQ_query_param_uint32 (&off),
474 GNUNET_SQ_query_param_end }; 477 GNUNET_SQ_query_param_end };
475 struct GNUNET_SQ_ResultSpec rs[] = 478 struct GNUNET_SQ_ResultSpec rs[] =
476 { GNUNET_SQ_result_spec_variable_size(&dat, &size), 479 { GNUNET_SQ_result_spec_variable_size (&dat, &size),
477 GNUNET_SQ_result_spec_absolute_time(&exp), 480 GNUNET_SQ_result_spec_absolute_time (&exp),
478 GNUNET_SQ_result_spec_variable_size((void **)&path, &psize), 481 GNUNET_SQ_result_spec_variable_size ((void **) &path, &psize),
479 GNUNET_SQ_result_spec_auto_from_type(&key), 482 GNUNET_SQ_result_spec_auto_from_type (&key),
480 GNUNET_SQ_result_spec_uint32(&type), 483 GNUNET_SQ_result_spec_uint32 (&type),
481 GNUNET_SQ_result_spec_end }; 484 GNUNET_SQ_result_spec_end };
482 485
483 if (0 == plugin->num_items) 486 if (0 == plugin->num_items)
@@ -485,44 +488,44 @@ sqlite_plugin_get_random(void *cls,
485 if (NULL == iter) 488 if (NULL == iter)
486 return 1; 489 return 1;
487 off = 490 off =
488 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE, plugin->num_items); 491 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, plugin->num_items);
489 if (GNUNET_OK != GNUNET_SQ_bind(plugin->get_random_stmt, params)) 492 if (GNUNET_OK != GNUNET_SQ_bind (plugin->get_random_stmt, params))
490 { 493 {
491 return 0; 494 return 0;
492 } 495 }
493 if (SQLITE_ROW != sqlite3_step(plugin->get_random_stmt)) 496 if (SQLITE_ROW != sqlite3_step (plugin->get_random_stmt))
494 { 497 {
495 GNUNET_break(0); 498 GNUNET_break (0);
496 GNUNET_SQ_reset(plugin->dbh, plugin->get_random_stmt); 499 GNUNET_SQ_reset (plugin->dbh, plugin->get_random_stmt);
497 return 0; 500 return 0;
498 } 501 }
499 if (GNUNET_OK != GNUNET_SQ_extract_result(plugin->get_random_stmt, rs)) 502 if (GNUNET_OK != GNUNET_SQ_extract_result (plugin->get_random_stmt, rs))
500 { 503 {
501 GNUNET_break(0); 504 GNUNET_break (0);
502 GNUNET_SQ_reset(plugin->dbh, plugin->get_random_stmt); 505 GNUNET_SQ_reset (plugin->dbh, plugin->get_random_stmt);
503 return 0; 506 return 0;
504 } 507 }
505 if (0 != psize % sizeof(struct GNUNET_PeerIdentity)) 508 if (0 != psize % sizeof(struct GNUNET_PeerIdentity))
506 { 509 {
507 GNUNET_break(0); 510 GNUNET_break (0);
508 psize = 0; 511 psize = 0;
509 path = NULL; 512 path = NULL;
510 } 513 }
511 psize /= sizeof(struct GNUNET_PeerIdentity); 514 psize /= sizeof(struct GNUNET_PeerIdentity);
512 LOG(GNUNET_ERROR_TYPE_DEBUG, 515 LOG (GNUNET_ERROR_TYPE_DEBUG,
513 "Found %u-byte result with key %s when processing GET-RANDOM\n", 516 "Found %u-byte result with key %s when processing GET-RANDOM\n",
514 (unsigned int)size, 517 (unsigned int) size,
515 GNUNET_h2s(&key)); 518 GNUNET_h2s (&key));
516 (void)iter(iter_cls, 519 (void) iter (iter_cls,
517 &key, 520 &key,
518 size, 521 size,
519 dat, 522 dat,
520 (enum GNUNET_BLOCK_Type)type, 523 (enum GNUNET_BLOCK_Type) type,
521 exp, 524 exp,
522 psize, 525 psize,
523 path); 526 path);
524 GNUNET_SQ_cleanup_result(rs); 527 GNUNET_SQ_cleanup_result (rs);
525 GNUNET_SQ_reset(plugin->dbh, plugin->get_random_stmt); 528 GNUNET_SQ_reset (plugin->dbh, plugin->get_random_stmt);
526 return 1; 529 return 1;
527} 530}
528 531
@@ -541,11 +544,11 @@ sqlite_plugin_get_random(void *cls,
541 * @return the number of results found 544 * @return the number of results found
542 */ 545 */
543static unsigned int 546static unsigned int
544sqlite_plugin_get_closest(void *cls, 547sqlite_plugin_get_closest (void *cls,
545 const struct GNUNET_HashCode *key, 548 const struct GNUNET_HashCode *key,
546 unsigned int num_results, 549 unsigned int num_results,
547 GNUNET_DATACACHE_Iterator iter, 550 GNUNET_DATACACHE_Iterator iter,
548 void *iter_cls) 551 void *iter_cls)
549{ 552{
550 struct Plugin *plugin = cls; 553 struct Plugin *plugin = cls;
551 uint32_t num_results32 = num_results; 554 uint32_t num_results32 = num_results;
@@ -559,58 +562,58 @@ sqlite_plugin_get_closest(void *cls,
559 struct GNUNET_HashCode hc; 562 struct GNUNET_HashCode hc;
560 struct GNUNET_PeerIdentity *path; 563 struct GNUNET_PeerIdentity *path;
561 struct GNUNET_SQ_QueryParam params[] = 564 struct GNUNET_SQ_QueryParam params[] =
562 { GNUNET_SQ_query_param_auto_from_type(key), 565 { GNUNET_SQ_query_param_auto_from_type (key),
563 GNUNET_SQ_query_param_absolute_time(&now), 566 GNUNET_SQ_query_param_absolute_time (&now),
564 GNUNET_SQ_query_param_uint32(&num_results32), 567 GNUNET_SQ_query_param_uint32 (&num_results32),
565 GNUNET_SQ_query_param_end }; 568 GNUNET_SQ_query_param_end };
566 struct GNUNET_SQ_ResultSpec rs[] = 569 struct GNUNET_SQ_ResultSpec rs[] =
567 { GNUNET_SQ_result_spec_variable_size(&dat, &size), 570 { GNUNET_SQ_result_spec_variable_size (&dat, &size),
568 GNUNET_SQ_result_spec_absolute_time(&exp), 571 GNUNET_SQ_result_spec_absolute_time (&exp),
569 GNUNET_SQ_result_spec_variable_size((void **)&path, &psize), 572 GNUNET_SQ_result_spec_variable_size ((void **) &path, &psize),
570 GNUNET_SQ_result_spec_uint32(&type), 573 GNUNET_SQ_result_spec_uint32 (&type),
571 GNUNET_SQ_result_spec_auto_from_type(&hc), 574 GNUNET_SQ_result_spec_auto_from_type (&hc),
572 GNUNET_SQ_result_spec_end }; 575 GNUNET_SQ_result_spec_end };
573 576
574 now = GNUNET_TIME_absolute_get(); 577 now = GNUNET_TIME_absolute_get ();
575 LOG(GNUNET_ERROR_TYPE_DEBUG, 578 LOG (GNUNET_ERROR_TYPE_DEBUG,
576 "Processing GET_CLOSEST for key `%s'\n", 579 "Processing GET_CLOSEST for key `%s'\n",
577 GNUNET_h2s(key)); 580 GNUNET_h2s (key));
578 if (GNUNET_OK != GNUNET_SQ_bind(plugin->get_closest_stmt, params)) 581 if (GNUNET_OK != GNUNET_SQ_bind (plugin->get_closest_stmt, params))
582 {
583 LOG_SQLITE (plugin->dbh,
584 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
585 "sqlite3_bind_xxx");
586 GNUNET_SQ_reset (plugin->dbh, plugin->get_closest_stmt);
587 return 0;
588 }
589 cnt = 0;
590 while (SQLITE_ROW == sqlite3_step (plugin->get_closest_stmt))
591 {
592 if (GNUNET_OK != GNUNET_SQ_extract_result (plugin->get_closest_stmt, rs))
579 { 593 {
580 LOG_SQLITE(plugin->dbh, 594 GNUNET_break (0);
581 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 595 break;
582 "sqlite3_bind_xxx");
583 GNUNET_SQ_reset(plugin->dbh, plugin->get_closest_stmt);
584 return 0;
585 } 596 }
586 cnt = 0; 597 if (0 != psize % sizeof(struct GNUNET_PeerIdentity))
587 while (SQLITE_ROW == sqlite3_step(plugin->get_closest_stmt))
588 { 598 {
589 if (GNUNET_OK != GNUNET_SQ_extract_result(plugin->get_closest_stmt, rs)) 599 GNUNET_break (0);
590 { 600 psize = 0;
591 GNUNET_break(0); 601 path = NULL;
592 break; 602 }
593 } 603 psize /= sizeof(struct GNUNET_PeerIdentity);
594 if (0 != psize % sizeof(struct GNUNET_PeerIdentity)) 604 cnt++;
595 { 605 LOG (GNUNET_ERROR_TYPE_DEBUG,
596 GNUNET_break(0); 606 "Found %u-byte result at %s when processing GET_CLOSE\n",
597 psize = 0; 607 (unsigned int) size,
598 path = NULL; 608 GNUNET_h2s (&hc));
599 } 609 if (GNUNET_OK != iter (iter_cls, &hc, size, dat, type, exp, psize, path))
600 psize /= sizeof(struct GNUNET_PeerIdentity); 610 {
601 cnt++; 611 GNUNET_SQ_cleanup_result (rs);
602 LOG(GNUNET_ERROR_TYPE_DEBUG, 612 break;
603 "Found %u-byte result at %s when processing GET_CLOSE\n",
604 (unsigned int)size,
605 GNUNET_h2s(&hc));
606 if (GNUNET_OK != iter(iter_cls, &hc, size, dat, type, exp, psize, path))
607 {
608 GNUNET_SQ_cleanup_result(rs);
609 break;
610 }
611 GNUNET_SQ_cleanup_result(rs);
612 } 613 }
613 GNUNET_SQ_reset(plugin->dbh, plugin->get_closest_stmt); 614 GNUNET_SQ_cleanup_result (rs);
615 }
616 GNUNET_SQ_reset (plugin->dbh, plugin->get_closest_stmt);
614 return cnt; 617 return cnt;
615} 618}
616 619
@@ -622,7 +625,7 @@ sqlite_plugin_get_closest(void *cls,
622 * @return the plugin's closure (our `struct Plugin`) 625 * @return the plugin's closure (our `struct Plugin`)
623 */ 626 */
624void * 627void *
625libgnunet_plugin_datacache_sqlite_init(void *cls) 628libgnunet_plugin_datacache_sqlite_init (void *cls)
626{ 629{
627 struct GNUNET_DATACACHE_PluginEnvironment *env = cls; 630 struct GNUNET_DATACACHE_PluginEnvironment *env = cls;
628 struct GNUNET_DATACACHE_PluginFunctions *api; 631 struct GNUNET_DATACACHE_PluginFunctions *api;
@@ -632,111 +635,111 @@ libgnunet_plugin_datacache_sqlite_init(void *cls)
632 sqlite3 *dbh; 635 sqlite3 *dbh;
633 char *emsg; 636 char *emsg;
634 637
635 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(env->cfg, 638 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
636 "datacache-sqlite", 639 "datacache-sqlite",
637 "IN_MEMORY")) 640 "IN_MEMORY"))
641 {
642 if (SQLITE_OK != sqlite3_open (":memory:", &dbh))
643 return NULL;
644 fn_utf8 = NULL;
645 }
646 else
647 {
648 fn = GNUNET_DISK_mktemp ("gnunet-datacache");
649 if (fn == NULL)
638 { 650 {
639 if (SQLITE_OK != sqlite3_open(":memory:", &dbh)) 651 GNUNET_break (0);
640 return NULL; 652 return NULL;
641 fn_utf8 = NULL;
642 } 653 }
643 else 654 /* fn should be UTF-8-encoded. If it isn't, it's a bug. */
655 fn_utf8 = GNUNET_strdup (fn);
656 if (SQLITE_OK != sqlite3_open (fn_utf8, &dbh))
644 { 657 {
645 fn = GNUNET_DISK_mktemp("gnunet-datacache"); 658 GNUNET_free (fn);
646 if (fn == NULL) 659 GNUNET_free (fn_utf8);
647 { 660 return NULL;
648 GNUNET_break(0);
649 return NULL;
650 }
651 /* fn should be UTF-8-encoded. If it isn't, it's a bug. */
652 fn_utf8 = GNUNET_strdup(fn);
653 if (SQLITE_OK != sqlite3_open(fn_utf8, &dbh))
654 {
655 GNUNET_free(fn);
656 GNUNET_free(fn_utf8);
657 return NULL;
658 }
659 GNUNET_free(fn);
660 } 661 }
661 662 GNUNET_free (fn);
662 SQLITE3_EXEC(dbh, "PRAGMA temp_store=MEMORY"); 663 }
663 SQLITE3_EXEC(dbh, "PRAGMA locking_mode=EXCLUSIVE"); 664
664 SQLITE3_EXEC(dbh, "PRAGMA journal_mode=OFF"); 665 SQLITE3_EXEC (dbh, "PRAGMA temp_store=MEMORY");
665 SQLITE3_EXEC(dbh, "PRAGMA synchronous=OFF"); 666 SQLITE3_EXEC (dbh, "PRAGMA locking_mode=EXCLUSIVE");
666 SQLITE3_EXEC(dbh, "PRAGMA page_size=4092"); 667 SQLITE3_EXEC (dbh, "PRAGMA journal_mode=OFF");
667 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(env->cfg, 668 SQLITE3_EXEC (dbh, "PRAGMA synchronous=OFF");
668 "datacache-sqlite", 669 SQLITE3_EXEC (dbh, "PRAGMA page_size=4092");
669 "IN_MEMORY")) 670 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
670 SQLITE3_EXEC(dbh, "PRAGMA sqlite_temp_store=3"); 671 "datacache-sqlite",
671 672 "IN_MEMORY"))
672 SQLITE3_EXEC(dbh, 673 SQLITE3_EXEC (dbh, "PRAGMA sqlite_temp_store=3");
673 "CREATE TABLE ds091 (" 674
674 " type INTEGER NOT NULL DEFAULT 0," 675 SQLITE3_EXEC (dbh,
675 " expire INTEGER NOT NULL," 676 "CREATE TABLE ds091 ("
676 " key BLOB NOT NULL DEFAULT ''," 677 " type INTEGER NOT NULL DEFAULT 0,"
677 " prox INTEGER NOT NULL," 678 " expire INTEGER NOT NULL,"
678 " value BLOB NOT NULL," 679 " key BLOB NOT NULL DEFAULT '',"
679 " path BLOB DEFAULT '')"); 680 " prox INTEGER NOT NULL,"
680 SQLITE3_EXEC(dbh, "CREATE INDEX idx_hashidx ON ds091 (key,type,expire)"); 681 " value BLOB NOT NULL,"
681 SQLITE3_EXEC(dbh, "CREATE INDEX idx_prox_expire ON ds091 (prox,expire)"); 682 " path BLOB DEFAULT '')");
682 SQLITE3_EXEC(dbh, "CREATE INDEX idx_expire_only ON ds091 (expire)"); 683 SQLITE3_EXEC (dbh, "CREATE INDEX idx_hashidx ON ds091 (key,type,expire)");
683 plugin = GNUNET_new(struct Plugin); 684 SQLITE3_EXEC (dbh, "CREATE INDEX idx_prox_expire ON ds091 (prox,expire)");
685 SQLITE3_EXEC (dbh, "CREATE INDEX idx_expire_only ON ds091 (expire)");
686 plugin = GNUNET_new (struct Plugin);
684 plugin->env = env; 687 plugin->env = env;
685 plugin->dbh = dbh; 688 plugin->dbh = dbh;
686 plugin->fn = fn_utf8; 689 plugin->fn = fn_utf8;
687 690
688 if ((SQLITE_OK != 691 if ((SQLITE_OK !=
689 sq_prepare(plugin->dbh, 692 sq_prepare (plugin->dbh,
690 "INSERT INTO ds091 (type, expire, key, prox, value, path) " 693 "INSERT INTO ds091 (type, expire, key, prox, value, path) "
691 "VALUES (?, ?, ?, ?, ?, ?)", 694 "VALUES (?, ?, ?, ?, ?, ?)",
692 &plugin->insert_stmt)) || 695 &plugin->insert_stmt)) ||
693 (SQLITE_OK != sq_prepare(plugin->dbh, 696 (SQLITE_OK != sq_prepare (plugin->dbh,
694 "SELECT count(*) FROM ds091 " 697 "SELECT count(*) FROM ds091 "
695 "WHERE key=? AND type=? AND expire >= ?", 698 "WHERE key=? AND type=? AND expire >= ?",
696 &plugin->get_count_stmt)) || 699 &plugin->get_count_stmt)) ||
697 (SQLITE_OK != 700 (SQLITE_OK !=
698 sq_prepare(plugin->dbh, 701 sq_prepare (plugin->dbh,
699 "SELECT value,expire,path FROM ds091" 702 "SELECT value,expire,path FROM ds091"
700 " WHERE key=? AND type=? AND expire >= ? LIMIT 1 OFFSET ?", 703 " WHERE key=? AND type=? AND expire >= ? LIMIT 1 OFFSET ?",
701 &plugin->get_stmt)) || 704 &plugin->get_stmt)) ||
702 (SQLITE_OK != sq_prepare(plugin->dbh, 705 (SQLITE_OK != sq_prepare (plugin->dbh,
703 "SELECT _ROWID_,key,value FROM ds091" 706 "SELECT _ROWID_,key,value FROM ds091"
704 " WHERE expire < ?" 707 " WHERE expire < ?"
705 " ORDER BY expire ASC LIMIT 1", 708 " ORDER BY expire ASC LIMIT 1",
706 &plugin->del_expired_stmt)) || 709 &plugin->del_expired_stmt)) ||
707 (SQLITE_OK != sq_prepare(plugin->dbh, 710 (SQLITE_OK != sq_prepare (plugin->dbh,
708 "SELECT _ROWID_,key,value FROM ds091" 711 "SELECT _ROWID_,key,value FROM ds091"
709 " ORDER BY prox ASC, expire ASC LIMIT 1", 712 " ORDER BY prox ASC, expire ASC LIMIT 1",
710 &plugin->del_select_stmt)) || 713 &plugin->del_select_stmt)) ||
711 (SQLITE_OK != sq_prepare(plugin->dbh, 714 (SQLITE_OK != sq_prepare (plugin->dbh,
712 "DELETE FROM ds091 WHERE _ROWID_=?", 715 "DELETE FROM ds091 WHERE _ROWID_=?",
713 &plugin->del_stmt)) || 716 &plugin->del_stmt)) ||
714 (SQLITE_OK != sq_prepare(plugin->dbh, 717 (SQLITE_OK != sq_prepare (plugin->dbh,
715 "SELECT value,expire,path,key,type FROM ds091 " 718 "SELECT value,expire,path,key,type FROM ds091 "
716 "ORDER BY key LIMIT 1 OFFSET ?", 719 "ORDER BY key LIMIT 1 OFFSET ?",
717 &plugin->get_random_stmt)) || 720 &plugin->get_random_stmt)) ||
718 (SQLITE_OK != 721 (SQLITE_OK !=
719 sq_prepare(plugin->dbh, 722 sq_prepare (plugin->dbh,
720 "SELECT value,expire,path,type,key FROM ds091 " 723 "SELECT value,expire,path,type,key FROM ds091 "
721 "WHERE key>=? AND expire >= ? ORDER BY KEY ASC LIMIT ?", 724 "WHERE key>=? AND expire >= ? ORDER BY KEY ASC LIMIT ?",
722 &plugin->get_closest_stmt))) 725 &plugin->get_closest_stmt)))
723 { 726 {
724 LOG_SQLITE(plugin->dbh, 727 LOG_SQLITE (plugin->dbh,
725 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 728 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
726 "sq_prepare"); 729 "sq_prepare");
727 GNUNET_break(SQLITE_OK == sqlite3_close(plugin->dbh)); 730 GNUNET_break (SQLITE_OK == sqlite3_close (plugin->dbh));
728 GNUNET_free(plugin); 731 GNUNET_free (plugin);
729 return NULL; 732 return NULL;
730 } 733 }
731 734
732 api = GNUNET_new(struct GNUNET_DATACACHE_PluginFunctions); 735 api = GNUNET_new (struct GNUNET_DATACACHE_PluginFunctions);
733 api->cls = plugin; 736 api->cls = plugin;
734 api->get = &sqlite_plugin_get; 737 api->get = &sqlite_plugin_get;
735 api->put = &sqlite_plugin_put; 738 api->put = &sqlite_plugin_put;
736 api->del = &sqlite_plugin_del; 739 api->del = &sqlite_plugin_del;
737 api->get_random = &sqlite_plugin_get_random; 740 api->get_random = &sqlite_plugin_get_random;
738 api->get_closest = &sqlite_plugin_get_closest; 741 api->get_closest = &sqlite_plugin_get_closest;
739 LOG(GNUNET_ERROR_TYPE_INFO, "Sqlite datacache running\n"); 742 LOG (GNUNET_ERROR_TYPE_INFO, "Sqlite datacache running\n");
740 return api; 743 return api;
741} 744}
742 745
@@ -748,7 +751,7 @@ libgnunet_plugin_datacache_sqlite_init(void *cls)
748 * @return NULL 751 * @return NULL
749 */ 752 */
750void * 753void *
751libgnunet_plugin_datacache_sqlite_done(void *cls) 754libgnunet_plugin_datacache_sqlite_done (void *cls)
752{ 755{
753 struct GNUNET_DATACACHE_PluginFunctions *api = cls; 756 struct GNUNET_DATACACHE_PluginFunctions *api = cls;
754 struct Plugin *plugin = api->cls; 757 struct Plugin *plugin = api->cls;
@@ -758,45 +761,45 @@ libgnunet_plugin_datacache_sqlite_done(void *cls)
758 sqlite3_stmt *stmt; 761 sqlite3_stmt *stmt;
759#endif 762#endif
760 763
761#if !WINDOWS || defined(__CYGWIN__) 764#if ! WINDOWS || defined(__CYGWIN__)
762 if ((NULL != plugin->fn) && (0 != unlink(plugin->fn))) 765 if ((NULL != plugin->fn) && (0 != unlink (plugin->fn)))
763 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "unlink", plugin->fn); 766 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", plugin->fn);
764 GNUNET_free_non_null(plugin->fn); 767 GNUNET_free_non_null (plugin->fn);
765#endif 768#endif
766 sqlite3_finalize(plugin->insert_stmt); 769 sqlite3_finalize (plugin->insert_stmt);
767 sqlite3_finalize(plugin->get_count_stmt); 770 sqlite3_finalize (plugin->get_count_stmt);
768 sqlite3_finalize(plugin->get_stmt); 771 sqlite3_finalize (plugin->get_stmt);
769 sqlite3_finalize(plugin->del_select_stmt); 772 sqlite3_finalize (plugin->del_select_stmt);
770 sqlite3_finalize(plugin->del_expired_stmt); 773 sqlite3_finalize (plugin->del_expired_stmt);
771 sqlite3_finalize(plugin->del_stmt); 774 sqlite3_finalize (plugin->del_stmt);
772 sqlite3_finalize(plugin->get_random_stmt); 775 sqlite3_finalize (plugin->get_random_stmt);
773 sqlite3_finalize(plugin->get_closest_stmt); 776 sqlite3_finalize (plugin->get_closest_stmt);
774 result = sqlite3_close(plugin->dbh); 777 result = sqlite3_close (plugin->dbh);
775#if SQLITE_VERSION_NUMBER >= 3007000 778#if SQLITE_VERSION_NUMBER >= 3007000
776 if (SQLITE_BUSY == result) 779 if (SQLITE_BUSY == result)
780 {
781 LOG (GNUNET_ERROR_TYPE_WARNING,
782 _ (
783 "Tried to close sqlite without finalizing all prepared statements.\n"));
784 stmt = sqlite3_next_stmt (plugin->dbh, NULL);
785 while (NULL != stmt)
777 { 786 {
778 LOG(GNUNET_ERROR_TYPE_WARNING, 787 result = sqlite3_finalize (stmt);
779 _( 788 if (result != SQLITE_OK)
780 "Tried to close sqlite without finalizing all prepared statements.\n")); 789 LOG (GNUNET_ERROR_TYPE_WARNING,
781 stmt = sqlite3_next_stmt(plugin->dbh, NULL); 790 "Failed to close statement %p: %d\n",
782 while (NULL != stmt) 791 stmt,
783 { 792 result);
784 result = sqlite3_finalize(stmt); 793 stmt = sqlite3_next_stmt (plugin->dbh, NULL);
785 if (result != SQLITE_OK)
786 LOG(GNUNET_ERROR_TYPE_WARNING,
787 "Failed to close statement %p: %d\n",
788 stmt,
789 result);
790 stmt = sqlite3_next_stmt(plugin->dbh, NULL);
791 }
792 result = sqlite3_close(plugin->dbh);
793 } 794 }
795 result = sqlite3_close (plugin->dbh);
796 }
794#endif 797#endif
795 if (SQLITE_OK != result) 798 if (SQLITE_OK != result)
796 LOG_SQLITE(plugin->dbh, GNUNET_ERROR_TYPE_ERROR, "sqlite3_close"); 799 LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR, "sqlite3_close");
797 800
798 GNUNET_free(plugin); 801 GNUNET_free (plugin);
799 GNUNET_free(api); 802 GNUNET_free (api);
800 return NULL; 803 return NULL;
801} 804}
802 805
diff --git a/src/datacache/plugin_datacache_template.c b/src/datacache/plugin_datacache_template.c
index 4c322d3ab..edb3b6757 100644
--- a/src/datacache/plugin_datacache_template.c
+++ b/src/datacache/plugin_datacache_template.c
@@ -31,7 +31,8 @@
31/** 31/**
32 * Context for all functions in this plugin. 32 * Context for all functions in this plugin.
33 */ 33 */
34struct Plugin { 34struct Plugin
35{
35 /** 36 /**
36 * Our execution environment. 37 * Our execution environment.
37 */ 38 */
@@ -54,17 +55,17 @@ struct Plugin {
54 * @return 0 if duplicate, -1 on error, number of bytes used otherwise 55 * @return 0 if duplicate, -1 on error, number of bytes used otherwise
55 */ 56 */
56static ssize_t 57static ssize_t
57template_plugin_put(void *cls, 58template_plugin_put (void *cls,
58 const struct GNUNET_HashCode *key, 59 const struct GNUNET_HashCode *key,
59 uint32_t xor_distance, 60 uint32_t xor_distance,
60 size_t size, 61 size_t size,
61 const char *data, 62 const char *data,
62 enum GNUNET_BLOCK_Type type, 63 enum GNUNET_BLOCK_Type type,
63 struct GNUNET_TIME_Absolute discard_time, 64 struct GNUNET_TIME_Absolute discard_time,
64 unsigned int path_info_len, 65 unsigned int path_info_len,
65 const struct GNUNET_PeerIdentity *path_info) 66 const struct GNUNET_PeerIdentity *path_info)
66{ 67{
67 GNUNET_break(0); 68 GNUNET_break (0);
68 return -1; 69 return -1;
69} 70}
70 71
@@ -81,13 +82,13 @@ template_plugin_put(void *cls,
81 * @return the number of results found 82 * @return the number of results found
82 */ 83 */
83static unsigned int 84static unsigned int
84template_plugin_get(void *cls, 85template_plugin_get (void *cls,
85 const struct GNUNET_HashCode *key, 86 const struct GNUNET_HashCode *key,
86 enum GNUNET_BLOCK_Type type, 87 enum GNUNET_BLOCK_Type type,
87 GNUNET_DATACACHE_Iterator iter, 88 GNUNET_DATACACHE_Iterator iter,
88 void *iter_cls) 89 void *iter_cls)
89{ 90{
90 GNUNET_break(0); 91 GNUNET_break (0);
91 return 0; 92 return 0;
92} 93}
93 94
@@ -100,9 +101,9 @@ template_plugin_get(void *cls,
100 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 101 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
101 */ 102 */
102static int 103static int
103template_plugin_del(void *cls) 104template_plugin_del (void *cls)
104{ 105{
105 GNUNET_break(0); 106 GNUNET_break (0);
106 return GNUNET_SYSERR; 107 return GNUNET_SYSERR;
107} 108}
108 109
@@ -116,11 +117,11 @@ template_plugin_del(void *cls)
116 * @return the number of results found (zero or one) 117 * @return the number of results found (zero or one)
117 */ 118 */
118static unsigned int 119static unsigned int
119template_plugin_get_random(void *cls, 120template_plugin_get_random (void *cls,
120 GNUNET_DATACACHE_Iterator iter, 121 GNUNET_DATACACHE_Iterator iter,
121 void *iter_cls) 122 void *iter_cls)
122{ 123{
123 GNUNET_break(0); 124 GNUNET_break (0);
124 return 0; 125 return 0;
125} 126}
126 127
@@ -140,13 +141,13 @@ template_plugin_get_random(void *cls,
140 * @return the number of results found 141 * @return the number of results found
141 */ 142 */
142static unsigned int 143static unsigned int
143template_plugin_get_closest(void *cls, 144template_plugin_get_closest (void *cls,
144 const struct GNUNET_HashCode *key, 145 const struct GNUNET_HashCode *key,
145 unsigned int num_results, 146 unsigned int num_results,
146 GNUNET_DATACACHE_Iterator iter, 147 GNUNET_DATACACHE_Iterator iter,
147 void *iter_cls) 148 void *iter_cls)
148{ 149{
149 GNUNET_break(0); 150 GNUNET_break (0);
150 return 0; 151 return 0;
151} 152}
152 153
@@ -158,24 +159,24 @@ template_plugin_get_closest(void *cls,
158 * @return the plugin's closure (our `struct Plugin`) 159 * @return the plugin's closure (our `struct Plugin`)
159 */ 160 */
160void * 161void *
161libgnunet_plugin_datacache_template_init(void *cls) 162libgnunet_plugin_datacache_template_init (void *cls)
162{ 163{
163 struct GNUNET_DATACACHE_PluginEnvironment *env = cls; 164 struct GNUNET_DATACACHE_PluginEnvironment *env = cls;
164 struct GNUNET_DATACACHE_PluginFunctions *api; 165 struct GNUNET_DATACACHE_PluginFunctions *api;
165 struct Plugin *plugin; 166 struct Plugin *plugin;
166 167
167 plugin = GNUNET_new(struct Plugin); 168 plugin = GNUNET_new (struct Plugin);
168 plugin->env = env; 169 plugin->env = env;
169 api = GNUNET_new(struct GNUNET_DATACACHE_PluginFunctions); 170 api = GNUNET_new (struct GNUNET_DATACACHE_PluginFunctions);
170 api->cls = plugin; 171 api->cls = plugin;
171 api->get = &template_plugin_get; 172 api->get = &template_plugin_get;
172 api->put = &template_plugin_put; 173 api->put = &template_plugin_put;
173 api->del = &template_plugin_del; 174 api->del = &template_plugin_del;
174 api->get_random = &template_plugin_get_random; 175 api->get_random = &template_plugin_get_random;
175 api->get_closest = &template_plugin_get_closest; 176 api->get_closest = &template_plugin_get_closest;
176 GNUNET_log_from(GNUNET_ERROR_TYPE_INFO, 177 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
177 "template", 178 "template",
178 "Template datacache running\n"); 179 "Template datacache running\n");
179 return api; 180 return api;
180} 181}
181 182
@@ -187,13 +188,13 @@ libgnunet_plugin_datacache_template_init(void *cls)
187 * @return NULL 188 * @return NULL
188 */ 189 */
189void * 190void *
190libgnunet_plugin_datacache_template_done(void *cls) 191libgnunet_plugin_datacache_template_done (void *cls)
191{ 192{
192 struct GNUNET_DATACACHE_PluginFunctions *api = cls; 193 struct GNUNET_DATACACHE_PluginFunctions *api = cls;
193 struct Plugin *plugin = api->cls; 194 struct Plugin *plugin = api->cls;
194 195
195 GNUNET_free(plugin); 196 GNUNET_free (plugin);
196 GNUNET_free(api); 197 GNUNET_free (api);
197 return NULL; 198 return NULL;
198} 199}
199 200
diff --git a/src/datacache/test_datacache.c b/src/datacache/test_datacache.c
index 30ef81a70..58a03914b 100644
--- a/src/datacache/test_datacache.c
+++ b/src/datacache/test_datacache.c
@@ -27,7 +27,9 @@
27#include "gnunet_datacache_lib.h" 27#include "gnunet_datacache_lib.h"
28#include "gnunet_testing_lib.h" 28#include "gnunet_testing_lib.h"
29 29
30#define ASSERT(x) do { if (!(x)) { printf("Error at %s:%d\n", __FILE__, __LINE__); goto FAILURE; } } while (0) 30#define ASSERT(x) do { if (! (x)) { printf ("Error at %s:%d\n", __FILE__, \
31 __LINE__); goto FAILURE; \
32 } } while (0)
31 33
32static int ok; 34static int ok;
33 35
@@ -38,122 +40,122 @@ static const char *plugin_name;
38 40
39 41
40static int 42static int
41checkIt(void *cls, 43checkIt (void *cls,
42 const struct GNUNET_HashCode *key, 44 const struct GNUNET_HashCode *key,
43 size_t size, const char *data, 45 size_t size, const char *data,
44 enum GNUNET_BLOCK_Type type, 46 enum GNUNET_BLOCK_Type type,
45 struct GNUNET_TIME_Absolute exp, 47 struct GNUNET_TIME_Absolute exp,
46 unsigned int path_len, 48 unsigned int path_len,
47 const struct GNUNET_PeerIdentity *path) 49 const struct GNUNET_PeerIdentity *path)
48{ 50{
49 (void)key; 51 (void) key;
50 (void)type; 52 (void) type;
51 (void)exp; 53 (void) exp;
52 (void)path_len; 54 (void) path_len;
53 (void)path; 55 (void) path;
54 if (size != sizeof(struct GNUNET_HashCode)) 56 if (size != sizeof(struct GNUNET_HashCode))
55 { 57 {
56 GNUNET_break(0); 58 GNUNET_break (0);
57 ok = 2; 59 ok = 2;
58 } 60 }
59 if (0 != memcmp(data, cls, size)) 61 if (0 != memcmp (data, cls, size))
60 { 62 {
61 GNUNET_break(0); 63 GNUNET_break (0);
62 ok = 3; 64 ok = 3;
63 } 65 }
64 return GNUNET_OK; 66 return GNUNET_OK;
65} 67}
66 68
67 69
68static void 70static void
69run(void *cls, 71run (void *cls,
70 char *const *args, 72 char *const *args,
71 const char *cfgfile, 73 const char *cfgfile,
72 const struct GNUNET_CONFIGURATION_Handle *cfg) 74 const struct GNUNET_CONFIGURATION_Handle *cfg)
73{ 75{
74 struct GNUNET_DATACACHE_Handle *h; 76 struct GNUNET_DATACACHE_Handle *h;
75 struct GNUNET_HashCode k; 77 struct GNUNET_HashCode k;
76 struct GNUNET_HashCode n; 78 struct GNUNET_HashCode n;
77 struct GNUNET_TIME_Absolute exp; 79 struct GNUNET_TIME_Absolute exp;
78 80
79 (void)cls; 81 (void) cls;
80 (void)args; 82 (void) args;
81 (void)cfgfile; 83 (void) cfgfile;
82 ok = 0; 84 ok = 0;
83 h = GNUNET_DATACACHE_create(cfg, 85 h = GNUNET_DATACACHE_create (cfg,
84 "testcache"); 86 "testcache");
85 if (h == NULL) 87 if (h == NULL)
86 { 88 {
87 fprintf(stderr, 89 fprintf (stderr,
88 "%s", 90 "%s",
89 "Failed to initialize datacache. Database likely not setup, skipping test.\n"); 91 "Failed to initialize datacache. Database likely not setup, skipping test.\n");
90 ok = 77; /* mark test as skipped */ 92 ok = 77; /* mark test as skipped */
91 return; 93 return;
92 } 94 }
93 exp = GNUNET_TIME_absolute_get(); 95 exp = GNUNET_TIME_absolute_get ();
94 exp.abs_value_us += 5 * 60 * 1000 * 1000LL; 96 exp.abs_value_us += 5 * 60 * 1000 * 1000LL;
95 memset(&k, 0, sizeof(struct GNUNET_HashCode)); 97 memset (&k, 0, sizeof(struct GNUNET_HashCode));
96 for (unsigned int i = 0; i < 100; i++) 98 for (unsigned int i = 0; i < 100; i++)
97 { 99 {
98 GNUNET_CRYPTO_hash(&k, sizeof(struct GNUNET_HashCode), &n); 100 GNUNET_CRYPTO_hash (&k, sizeof(struct GNUNET_HashCode), &n);
99 ASSERT(GNUNET_OK == 101 ASSERT (GNUNET_OK ==
100 GNUNET_DATACACHE_put(h, 102 GNUNET_DATACACHE_put (h,
101 &k, 103 &k,
102 GNUNET_YES, 104 GNUNET_YES,
103 sizeof(struct GNUNET_HashCode), 105 sizeof(struct GNUNET_HashCode),
104 (const char *)&n, 1 + i % 16, exp, 106 (const char *) &n, 1 + i % 16, exp,
105 0, NULL)); 107 0, NULL));
106 k = n; 108 k = n;
107 } 109 }
108 memset(&k, 110 memset (&k,
109 0, 111 0,
110 sizeof(struct GNUNET_HashCode)); 112 sizeof(struct GNUNET_HashCode));
111 for (unsigned int i = 0; i < 100; i++) 113 for (unsigned int i = 0; i < 100; i++)
112 { 114 {
113 GNUNET_CRYPTO_hash(&k, 115 GNUNET_CRYPTO_hash (&k,
114 sizeof(struct GNUNET_HashCode), 116 sizeof(struct GNUNET_HashCode),
115 &n); 117 &n);
116 ASSERT(1 == GNUNET_DATACACHE_get(h, 118 ASSERT (1 == GNUNET_DATACACHE_get (h,
117 &k, 119 &k,
118 1 + i % 16, 120 1 + i % 16,
119 &checkIt, 121 &checkIt,
120 &n)); 122 &n));
121 k = n; 123 k = n;
122 } 124 }
123 125
124 memset(&k, 126 memset (&k,
125 42, 127 42,
126 sizeof(struct GNUNET_HashCode)); 128 sizeof(struct GNUNET_HashCode));
127 GNUNET_CRYPTO_hash(&k, 129 GNUNET_CRYPTO_hash (&k,
128 sizeof(struct GNUNET_HashCode), 130 sizeof(struct GNUNET_HashCode),
129 &n); 131 &n);
130 ASSERT(GNUNET_OK == 132 ASSERT (GNUNET_OK ==
131 GNUNET_DATACACHE_put(h, 133 GNUNET_DATACACHE_put (h,
132 &k, 134 &k,
133 GNUNET_YES, 135 GNUNET_YES,
134 sizeof(struct GNUNET_HashCode), 136 sizeof(struct GNUNET_HashCode),
135 (const char *)&n, 137 (const char *) &n,
136 792, 138 792,
137 GNUNET_TIME_UNIT_FOREVER_ABS, 139 GNUNET_TIME_UNIT_FOREVER_ABS,
138 0, 140 0,
139 NULL)); 141 NULL));
140 ASSERT(0 != GNUNET_DATACACHE_get(h, 142 ASSERT (0 != GNUNET_DATACACHE_get (h,
141 &k, 143 &k,
142 792, 144 792,
143 &checkIt, 145 &checkIt,
144 &n)); 146 &n));
145 GNUNET_DATACACHE_destroy(h); 147 GNUNET_DATACACHE_destroy (h);
146 ASSERT(ok == 0); 148 ASSERT (ok == 0);
147 return; 149 return;
148FAILURE: 150FAILURE:
149 if (h != NULL) 151 if (h != NULL)
150 GNUNET_DATACACHE_destroy(h); 152 GNUNET_DATACACHE_destroy (h);
151 ok = GNUNET_SYSERR; 153 ok = GNUNET_SYSERR;
152} 154}
153 155
154 156
155int 157int
156main(int argc, char *argv[]) 158main (int argc, char *argv[])
157{ 159{
158 char cfg_name[PATH_MAX]; 160 char cfg_name[PATH_MAX];
159 char *const xargv[] = { 161 char *const xargv[] = {
@@ -166,26 +168,26 @@ main(int argc, char *argv[])
166 GNUNET_GETOPT_OPTION_END 168 GNUNET_GETOPT_OPTION_END
167 }; 169 };
168 170
169 (void)argc; 171 (void) argc;
170 GNUNET_log_setup("test-datacache", 172 GNUNET_log_setup ("test-datacache",
171 "WARNING", 173 "WARNING",
172 NULL); 174 NULL);
173 plugin_name = GNUNET_TESTING_get_testname_from_underscore(argv[0]); 175 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
174 GNUNET_snprintf(cfg_name, 176 GNUNET_snprintf (cfg_name,
175 sizeof(cfg_name), 177 sizeof(cfg_name),
176 "test_datacache_data_%s.conf", 178 "test_datacache_data_%s.conf",
177 plugin_name); 179 plugin_name);
178 GNUNET_PROGRAM_run((sizeof(xargv) / sizeof(char *)) - 1, 180 GNUNET_PROGRAM_run ((sizeof(xargv) / sizeof(char *)) - 1,
179 xargv, 181 xargv,
180 "test-datacache", 182 "test-datacache",
181 "nohelp", 183 "nohelp",
182 options, 184 options,
183 &run, 185 &run,
184 NULL); 186 NULL);
185 if ((0 != ok) && (77 != ok)) 187 if ((0 != ok) && (77 != ok))
186 fprintf(stderr, 188 fprintf (stderr,
187 "Missed some testcases: %d\n", 189 "Missed some testcases: %d\n",
188 ok); 190 ok);
189 return ok; 191 return ok;
190} 192}
191 193
diff --git a/src/datacache/test_datacache_quota.c b/src/datacache/test_datacache_quota.c
index 36e369abb..26a23d632 100644
--- a/src/datacache/test_datacache_quota.c
+++ b/src/datacache/test_datacache_quota.c
@@ -27,7 +27,9 @@
27#include "gnunet_datacache_lib.h" 27#include "gnunet_datacache_lib.h"
28#include "gnunet_testing_lib.h" 28#include "gnunet_testing_lib.h"
29 29
30#define ASSERT(x) do { if (!(x)) { printf("Error at %s:%d\n", __FILE__, __LINE__); goto FAILURE; } } while (0) 30#define ASSERT(x) do { if (! (x)) { printf ("Error at %s:%d\n", __FILE__, \
31 __LINE__); goto FAILURE; \
32 } } while (0)
31 33
32static int ok; 34static int ok;
33 35
@@ -43,10 +45,10 @@ static const char *plugin_name;
43 * some of the data from the last iteration is still there. 45 * some of the data from the last iteration is still there.
44 */ 46 */
45static void 47static void
46run(void *cls, 48run (void *cls,
47 char *const *args, 49 char *const *args,
48 const char *cfgfile, 50 const char *cfgfile,
49 const struct GNUNET_CONFIGURATION_Handle *cfg) 51 const struct GNUNET_CONFIGURATION_Handle *cfg)
50{ 52{
51 struct GNUNET_DATACACHE_Handle *h; 53 struct GNUNET_DATACACHE_Handle *h;
52 struct GNUNET_HashCode k; 54 struct GNUNET_HashCode k;
@@ -54,74 +56,74 @@ run(void *cls,
54 char buf[3200]; 56 char buf[3200];
55 struct GNUNET_TIME_Absolute exp; 57 struct GNUNET_TIME_Absolute exp;
56 58
57 (void)cls; 59 (void) cls;
58 (void)args; 60 (void) args;
59 (void)cfgfile; 61 (void) cfgfile;
60 ok = 0; 62 ok = 0;
61 h = GNUNET_DATACACHE_create(cfg, 63 h = GNUNET_DATACACHE_create (cfg,
62 "testcache"); 64 "testcache");
63 65
64 if (h == NULL) 66 if (h == NULL)
65 { 67 {
66 fprintf(stderr, 68 fprintf (stderr,
67 "%s", 69 "%s",
68 "Failed to initialize datacache. Database likely not setup, skipping test.\n"); 70 "Failed to initialize datacache. Database likely not setup, skipping test.\n");
69 return; 71 return;
70 } 72 }
71 exp = GNUNET_TIME_relative_to_absolute(GNUNET_TIME_UNIT_HOURS); 73 exp = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS);
72 memset(buf, 1, sizeof(buf)); 74 memset (buf, 1, sizeof(buf));
73 memset(&k, 0, sizeof(struct GNUNET_HashCode)); 75 memset (&k, 0, sizeof(struct GNUNET_HashCode));
74 for (unsigned int i = 0; i < 10; i++) 76 for (unsigned int i = 0; i < 10; i++)
77 {
78 fprintf (stderr,
79 "%s",
80 ".");
81 GNUNET_CRYPTO_hash (&k,
82 sizeof(struct GNUNET_HashCode),
83 &n);
84 for (unsigned int j = i; j < sizeof(buf); j += 10)
75 { 85 {
76 fprintf(stderr, 86 exp.abs_value_us++;
77 "%s", 87 buf[j] = i;
78 "."); 88 ASSERT (GNUNET_OK ==
79 GNUNET_CRYPTO_hash(&k, 89 GNUNET_DATACACHE_put (h,
80 sizeof(struct GNUNET_HashCode), 90 &k,
81 &n); 91 GNUNET_YES,
82 for (unsigned int j = i; j < sizeof(buf); j += 10) 92 j,
83 { 93 buf,
84 exp.abs_value_us++; 94 1 + i,
85 buf[j] = i; 95 exp,
86 ASSERT(GNUNET_OK == 96 0,
87 GNUNET_DATACACHE_put(h, 97 NULL));
88 &k, 98 ASSERT (0 < GNUNET_DATACACHE_get (h, &k, 1 + i, NULL, NULL));
89 GNUNET_YES,
90 j,
91 buf,
92 1 + i,
93 exp,
94 0,
95 NULL));
96 ASSERT(0 < GNUNET_DATACACHE_get(h, &k, 1 + i, NULL, NULL));
97 }
98 k = n;
99 } 99 }
100 fprintf(stderr, "%s", "\n"); 100 k = n;
101 memset(&k, 0, sizeof(struct GNUNET_HashCode)); 101 }
102 fprintf (stderr, "%s", "\n");
103 memset (&k, 0, sizeof(struct GNUNET_HashCode));
102 for (unsigned int i = 0; i < 10; i++) 104 for (unsigned int i = 0; i < 10; i++)
103 { 105 {
104 fprintf(stderr, "%s", "."); 106 fprintf (stderr, "%s", ".");
105 GNUNET_CRYPTO_hash(&k, sizeof(struct GNUNET_HashCode), &n); 107 GNUNET_CRYPTO_hash (&k, sizeof(struct GNUNET_HashCode), &n);
106 if (i < 2) 108 if (i < 2)
107 ASSERT(0 == GNUNET_DATACACHE_get(h, &k, 1 + i, NULL, NULL)); 109 ASSERT (0 == GNUNET_DATACACHE_get (h, &k, 1 + i, NULL, NULL));
108 if (i == 9) 110 if (i == 9)
109 ASSERT(0 < GNUNET_DATACACHE_get(h, &k, 1 + i, NULL, NULL)); 111 ASSERT (0 < GNUNET_DATACACHE_get (h, &k, 1 + i, NULL, NULL));
110 k = n; 112 k = n;
111 } 113 }
112 fprintf(stderr, "%s", "\n"); 114 fprintf (stderr, "%s", "\n");
113 GNUNET_DATACACHE_destroy(h); 115 GNUNET_DATACACHE_destroy (h);
114 return; 116 return;
115FAILURE: 117FAILURE:
116 if (h != NULL) 118 if (h != NULL)
117 GNUNET_DATACACHE_destroy(h); 119 GNUNET_DATACACHE_destroy (h);
118 ok = GNUNET_SYSERR; 120 ok = GNUNET_SYSERR;
119} 121}
120 122
121 123
122int 124int
123main(int argc, 125main (int argc,
124 char *argv[]) 126 char *argv[])
125{ 127{
126 char cfg_name[PATH_MAX]; 128 char cfg_name[PATH_MAX];
127 char *const xargv[] = { 129 char *const xargv[] = {
@@ -134,27 +136,27 @@ main(int argc,
134 GNUNET_GETOPT_OPTION_END 136 GNUNET_GETOPT_OPTION_END
135 }; 137 };
136 138
137 (void)argc; 139 (void) argc;
138 GNUNET_log_setup("test-datacache-quota", 140 GNUNET_log_setup ("test-datacache-quota",
139 "WARNING", 141 "WARNING",
140 NULL); 142 NULL);
141 143
142 plugin_name = GNUNET_TESTING_get_testname_from_underscore(argv[0]); 144 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
143 GNUNET_snprintf(cfg_name, 145 GNUNET_snprintf (cfg_name,
144 sizeof(cfg_name), 146 sizeof(cfg_name),
145 "test_datacache_data_%s.conf", 147 "test_datacache_data_%s.conf",
146 plugin_name); 148 plugin_name);
147 GNUNET_PROGRAM_run((sizeof(xargv) / sizeof(char *)) - 1, 149 GNUNET_PROGRAM_run ((sizeof(xargv) / sizeof(char *)) - 1,
148 xargv, 150 xargv,
149 "test-datacache-quota", 151 "test-datacache-quota",
150 "nohelp", 152 "nohelp",
151 options, 153 options,
152 &run, 154 &run,
153 NULL); 155 NULL);
154 if (0 != ok) 156 if (0 != ok)
155 fprintf(stderr, 157 fprintf (stderr,
156 "Missed some testcases: %d\n", 158 "Missed some testcases: %d\n",
157 ok); 159 ok);
158 return ok; 160 return ok;
159} 161}
160 162