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