aboutsummaryrefslogtreecommitdiff
path: root/src/datacache/plugin_datacache_sqlite.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/datacache/plugin_datacache_sqlite.c')
-rw-r--r--src/datacache/plugin_datacache_sqlite.c847
1 files changed, 425 insertions, 422 deletions
diff --git a/src/datacache/plugin_datacache_sqlite.c b/src/datacache/plugin_datacache_sqlite.c
index b173af2f3..55a8a7fae 100644
--- a/src/datacache/plugin_datacache_sqlite.c
+++ b/src/datacache/plugin_datacache_sqlite.c
@@ -29,10 +29,10 @@
29#include "gnunet_sq_lib.h" 29#include "gnunet_sq_lib.h"
30#include <sqlite3.h> 30#include <sqlite3.h>
31 31
32#define LOG(kind, ...) GNUNET_log_from(kind, "datacache-sqlite", __VA_ARGS__) 32#define LOG(kind, ...) GNUNET_log_from (kind, "datacache-sqlite", __VA_ARGS__)
33 33
34#define LOG_STRERROR_FILE(kind, op, fn) \ 34#define LOG_STRERROR_FILE(kind, op, fn) \
35 GNUNET_log_from_strerror_file(kind, "datacache-sqlite", op, fn) 35 GNUNET_log_from_strerror_file (kind, "datacache-sqlite", op, fn)
36 36
37 37
38/** 38/**
@@ -44,7 +44,8 @@
44/** 44/**
45 * Context for all functions in this plugin. 45 * Context for all functions in this plugin.
46 */ 46 */
47struct Plugin { 47struct Plugin
48{
48 /** 49 /**
49 * Our execution environment. 50 * Our execution environment.
50 */ 51 */
@@ -117,14 +118,14 @@ struct Plugin {
117 */ 118 */
118#define LOG_SQLITE(db, level, cmd) \ 119#define LOG_SQLITE(db, level, cmd) \
119 do \ 120 do \
120 { \ 121 { \
121 LOG(level, \ 122 LOG (level, \
122 _("`%s' failed at %s:%d with error: %s\n"), \ 123 _ ("`%s' failed at %s:%d with error: %s\n"), \
123 cmd, \ 124 cmd, \
124 __FILE__, \ 125 __FILE__, \
125 __LINE__, \ 126 __LINE__, \
126 sqlite3_errmsg(db)); \ 127 sqlite3_errmsg (db)); \
127 } while (0) 128 } while (0)
128 129
129 130
130/** 131/**
@@ -135,19 +136,19 @@ struct Plugin {
135 */ 136 */
136#define SQLITE3_EXEC(db, cmd) \ 137#define SQLITE3_EXEC(db, cmd) \
137 do \ 138 do \
138 { \ 139 { \
139 emsg = NULL; \ 140 emsg = NULL; \
140 if (SQLITE_OK != sqlite3_exec(db, cmd, NULL, NULL, &emsg)) \ 141 if (SQLITE_OK != sqlite3_exec (db, cmd, NULL, NULL, &emsg)) \
141 { \ 142 { \
142 LOG(GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, \ 143 LOG (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, \
143 _("`%s' failed at %s:%d with error: %s\n"), \ 144 _ ("`%s' failed at %s:%d with error: %s\n"), \
144 "sqlite3_exec", \ 145 "sqlite3_exec", \
145 __FILE__, \ 146 __FILE__, \
146 __LINE__, \ 147 __LINE__, \
147 emsg); \ 148 emsg); \
148 sqlite3_free(emsg); \ 149 sqlite3_free (emsg); \
149 } \ 150 } \
150 } while (0) 151 } while (0)
151 152
152 153
153/** 154/**
@@ -159,17 +160,17 @@ struct Plugin {
159 * @return 0 on success 160 * @return 0 on success
160 */ 161 */
161static int 162static int
162sq_prepare(sqlite3 *dbh, 163sq_prepare (sqlite3 *dbh,
163 const char *zSql, /* SQL statement, UTF-8 encoded */ 164 const char *zSql, /* SQL statement, UTF-8 encoded */
164 sqlite3_stmt **ppStmt) 165 sqlite3_stmt **ppStmt)
165{ /* OUT: Statement handle */ 166{ /* OUT: Statement handle */
166 char *dummy; 167 char *dummy;
167 168
168 return sqlite3_prepare(dbh, 169 return sqlite3_prepare (dbh,
169 zSql, 170 zSql,
170 strlen(zSql), 171 strlen (zSql),
171 ppStmt, 172 ppStmt,
172 (const char **)&dummy); 173 (const char **) &dummy);
173} 174}
174 175
175 176
@@ -188,54 +189,55 @@ sq_prepare(sqlite3 *dbh,
188 * @return 0 if duplicate, -1 on error, number of bytes used otherwise 189 * @return 0 if duplicate, -1 on error, number of bytes used otherwise
189 */ 190 */
190static ssize_t 191static ssize_t
191sqlite_plugin_put(void *cls, 192sqlite_plugin_put (void *cls,
192 const struct GNUNET_HashCode *key, 193 const struct GNUNET_HashCode *key,
193 uint32_t xor_distance, 194 uint32_t xor_distance,
194 size_t size, 195 size_t size,
195 const char *data, 196 const char *data,
196 enum GNUNET_BLOCK_Type type, 197 enum GNUNET_BLOCK_Type type,
197 struct GNUNET_TIME_Absolute discard_time, 198 struct GNUNET_TIME_Absolute discard_time,
198 unsigned int path_info_len, 199 unsigned int path_info_len,
199 const struct GNUNET_PeerIdentity *path_info) 200 const struct GNUNET_PeerIdentity *path_info)
200{ 201{
201 struct Plugin *plugin = cls; 202 struct Plugin *plugin = cls;
202 uint32_t type32 = type; 203 uint32_t type32 = type;
203 struct GNUNET_SQ_QueryParam params[] = 204 struct GNUNET_SQ_QueryParam params[] =
204 { GNUNET_SQ_query_param_uint32(&type32), 205 { GNUNET_SQ_query_param_uint32 (&type32),
205 GNUNET_SQ_query_param_absolute_time(&discard_time), 206 GNUNET_SQ_query_param_absolute_time (&discard_time),
206 GNUNET_SQ_query_param_auto_from_type(key), 207 GNUNET_SQ_query_param_auto_from_type (key),
207 GNUNET_SQ_query_param_uint32(&xor_distance), 208 GNUNET_SQ_query_param_uint32 (&xor_distance),
208 GNUNET_SQ_query_param_fixed_size(data, size), 209 GNUNET_SQ_query_param_fixed_size (data, size),
209 GNUNET_SQ_query_param_fixed_size(path_info, 210 GNUNET_SQ_query_param_fixed_size (path_info,
210 path_info_len * 211 path_info_len
211 sizeof(struct GNUNET_PeerIdentity)), 212 * sizeof(struct GNUNET_PeerIdentity)),
212 GNUNET_SQ_query_param_end }; 213 GNUNET_SQ_query_param_end };
213 214
214 LOG(GNUNET_ERROR_TYPE_DEBUG, 215 LOG (GNUNET_ERROR_TYPE_DEBUG,
215 "Processing PUT of %u bytes with key `%s' and expiration %s\n", 216 "Processing PUT of %u bytes with key `%s' and expiration %s\n",
216 (unsigned int)size, 217 (unsigned int) size,
217 GNUNET_h2s(key), 218 GNUNET_h2s (key),
218 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_remaining( 219 GNUNET_STRINGS_relative_time_to_string (
219 discard_time), 220 GNUNET_TIME_absolute_get_remaining (
220 GNUNET_YES)); 221 discard_time),
221 if (GNUNET_OK != GNUNET_SQ_bind(plugin->insert_stmt, params)) 222 GNUNET_YES));
222 { 223 if (GNUNET_OK != GNUNET_SQ_bind (plugin->insert_stmt, params))
223 LOG_SQLITE(plugin->dbh, 224 {
224 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 225 LOG_SQLITE (plugin->dbh,
225 "sqlite3_bind_xxx"); 226 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
226 GNUNET_SQ_reset(plugin->dbh, plugin->insert_stmt); 227 "sqlite3_bind_xxx");
227 return -1; 228 GNUNET_SQ_reset (plugin->dbh, plugin->insert_stmt);
228 } 229 return -1;
229 if (SQLITE_DONE != sqlite3_step(plugin->insert_stmt)) 230 }
230 { 231 if (SQLITE_DONE != sqlite3_step (plugin->insert_stmt))
231 LOG_SQLITE(plugin->dbh, 232 {
232 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 233 LOG_SQLITE (plugin->dbh,
233 "sqlite3_step"); 234 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
234 GNUNET_SQ_reset(plugin->dbh, plugin->insert_stmt); 235 "sqlite3_step");
235 return -1; 236 GNUNET_SQ_reset (plugin->dbh, plugin->insert_stmt);
236 } 237 return -1;
238 }
237 plugin->num_items++; 239 plugin->num_items++;
238 GNUNET_SQ_reset(plugin->dbh, plugin->insert_stmt); 240 GNUNET_SQ_reset (plugin->dbh, plugin->insert_stmt);
239 return size + OVERHEAD; 241 return size + OVERHEAD;
240} 242}
241 243
@@ -252,11 +254,11 @@ sqlite_plugin_put(void *cls,
252 * @return the number of results found 254 * @return the number of results found
253 */ 255 */
254static unsigned int 256static unsigned int
255sqlite_plugin_get(void *cls, 257sqlite_plugin_get (void *cls,
256 const struct GNUNET_HashCode *key, 258 const struct GNUNET_HashCode *key,
257 enum GNUNET_BLOCK_Type type, 259 enum GNUNET_BLOCK_Type type,
258 GNUNET_DATACACHE_Iterator iter, 260 GNUNET_DATACACHE_Iterator iter,
259 void *iter_cls) 261 void *iter_cls)
260{ 262{
261 struct Plugin *plugin = cls; 263 struct Plugin *plugin = cls;
262 uint32_t type32 = type; 264 uint32_t type32 = type;
@@ -270,100 +272,100 @@ sqlite_plugin_get(void *cls,
270 size_t psize; 272 size_t psize;
271 struct GNUNET_PeerIdentity *path; 273 struct GNUNET_PeerIdentity *path;
272 struct GNUNET_SQ_QueryParam params_count[] = 274 struct GNUNET_SQ_QueryParam params_count[] =
273 { GNUNET_SQ_query_param_auto_from_type(key), 275 { GNUNET_SQ_query_param_auto_from_type (key),
274 GNUNET_SQ_query_param_uint32(&type32), 276 GNUNET_SQ_query_param_uint32 (&type32),
275 GNUNET_SQ_query_param_absolute_time(&now), 277 GNUNET_SQ_query_param_absolute_time (&now),
276 GNUNET_SQ_query_param_end }; 278 GNUNET_SQ_query_param_end };
277 struct GNUNET_SQ_QueryParam params_select[] = 279 struct GNUNET_SQ_QueryParam params_select[] =
278 { GNUNET_SQ_query_param_auto_from_type(key), 280 { GNUNET_SQ_query_param_auto_from_type (key),
279 GNUNET_SQ_query_param_uint32(&type32), 281 GNUNET_SQ_query_param_uint32 (&type32),
280 GNUNET_SQ_query_param_absolute_time(&now), 282 GNUNET_SQ_query_param_absolute_time (&now),
281 GNUNET_SQ_query_param_uint32(&off), 283 GNUNET_SQ_query_param_uint32 (&off),
282 GNUNET_SQ_query_param_end }; 284 GNUNET_SQ_query_param_end };
283 struct GNUNET_SQ_ResultSpec rs[] = 285 struct GNUNET_SQ_ResultSpec rs[] =
284 { GNUNET_SQ_result_spec_variable_size(&dat, &size), 286 { GNUNET_SQ_result_spec_variable_size (&dat, &size),
285 GNUNET_SQ_result_spec_absolute_time(&exp), 287 GNUNET_SQ_result_spec_absolute_time (&exp),
286 GNUNET_SQ_result_spec_variable_size((void **)&path, &psize), 288 GNUNET_SQ_result_spec_variable_size ((void **) &path, &psize),
287 GNUNET_SQ_result_spec_end }; 289 GNUNET_SQ_result_spec_end };
288 290
289 now = GNUNET_TIME_absolute_get(); 291 now = GNUNET_TIME_absolute_get ();
290 LOG(GNUNET_ERROR_TYPE_DEBUG, 292 LOG (GNUNET_ERROR_TYPE_DEBUG,
291 "Processing GET for key `%s'\n", 293 "Processing GET for key `%s'\n",
292 GNUNET_h2s(key)); 294 GNUNET_h2s (key));
295
296 if (GNUNET_OK != GNUNET_SQ_bind (plugin->get_count_stmt, params_count))
297 {
298 LOG_SQLITE (plugin->dbh,
299 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
300 "sqlite3_bind_xxx");
301 GNUNET_SQ_reset (plugin->dbh, plugin->get_count_stmt);
302 return 0;
303 }
304 if (SQLITE_ROW != sqlite3_step (plugin->get_count_stmt))
305 {
306 LOG_SQLITE (plugin->dbh,
307 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
308 "sqlite_step");
309 GNUNET_SQ_reset (plugin->dbh, plugin->get_count_stmt);
310 LOG (GNUNET_ERROR_TYPE_DEBUG,
311 "No content found when processing GET for key `%s'\n",
312 GNUNET_h2s (key));
313 return 0;
314 }
315 total = sqlite3_column_int (plugin->get_count_stmt, 0);
316 GNUNET_SQ_reset (plugin->dbh, plugin->get_count_stmt);
317 if ((0 == total) || (NULL == iter))
318 {
319 if (0 == total)
320 LOG (GNUNET_ERROR_TYPE_DEBUG,
321 "No content found when processing GET for key `%s'\n",
322 GNUNET_h2s (key));
323 return total;
324 }
293 325
294 if (GNUNET_OK != GNUNET_SQ_bind(plugin->get_count_stmt, params_count)) 326 cnt = 0;
327 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, total);
328 while (cnt < total)
329 {
330 off = (off + 1) % total;
331 if (GNUNET_OK != GNUNET_SQ_bind (plugin->get_stmt, params_select))
295 { 332 {
296 LOG_SQLITE(plugin->dbh, 333 LOG_SQLITE (plugin->dbh,
297 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 334 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
298 "sqlite3_bind_xxx"); 335 "sqlite3_bind_xxx");
299 GNUNET_SQ_reset(plugin->dbh, plugin->get_count_stmt); 336 GNUNET_SQ_reset (plugin->dbh, plugin->get_stmt);
300 return 0; 337 return cnt;
301 } 338 }
302 if (SQLITE_ROW != sqlite3_step(plugin->get_count_stmt)) 339 if (SQLITE_ROW != sqlite3_step (plugin->get_stmt))
340 break;
341 if (GNUNET_OK != GNUNET_SQ_extract_result (plugin->get_stmt, rs))
303 { 342 {
304 LOG_SQLITE(plugin->dbh, 343 GNUNET_break (0);
305 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 344 GNUNET_SQ_reset (plugin->dbh, plugin->get_stmt);
306 "sqlite_step"); 345 break;
307 GNUNET_SQ_reset(plugin->dbh, plugin->get_count_stmt);
308 LOG(GNUNET_ERROR_TYPE_DEBUG,
309 "No content found when processing GET for key `%s'\n",
310 GNUNET_h2s(key));
311 return 0;
312 } 346 }
313 total = sqlite3_column_int(plugin->get_count_stmt, 0); 347 if (0 != psize % sizeof(struct GNUNET_PeerIdentity))
314 GNUNET_SQ_reset(plugin->dbh, plugin->get_count_stmt);
315 if ((0 == total) || (NULL == iter))
316 { 348 {
317 if (0 == total) 349 GNUNET_break (0);
318 LOG(GNUNET_ERROR_TYPE_DEBUG, 350 psize = 0;
319 "No content found when processing GET for key `%s'\n", 351 path = NULL;
320 GNUNET_h2s(key));
321 return total;
322 } 352 }
323 353 psize /= sizeof(struct GNUNET_PeerIdentity);
324 cnt = 0; 354 cnt++;
325 off = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, total); 355 LOG (GNUNET_ERROR_TYPE_DEBUG,
326 while (cnt < total) 356 "Found %u-byte result when processing GET for key `%s'\n",
357 (unsigned int) size,
358 GNUNET_h2s (key));
359 if (GNUNET_OK != iter (iter_cls, key, size, dat, type, exp, psize, path))
327 { 360 {
328 off = (off + 1) % total; 361 GNUNET_SQ_cleanup_result (rs);
329 if (GNUNET_OK != GNUNET_SQ_bind(plugin->get_stmt, params_select)) 362 GNUNET_SQ_reset (plugin->dbh, plugin->get_stmt);
330 { 363 break;
331 LOG_SQLITE(plugin->dbh,
332 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
333 "sqlite3_bind_xxx");
334 GNUNET_SQ_reset(plugin->dbh, plugin->get_stmt);
335 return cnt;
336 }
337 if (SQLITE_ROW != sqlite3_step(plugin->get_stmt))
338 break;
339 if (GNUNET_OK != GNUNET_SQ_extract_result(plugin->get_stmt, rs))
340 {
341 GNUNET_break(0);
342 GNUNET_SQ_reset(plugin->dbh, plugin->get_stmt);
343 break;
344 }
345 if (0 != psize % sizeof(struct GNUNET_PeerIdentity))
346 {
347 GNUNET_break(0);
348 psize = 0;
349 path = NULL;
350 }
351 psize /= sizeof(struct GNUNET_PeerIdentity);
352 cnt++;
353 LOG(GNUNET_ERROR_TYPE_DEBUG,
354 "Found %u-byte result when processing GET for key `%s'\n",
355 (unsigned int)size,
356 GNUNET_h2s(key));
357 if (GNUNET_OK != iter(iter_cls, key, size, dat, type, exp, psize, path))
358 {
359 GNUNET_SQ_cleanup_result(rs);
360 GNUNET_SQ_reset(plugin->dbh, plugin->get_stmt);
361 break;
362 }
363 GNUNET_SQ_cleanup_result(rs);
364 GNUNET_SQ_reset(plugin->dbh, plugin->get_stmt);
365 } 364 }
366 GNUNET_SQ_reset(plugin->dbh, plugin->get_stmt); 365 GNUNET_SQ_cleanup_result (rs);
366 GNUNET_SQ_reset (plugin->dbh, plugin->get_stmt);
367 }
368 GNUNET_SQ_reset (plugin->dbh, plugin->get_stmt);
367 return cnt; 369 return cnt;
368} 370}
369 371
@@ -376,7 +378,7 @@ sqlite_plugin_get(void *cls,
376 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 378 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
377 */ 379 */
378static int 380static int
379sqlite_plugin_del(void *cls) 381sqlite_plugin_del (void *cls)
380{ 382{
381 struct Plugin *plugin = cls; 383 struct Plugin *plugin = cls;
382 uint64_t rowid; 384 uint64_t rowid;
@@ -385,65 +387,66 @@ sqlite_plugin_del(void *cls)
385 struct GNUNET_HashCode hc; 387 struct GNUNET_HashCode hc;
386 struct GNUNET_TIME_Absolute now; 388 struct GNUNET_TIME_Absolute now;
387 struct GNUNET_SQ_ResultSpec rs[] = 389 struct GNUNET_SQ_ResultSpec rs[] =
388 { GNUNET_SQ_result_spec_uint64(&rowid), 390 { GNUNET_SQ_result_spec_uint64 (&rowid),
389 GNUNET_SQ_result_spec_auto_from_type(&hc), 391 GNUNET_SQ_result_spec_auto_from_type (&hc),
390 GNUNET_SQ_result_spec_variable_size((void **)&data, &dsize), 392 GNUNET_SQ_result_spec_variable_size ((void **) &data, &dsize),
391 GNUNET_SQ_result_spec_end }; 393 GNUNET_SQ_result_spec_end };
392 struct GNUNET_SQ_QueryParam params[] = { GNUNET_SQ_query_param_uint64(&rowid), 394 struct GNUNET_SQ_QueryParam params[] = { GNUNET_SQ_query_param_uint64 (
395 &rowid),
393 GNUNET_SQ_query_param_end }; 396 GNUNET_SQ_query_param_end };
394 struct GNUNET_SQ_QueryParam time_params[] = 397 struct GNUNET_SQ_QueryParam time_params[] =
395 { GNUNET_SQ_query_param_absolute_time(&now), GNUNET_SQ_query_param_end }; 398 { GNUNET_SQ_query_param_absolute_time (&now), GNUNET_SQ_query_param_end };
396 399
397 LOG(GNUNET_ERROR_TYPE_DEBUG, "Processing DEL\n"); 400 LOG (GNUNET_ERROR_TYPE_DEBUG, "Processing DEL\n");
398 now = GNUNET_TIME_absolute_get(); 401 now = GNUNET_TIME_absolute_get ();
399 if (GNUNET_OK != GNUNET_SQ_bind(plugin->del_expired_stmt, time_params)) 402 if (GNUNET_OK != GNUNET_SQ_bind (plugin->del_expired_stmt, time_params))
400 { 403 {
401 LOG_SQLITE(plugin->dbh, 404 LOG_SQLITE (plugin->dbh,
402 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 405 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
403 "sqlite3_bind"); 406 "sqlite3_bind");
404 GNUNET_SQ_reset(plugin->dbh, plugin->del_expired_stmt); 407 GNUNET_SQ_reset (plugin->dbh, plugin->del_expired_stmt);
405 return GNUNET_SYSERR; 408 return GNUNET_SYSERR;
406 } 409 }
407 if ((SQLITE_ROW != sqlite3_step(plugin->del_expired_stmt)) || 410 if ((SQLITE_ROW != sqlite3_step (plugin->del_expired_stmt)) ||
408 (GNUNET_OK != GNUNET_SQ_extract_result(plugin->del_expired_stmt, rs))) 411 (GNUNET_OK != GNUNET_SQ_extract_result (plugin->del_expired_stmt, rs)))
409 { 412 {
410 GNUNET_SQ_reset(plugin->dbh, plugin->del_expired_stmt); 413 GNUNET_SQ_reset (plugin->dbh, plugin->del_expired_stmt);
411 if (SQLITE_ROW != sqlite3_step(plugin->del_select_stmt)) 414 if (SQLITE_ROW != sqlite3_step (plugin->del_select_stmt))
412 {
413 LOG_SQLITE(plugin->dbh,
414 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
415 "sqlite3_step");
416 GNUNET_SQ_reset(plugin->dbh, plugin->del_select_stmt);
417 return GNUNET_SYSERR;
418 }
419 if (GNUNET_OK != GNUNET_SQ_extract_result(plugin->del_select_stmt, rs))
420 {
421 GNUNET_SQ_reset(plugin->dbh, plugin->del_select_stmt);
422 GNUNET_break(0);
423 return GNUNET_SYSERR;
424 }
425 }
426 GNUNET_SQ_cleanup_result(rs);
427 GNUNET_SQ_reset(plugin->dbh, plugin->del_select_stmt);
428 if (GNUNET_OK != GNUNET_SQ_bind(plugin->del_stmt, params))
429 { 415 {
430 LOG_SQLITE(plugin->dbh, 416 LOG_SQLITE (plugin->dbh,
431 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 417 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
432 "sqlite3_bind"); 418 "sqlite3_step");
433 GNUNET_SQ_reset(plugin->dbh, plugin->del_stmt); 419 GNUNET_SQ_reset (plugin->dbh, plugin->del_select_stmt);
434 return GNUNET_SYSERR; 420 return GNUNET_SYSERR;
435 } 421 }
436 if (SQLITE_DONE != sqlite3_step(plugin->del_stmt)) 422 if (GNUNET_OK != GNUNET_SQ_extract_result (plugin->del_select_stmt, rs))
437 { 423 {
438 LOG_SQLITE(plugin->dbh, 424 GNUNET_SQ_reset (plugin->dbh, plugin->del_select_stmt);
439 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 425 GNUNET_break (0);
440 "sqlite3_step");
441 GNUNET_SQ_reset(plugin->dbh, plugin->del_stmt);
442 return GNUNET_SYSERR; 426 return GNUNET_SYSERR;
443 } 427 }
428 }
429 GNUNET_SQ_cleanup_result (rs);
430 GNUNET_SQ_reset (plugin->dbh, plugin->del_select_stmt);
431 if (GNUNET_OK != GNUNET_SQ_bind (plugin->del_stmt, params))
432 {
433 LOG_SQLITE (plugin->dbh,
434 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
435 "sqlite3_bind");
436 GNUNET_SQ_reset (plugin->dbh, plugin->del_stmt);
437 return GNUNET_SYSERR;
438 }
439 if (SQLITE_DONE != sqlite3_step (plugin->del_stmt))
440 {
441 LOG_SQLITE (plugin->dbh,
442 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
443 "sqlite3_step");
444 GNUNET_SQ_reset (plugin->dbh, plugin->del_stmt);
445 return GNUNET_SYSERR;
446 }
444 plugin->num_items--; 447 plugin->num_items--;
445 plugin->env->delete_notify(plugin->env->cls, &hc, dsize + OVERHEAD); 448 plugin->env->delete_notify (plugin->env->cls, &hc, dsize + OVERHEAD);
446 GNUNET_SQ_reset(plugin->dbh, plugin->del_stmt); 449 GNUNET_SQ_reset (plugin->dbh, plugin->del_stmt);
447 return GNUNET_OK; 450 return GNUNET_OK;
448} 451}
449 452
@@ -457,9 +460,9 @@ sqlite_plugin_del(void *cls)
457 * @return the number of results found, zero (datacache empty) or one 460 * @return the number of results found, zero (datacache empty) or one
458 */ 461 */
459static unsigned int 462static unsigned int
460sqlite_plugin_get_random(void *cls, 463sqlite_plugin_get_random (void *cls,
461 GNUNET_DATACACHE_Iterator iter, 464 GNUNET_DATACACHE_Iterator iter,
462 void *iter_cls) 465 void *iter_cls)
463{ 466{
464 struct Plugin *plugin = cls; 467 struct Plugin *plugin = cls;
465 struct GNUNET_TIME_Absolute exp; 468 struct GNUNET_TIME_Absolute exp;
@@ -470,14 +473,14 @@ sqlite_plugin_get_random(void *cls,
470 uint32_t type; 473 uint32_t type;
471 struct GNUNET_PeerIdentity *path; 474 struct GNUNET_PeerIdentity *path;
472 struct GNUNET_HashCode key; 475 struct GNUNET_HashCode key;
473 struct GNUNET_SQ_QueryParam params[] = { GNUNET_SQ_query_param_uint32(&off), 476 struct GNUNET_SQ_QueryParam params[] = { GNUNET_SQ_query_param_uint32 (&off),
474 GNUNET_SQ_query_param_end }; 477 GNUNET_SQ_query_param_end };
475 struct GNUNET_SQ_ResultSpec rs[] = 478 struct GNUNET_SQ_ResultSpec rs[] =
476 { GNUNET_SQ_result_spec_variable_size(&dat, &size), 479 { GNUNET_SQ_result_spec_variable_size (&dat, &size),
477 GNUNET_SQ_result_spec_absolute_time(&exp), 480 GNUNET_SQ_result_spec_absolute_time (&exp),
478 GNUNET_SQ_result_spec_variable_size((void **)&path, &psize), 481 GNUNET_SQ_result_spec_variable_size ((void **) &path, &psize),
479 GNUNET_SQ_result_spec_auto_from_type(&key), 482 GNUNET_SQ_result_spec_auto_from_type (&key),
480 GNUNET_SQ_result_spec_uint32(&type), 483 GNUNET_SQ_result_spec_uint32 (&type),
481 GNUNET_SQ_result_spec_end }; 484 GNUNET_SQ_result_spec_end };
482 485
483 if (0 == plugin->num_items) 486 if (0 == plugin->num_items)
@@ -485,44 +488,44 @@ sqlite_plugin_get_random(void *cls,
485 if (NULL == iter) 488 if (NULL == iter)
486 return 1; 489 return 1;
487 off = 490 off =
488 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE, plugin->num_items); 491 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, plugin->num_items);
489 if (GNUNET_OK != GNUNET_SQ_bind(plugin->get_random_stmt, params)) 492 if (GNUNET_OK != GNUNET_SQ_bind (plugin->get_random_stmt, params))
490 { 493 {
491 return 0; 494 return 0;
492 } 495 }
493 if (SQLITE_ROW != sqlite3_step(plugin->get_random_stmt)) 496 if (SQLITE_ROW != sqlite3_step (plugin->get_random_stmt))
494 { 497 {
495 GNUNET_break(0); 498 GNUNET_break (0);
496 GNUNET_SQ_reset(plugin->dbh, plugin->get_random_stmt); 499 GNUNET_SQ_reset (plugin->dbh, plugin->get_random_stmt);
497 return 0; 500 return 0;
498 } 501 }
499 if (GNUNET_OK != GNUNET_SQ_extract_result(plugin->get_random_stmt, rs)) 502 if (GNUNET_OK != GNUNET_SQ_extract_result (plugin->get_random_stmt, rs))
500 { 503 {
501 GNUNET_break(0); 504 GNUNET_break (0);
502 GNUNET_SQ_reset(plugin->dbh, plugin->get_random_stmt); 505 GNUNET_SQ_reset (plugin->dbh, plugin->get_random_stmt);
503 return 0; 506 return 0;
504 } 507 }
505 if (0 != psize % sizeof(struct GNUNET_PeerIdentity)) 508 if (0 != psize % sizeof(struct GNUNET_PeerIdentity))
506 { 509 {
507 GNUNET_break(0); 510 GNUNET_break (0);
508 psize = 0; 511 psize = 0;
509 path = NULL; 512 path = NULL;
510 } 513 }
511 psize /= sizeof(struct GNUNET_PeerIdentity); 514 psize /= sizeof(struct GNUNET_PeerIdentity);
512 LOG(GNUNET_ERROR_TYPE_DEBUG, 515 LOG (GNUNET_ERROR_TYPE_DEBUG,
513 "Found %u-byte result with key %s when processing GET-RANDOM\n", 516 "Found %u-byte result with key %s when processing GET-RANDOM\n",
514 (unsigned int)size, 517 (unsigned int) size,
515 GNUNET_h2s(&key)); 518 GNUNET_h2s (&key));
516 (void)iter(iter_cls, 519 (void) iter (iter_cls,
517 &key, 520 &key,
518 size, 521 size,
519 dat, 522 dat,
520 (enum GNUNET_BLOCK_Type)type, 523 (enum GNUNET_BLOCK_Type) type,
521 exp, 524 exp,
522 psize, 525 psize,
523 path); 526 path);
524 GNUNET_SQ_cleanup_result(rs); 527 GNUNET_SQ_cleanup_result (rs);
525 GNUNET_SQ_reset(plugin->dbh, plugin->get_random_stmt); 528 GNUNET_SQ_reset (plugin->dbh, plugin->get_random_stmt);
526 return 1; 529 return 1;
527} 530}
528 531
@@ -541,11 +544,11 @@ sqlite_plugin_get_random(void *cls,
541 * @return the number of results found 544 * @return the number of results found
542 */ 545 */
543static unsigned int 546static unsigned int
544sqlite_plugin_get_closest(void *cls, 547sqlite_plugin_get_closest (void *cls,
545 const struct GNUNET_HashCode *key, 548 const struct GNUNET_HashCode *key,
546 unsigned int num_results, 549 unsigned int num_results,
547 GNUNET_DATACACHE_Iterator iter, 550 GNUNET_DATACACHE_Iterator iter,
548 void *iter_cls) 551 void *iter_cls)
549{ 552{
550 struct Plugin *plugin = cls; 553 struct Plugin *plugin = cls;
551 uint32_t num_results32 = num_results; 554 uint32_t num_results32 = num_results;
@@ -559,58 +562,58 @@ sqlite_plugin_get_closest(void *cls,
559 struct GNUNET_HashCode hc; 562 struct GNUNET_HashCode hc;
560 struct GNUNET_PeerIdentity *path; 563 struct GNUNET_PeerIdentity *path;
561 struct GNUNET_SQ_QueryParam params[] = 564 struct GNUNET_SQ_QueryParam params[] =
562 { GNUNET_SQ_query_param_auto_from_type(key), 565 { GNUNET_SQ_query_param_auto_from_type (key),
563 GNUNET_SQ_query_param_absolute_time(&now), 566 GNUNET_SQ_query_param_absolute_time (&now),
564 GNUNET_SQ_query_param_uint32(&num_results32), 567 GNUNET_SQ_query_param_uint32 (&num_results32),
565 GNUNET_SQ_query_param_end }; 568 GNUNET_SQ_query_param_end };
566 struct GNUNET_SQ_ResultSpec rs[] = 569 struct GNUNET_SQ_ResultSpec rs[] =
567 { GNUNET_SQ_result_spec_variable_size(&dat, &size), 570 { GNUNET_SQ_result_spec_variable_size (&dat, &size),
568 GNUNET_SQ_result_spec_absolute_time(&exp), 571 GNUNET_SQ_result_spec_absolute_time (&exp),
569 GNUNET_SQ_result_spec_variable_size((void **)&path, &psize), 572 GNUNET_SQ_result_spec_variable_size ((void **) &path, &psize),
570 GNUNET_SQ_result_spec_uint32(&type), 573 GNUNET_SQ_result_spec_uint32 (&type),
571 GNUNET_SQ_result_spec_auto_from_type(&hc), 574 GNUNET_SQ_result_spec_auto_from_type (&hc),
572 GNUNET_SQ_result_spec_end }; 575 GNUNET_SQ_result_spec_end };
573 576
574 now = GNUNET_TIME_absolute_get(); 577 now = GNUNET_TIME_absolute_get ();
575 LOG(GNUNET_ERROR_TYPE_DEBUG, 578 LOG (GNUNET_ERROR_TYPE_DEBUG,
576 "Processing GET_CLOSEST for key `%s'\n", 579 "Processing GET_CLOSEST for key `%s'\n",
577 GNUNET_h2s(key)); 580 GNUNET_h2s (key));
578 if (GNUNET_OK != GNUNET_SQ_bind(plugin->get_closest_stmt, params)) 581 if (GNUNET_OK != GNUNET_SQ_bind (plugin->get_closest_stmt, params))
582 {
583 LOG_SQLITE (plugin->dbh,
584 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
585 "sqlite3_bind_xxx");
586 GNUNET_SQ_reset (plugin->dbh, plugin->get_closest_stmt);
587 return 0;
588 }
589 cnt = 0;
590 while (SQLITE_ROW == sqlite3_step (plugin->get_closest_stmt))
591 {
592 if (GNUNET_OK != GNUNET_SQ_extract_result (plugin->get_closest_stmt, rs))
579 { 593 {
580 LOG_SQLITE(plugin->dbh, 594 GNUNET_break (0);
581 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 595 break;
582 "sqlite3_bind_xxx");
583 GNUNET_SQ_reset(plugin->dbh, plugin->get_closest_stmt);
584 return 0;
585 } 596 }
586 cnt = 0; 597 if (0 != psize % sizeof(struct GNUNET_PeerIdentity))
587 while (SQLITE_ROW == sqlite3_step(plugin->get_closest_stmt))
588 { 598 {
589 if (GNUNET_OK != GNUNET_SQ_extract_result(plugin->get_closest_stmt, rs)) 599 GNUNET_break (0);
590 { 600 psize = 0;
591 GNUNET_break(0); 601 path = NULL;
592 break; 602 }
593 } 603 psize /= sizeof(struct GNUNET_PeerIdentity);
594 if (0 != psize % sizeof(struct GNUNET_PeerIdentity)) 604 cnt++;
595 { 605 LOG (GNUNET_ERROR_TYPE_DEBUG,
596 GNUNET_break(0); 606 "Found %u-byte result at %s when processing GET_CLOSE\n",
597 psize = 0; 607 (unsigned int) size,
598 path = NULL; 608 GNUNET_h2s (&hc));
599 } 609 if (GNUNET_OK != iter (iter_cls, &hc, size, dat, type, exp, psize, path))
600 psize /= sizeof(struct GNUNET_PeerIdentity); 610 {
601 cnt++; 611 GNUNET_SQ_cleanup_result (rs);
602 LOG(GNUNET_ERROR_TYPE_DEBUG, 612 break;
603 "Found %u-byte result at %s when processing GET_CLOSE\n",
604 (unsigned int)size,
605 GNUNET_h2s(&hc));
606 if (GNUNET_OK != iter(iter_cls, &hc, size, dat, type, exp, psize, path))
607 {
608 GNUNET_SQ_cleanup_result(rs);
609 break;
610 }
611 GNUNET_SQ_cleanup_result(rs);
612 } 613 }
613 GNUNET_SQ_reset(plugin->dbh, plugin->get_closest_stmt); 614 GNUNET_SQ_cleanup_result (rs);
615 }
616 GNUNET_SQ_reset (plugin->dbh, plugin->get_closest_stmt);
614 return cnt; 617 return cnt;
615} 618}
616 619
@@ -622,7 +625,7 @@ sqlite_plugin_get_closest(void *cls,
622 * @return the plugin's closure (our `struct Plugin`) 625 * @return the plugin's closure (our `struct Plugin`)
623 */ 626 */
624void * 627void *
625libgnunet_plugin_datacache_sqlite_init(void *cls) 628libgnunet_plugin_datacache_sqlite_init (void *cls)
626{ 629{
627 struct GNUNET_DATACACHE_PluginEnvironment *env = cls; 630 struct GNUNET_DATACACHE_PluginEnvironment *env = cls;
628 struct GNUNET_DATACACHE_PluginFunctions *api; 631 struct GNUNET_DATACACHE_PluginFunctions *api;
@@ -632,111 +635,111 @@ libgnunet_plugin_datacache_sqlite_init(void *cls)
632 sqlite3 *dbh; 635 sqlite3 *dbh;
633 char *emsg; 636 char *emsg;
634 637
635 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(env->cfg, 638 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
636 "datacache-sqlite", 639 "datacache-sqlite",
637 "IN_MEMORY")) 640 "IN_MEMORY"))
641 {
642 if (SQLITE_OK != sqlite3_open (":memory:", &dbh))
643 return NULL;
644 fn_utf8 = NULL;
645 }
646 else
647 {
648 fn = GNUNET_DISK_mktemp ("gnunet-datacache");
649 if (fn == NULL)
638 { 650 {
639 if (SQLITE_OK != sqlite3_open(":memory:", &dbh)) 651 GNUNET_break (0);
640 return NULL; 652 return NULL;
641 fn_utf8 = NULL;
642 } 653 }
643 else 654 /* fn should be UTF-8-encoded. If it isn't, it's a bug. */
655 fn_utf8 = GNUNET_strdup (fn);
656 if (SQLITE_OK != sqlite3_open (fn_utf8, &dbh))
644 { 657 {
645 fn = GNUNET_DISK_mktemp("gnunet-datacache"); 658 GNUNET_free (fn);
646 if (fn == NULL) 659 GNUNET_free (fn_utf8);
647 { 660 return NULL;
648 GNUNET_break(0);
649 return NULL;
650 }
651 /* fn should be UTF-8-encoded. If it isn't, it's a bug. */
652 fn_utf8 = GNUNET_strdup(fn);
653 if (SQLITE_OK != sqlite3_open(fn_utf8, &dbh))
654 {
655 GNUNET_free(fn);
656 GNUNET_free(fn_utf8);
657 return NULL;
658 }
659 GNUNET_free(fn);
660 } 661 }
661 662 GNUNET_free (fn);
662 SQLITE3_EXEC(dbh, "PRAGMA temp_store=MEMORY"); 663 }
663 SQLITE3_EXEC(dbh, "PRAGMA locking_mode=EXCLUSIVE"); 664
664 SQLITE3_EXEC(dbh, "PRAGMA journal_mode=OFF"); 665 SQLITE3_EXEC (dbh, "PRAGMA temp_store=MEMORY");
665 SQLITE3_EXEC(dbh, "PRAGMA synchronous=OFF"); 666 SQLITE3_EXEC (dbh, "PRAGMA locking_mode=EXCLUSIVE");
666 SQLITE3_EXEC(dbh, "PRAGMA page_size=4092"); 667 SQLITE3_EXEC (dbh, "PRAGMA journal_mode=OFF");
667 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(env->cfg, 668 SQLITE3_EXEC (dbh, "PRAGMA synchronous=OFF");
668 "datacache-sqlite", 669 SQLITE3_EXEC (dbh, "PRAGMA page_size=4092");
669 "IN_MEMORY")) 670 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
670 SQLITE3_EXEC(dbh, "PRAGMA sqlite_temp_store=3"); 671 "datacache-sqlite",
671 672 "IN_MEMORY"))
672 SQLITE3_EXEC(dbh, 673 SQLITE3_EXEC (dbh, "PRAGMA sqlite_temp_store=3");
673 "CREATE TABLE ds091 (" 674
674 " type INTEGER NOT NULL DEFAULT 0," 675 SQLITE3_EXEC (dbh,
675 " expire INTEGER NOT NULL," 676 "CREATE TABLE ds091 ("
676 " key BLOB NOT NULL DEFAULT ''," 677 " type INTEGER NOT NULL DEFAULT 0,"
677 " prox INTEGER NOT NULL," 678 " expire INTEGER NOT NULL,"
678 " value BLOB NOT NULL," 679 " key BLOB NOT NULL DEFAULT '',"
679 " path BLOB DEFAULT '')"); 680 " prox INTEGER NOT NULL,"
680 SQLITE3_EXEC(dbh, "CREATE INDEX idx_hashidx ON ds091 (key,type,expire)"); 681 " value BLOB NOT NULL,"
681 SQLITE3_EXEC(dbh, "CREATE INDEX idx_prox_expire ON ds091 (prox,expire)"); 682 " path BLOB DEFAULT '')");
682 SQLITE3_EXEC(dbh, "CREATE INDEX idx_expire_only ON ds091 (expire)"); 683 SQLITE3_EXEC (dbh, "CREATE INDEX idx_hashidx ON ds091 (key,type,expire)");
683 plugin = GNUNET_new(struct Plugin); 684 SQLITE3_EXEC (dbh, "CREATE INDEX idx_prox_expire ON ds091 (prox,expire)");
685 SQLITE3_EXEC (dbh, "CREATE INDEX idx_expire_only ON ds091 (expire)");
686 plugin = GNUNET_new (struct Plugin);
684 plugin->env = env; 687 plugin->env = env;
685 plugin->dbh = dbh; 688 plugin->dbh = dbh;
686 plugin->fn = fn_utf8; 689 plugin->fn = fn_utf8;
687 690
688 if ((SQLITE_OK != 691 if ((SQLITE_OK !=
689 sq_prepare(plugin->dbh, 692 sq_prepare (plugin->dbh,
690 "INSERT INTO ds091 (type, expire, key, prox, value, path) " 693 "INSERT INTO ds091 (type, expire, key, prox, value, path) "
691 "VALUES (?, ?, ?, ?, ?, ?)", 694 "VALUES (?, ?, ?, ?, ?, ?)",
692 &plugin->insert_stmt)) || 695 &plugin->insert_stmt)) ||
693 (SQLITE_OK != sq_prepare(plugin->dbh, 696 (SQLITE_OK != sq_prepare (plugin->dbh,
694 "SELECT count(*) FROM ds091 " 697 "SELECT count(*) FROM ds091 "
695 "WHERE key=? AND type=? AND expire >= ?", 698 "WHERE key=? AND type=? AND expire >= ?",
696 &plugin->get_count_stmt)) || 699 &plugin->get_count_stmt)) ||
697 (SQLITE_OK != 700 (SQLITE_OK !=
698 sq_prepare(plugin->dbh, 701 sq_prepare (plugin->dbh,
699 "SELECT value,expire,path FROM ds091" 702 "SELECT value,expire,path FROM ds091"
700 " WHERE key=? AND type=? AND expire >= ? LIMIT 1 OFFSET ?", 703 " WHERE key=? AND type=? AND expire >= ? LIMIT 1 OFFSET ?",
701 &plugin->get_stmt)) || 704 &plugin->get_stmt)) ||
702 (SQLITE_OK != sq_prepare(plugin->dbh, 705 (SQLITE_OK != sq_prepare (plugin->dbh,
703 "SELECT _ROWID_,key,value FROM ds091" 706 "SELECT _ROWID_,key,value FROM ds091"
704 " WHERE expire < ?" 707 " WHERE expire < ?"
705 " ORDER BY expire ASC LIMIT 1", 708 " ORDER BY expire ASC LIMIT 1",
706 &plugin->del_expired_stmt)) || 709 &plugin->del_expired_stmt)) ||
707 (SQLITE_OK != sq_prepare(plugin->dbh, 710 (SQLITE_OK != sq_prepare (plugin->dbh,
708 "SELECT _ROWID_,key,value FROM ds091" 711 "SELECT _ROWID_,key,value FROM ds091"
709 " ORDER BY prox ASC, expire ASC LIMIT 1", 712 " ORDER BY prox ASC, expire ASC LIMIT 1",
710 &plugin->del_select_stmt)) || 713 &plugin->del_select_stmt)) ||
711 (SQLITE_OK != sq_prepare(plugin->dbh, 714 (SQLITE_OK != sq_prepare (plugin->dbh,
712 "DELETE FROM ds091 WHERE _ROWID_=?", 715 "DELETE FROM ds091 WHERE _ROWID_=?",
713 &plugin->del_stmt)) || 716 &plugin->del_stmt)) ||
714 (SQLITE_OK != sq_prepare(plugin->dbh, 717 (SQLITE_OK != sq_prepare (plugin->dbh,
715 "SELECT value,expire,path,key,type FROM ds091 " 718 "SELECT value,expire,path,key,type FROM ds091 "
716 "ORDER BY key LIMIT 1 OFFSET ?", 719 "ORDER BY key LIMIT 1 OFFSET ?",
717 &plugin->get_random_stmt)) || 720 &plugin->get_random_stmt)) ||
718 (SQLITE_OK != 721 (SQLITE_OK !=
719 sq_prepare(plugin->dbh, 722 sq_prepare (plugin->dbh,
720 "SELECT value,expire,path,type,key FROM ds091 " 723 "SELECT value,expire,path,type,key FROM ds091 "
721 "WHERE key>=? AND expire >= ? ORDER BY KEY ASC LIMIT ?", 724 "WHERE key>=? AND expire >= ? ORDER BY KEY ASC LIMIT ?",
722 &plugin->get_closest_stmt))) 725 &plugin->get_closest_stmt)))
723 { 726 {
724 LOG_SQLITE(plugin->dbh, 727 LOG_SQLITE (plugin->dbh,
725 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 728 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
726 "sq_prepare"); 729 "sq_prepare");
727 GNUNET_break(SQLITE_OK == sqlite3_close(plugin->dbh)); 730 GNUNET_break (SQLITE_OK == sqlite3_close (plugin->dbh));
728 GNUNET_free(plugin); 731 GNUNET_free (plugin);
729 return NULL; 732 return NULL;
730 } 733 }
731 734
732 api = GNUNET_new(struct GNUNET_DATACACHE_PluginFunctions); 735 api = GNUNET_new (struct GNUNET_DATACACHE_PluginFunctions);
733 api->cls = plugin; 736 api->cls = plugin;
734 api->get = &sqlite_plugin_get; 737 api->get = &sqlite_plugin_get;
735 api->put = &sqlite_plugin_put; 738 api->put = &sqlite_plugin_put;
736 api->del = &sqlite_plugin_del; 739 api->del = &sqlite_plugin_del;
737 api->get_random = &sqlite_plugin_get_random; 740 api->get_random = &sqlite_plugin_get_random;
738 api->get_closest = &sqlite_plugin_get_closest; 741 api->get_closest = &sqlite_plugin_get_closest;
739 LOG(GNUNET_ERROR_TYPE_INFO, "Sqlite datacache running\n"); 742 LOG (GNUNET_ERROR_TYPE_INFO, "Sqlite datacache running\n");
740 return api; 743 return api;
741} 744}
742 745
@@ -748,7 +751,7 @@ libgnunet_plugin_datacache_sqlite_init(void *cls)
748 * @return NULL 751 * @return NULL
749 */ 752 */
750void * 753void *
751libgnunet_plugin_datacache_sqlite_done(void *cls) 754libgnunet_plugin_datacache_sqlite_done (void *cls)
752{ 755{
753 struct GNUNET_DATACACHE_PluginFunctions *api = cls; 756 struct GNUNET_DATACACHE_PluginFunctions *api = cls;
754 struct Plugin *plugin = api->cls; 757 struct Plugin *plugin = api->cls;
@@ -758,45 +761,45 @@ libgnunet_plugin_datacache_sqlite_done(void *cls)
758 sqlite3_stmt *stmt; 761 sqlite3_stmt *stmt;
759#endif 762#endif
760 763
761#if !WINDOWS || defined(__CYGWIN__) 764#if ! WINDOWS || defined(__CYGWIN__)
762 if ((NULL != plugin->fn) && (0 != unlink(plugin->fn))) 765 if ((NULL != plugin->fn) && (0 != unlink (plugin->fn)))
763 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "unlink", plugin->fn); 766 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", plugin->fn);
764 GNUNET_free_non_null(plugin->fn); 767 GNUNET_free_non_null (plugin->fn);
765#endif 768#endif
766 sqlite3_finalize(plugin->insert_stmt); 769 sqlite3_finalize (plugin->insert_stmt);
767 sqlite3_finalize(plugin->get_count_stmt); 770 sqlite3_finalize (plugin->get_count_stmt);
768 sqlite3_finalize(plugin->get_stmt); 771 sqlite3_finalize (plugin->get_stmt);
769 sqlite3_finalize(plugin->del_select_stmt); 772 sqlite3_finalize (plugin->del_select_stmt);
770 sqlite3_finalize(plugin->del_expired_stmt); 773 sqlite3_finalize (plugin->del_expired_stmt);
771 sqlite3_finalize(plugin->del_stmt); 774 sqlite3_finalize (plugin->del_stmt);
772 sqlite3_finalize(plugin->get_random_stmt); 775 sqlite3_finalize (plugin->get_random_stmt);
773 sqlite3_finalize(plugin->get_closest_stmt); 776 sqlite3_finalize (plugin->get_closest_stmt);
774 result = sqlite3_close(plugin->dbh); 777 result = sqlite3_close (plugin->dbh);
775#if SQLITE_VERSION_NUMBER >= 3007000 778#if SQLITE_VERSION_NUMBER >= 3007000
776 if (SQLITE_BUSY == result) 779 if (SQLITE_BUSY == result)
780 {
781 LOG (GNUNET_ERROR_TYPE_WARNING,
782 _ (
783 "Tried to close sqlite without finalizing all prepared statements.\n"));
784 stmt = sqlite3_next_stmt (plugin->dbh, NULL);
785 while (NULL != stmt)
777 { 786 {
778 LOG(GNUNET_ERROR_TYPE_WARNING, 787 result = sqlite3_finalize (stmt);
779 _( 788 if (result != SQLITE_OK)
780 "Tried to close sqlite without finalizing all prepared statements.\n")); 789 LOG (GNUNET_ERROR_TYPE_WARNING,
781 stmt = sqlite3_next_stmt(plugin->dbh, NULL); 790 "Failed to close statement %p: %d\n",
782 while (NULL != stmt) 791 stmt,
783 { 792 result);
784 result = sqlite3_finalize(stmt); 793 stmt = sqlite3_next_stmt (plugin->dbh, NULL);
785 if (result != SQLITE_OK)
786 LOG(GNUNET_ERROR_TYPE_WARNING,
787 "Failed to close statement %p: %d\n",
788 stmt,
789 result);
790 stmt = sqlite3_next_stmt(plugin->dbh, NULL);
791 }
792 result = sqlite3_close(plugin->dbh);
793 } 794 }
795 result = sqlite3_close (plugin->dbh);
796 }
794#endif 797#endif
795 if (SQLITE_OK != result) 798 if (SQLITE_OK != result)
796 LOG_SQLITE(plugin->dbh, GNUNET_ERROR_TYPE_ERROR, "sqlite3_close"); 799 LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR, "sqlite3_close");
797 800
798 GNUNET_free(plugin); 801 GNUNET_free (plugin);
799 GNUNET_free(api); 802 GNUNET_free (api);
800 return NULL; 803 return NULL;
801} 804}
802 805