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.c887
1 files changed, 443 insertions, 444 deletions
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