aboutsummaryrefslogtreecommitdiff
path: root/src/datastore/plugin_datastore_sqlite.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/datastore/plugin_datastore_sqlite.c')
-rw-r--r--src/datastore/plugin_datastore_sqlite.c1072
1 files changed, 517 insertions, 555 deletions
diff --git a/src/datastore/plugin_datastore_sqlite.c b/src/datastore/plugin_datastore_sqlite.c
index f551f048d..075d31623 100644
--- a/src/datastore/plugin_datastore_sqlite.c
+++ b/src/datastore/plugin_datastore_sqlite.c
@@ -1,22 +1,22 @@
1 /* 1 /*
2 This file is part of GNUnet 2 * This file is part of GNUnet
3 (C) 2009, 2011 Christian Grothoff (and other contributing authors) 3 * (C) 2009, 2011 Christian Grothoff (and other contributing authors)
4 4 *
5 GNUnet is free software; you can redistribute it and/or modify 5 * GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published 6 * it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your 7 * by the Free Software Foundation; either version 3, or (at your
8 option) any later version. 8 * option) any later version.
9 9 *
10 GNUnet is distributed in the hope that it will be useful, but 10 * GNUnet is distributed in the hope that it will be useful, but
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 General Public License for more details. 13 * General Public License for more details.
14 14 *
15 You should have received a copy of the GNU General Public License 15 * You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the 16 * along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. 18 * Boston, MA 02111-1307, USA.
19*/ 19 */
20 20
21/** 21/**
22 * @file datastore/plugin_datastore_sqlite.c 22 * @file datastore/plugin_datastore_sqlite.c
@@ -65,7 +65,7 @@
65/** 65/**
66 * Context for all functions in this plugin. 66 * Context for all functions in this plugin.
67 */ 67 */
68struct Plugin 68struct Plugin
69{ 69{
70 /** 70 /**
71 * Our execution environment. 71 * Our execution environment.
@@ -139,25 +139,17 @@ struct Plugin
139 * @return 0 on success 139 * @return 0 on success
140 */ 140 */
141static int 141static int
142sq_prepare (sqlite3 * dbh, 142sq_prepare (sqlite3 * dbh, const char *zSql, sqlite3_stmt ** ppStmt)
143 const char *zSql,
144 sqlite3_stmt ** ppStmt)
145{ 143{
146 char *dummy; 144 char *dummy;
147 int result; 145 int result;
148 146
149 result = sqlite3_prepare_v2 (dbh, 147 result = sqlite3_prepare_v2 (dbh,
150 zSql, 148 zSql,
151 strlen (zSql), 149 strlen (zSql), ppStmt, (const char **) &dummy);
152 ppStmt,
153 (const char **) &dummy);
154#if DEBUG_SQLITE && 0 150#if DEBUG_SQLITE && 0
155 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 151 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
156 "sqlite", 152 "sqlite", "Prepared `%s' / %p: %d\n", zSql, *ppStmt, result);
157 "Prepared `%s' / %p: %d\n",
158 zSql,
159 *ppStmt,
160 result);
161#endif 153#endif
162 return result; 154 return result;
163} 155}
@@ -172,35 +164,40 @@ static void
172create_indices (sqlite3 * dbh) 164create_indices (sqlite3 * dbh)
173{ 165{
174 /* create indices */ 166 /* create indices */
175 if ( (SQLITE_OK != 167 if ((SQLITE_OK !=
176 sqlite3_exec (dbh, 168 sqlite3_exec (dbh,
177 "CREATE INDEX IF NOT EXISTS idx_hash ON gn090 (hash)", NULL, NULL, NULL)) || 169 "CREATE INDEX IF NOT EXISTS idx_hash ON gn090 (hash)",
178 (SQLITE_OK != 170 NULL, NULL, NULL)) ||
179 sqlite3_exec (dbh, 171 (SQLITE_OK !=
180 "CREATE INDEX IF NOT EXISTS idx_hash_vhash ON gn090 (hash,vhash)", NULL, 172 sqlite3_exec (dbh,
181 NULL, NULL)) || 173 "CREATE INDEX IF NOT EXISTS idx_hash_vhash ON gn090 (hash,vhash)",
182 (SQLITE_OK != 174 NULL, NULL, NULL)) ||
183 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS idx_expire_repl ON gn090 (expire ASC,repl DESC)", NULL, NULL, 175 (SQLITE_OK !=
184 NULL)) || 176 sqlite3_exec (dbh,
185 (SQLITE_OK != 177 "CREATE INDEX IF NOT EXISTS idx_expire_repl ON gn090 (expire ASC,repl DESC)",
186 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS idx_comb ON gn090 (anonLevel ASC,expire ASC,prio,type,hash)", 178 NULL, NULL, NULL)) ||
187 NULL, NULL, NULL)) || 179 (SQLITE_OK !=
188 (SQLITE_OK != 180 sqlite3_exec (dbh,
189 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS idx_anon_type_hash ON gn090 (anonLevel ASC,type,hash)", 181 "CREATE INDEX IF NOT EXISTS idx_comb ON gn090 (anonLevel ASC,expire ASC,prio,type,hash)",
190 NULL, NULL, NULL)) || 182 NULL, NULL, NULL)) ||
191 (SQLITE_OK != 183 (SQLITE_OK !=
192 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS idx_expire ON gn090 (expire ASC)", 184 sqlite3_exec (dbh,
193 NULL, NULL, NULL)) || 185 "CREATE INDEX IF NOT EXISTS idx_anon_type_hash ON gn090 (anonLevel ASC,type,hash)",
194 (SQLITE_OK != 186 NULL, NULL, NULL)) ||
195 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS idx_repl_rvalue ON gn090 (repl,rvalue)", 187 (SQLITE_OK !=
196 NULL, NULL, NULL)) || 188 sqlite3_exec (dbh,
197 (SQLITE_OK != 189 "CREATE INDEX IF NOT EXISTS idx_expire ON gn090 (expire ASC)",
198 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS idx_repl ON gn090 (repl DESC)", 190 NULL, NULL, NULL)) ||
199 NULL, NULL, NULL)) ) 191 (SQLITE_OK !=
200 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, 192 sqlite3_exec (dbh,
201 "sqlite", 193 "CREATE INDEX IF NOT EXISTS idx_repl_rvalue ON gn090 (repl,rvalue)",
202 "Failed to create indices: %s\n", 194 NULL, NULL, NULL)) ||
203 sqlite3_errmsg (dbh)); 195 (SQLITE_OK !=
196 sqlite3_exec (dbh,
197 "CREATE INDEX IF NOT EXISTS idx_repl ON gn090 (repl DESC)",
198 NULL, NULL, NULL)))
199 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "sqlite",
200 "Failed to create indices: %s\n", sqlite3_errmsg (dbh));
204} 201}
205 202
206 203
@@ -225,56 +222,55 @@ create_indices (sqlite3 * dbh)
225 */ 222 */
226static int 223static int
227database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg, 224database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg,
228 struct Plugin *plugin) 225 struct Plugin *plugin)
229{ 226{
230 sqlite3_stmt *stmt; 227 sqlite3_stmt *stmt;
231 char *afsdir; 228 char *afsdir;
229
232#if ENULL_DEFINED 230#if ENULL_DEFINED
233 char *e; 231 char *e;
234#endif 232#endif
235 233
236 if (GNUNET_OK != 234 if (GNUNET_OK !=
237 GNUNET_CONFIGURATION_get_value_filename (cfg, 235 GNUNET_CONFIGURATION_get_value_filename (cfg,
238 "datastore-sqlite", 236 "datastore-sqlite",
239 "FILENAME", 237 "FILENAME", &afsdir))
240 &afsdir)) 238 {
241 { 239 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
242 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, 240 "sqlite",
243 "sqlite", 241 _
244 _("Option `%s' in section `%s' missing in configuration!\n"), 242 ("Option `%s' in section `%s' missing in configuration!\n"),
245 "FILENAME", 243 "FILENAME", "datastore-sqlite");
246 "datastore-sqlite"); 244 return GNUNET_SYSERR;
247 return GNUNET_SYSERR; 245 }
248 }
249 if (GNUNET_OK != GNUNET_DISK_file_test (afsdir)) 246 if (GNUNET_OK != GNUNET_DISK_file_test (afsdir))
247 {
248 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (afsdir))
250 { 249 {
251 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (afsdir)) 250 GNUNET_break (0);
252 { 251 GNUNET_free (afsdir);
253 GNUNET_break (0); 252 return GNUNET_SYSERR;
254 GNUNET_free (afsdir);
255 return GNUNET_SYSERR;
256 }
257 /* database is new or got deleted, reset payload to zero! */
258 plugin->env->duc (plugin->env->cls, 0);
259 } 253 }
254 /* database is new or got deleted, reset payload to zero! */
255 plugin->env->duc (plugin->env->cls, 0);
256 }
260#ifdef ENABLE_NLS 257#ifdef ENABLE_NLS
261 plugin->fn = GNUNET_STRINGS_to_utf8 (afsdir, strlen (afsdir), 258 plugin->fn = GNUNET_STRINGS_to_utf8 (afsdir, strlen (afsdir),
262 nl_langinfo (CODESET)); 259 nl_langinfo (CODESET));
263#else 260#else
264 plugin->fn = GNUNET_STRINGS_to_utf8 (afsdir, strlen (afsdir), 261 plugin->fn = GNUNET_STRINGS_to_utf8 (afsdir, strlen (afsdir), "UTF-8"); /* good luck */
265 "UTF-8"); /* good luck */
266#endif 262#endif
267 GNUNET_free (afsdir); 263 GNUNET_free (afsdir);
268 264
269 /* Open database and precompile statements */ 265 /* Open database and precompile statements */
270 if (sqlite3_open (plugin->fn, &plugin->dbh) != SQLITE_OK) 266 if (sqlite3_open (plugin->fn, &plugin->dbh) != SQLITE_OK)
271 { 267 {
272 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, 268 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
273 "sqlite", 269 "sqlite",
274 _("Unable to initialize SQLite: %s.\n"), 270 _("Unable to initialize SQLite: %s.\n"),
275 sqlite3_errmsg (plugin->dbh)); 271 sqlite3_errmsg (plugin->dbh));
276 return GNUNET_SYSERR; 272 return GNUNET_SYSERR;
277 } 273 }
278 CHECK (SQLITE_OK == 274 CHECK (SQLITE_OK ==
279 sqlite3_exec (plugin->dbh, 275 sqlite3_exec (plugin->dbh,
280 "PRAGMA temp_store=MEMORY", NULL, NULL, ENULL)); 276 "PRAGMA temp_store=MEMORY", NULL, NULL, ENULL));
@@ -294,8 +290,8 @@ database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg,
294 sqlite3_exec (plugin->dbh, 290 sqlite3_exec (plugin->dbh,
295 "PRAGMA count_changes=OFF", NULL, NULL, ENULL)); 291 "PRAGMA count_changes=OFF", NULL, NULL, ENULL));
296 CHECK (SQLITE_OK == 292 CHECK (SQLITE_OK ==
297 sqlite3_exec (plugin->dbh, 293 sqlite3_exec (plugin->dbh,
298 "PRAGMA page_size=4092", NULL, NULL, ENULL)); 294 "PRAGMA page_size=4092", NULL, NULL, ENULL));
299 295
300 CHECK (SQLITE_OK == sqlite3_busy_timeout (plugin->dbh, BUSY_TIMEOUT_MS)); 296 CHECK (SQLITE_OK == sqlite3_busy_timeout (plugin->dbh, BUSY_TIMEOUT_MS));
301 297
@@ -305,62 +301,60 @@ database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg,
305 sq_prepare (plugin->dbh, 301 sq_prepare (plugin->dbh,
306 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'gn090'", 302 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'gn090'",
307 &stmt)); 303 &stmt));
308 if ( (sqlite3_step (stmt) == SQLITE_DONE) && 304 if ((sqlite3_step (stmt) == SQLITE_DONE) &&
309 (sqlite3_exec (plugin->dbh, 305 (sqlite3_exec (plugin->dbh,
310 "CREATE TABLE gn090 (" 306 "CREATE TABLE gn090 ("
311 " repl INT4 NOT NULL DEFAULT 0," 307 " repl INT4 NOT NULL DEFAULT 0,"
312 " type INT4 NOT NULL DEFAULT 0," 308 " type INT4 NOT NULL DEFAULT 0,"
313 " prio INT4 NOT NULL DEFAULT 0," 309 " prio INT4 NOT NULL DEFAULT 0,"
314 " anonLevel INT4 NOT NULL DEFAULT 0," 310 " anonLevel INT4 NOT NULL DEFAULT 0,"
315 " expire INT8 NOT NULL DEFAULT 0," 311 " expire INT8 NOT NULL DEFAULT 0,"
316 " rvalue INT8 NOT NULL," 312 " rvalue INT8 NOT NULL,"
317 " hash TEXT NOT NULL DEFAULT ''," 313 " hash TEXT NOT NULL DEFAULT '',"
318 " vhash TEXT NOT NULL DEFAULT ''," 314 " vhash TEXT NOT NULL DEFAULT '',"
319 " value BLOB NOT NULL DEFAULT '')", NULL, NULL, 315 " value BLOB NOT NULL DEFAULT '')", NULL, NULL,
320 NULL) != SQLITE_OK) ) 316 NULL) != SQLITE_OK))
321 { 317 {
322 LOG_SQLITE (plugin, NULL, 318 LOG_SQLITE (plugin, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec");
323 GNUNET_ERROR_TYPE_ERROR, 319 sqlite3_finalize (stmt);
324 "sqlite3_exec"); 320 return GNUNET_SYSERR;
325 sqlite3_finalize (stmt); 321 }
326 return GNUNET_SYSERR;
327 }
328 sqlite3_finalize (stmt); 322 sqlite3_finalize (stmt);
329 create_indices (plugin->dbh); 323 create_indices (plugin->dbh);
330 324
331 if ((sq_prepare (plugin->dbh, 325 if ((sq_prepare (plugin->dbh,
332 "UPDATE gn090 " 326 "UPDATE gn090 "
333 "SET prio = prio + ?, expire = MAX(expire,?) WHERE _ROWID_ = ?", 327 "SET prio = prio + ?, expire = MAX(expire,?) WHERE _ROWID_ = ?",
334 &plugin->updPrio) != SQLITE_OK) || 328 &plugin->updPrio) != SQLITE_OK) ||
335 (sq_prepare (plugin->dbh, 329 (sq_prepare (plugin->dbh,
336 "UPDATE gn090 " 330 "UPDATE gn090 "
337 "SET repl = MAX (0, repl - 1) WHERE _ROWID_ = ?", 331 "SET repl = MAX (0, repl - 1) WHERE _ROWID_ = ?",
338 &plugin->updRepl) != SQLITE_OK) || 332 &plugin->updRepl) != SQLITE_OK) ||
339 (sq_prepare (plugin->dbh, 333 (sq_prepare (plugin->dbh,
340 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ " 334 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ "
341 "FROM gn090 INDEXED BY idx_repl_rvalue " 335 "FROM gn090 INDEXED BY idx_repl_rvalue "
342 "WHERE repl=?2 AND" 336 "WHERE repl=?2 AND"
343 " (rvalue>=?1 OR" 337 " (rvalue>=?1 OR"
344 " NOT EXISTS (SELECT 1 FROM gn090 INDEXED BY idx_repl_rvalue WHERE repl=?2 AND rvalue>=?1 LIMIT 1))" 338 " NOT EXISTS (SELECT 1 FROM gn090 INDEXED BY idx_repl_rvalue WHERE repl=?2 AND rvalue>=?1 LIMIT 1))"
345 " ORDER BY rvalue ASC" 339 " ORDER BY rvalue ASC"
346 " LIMIT 1", 340 " LIMIT 1",
347 &plugin->selRepl) != SQLITE_OK) || 341 &plugin->selRepl) != SQLITE_OK) ||
348 (sq_prepare (plugin->dbh, 342 (sq_prepare (plugin->dbh,
349 "SELECT MAX(repl) " 343 "SELECT MAX(repl) "
350 "FROM gn090 INDEXED BY idx_repl_rvalue", 344 "FROM gn090 INDEXED BY idx_repl_rvalue",
351 &plugin->maxRepl) != SQLITE_OK) || 345 &plugin->maxRepl) != SQLITE_OK) ||
352 (sq_prepare (plugin->dbh, 346 (sq_prepare (plugin->dbh,
353 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ " 347 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ "
354 "FROM gn090 INDEXED BY idx_expire" 348 "FROM gn090 INDEXED BY idx_expire"
355 " WHERE NOT EXISTS (SELECT 1 FROM gn090 WHERE expire < ?1 LIMIT 1) OR (expire < ?1) " 349 " WHERE NOT EXISTS (SELECT 1 FROM gn090 WHERE expire < ?1 LIMIT 1) OR (expire < ?1) "
356 " ORDER BY expire ASC LIMIT 1", 350 " ORDER BY expire ASC LIMIT 1",
357 &plugin->selExpi) != SQLITE_OK) || 351 &plugin->selExpi) != SQLITE_OK) ||
358 (sq_prepare (plugin->dbh, 352 (sq_prepare (plugin->dbh,
359 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ " 353 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ "
360 "FROM gn090 INDEXED BY idx_anon_type_hash " 354 "FROM gn090 INDEXED BY idx_anon_type_hash "
361 "WHERE (anonLevel = 0 AND type=?1) " 355 "WHERE (anonLevel = 0 AND type=?1) "
362 "ORDER BY hash DESC LIMIT 1 OFFSET ?2", 356 "ORDER BY hash DESC LIMIT 1 OFFSET ?2",
363 &plugin->selZeroAnon) != SQLITE_OK) || 357 &plugin->selZeroAnon) != SQLITE_OK) ||
364 (sq_prepare (plugin->dbh, 358 (sq_prepare (plugin->dbh,
365 "INSERT INTO gn090 (repl, type, prio, " 359 "INSERT INTO gn090 (repl, type, prio, "
366 "anonLevel, expire, rvalue, hash, vhash, value) " 360 "anonLevel, expire, rvalue, hash, vhash, value) "
@@ -369,11 +363,10 @@ database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg,
369 (sq_prepare (plugin->dbh, 363 (sq_prepare (plugin->dbh,
370 "DELETE FROM gn090 WHERE _ROWID_ = ?", 364 "DELETE FROM gn090 WHERE _ROWID_ = ?",
371 &plugin->delRow) != SQLITE_OK)) 365 &plugin->delRow) != SQLITE_OK))
372 { 366 {
373 LOG_SQLITE (plugin, NULL, 367 LOG_SQLITE (plugin, NULL, GNUNET_ERROR_TYPE_ERROR, "precompiling");
374 GNUNET_ERROR_TYPE_ERROR, "precompiling"); 368 return GNUNET_SYSERR;
375 return GNUNET_SYSERR; 369 }
376 }
377 370
378 return GNUNET_OK; 371 return GNUNET_OK;
379} 372}
@@ -388,6 +381,7 @@ static void
388database_shutdown (struct Plugin *plugin) 381database_shutdown (struct Plugin *plugin)
389{ 382{
390 int result; 383 int result;
384
391#if SQLITE_VERSION_NUMBER >= 3007000 385#if SQLITE_VERSION_NUMBER >= 3007000
392 sqlite3_stmt *stmt; 386 sqlite3_stmt *stmt;
393#endif 387#endif
@@ -408,36 +402,35 @@ database_shutdown (struct Plugin *plugin)
408 sqlite3_finalize (plugin->selZeroAnon); 402 sqlite3_finalize (plugin->selZeroAnon);
409 if (plugin->insertContent != NULL) 403 if (plugin->insertContent != NULL)
410 sqlite3_finalize (plugin->insertContent); 404 sqlite3_finalize (plugin->insertContent);
411 result = sqlite3_close(plugin->dbh); 405 result = sqlite3_close (plugin->dbh);
412#if SQLITE_VERSION_NUMBER >= 3007000 406#if SQLITE_VERSION_NUMBER >= 3007000
413 if (result == SQLITE_BUSY) 407 if (result == SQLITE_BUSY)
408 {
409 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
410 "sqlite",
411 _
412 ("Tried to close sqlite without finalizing all prepared statements.\n"));
413 stmt = sqlite3_next_stmt (plugin->dbh, NULL);
414 while (stmt != NULL)
414 { 415 {
415 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
416 "sqlite",
417 _("Tried to close sqlite without finalizing all prepared statements.\n"));
418 stmt = sqlite3_next_stmt(plugin->dbh, NULL);
419 while (stmt != NULL)
420 {
421#if DEBUG_SQLITE 416#if DEBUG_SQLITE
422 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 417 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
423 "sqlite", "Closing statement %p\n", stmt); 418 "sqlite", "Closing statement %p\n", stmt);
424#endif 419#endif
425 result = sqlite3_finalize(stmt); 420 result = sqlite3_finalize (stmt);
426#if DEBUG_SQLITE 421#if DEBUG_SQLITE
427 if (result != SQLITE_OK) 422 if (result != SQLITE_OK)
428 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 423 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
429 "sqlite", 424 "sqlite",
430 "Failed to close statement %p: %d\n", stmt, result); 425 "Failed to close statement %p: %d\n", stmt, result);
431#endif 426#endif
432 stmt = sqlite3_next_stmt(plugin->dbh, NULL); 427 stmt = sqlite3_next_stmt (plugin->dbh, NULL);
433 }
434 result = sqlite3_close(plugin->dbh);
435 } 428 }
429 result = sqlite3_close (plugin->dbh);
430 }
436#endif 431#endif
437 if (SQLITE_OK != result) 432 if (SQLITE_OK != result)
438 LOG_SQLITE (plugin, NULL, 433 LOG_SQLITE (plugin, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_close");
439 GNUNET_ERROR_TYPE_ERROR,
440 "sqlite3_close");
441 434
442 GNUNET_free_non_null (plugin->fn); 435 GNUNET_free_non_null (plugin->fn);
443} 436}
@@ -451,34 +444,34 @@ database_shutdown (struct Plugin *plugin)
451 * @param rid the ID of the row to delete 444 * @param rid the ID of the row to delete
452 */ 445 */
453static int 446static int
454delete_by_rowid (struct Plugin* plugin, 447delete_by_rowid (struct Plugin *plugin, unsigned long long rid)
455 unsigned long long rid)
456{ 448{
457 if (SQLITE_OK != 449 if (SQLITE_OK != sqlite3_bind_int64 (plugin->delRow, 1, rid))
458 sqlite3_bind_int64 (plugin->delRow, 1, rid)) 450 {
459 { 451 LOG_SQLITE (plugin, NULL,
452 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
453 "sqlite3_bind_XXXX");
454 if (SQLITE_OK != sqlite3_reset (plugin->delRow))
460 LOG_SQLITE (plugin, NULL, 455 LOG_SQLITE (plugin, NULL,
461 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX"); 456 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
462 if (SQLITE_OK != sqlite3_reset (plugin->delRow)) 457 "sqlite3_reset");
463 LOG_SQLITE (plugin, NULL, 458 return GNUNET_SYSERR;
464 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_reset"); 459 }
465 return GNUNET_SYSERR;
466 }
467 if (SQLITE_DONE != sqlite3_step (plugin->delRow)) 460 if (SQLITE_DONE != sqlite3_step (plugin->delRow))
468 { 461 {
462 LOG_SQLITE (plugin, NULL,
463 GNUNET_ERROR_TYPE_ERROR |
464 GNUNET_ERROR_TYPE_BULK, "sqlite3_step");
465 if (SQLITE_OK != sqlite3_reset (plugin->delRow))
469 LOG_SQLITE (plugin, NULL, 466 LOG_SQLITE (plugin, NULL,
470 GNUNET_ERROR_TYPE_ERROR | 467 GNUNET_ERROR_TYPE_ERROR |
471 GNUNET_ERROR_TYPE_BULK, "sqlite3_step"); 468 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
472 if (SQLITE_OK != sqlite3_reset (plugin->delRow)) 469 return GNUNET_SYSERR;
473 LOG_SQLITE (plugin, NULL, 470 }
474 GNUNET_ERROR_TYPE_ERROR |
475 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
476 return GNUNET_SYSERR;
477 }
478 if (SQLITE_OK != sqlite3_reset (plugin->delRow)) 471 if (SQLITE_OK != sqlite3_reset (plugin->delRow))
479 LOG_SQLITE (plugin, NULL, 472 LOG_SQLITE (plugin, NULL,
480 GNUNET_ERROR_TYPE_ERROR | 473 GNUNET_ERROR_TYPE_ERROR |
481 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset"); 474 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
482 return GNUNET_OK; 475 return GNUNET_OK;
483} 476}
484 477
@@ -500,15 +493,14 @@ delete_by_rowid (struct Plugin* plugin,
500 */ 493 */
501static int 494static int
502sqlite_plugin_put (void *cls, 495sqlite_plugin_put (void *cls,
503 const GNUNET_HashCode *key, 496 const GNUNET_HashCode * key,
504 uint32_t size, 497 uint32_t size,
505 const void *data, 498 const void *data,
506 enum GNUNET_BLOCK_Type type, 499 enum GNUNET_BLOCK_Type type,
507 uint32_t priority, 500 uint32_t priority,
508 uint32_t anonymity, 501 uint32_t anonymity,
509 uint32_t replication, 502 uint32_t replication,
510 struct GNUNET_TIME_Absolute expiration, 503 struct GNUNET_TIME_Absolute expiration, char **msg)
511 char ** msg)
512{ 504{
513 struct Plugin *plugin = cls; 505 struct Plugin *plugin = cls;
514 int n; 506 int n;
@@ -521,13 +513,14 @@ sqlite_plugin_put (void *cls,
521 return GNUNET_SYSERR; 513 return GNUNET_SYSERR;
522#if DEBUG_SQLITE 514#if DEBUG_SQLITE
523 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 515 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
524 "sqlite", 516 "sqlite",
525 "Storing in database block with type %u/key `%s'/priority %u/expiration in %llu ms (%lld).\n", 517 "Storing in database block with type %u/key `%s'/priority %u/expiration in %llu ms (%lld).\n",
526 type, 518 type,
527 GNUNET_h2s(key), 519 GNUNET_h2s (key),
528 priority, 520 priority,
529 (unsigned long long) GNUNET_TIME_absolute_get_remaining (expiration).rel_value, 521 (unsigned long long)
530 (long long) expiration.abs_value); 522 GNUNET_TIME_absolute_get_remaining (expiration).rel_value,
523 (long long) expiration.abs_value);
531#endif 524#endif
532 GNUNET_CRYPTO_hash (data, size, &vhash); 525 GNUNET_CRYPTO_hash (data, size, &vhash);
533 stmt = plugin->insertContent; 526 stmt = plugin->insertContent;
@@ -545,55 +538,54 @@ sqlite_plugin_put (void *cls,
545 sqlite3_bind_blob (stmt, 8, &vhash, sizeof (GNUNET_HashCode), 538 sqlite3_bind_blob (stmt, 8, &vhash, sizeof (GNUNET_HashCode),
546 SQLITE_TRANSIENT)) 539 SQLITE_TRANSIENT))
547 || (SQLITE_OK != 540 || (SQLITE_OK !=
548 sqlite3_bind_blob (stmt, 9, data, size, 541 sqlite3_bind_blob (stmt, 9, data, size, SQLITE_TRANSIENT)))
549 SQLITE_TRANSIENT))) 542 {
550 { 543 LOG_SQLITE (plugin,
551 LOG_SQLITE (plugin, 544 msg,
552 msg, 545 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
553 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX"); 546 "sqlite3_bind_XXXX");
554 if (SQLITE_OK != sqlite3_reset (stmt)) 547 if (SQLITE_OK != sqlite3_reset (stmt))
555 LOG_SQLITE (plugin, NULL, 548 LOG_SQLITE (plugin, NULL,
556 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_reset"); 549 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
557 return GNUNET_SYSERR; 550 "sqlite3_reset");
558 } 551 return GNUNET_SYSERR;
552 }
559 n = sqlite3_step (stmt); 553 n = sqlite3_step (stmt);
560 switch (n) 554 switch (n)
561 { 555 {
562 case SQLITE_DONE: 556 case SQLITE_DONE:
563 plugin->env->duc (plugin->env->cls, 557 plugin->env->duc (plugin->env->cls, size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
564 size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
565#if DEBUG_SQLITE 558#if DEBUG_SQLITE
566 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 559 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
567 "sqlite", 560 "sqlite",
568 "Stored new entry (%u bytes)\n", 561 "Stored new entry (%u bytes)\n",
569 size + GNUNET_DATASTORE_ENTRY_OVERHEAD); 562 size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
570#endif 563#endif
571 ret = GNUNET_OK; 564 ret = GNUNET_OK;
572 break; 565 break;
573 case SQLITE_BUSY: 566 case SQLITE_BUSY:
574 GNUNET_break (0); 567 GNUNET_break (0);
575 LOG_SQLITE (plugin, msg, 568 LOG_SQLITE (plugin, msg,
576 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 569 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
577 "sqlite3_step"); 570 "sqlite3_step");
578 ret = GNUNET_SYSERR; 571 ret = GNUNET_SYSERR;
579 break; 572 break;
580 default: 573 default:
581 LOG_SQLITE (plugin, msg, 574 LOG_SQLITE (plugin, msg,
582 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 575 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
583 "sqlite3_step"); 576 "sqlite3_step");
584 if (SQLITE_OK != sqlite3_reset (stmt)) 577 if (SQLITE_OK != sqlite3_reset (stmt))
585 LOG_SQLITE (plugin, NULL, 578 LOG_SQLITE (plugin, NULL,
586 GNUNET_ERROR_TYPE_ERROR | 579 GNUNET_ERROR_TYPE_ERROR |
587 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset"); 580 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
588 database_shutdown (plugin); 581 database_shutdown (plugin);
589 database_setup (plugin->env->cfg, 582 database_setup (plugin->env->cfg, plugin);
590 plugin); 583 return GNUNET_SYSERR;
591 return GNUNET_SYSERR; 584 }
592 }
593 if (SQLITE_OK != sqlite3_reset (stmt)) 585 if (SQLITE_OK != sqlite3_reset (stmt))
594 LOG_SQLITE (plugin, NULL, 586 LOG_SQLITE (plugin, NULL,
595 GNUNET_ERROR_TYPE_ERROR | 587 GNUNET_ERROR_TYPE_ERROR |
596 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset"); 588 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
597 return ret; 589 return ret;
598} 590}
599 591
@@ -623,52 +615,51 @@ sqlite_plugin_put (void *cls,
623 */ 615 */
624static int 616static int
625sqlite_plugin_update (void *cls, 617sqlite_plugin_update (void *cls,
626 uint64_t uid, 618 uint64_t uid,
627 int delta, struct GNUNET_TIME_Absolute expire, 619 int delta, struct GNUNET_TIME_Absolute expire, char **msg)
628 char **msg)
629{ 620{
630 struct Plugin *plugin = cls; 621 struct Plugin *plugin = cls;
631 int n; 622 int n;
632 623
633 if ( (SQLITE_OK != 624 if ((SQLITE_OK !=
634 sqlite3_bind_int (plugin->updPrio, 1, delta)) || 625 sqlite3_bind_int (plugin->updPrio, 1, delta)) ||
635 (SQLITE_OK != 626 (SQLITE_OK !=
636 sqlite3_bind_int64 (plugin->updPrio, 2, expire.abs_value)) || 627 sqlite3_bind_int64 (plugin->updPrio, 2, expire.abs_value)) ||
637 (SQLITE_OK != 628 (SQLITE_OK != sqlite3_bind_int64 (plugin->updPrio, 3, uid)))
638 sqlite3_bind_int64 (plugin->updPrio, 3, uid)) ) 629 {
639 { 630 LOG_SQLITE (plugin, msg,
640 LOG_SQLITE (plugin, msg, 631 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
641 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX"); 632 "sqlite3_bind_XXXX");
642 if (SQLITE_OK != sqlite3_reset (plugin->updPrio)) 633 if (SQLITE_OK != sqlite3_reset (plugin->updPrio))
643 LOG_SQLITE (plugin, NULL, 634 LOG_SQLITE (plugin, NULL,
644 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_reset"); 635 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
645 return GNUNET_SYSERR; 636 "sqlite3_reset");
637 return GNUNET_SYSERR;
646 638
647 } 639 }
648 n = sqlite3_step (plugin->updPrio); 640 n = sqlite3_step (plugin->updPrio);
649 if (SQLITE_OK != sqlite3_reset (plugin->updPrio)) 641 if (SQLITE_OK != sqlite3_reset (plugin->updPrio))
650 LOG_SQLITE (plugin, NULL, 642 LOG_SQLITE (plugin, NULL,
651 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_reset"); 643 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
652 switch (n) 644 "sqlite3_reset");
653 { 645 switch (n)
654 case SQLITE_DONE: 646 {
647 case SQLITE_DONE:
655#if DEBUG_SQLITE 648#if DEBUG_SQLITE
656 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 649 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", "Block updated\n");
657 "sqlite",
658 "Block updated\n");
659#endif 650#endif
660 return GNUNET_OK; 651 return GNUNET_OK;
661 case SQLITE_BUSY: 652 case SQLITE_BUSY:
662 LOG_SQLITE (plugin, msg, 653 LOG_SQLITE (plugin, msg,
663 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, 654 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
664 "sqlite3_step"); 655 "sqlite3_step");
665 return GNUNET_NO; 656 return GNUNET_NO;
666 default: 657 default:
667 LOG_SQLITE (plugin, msg, 658 LOG_SQLITE (plugin, msg,
668 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 659 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
669 "sqlite3_step"); 660 "sqlite3_step");
670 return GNUNET_SYSERR; 661 return GNUNET_SYSERR;
671 } 662 }
672} 663}
673 664
674 665
@@ -683,8 +674,7 @@ sqlite_plugin_update (void *cls,
683 */ 674 */
684static void 675static void
685execute_get (struct Plugin *plugin, 676execute_get (struct Plugin *plugin,
686 sqlite3_stmt *stmt, 677 sqlite3_stmt * stmt, PluginDatumProcessor proc, void *proc_cls)
687 PluginDatumProcessor proc, void *proc_cls)
688{ 678{
689 int n; 679 int n;
690 struct GNUNET_TIME_Absolute expiration; 680 struct GNUNET_TIME_Absolute expiration;
@@ -694,80 +684,74 @@ execute_get (struct Plugin *plugin,
694 684
695 n = sqlite3_step (stmt); 685 n = sqlite3_step (stmt);
696 switch (n) 686 switch (n)
687 {
688 case SQLITE_ROW:
689 size = sqlite3_column_bytes (stmt, 5);
690 rowid = sqlite3_column_int64 (stmt, 6);
691 if (sqlite3_column_bytes (stmt, 4) != sizeof (GNUNET_HashCode))
697 { 692 {
698 case SQLITE_ROW: 693 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
699 size = sqlite3_column_bytes (stmt, 5); 694 "sqlite",
700 rowid = sqlite3_column_int64 (stmt, 6); 695 _
701 if (sqlite3_column_bytes (stmt, 4) != sizeof (GNUNET_HashCode)) 696 ("Invalid data in database. Trying to fix (by deletion).\n"));
702 {
703 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
704 "sqlite",
705 _("Invalid data in database. Trying to fix (by deletion).\n"));
706 if (SQLITE_OK != sqlite3_reset (stmt))
707 LOG_SQLITE (plugin, NULL,
708 GNUNET_ERROR_TYPE_ERROR |
709 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
710 if (GNUNET_OK == delete_by_rowid (plugin, rowid))
711 plugin->env->duc (plugin->env->cls,
712 - (size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
713 break;
714 }
715 expiration.abs_value = sqlite3_column_int64 (stmt, 3);
716#if DEBUG_SQLITE
717 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
718 "sqlite",
719 "Found reply in database with expiration %llu\n",
720 (unsigned long long) expiration.abs_value);
721#endif
722 ret = proc (proc_cls,
723 sqlite3_column_blob (stmt, 4) /* key */,
724 size,
725 sqlite3_column_blob (stmt, 5) /* data */,
726 sqlite3_column_int (stmt, 0) /* type */,
727 sqlite3_column_int (stmt, 1) /* priority */,
728 sqlite3_column_int (stmt, 2) /* anonymity */,
729 expiration,
730 rowid);
731 if (SQLITE_OK != sqlite3_reset (stmt))
732 LOG_SQLITE (plugin, NULL,
733 GNUNET_ERROR_TYPE_ERROR |
734 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
735 if ( (GNUNET_NO == ret) &&
736 (GNUNET_OK == delete_by_rowid (plugin, rowid)) )
737 plugin->env->duc (plugin->env->cls,
738 - (size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
739 return;
740 case SQLITE_DONE:
741 /* database must be empty */
742 if (SQLITE_OK != sqlite3_reset (stmt))
743 LOG_SQLITE (plugin, NULL,
744 GNUNET_ERROR_TYPE_ERROR |
745 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
746 break;
747 case SQLITE_BUSY:
748 case SQLITE_ERROR:
749 case SQLITE_MISUSE:
750 default:
751 LOG_SQLITE (plugin, NULL,
752 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
753 "sqlite3_step");
754 if (SQLITE_OK != sqlite3_reset (stmt)) 697 if (SQLITE_OK != sqlite3_reset (stmt))
755 LOG_SQLITE (plugin, NULL, 698 LOG_SQLITE (plugin, NULL,
756 GNUNET_ERROR_TYPE_ERROR | 699 GNUNET_ERROR_TYPE_ERROR |
757 GNUNET_ERROR_TYPE_BULK, 700 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
758 "sqlite3_reset"); 701 if (GNUNET_OK == delete_by_rowid (plugin, rowid))
759 GNUNET_break (0); 702 plugin->env->duc (plugin->env->cls,
760 database_shutdown (plugin); 703 -(size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
761 database_setup (plugin->env->cfg,
762 plugin);
763 break; 704 break;
764 } 705 }
706 expiration.abs_value = sqlite3_column_int64 (stmt, 3);
707#if DEBUG_SQLITE
708 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
709 "sqlite",
710 "Found reply in database with expiration %llu\n",
711 (unsigned long long) expiration.abs_value);
712#endif
713 ret = proc (proc_cls, sqlite3_column_blob (stmt, 4) /* key */ ,
714 size, sqlite3_column_blob (stmt, 5) /* data */ ,
715 sqlite3_column_int (stmt, 0) /* type */ ,
716 sqlite3_column_int (stmt, 1) /* priority */ ,
717 sqlite3_column_int (stmt, 2) /* anonymity */ ,
718 expiration, rowid);
719 if (SQLITE_OK != sqlite3_reset (stmt))
720 LOG_SQLITE (plugin, NULL,
721 GNUNET_ERROR_TYPE_ERROR |
722 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
723 if ((GNUNET_NO == ret) && (GNUNET_OK == delete_by_rowid (plugin, rowid)))
724 plugin->env->duc (plugin->env->cls,
725 -(size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
726 return;
727 case SQLITE_DONE:
728 /* database must be empty */
729 if (SQLITE_OK != sqlite3_reset (stmt))
730 LOG_SQLITE (plugin, NULL,
731 GNUNET_ERROR_TYPE_ERROR |
732 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
733 break;
734 case SQLITE_BUSY:
735 case SQLITE_ERROR:
736 case SQLITE_MISUSE:
737 default:
738 LOG_SQLITE (plugin, NULL,
739 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
740 "sqlite3_step");
741 if (SQLITE_OK != sqlite3_reset (stmt))
742 LOG_SQLITE (plugin, NULL,
743 GNUNET_ERROR_TYPE_ERROR |
744 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
745 GNUNET_break (0);
746 database_shutdown (plugin);
747 database_setup (plugin->env->cfg, plugin);
748 break;
749 }
765 if (SQLITE_OK != sqlite3_reset (stmt)) 750 if (SQLITE_OK != sqlite3_reset (stmt))
766 LOG_SQLITE (plugin, NULL, 751 LOG_SQLITE (plugin, NULL,
767 GNUNET_ERROR_TYPE_ERROR | 752 GNUNET_ERROR_TYPE_ERROR |
768 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset"); 753 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
769 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 754 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
770 GNUNET_TIME_UNIT_ZERO_ABS, 0);
771} 755}
772 756
773 757
@@ -787,31 +771,28 @@ execute_get (struct Plugin *plugin,
787 */ 771 */
788static void 772static void
789sqlite_plugin_get_zero_anonymity (void *cls, 773sqlite_plugin_get_zero_anonymity (void *cls,
790 uint64_t offset, 774 uint64_t offset,
791 enum GNUNET_BLOCK_Type type, 775 enum GNUNET_BLOCK_Type type,
792 PluginDatumProcessor proc, 776 PluginDatumProcessor proc, void *proc_cls)
793 void *proc_cls)
794{ 777{
795 struct Plugin *plugin = cls; 778 struct Plugin *plugin = cls;
796 sqlite3_stmt *stmt; 779 sqlite3_stmt *stmt;
797 780
798 GNUNET_assert (type != GNUNET_BLOCK_TYPE_ANY); 781 GNUNET_assert (type != GNUNET_BLOCK_TYPE_ANY);
799 stmt = plugin->selZeroAnon; 782 stmt = plugin->selZeroAnon;
800 if ( (SQLITE_OK != sqlite3_bind_int (stmt, 1, type)) || 783 if ((SQLITE_OK != sqlite3_bind_int (stmt, 1, type)) ||
801 (SQLITE_OK != sqlite3_bind_int64 (stmt, 2, offset)) ) 784 (SQLITE_OK != sqlite3_bind_int64 (stmt, 2, offset)))
802 { 785 {
786 LOG_SQLITE (plugin, NULL,
787 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
788 "sqlite3_bind_XXXX");
789 if (SQLITE_OK != sqlite3_reset (stmt))
803 LOG_SQLITE (plugin, NULL, 790 LOG_SQLITE (plugin, NULL,
804 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 791 GNUNET_ERROR_TYPE_ERROR |
805 "sqlite3_bind_XXXX"); 792 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
806 if (SQLITE_OK != sqlite3_reset (stmt)) 793 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
807 LOG_SQLITE (plugin, NULL, 794 return;
808 GNUNET_ERROR_TYPE_ERROR | 795 }
809 GNUNET_ERROR_TYPE_BULK,
810 "sqlite3_reset");
811 proc (proc_cls, NULL, 0, NULL, 0, 0, 0,
812 GNUNET_TIME_UNIT_ZERO_ABS, 0);
813 return;
814 }
815 execute_get (plugin, stmt, proc, proc_cls); 796 execute_get (plugin, stmt, proc, proc_cls);
816} 797}
817 798
@@ -836,11 +817,11 @@ sqlite_plugin_get_zero_anonymity (void *cls,
836 */ 817 */
837static void 818static void
838sqlite_plugin_get_key (void *cls, 819sqlite_plugin_get_key (void *cls,
839 uint64_t offset, 820 uint64_t offset,
840 const GNUNET_HashCode *key, 821 const GNUNET_HashCode * key,
841 const GNUNET_HashCode *vhash, 822 const GNUNET_HashCode * vhash,
842 enum GNUNET_BLOCK_Type type, 823 enum GNUNET_BLOCK_Type type,
843 PluginDatumProcessor proc, void *proc_cls) 824 PluginDatumProcessor proc, void *proc_cls)
844{ 825{
845 struct Plugin *plugin = cls; 826 struct Plugin *plugin = cls;
846 int ret; 827 int ret;
@@ -855,48 +836,47 @@ sqlite_plugin_get_key (void *cls,
855 GNUNET_snprintf (scratch, sizeof (scratch), 836 GNUNET_snprintf (scratch, sizeof (scratch),
856 "SELECT count(*) FROM gn090 WHERE hash=?%s%s", 837 "SELECT count(*) FROM gn090 WHERE hash=?%s%s",
857 vhash == NULL ? "" : " AND vhash=?", 838 vhash == NULL ? "" : " AND vhash=?",
858 type == 0 ? "" : " AND type=?"); 839 type == 0 ? "" : " AND type=?");
859 if (sq_prepare (plugin->dbh, scratch, &stmt) != SQLITE_OK) 840 if (sq_prepare (plugin->dbh, scratch, &stmt) != SQLITE_OK)
860 { 841 {
861 LOG_SQLITE (plugin, NULL, 842 LOG_SQLITE (plugin, NULL,
862 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite_prepare"); 843 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
863 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); 844 "sqlite_prepare");
864 return; 845 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
865 } 846 return;
847 }
866 sqoff = 1; 848 sqoff = 1;
867 ret = sqlite3_bind_blob (stmt, sqoff++, 849 ret = sqlite3_bind_blob (stmt, sqoff++,
868 key, sizeof (GNUNET_HashCode), SQLITE_TRANSIENT); 850 key, sizeof (GNUNET_HashCode), SQLITE_TRANSIENT);
869 if ((vhash != NULL) && (ret == SQLITE_OK)) 851 if ((vhash != NULL) && (ret == SQLITE_OK))
870 ret = sqlite3_bind_blob (stmt, sqoff++, 852 ret = sqlite3_bind_blob (stmt, sqoff++,
871 vhash, 853 vhash, sizeof (GNUNET_HashCode), SQLITE_TRANSIENT);
872 sizeof (GNUNET_HashCode), SQLITE_TRANSIENT);
873 if ((type != 0) && (ret == SQLITE_OK)) 854 if ((type != 0) && (ret == SQLITE_OK))
874 ret = sqlite3_bind_int (stmt, sqoff++, type); 855 ret = sqlite3_bind_int (stmt, sqoff++, type);
875 if (SQLITE_OK != ret) 856 if (SQLITE_OK != ret)
876 { 857 {
877 LOG_SQLITE (plugin, NULL, 858 LOG_SQLITE (plugin, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite_bind");
878 GNUNET_ERROR_TYPE_ERROR, "sqlite_bind"); 859 sqlite3_finalize (stmt);
879 sqlite3_finalize (stmt); 860 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
880 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); 861 return;
881 return; 862 }
882 }
883 ret = sqlite3_step (stmt); 863 ret = sqlite3_step (stmt);
884 if (ret != SQLITE_ROW) 864 if (ret != SQLITE_ROW)
885 { 865 {
886 LOG_SQLITE (plugin, NULL, 866 LOG_SQLITE (plugin, NULL,
887 GNUNET_ERROR_TYPE_ERROR| GNUNET_ERROR_TYPE_BULK, 867 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
888 "sqlite_step"); 868 "sqlite_step");
889 sqlite3_finalize (stmt); 869 sqlite3_finalize (stmt);
890 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); 870 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
891 return; 871 return;
892 } 872 }
893 total = sqlite3_column_int (stmt, 0); 873 total = sqlite3_column_int (stmt, 0);
894 sqlite3_finalize (stmt); 874 sqlite3_finalize (stmt);
895 if (0 == total) 875 if (0 == total)
896 { 876 {
897 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); 877 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
898 return; 878 return;
899 } 879 }
900 limit_off = (int) (offset % total); 880 limit_off = (int) (offset % total);
901 if (limit_off < 0) 881 if (limit_off < 0)
902 limit_off += total; 882 limit_off += total;
@@ -907,36 +887,33 @@ sqlite_plugin_get_key (void *cls,
907 vhash == NULL ? "" : " AND vhash=?", 887 vhash == NULL ? "" : " AND vhash=?",
908 type == 0 ? "" : " AND type=?"); 888 type == 0 ? "" : " AND type=?");
909 if (sq_prepare (plugin->dbh, scratch, &stmt) != SQLITE_OK) 889 if (sq_prepare (plugin->dbh, scratch, &stmt) != SQLITE_OK)
910 { 890 {
911 LOG_SQLITE (plugin, NULL, 891 LOG_SQLITE (plugin, NULL,
912 GNUNET_ERROR_TYPE_ERROR | 892 GNUNET_ERROR_TYPE_ERROR |
913 GNUNET_ERROR_TYPE_BULK, "sqlite_prepare"); 893 GNUNET_ERROR_TYPE_BULK, "sqlite_prepare");
914 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); 894 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
915 return; 895 return;
916 } 896 }
917 sqoff = 1; 897 sqoff = 1;
918 ret = sqlite3_bind_blob (stmt, 898 ret = sqlite3_bind_blob (stmt,
919 sqoff++, 899 sqoff++,
920 key, 900 key, sizeof (GNUNET_HashCode), SQLITE_TRANSIENT);
921 sizeof (GNUNET_HashCode),
922 SQLITE_TRANSIENT);
923 if ((vhash != NULL) && (ret == SQLITE_OK)) 901 if ((vhash != NULL) && (ret == SQLITE_OK))
924 ret = sqlite3_bind_blob (stmt, 902 ret = sqlite3_bind_blob (stmt,
925 sqoff++, 903 sqoff++,
926 vhash, 904 vhash, sizeof (GNUNET_HashCode), SQLITE_TRANSIENT);
927 sizeof (GNUNET_HashCode), SQLITE_TRANSIENT);
928 if ((type != 0) && (ret == SQLITE_OK)) 905 if ((type != 0) && (ret == SQLITE_OK))
929 ret = sqlite3_bind_int (stmt, sqoff++, type); 906 ret = sqlite3_bind_int (stmt, sqoff++, type);
930 if (ret == SQLITE_OK) 907 if (ret == SQLITE_OK)
931 ret = sqlite3_bind_int64 (stmt, sqoff++, limit_off); 908 ret = sqlite3_bind_int64 (stmt, sqoff++, limit_off);
932 if (ret != SQLITE_OK) 909 if (ret != SQLITE_OK)
933 { 910 {
934 LOG_SQLITE (plugin, NULL, 911 LOG_SQLITE (plugin, NULL,
935 GNUNET_ERROR_TYPE_ERROR | 912 GNUNET_ERROR_TYPE_ERROR |
936 GNUNET_ERROR_TYPE_BULK, "sqlite_bind"); 913 GNUNET_ERROR_TYPE_BULK, "sqlite_bind");
937 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); 914 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
938 return; 915 return;
939 } 916 }
940 execute_get (plugin, stmt, proc, proc_cls); 917 execute_get (plugin, stmt, proc, proc_cls);
941 sqlite3_finalize (stmt); 918 sqlite3_finalize (stmt);
942} 919}
@@ -948,12 +925,12 @@ sqlite_plugin_get_key (void *cls,
948 */ 925 */
949struct ReplCtx 926struct ReplCtx
950{ 927{
951 928
952 /** 929 /**
953 * Function to call for the result (or the NULL). 930 * Function to call for the result (or the NULL).
954 */ 931 */
955 PluginDatumProcessor proc; 932 PluginDatumProcessor proc;
956 933
957 /** 934 /**
958 * Closure for proc. 935 * Closure for proc.
959 */ 936 */
@@ -992,28 +969,24 @@ struct ReplCtx
992 */ 969 */
993static int 970static int
994repl_proc (void *cls, 971repl_proc (void *cls,
995 const GNUNET_HashCode *key, 972 const GNUNET_HashCode * key,
996 uint32_t size, 973 uint32_t size,
997 const void *data, 974 const void *data,
998 enum GNUNET_BLOCK_Type type, 975 enum GNUNET_BLOCK_Type type,
999 uint32_t priority, 976 uint32_t priority,
1000 uint32_t anonymity, 977 uint32_t anonymity,
1001 struct GNUNET_TIME_Absolute expiration, 978 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
1002 uint64_t uid)
1003{ 979{
1004 struct ReplCtx *rc = cls; 980 struct ReplCtx *rc = cls;
1005 int ret; 981 int ret;
1006 982
1007 ret = rc->proc (rc->proc_cls, 983 ret = rc->proc (rc->proc_cls,
1008 key, 984 key, size, data, type, priority, anonymity, expiration, uid);
1009 size, data,
1010 type, priority, anonymity, expiration,
1011 uid);
1012 if (key != NULL) 985 if (key != NULL)
1013 { 986 {
1014 rc->uid = uid; 987 rc->uid = uid;
1015 rc->have_uid = GNUNET_YES; 988 rc->have_uid = GNUNET_YES;
1016 } 989 }
1017 return ret; 990 return ret;
1018} 991}
1019 992
@@ -1030,7 +1003,7 @@ repl_proc (void *cls,
1030 */ 1003 */
1031static void 1004static void
1032sqlite_plugin_get_replication (void *cls, 1005sqlite_plugin_get_replication (void *cls,
1033 PluginDatumProcessor proc, void *proc_cls) 1006 PluginDatumProcessor proc, void *proc_cls)
1034{ 1007{
1035 struct Plugin *plugin = cls; 1008 struct Plugin *plugin = cls;
1036 struct ReplCtx rc; 1009 struct ReplCtx rc;
@@ -1040,76 +1013,77 @@ sqlite_plugin_get_replication (void *cls,
1040 1013
1041#if DEBUG_SQLITE 1014#if DEBUG_SQLITE
1042 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 1015 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1043 "sqlite", 1016 "sqlite",
1044 "Getting random block based on replication order.\n"); 1017 "Getting random block based on replication order.\n");
1045#endif 1018#endif
1046 rc.have_uid = GNUNET_NO; 1019 rc.have_uid = GNUNET_NO;
1047 rc.proc = proc; 1020 rc.proc = proc;
1048 rc.proc_cls = proc_cls; 1021 rc.proc_cls = proc_cls;
1049 stmt = plugin->maxRepl; 1022 stmt = plugin->maxRepl;
1050 if (SQLITE_ROW != sqlite3_step (stmt)) 1023 if (SQLITE_ROW != sqlite3_step (stmt))
1051 { 1024 {
1052 if (SQLITE_OK != sqlite3_reset (stmt)) 1025 if (SQLITE_OK != sqlite3_reset (stmt))
1053 LOG_SQLITE (plugin, NULL, 1026 LOG_SQLITE (plugin, NULL,
1054 GNUNET_ERROR_TYPE_ERROR | 1027 GNUNET_ERROR_TYPE_ERROR |
1055 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset"); 1028 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
1056 /* DB empty */ 1029 /* DB empty */
1057 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 1030 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1058 GNUNET_TIME_UNIT_ZERO_ABS, 0); 1031 return;
1059 return; 1032 }
1060 }
1061 repl = sqlite3_column_int (stmt, 0); 1033 repl = sqlite3_column_int (stmt, 0);
1062 if (SQLITE_OK != sqlite3_reset (stmt)) 1034 if (SQLITE_OK != sqlite3_reset (stmt))
1063 LOG_SQLITE (plugin, NULL, 1035 LOG_SQLITE (plugin, NULL,
1064 GNUNET_ERROR_TYPE_ERROR | 1036 GNUNET_ERROR_TYPE_ERROR |
1065 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset"); 1037 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
1066 stmt = plugin->selRepl; 1038 stmt = plugin->selRepl;
1067 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX); 1039 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
1068 if (SQLITE_OK != sqlite3_bind_int64 (stmt, 1, rvalue)) 1040 if (SQLITE_OK != sqlite3_bind_int64 (stmt, 1, rvalue))
1069 { 1041 {
1070 LOG_SQLITE (plugin, NULL, 1042 LOG_SQLITE (plugin, NULL,
1071 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX"); 1043 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1072 if (SQLITE_OK != sqlite3_reset (stmt)) 1044 "sqlite3_bind_XXXX");
1073 LOG_SQLITE (plugin, NULL, 1045 if (SQLITE_OK != sqlite3_reset (stmt))
1074 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_reset"); 1046 LOG_SQLITE (plugin, NULL,
1075 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 1047 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1076 GNUNET_TIME_UNIT_ZERO_ABS, 0); 1048 "sqlite3_reset");
1077 return; 1049 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1078 } 1050 return;
1051 }
1079 if (SQLITE_OK != sqlite3_bind_int (stmt, 2, repl)) 1052 if (SQLITE_OK != sqlite3_bind_int (stmt, 2, repl))
1080 { 1053 {
1081 LOG_SQLITE (plugin, NULL, 1054 LOG_SQLITE (plugin, NULL,
1082 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX"); 1055 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1083 if (SQLITE_OK != sqlite3_reset (stmt)) 1056 "sqlite3_bind_XXXX");
1084 LOG_SQLITE (plugin, NULL, 1057 if (SQLITE_OK != sqlite3_reset (stmt))
1085 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_reset"); 1058 LOG_SQLITE (plugin, NULL,
1086 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 1059 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1087 GNUNET_TIME_UNIT_ZERO_ABS, 0); 1060 "sqlite3_reset");
1088 return; 1061 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1089 } 1062 return;
1090 execute_get (plugin, stmt, &repl_proc, &rc); 1063 }
1064 execute_get (plugin, stmt, &repl_proc, &rc);
1091 if (GNUNET_YES == rc.have_uid) 1065 if (GNUNET_YES == rc.have_uid)
1066 {
1067 if (SQLITE_OK != sqlite3_bind_int64 (plugin->updRepl, 1, rc.uid))
1092 { 1068 {
1093 if (SQLITE_OK != 1069 LOG_SQLITE (plugin, NULL,
1094 sqlite3_bind_int64 (plugin->updRepl, 1, rc.uid)) 1070 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1095 { 1071 "sqlite3_bind_XXXX");
1096 LOG_SQLITE (plugin, NULL,
1097 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX");
1098 if (SQLITE_OK != sqlite3_reset (plugin->updRepl))
1099 LOG_SQLITE (plugin, NULL,
1100 GNUNET_ERROR_TYPE_ERROR |
1101 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
1102 return;
1103 }
1104 if (SQLITE_DONE != sqlite3_step (plugin->updRepl))
1105 LOG_SQLITE (plugin, NULL,
1106 GNUNET_ERROR_TYPE_ERROR |
1107 GNUNET_ERROR_TYPE_BULK, "sqlite3_step");
1108 if (SQLITE_OK != sqlite3_reset (plugin->updRepl)) 1072 if (SQLITE_OK != sqlite3_reset (plugin->updRepl))
1109 LOG_SQLITE (plugin, NULL, 1073 LOG_SQLITE (plugin, NULL,
1110 GNUNET_ERROR_TYPE_ERROR | 1074 GNUNET_ERROR_TYPE_ERROR |
1111 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset"); 1075 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
1076 return;
1112 } 1077 }
1078 if (SQLITE_DONE != sqlite3_step (plugin->updRepl))
1079 LOG_SQLITE (plugin, NULL,
1080 GNUNET_ERROR_TYPE_ERROR |
1081 GNUNET_ERROR_TYPE_BULK, "sqlite3_step");
1082 if (SQLITE_OK != sqlite3_reset (plugin->updRepl))
1083 LOG_SQLITE (plugin, NULL,
1084 GNUNET_ERROR_TYPE_ERROR |
1085 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
1086 }
1113} 1087}
1114 1088
1115 1089
@@ -1124,7 +1098,7 @@ sqlite_plugin_get_replication (void *cls,
1124 */ 1098 */
1125static void 1099static void
1126sqlite_plugin_get_expiration (void *cls, 1100sqlite_plugin_get_expiration (void *cls,
1127 PluginDatumProcessor proc, void *proc_cls) 1101 PluginDatumProcessor proc, void *proc_cls)
1128{ 1102{
1129 struct Plugin *plugin = cls; 1103 struct Plugin *plugin = cls;
1130 sqlite3_stmt *stmt; 1104 sqlite3_stmt *stmt;
@@ -1132,22 +1106,23 @@ sqlite_plugin_get_expiration (void *cls,
1132 1106
1133#if DEBUG_SQLITE 1107#if DEBUG_SQLITE
1134 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 1108 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1135 "sqlite", 1109 "sqlite",
1136 "Getting random block based on expiration and priority order.\n"); 1110 "Getting random block based on expiration and priority order.\n");
1137#endif 1111#endif
1138 now = GNUNET_TIME_absolute_get (); 1112 now = GNUNET_TIME_absolute_get ();
1139 stmt = plugin->selExpi; 1113 stmt = plugin->selExpi;
1140 if (SQLITE_OK != sqlite3_bind_int64 (stmt, 1, now.abs_value)) 1114 if (SQLITE_OK != sqlite3_bind_int64 (stmt, 1, now.abs_value))
1141 { 1115 {
1142 LOG_SQLITE (plugin, NULL, 1116 LOG_SQLITE (plugin, NULL,
1143 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX"); 1117 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1144 if (SQLITE_OK != sqlite3_reset (stmt)) 1118 "sqlite3_bind_XXXX");
1145 LOG_SQLITE (plugin, NULL, 1119 if (SQLITE_OK != sqlite3_reset (stmt))
1146 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_reset"); 1120 LOG_SQLITE (plugin, NULL,
1147 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 1121 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1148 GNUNET_TIME_UNIT_ZERO_ABS, 0); 1122 "sqlite3_reset");
1149 return; 1123 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1150 } 1124 return;
1125 }
1151 execute_get (plugin, stmt, proc, proc_cls); 1126 execute_get (plugin, stmt, proc, proc_cls);
1152} 1127}
1153 1128
@@ -1157,10 +1132,11 @@ sqlite_plugin_get_expiration (void *cls,
1157 * 1132 *
1158 * @param cls our plugin context 1133 * @param cls our plugin context
1159 */ 1134 */
1160static void 1135static void
1161sqlite_plugin_drop (void *cls) 1136sqlite_plugin_drop (void *cls)
1162{ 1137{
1163 struct Plugin *plugin = cls; 1138 struct Plugin *plugin = cls;
1139
1164 plugin->drop_on_shutdown = GNUNET_YES; 1140 plugin->drop_on_shutdown = GNUNET_YES;
1165} 1141}
1166 1142
@@ -1179,48 +1155,40 @@ sqlite_plugin_estimate_size (void *cls)
1179 sqlite3_stmt *stmt; 1155 sqlite3_stmt *stmt;
1180 uint64_t pages; 1156 uint64_t pages;
1181 uint64_t page_size; 1157 uint64_t page_size;
1158
1182#if ENULL_DEFINED 1159#if ENULL_DEFINED
1183 char *e; 1160 char *e;
1184#endif 1161#endif
1185 1162
1186 if (SQLITE_VERSION_NUMBER < 3006000) 1163 if (SQLITE_VERSION_NUMBER < 3006000)
1187 { 1164 {
1188 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, 1165 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1189 "datastore-sqlite", 1166 "datastore-sqlite",
1190 _("sqlite version to old to determine size, assuming zero\n")); 1167 _
1191 return 0; 1168 ("sqlite version to old to determine size, assuming zero\n"));
1192 } 1169 return 0;
1193 CHECK (SQLITE_OK == 1170 }
1194 sqlite3_exec (plugin->dbh, 1171 CHECK (SQLITE_OK == sqlite3_exec (plugin->dbh, "VACUUM", NULL, NULL, ENULL));
1195 "VACUUM", NULL, NULL, ENULL));
1196 CHECK (SQLITE_OK ==
1197 sqlite3_exec (plugin->dbh,
1198 "PRAGMA auto_vacuum=INCREMENTAL", NULL, NULL, ENULL));
1199 CHECK (SQLITE_OK == 1172 CHECK (SQLITE_OK ==
1200 sq_prepare (plugin->dbh, 1173 sqlite3_exec (plugin->dbh,
1201 "PRAGMA page_count", 1174 "PRAGMA auto_vacuum=INCREMENTAL", NULL, NULL, ENULL));
1202 &stmt)); 1175 CHECK (SQLITE_OK == sq_prepare (plugin->dbh, "PRAGMA page_count", &stmt));
1203 if (SQLITE_ROW == 1176 if (SQLITE_ROW == sqlite3_step (stmt))
1204 sqlite3_step (stmt))
1205 pages = sqlite3_column_int64 (stmt, 0); 1177 pages = sqlite3_column_int64 (stmt, 0);
1206 else 1178 else
1207 pages = 0; 1179 pages = 0;
1208 sqlite3_finalize (stmt); 1180 sqlite3_finalize (stmt);
1209 CHECK (SQLITE_OK == 1181 CHECK (SQLITE_OK == sq_prepare (plugin->dbh, "PRAGMA page_size", &stmt));
1210 sq_prepare (plugin->dbh, 1182 CHECK (SQLITE_ROW == sqlite3_step (stmt));
1211 "PRAGMA page_size",
1212 &stmt));
1213 CHECK (SQLITE_ROW ==
1214 sqlite3_step (stmt));
1215 page_size = sqlite3_column_int64 (stmt, 0); 1183 page_size = sqlite3_column_int64 (stmt, 0);
1216 sqlite3_finalize (stmt); 1184 sqlite3_finalize (stmt);
1217 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1185 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1218 _("Using sqlite page utilization to estimate payload (%llu pages of size %llu bytes)\n"), 1186 _
1219 (unsigned long long) pages, 1187 ("Using sqlite page utilization to estimate payload (%llu pages of size %llu bytes)\n"),
1220 (unsigned long long) page_size); 1188 (unsigned long long) pages, (unsigned long long) page_size);
1221 return pages * page_size; 1189 return pages * page_size;
1222} 1190}
1223 1191
1224 1192
1225/** 1193/**
1226 * Entry point for the plugin. 1194 * Entry point for the plugin.
@@ -1236,15 +1204,14 @@ libgnunet_plugin_datastore_sqlite_init (void *cls)
1236 struct GNUNET_DATASTORE_PluginFunctions *api; 1204 struct GNUNET_DATASTORE_PluginFunctions *api;
1237 1205
1238 if (plugin.env != NULL) 1206 if (plugin.env != NULL)
1239 return NULL; /* can only initialize once! */ 1207 return NULL; /* can only initialize once! */
1240 memset (&plugin, 0, sizeof(struct Plugin)); 1208 memset (&plugin, 0, sizeof (struct Plugin));
1241 plugin.env = env; 1209 plugin.env = env;
1242 if (GNUNET_OK != 1210 if (GNUNET_OK != database_setup (env->cfg, &plugin))
1243 database_setup (env->cfg, &plugin)) 1211 {
1244 { 1212 database_shutdown (&plugin);
1245 database_shutdown (&plugin); 1213 return NULL;
1246 return NULL; 1214 }
1247 }
1248 api = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_PluginFunctions)); 1215 api = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_PluginFunctions));
1249 api->cls = &plugin; 1216 api->cls = &plugin;
1250 api->estimate_size = &sqlite_plugin_estimate_size; 1217 api->estimate_size = &sqlite_plugin_estimate_size;
@@ -1276,8 +1243,7 @@ libgnunet_plugin_datastore_sqlite_done (void *cls)
1276 1243
1277#if DEBUG_SQLITE 1244#if DEBUG_SQLITE
1278 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 1245 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1279 "sqlite", 1246 "sqlite", "sqlite plugin is done\n");
1280 "sqlite plugin is done\n");
1281#endif 1247#endif
1282 1248
1283 fn = NULL; 1249 fn = NULL;
@@ -1285,24 +1251,20 @@ libgnunet_plugin_datastore_sqlite_done (void *cls)
1285 fn = GNUNET_strdup (plugin->fn); 1251 fn = GNUNET_strdup (plugin->fn);
1286#if DEBUG_SQLITE 1252#if DEBUG_SQLITE
1287 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 1253 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1288 "sqlite", 1254 "sqlite", "Shutting down database\n");
1289 "Shutting down database\n");
1290#endif 1255#endif
1291 database_shutdown (plugin); 1256 database_shutdown (plugin);
1292 plugin->env = NULL; 1257 plugin->env = NULL;
1293 GNUNET_free (api); 1258 GNUNET_free (api);
1294 if (fn != NULL) 1259 if (fn != NULL)
1295 { 1260 {
1296 if (0 != UNLINK(fn)) 1261 if (0 != UNLINK (fn))
1297 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, 1262 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
1298 "unlink", 1263 GNUNET_free (fn);
1299 fn); 1264 }
1300 GNUNET_free (fn);
1301 }
1302#if DEBUG_SQLITE 1265#if DEBUG_SQLITE
1303 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 1266 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1304 "sqlite", 1267 "sqlite", "sqlite plugin is finished\n");
1305 "sqlite plugin is finished\n");
1306#endif 1268#endif
1307 return NULL; 1269 return NULL;
1308} 1270}