2 This file is part of GNUnet
3 (C) 2009, 2011 Christian Grothoff (and other contributing authors)
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
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
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
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file datastore/plugin_datastore_sqlite.c
23 * @brief sqlite-based datastore backend
24 * @author Christian Grothoff
28 #include "gnunet_datastore_plugin.h"
31 #define DEBUG_SQLITE GNUNET_NO
35 * Log an error message at log-level 'level' that indicates
36 * a failure of the command 'cmd' on file 'filename'
37 * with the message given by strerror(errno).
39 #define LOG_SQLITE(db, msg, level, cmd) do { GNUNET_log_from (level, "sqlite", _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, sqlite3_errmsg(db->dbh)); if (msg != NULL) GNUNET_asprintf(msg, _("`%s' failed at %s:%u with error: %s"), cmd, __FILE__, __LINE__, sqlite3_errmsg(db->dbh)); } while(0)
41 #define SELECT_IT_LOW_PRIORITY_1 \
42 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090 WHERE (prio = ? AND hash > ?) "\
43 "ORDER BY hash ASC LIMIT 1"
45 #define SELECT_IT_LOW_PRIORITY_2 \
46 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090 WHERE (prio > ?) "\
47 "ORDER BY prio ASC, hash ASC LIMIT 1"
49 #define SELECT_IT_NON_ANONYMOUS_1 \
50 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090 WHERE (prio = ? AND hash < ? AND anonLevel = 0 AND expire > %llu) "\
51 " ORDER BY hash DESC LIMIT 1"
53 #define SELECT_IT_NON_ANONYMOUS_2 \
54 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090 WHERE (prio < ? AND anonLevel = 0 AND expire > %llu)"\
55 " ORDER BY prio DESC, hash DESC LIMIT 1"
57 #define SELECT_IT_EXPIRATION_TIME_1 \
58 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090 WHERE (expire = ? AND hash > ?) "\
59 " ORDER BY hash ASC LIMIT 1"
61 #define SELECT_IT_EXPIRATION_TIME_2 \
62 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090 WHERE (expire > ?) "\
63 " ORDER BY expire ASC, hash ASC LIMIT 1"
65 #define SELECT_IT_MIGRATION_ORDER_1 \
66 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090 WHERE (expire = ? AND hash < ?) "\
67 " ORDER BY hash DESC LIMIT 1"
69 #define SELECT_IT_MIGRATION_ORDER_2 \
70 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090 WHERE (expire < ? AND expire > %llu) "\
71 " ORDER BY expire DESC, hash DESC LIMIT 1"
74 #define SELECT_IT_REPLICATION_ORDER \
75 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090 WHERE (expire > ?) "\
76 " ORDER BY repl DESC, Random() LIMIT 1"
80 * After how many ms "busy" should a DB operation fail for good?
81 * A low value makes sure that we are more responsive to requests
82 * (especially PUTs). A high value guarantees a higher success
83 * rate (SELECTs in iterate can take several seconds despite LIMIT=1).
85 * The default value of 250ms should ensure that users do not experience
86 * huge latencies while at the same time allowing operations to succeed
87 * with reasonable probability.
89 #define BUSY_TIMEOUT_MS 250
94 * Context for all functions in this plugin.
99 * Our execution environment.
101 struct GNUNET_DATASTORE_PluginEnvironment *env;
109 * Native SQLite database handle.
114 * Precompiled SQL for deletion.
116 sqlite3_stmt *delRow;
119 * Precompiled SQL for update.
121 sqlite3_stmt *updPrio;
124 * Precompiled SQL for replication decrement.
126 sqlite3_stmt *updRepl;
129 * Precompiled SQL for replication decrement.
131 sqlite3_stmt *selRepl;
134 * Precompiled SQL for insertion.
136 sqlite3_stmt *insertContent;
139 * Closure of the 'next_task' (must be freed if 'next_task' is cancelled).
141 struct NextContext *next_task_nc;
144 * Pending task with scheduler for running the next request.
146 GNUNET_SCHEDULER_TaskIdentifier next_task;
149 * Should the database be dropped on shutdown?
151 int drop_on_shutdown;
157 * @brief Prepare a SQL statement
159 * @param dbh handle to the database
160 * @param zSql SQL statement, UTF-8 encoded
161 * @param ppStmt set to the prepared statement
162 * @return 0 on success
165 sq_prepare (sqlite3 * dbh, const char *zSql,
166 sqlite3_stmt ** ppStmt)
170 result = sqlite3_prepare_v2 (dbh,
172 strlen (zSql), ppStmt, (const char **) &dummy);
174 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
176 "Prepared %p: %d\n", *ppStmt, result);
183 * Create our database indices.
185 * @param dbh handle to the database
188 create_indices (sqlite3 * dbh)
192 "CREATE INDEX idx_hash ON gn090 (hash)", NULL, NULL, NULL);
194 "CREATE INDEX idx_hash_vhash ON gn090 (hash,vhash)", NULL,
196 sqlite3_exec (dbh, "CREATE INDEX idx_prio ON gn090 (prio)", NULL, NULL,
198 sqlite3_exec (dbh, "CREATE INDEX idx_expire ON gn090 (expire)", NULL, NULL,
200 sqlite3_exec (dbh, "CREATE INDEX idx_comb3 ON gn090 (prio,anonLevel)", NULL,
202 sqlite3_exec (dbh, "CREATE INDEX idx_comb4 ON gn090 (prio,hash,anonLevel)",
204 sqlite3_exec (dbh, "CREATE INDEX idx_comb7 ON gn090 (expire,hash)", NULL,
206 sqlite3_exec (dbh, "CREATE INDEX idx_comb8 ON gn090 (expire)", NULL,
213 #define CHECK(a) GNUNET_break(a)
217 #define ENULL_DEFINED 1
218 #define CHECK(a) if (! a) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "%s\n", e); sqlite3_free(e); }
225 * Initialize the database connections and associated
226 * data structures (create tables and indices
227 * as needed as well).
229 * @param cfg our configuration
230 * @param plugin the plugin context (state for this module)
231 * @return GNUNET_OK on success
234 database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg,
235 struct Plugin *plugin)
244 GNUNET_CONFIGURATION_get_value_filename (cfg,
249 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
251 _("Option `%s' in section `%s' missing in configuration!\n"),
254 return GNUNET_SYSERR;
256 if (GNUNET_OK != GNUNET_DISK_file_test (afsdir))
258 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (afsdir))
261 GNUNET_free (afsdir);
262 return GNUNET_SYSERR;
264 /* database is new or got deleted, reset payload to zero! */
265 plugin->env->duc (plugin->env->cls, 0);
268 plugin->fn = GNUNET_STRINGS_to_utf8 (afsdir, strlen (afsdir),
269 nl_langinfo (CODESET));
271 plugin->fn = GNUNET_STRINGS_to_utf8 (afsdir, strlen (afsdir),
272 "UTF-8"); /* good luck */
274 GNUNET_free (afsdir);
276 /* Open database and precompile statements */
277 if (sqlite3_open (plugin->fn, &plugin->dbh) != SQLITE_OK)
279 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
281 _("Unable to initialize SQLite: %s.\n"),
282 sqlite3_errmsg (plugin->dbh));
283 return GNUNET_SYSERR;
286 sqlite3_exec (plugin->dbh,
287 "PRAGMA temp_store=MEMORY", NULL, NULL, ENULL));
289 sqlite3_exec (plugin->dbh,
290 "PRAGMA synchronous=OFF", NULL, NULL, ENULL));
292 sqlite3_exec (plugin->dbh,
293 "PRAGMA auto_vacuum=INCREMENTAL", NULL, NULL, ENULL));
295 sqlite3_exec (plugin->dbh,
296 "PRAGMA count_changes=OFF", NULL, NULL, ENULL));
298 sqlite3_exec (plugin->dbh,
299 "PRAGMA page_size=4092", NULL, NULL, ENULL));
301 CHECK (SQLITE_OK == sqlite3_busy_timeout (plugin->dbh, BUSY_TIMEOUT_MS));
304 /* We have to do it here, because otherwise precompiling SQL might fail */
306 sq_prepare (plugin->dbh,
307 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'gn090'",
309 if ( (sqlite3_step (stmt) == SQLITE_DONE) &&
310 (sqlite3_exec (plugin->dbh,
311 "CREATE TABLE gn090 ("
312 " repl INT4 NOT NULL DEFAULT 0,"
313 " type INT4 NOT NULL DEFAULT 0,"
314 " prio INT4 NOT NULL DEFAULT 0,"
315 " anonLevel INT4 NOT NULL DEFAULT 0,"
316 " expire INT8 NOT NULL DEFAULT 0,"
317 " hash TEXT NOT NULL DEFAULT '',"
318 " vhash TEXT NOT NULL DEFAULT '',"
319 " value BLOB NOT NULL DEFAULT '')", NULL, NULL,
320 NULL) != SQLITE_OK) )
322 LOG_SQLITE (plugin, NULL,
323 GNUNET_ERROR_TYPE_ERROR,
325 sqlite3_finalize (stmt);
326 return GNUNET_SYSERR;
328 sqlite3_finalize (stmt);
329 create_indices (plugin->dbh);
332 sq_prepare (plugin->dbh,
333 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'gn071'",
335 if ( (sqlite3_step (stmt) == SQLITE_DONE) &&
336 (sqlite3_exec (plugin->dbh,
337 "CREATE TABLE gn071 ("
338 " key TEXT NOT NULL DEFAULT '',"
339 " value INTEGER NOT NULL DEFAULT 0)", NULL, NULL,
340 NULL) != SQLITE_OK) )
342 LOG_SQLITE (plugin, NULL,
343 GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec");
344 sqlite3_finalize (stmt);
345 return GNUNET_SYSERR;
347 sqlite3_finalize (stmt);
349 if ((sq_prepare (plugin->dbh,
350 "UPDATE gn090 SET prio = prio + ?, expire = MAX(expire,?) WHERE "
352 &plugin->updPrio) != SQLITE_OK) ||
353 (sq_prepare (plugin->dbh,
354 "UPDATE gn090 SET repl = MAX (0, repl - 1) WHERE "
356 &plugin->updRepl) != SQLITE_OK) ||
357 (sq_prepare (plugin->dbh,
358 SELECT_IT_REPLICATION_ORDER,
359 &plugin->selRepl) != SQLITE_OK) ||
360 (sq_prepare (plugin->dbh,
361 "INSERT INTO gn090 (repl, type, prio, "
362 "anonLevel, expire, hash, vhash, value) VALUES "
363 "(?, ?, ?, ?, ?, ?, ?, ?)",
364 &plugin->insertContent) != SQLITE_OK) ||
365 (sq_prepare (plugin->dbh,
366 "DELETE FROM gn090 WHERE _ROWID_ = ?",
367 &plugin->delRow) != SQLITE_OK))
369 LOG_SQLITE (plugin, NULL,
370 GNUNET_ERROR_TYPE_ERROR, "precompiling");
371 return GNUNET_SYSERR;
379 * Shutdown database connection and associate data
381 * @param plugin the plugin context (state for this module)
384 database_shutdown (struct Plugin *plugin)
387 #if SQLITE_VERSION_NUMBER >= 3007000
391 if (plugin->delRow != NULL)
392 sqlite3_finalize (plugin->delRow);
393 if (plugin->updPrio != NULL)
394 sqlite3_finalize (plugin->updPrio);
395 if (plugin->updRepl != NULL)
396 sqlite3_finalize (plugin->updRepl);
397 if (plugin->selRepl != NULL)
398 sqlite3_finalize (plugin->selRepl);
399 if (plugin->insertContent != NULL)
400 sqlite3_finalize (plugin->insertContent);
401 result = sqlite3_close(plugin->dbh);
402 #if SQLITE_VERSION_NUMBER >= 3007000
403 if (result == SQLITE_BUSY)
405 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
407 _("Tried to close sqlite without finalizing all prepared statements.\n"));
408 stmt = sqlite3_next_stmt(plugin->dbh, NULL);
412 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
413 "sqlite", "Closing statement %p\n", stmt);
415 result = sqlite3_finalize(stmt);
417 if (result != SQLITE_OK)
418 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
420 "Failed to close statement %p: %d\n", stmt, result);
422 stmt = sqlite3_next_stmt(plugin->dbh, NULL);
424 result = sqlite3_close(plugin->dbh);
427 if (SQLITE_OK != result)
428 LOG_SQLITE (plugin, NULL,
429 GNUNET_ERROR_TYPE_ERROR,
432 GNUNET_free_non_null (plugin->fn);
437 * Delete the database entry with the given
440 * @param plugin the plugin context (state for this module)
441 * @param rid the ID of the row to delete
444 delete_by_rowid (struct Plugin* plugin,
445 unsigned long long rid)
447 sqlite3_bind_int64 (plugin->delRow, 1, rid);
448 if (SQLITE_DONE != sqlite3_step (plugin->delRow))
450 LOG_SQLITE (plugin, NULL,
451 GNUNET_ERROR_TYPE_ERROR |
452 GNUNET_ERROR_TYPE_BULK, "sqlite3_step");
453 if (SQLITE_OK != sqlite3_reset (plugin->delRow))
454 LOG_SQLITE (plugin, NULL,
455 GNUNET_ERROR_TYPE_ERROR |
456 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
457 return GNUNET_SYSERR;
459 if (SQLITE_OK != sqlite3_reset (plugin->delRow))
460 LOG_SQLITE (plugin, NULL,
461 GNUNET_ERROR_TYPE_ERROR |
462 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
468 * Context for the universal iterator.
473 * Type of a function that will prepare
474 * the next iteration.
477 * @param nc the next context; NULL for the last
478 * call which gives the callback a chance to
479 * clean up the closure
480 * @return GNUNET_OK on success, GNUNET_NO if there are
481 * no more values, GNUNET_SYSERR on error
483 typedef int (*PrepareFunction)(void *cls,
484 struct NextContext *nc);
488 * Context we keep for the "next request" callback.
495 struct Plugin *plugin;
498 * Function to call on the next value.
508 * Function to call to prepare the next
511 PrepareFunction prep;
519 * Statement that the iterator will get the data
520 * from (updated or set by prep).
525 * Row ID of the last result.
527 unsigned long long last_rowid;
530 * Key of the last result.
532 GNUNET_HashCode lastKey;
535 * Expiration time of the last value visited.
537 struct GNUNET_TIME_Absolute lastExpiration;
540 * Priority of the last value visited.
542 unsigned int lastPriority;
545 * Number of results processed so far.
550 * Set to GNUNET_YES if we must stop now.
557 * Continuation of "sqlite_next_request".
559 * @param cls the next context
560 * @param tc the task context (unused)
563 sqlite_next_request_cont (void *cls,
564 const struct GNUNET_SCHEDULER_TaskContext *tc)
566 struct NextContext * nc = cls;
567 struct Plugin *plugin;
568 unsigned long long rowid;
573 unsigned int priority;
574 unsigned int anonymity;
575 struct GNUNET_TIME_Absolute expiration;
576 const GNUNET_HashCode *key;
580 plugin->next_task = GNUNET_SCHEDULER_NO_TASK;
581 plugin->next_task_nc = NULL;
582 if ( (GNUNET_YES == nc->end_it) ||
583 (GNUNET_OK != (nc->prep(nc->prep_cls,
587 nc->iter (nc->iter_cls,
588 NULL, NULL, 0, NULL, 0, 0, 0,
589 GNUNET_TIME_UNIT_ZERO_ABS, 0);
590 nc->prep (nc->prep_cls, NULL);
595 rowid = sqlite3_column_int64 (nc->stmt, 6);
596 nc->last_rowid = rowid;
597 type = sqlite3_column_int (nc->stmt, 0);
598 size = sqlite3_column_bytes (nc->stmt, 5);
599 if (sqlite3_column_bytes (nc->stmt, 4) != sizeof (GNUNET_HashCode))
601 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
603 _("Invalid data in database. Trying to fix (by deletion).\n"));
604 if (SQLITE_OK != sqlite3_reset (nc->stmt))
605 LOG_SQLITE (nc->plugin, NULL,
606 GNUNET_ERROR_TYPE_ERROR |
607 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
610 "DELETE FROM gn090 WHERE NOT LENGTH(hash) = ?",
611 &stmtd) != SQLITE_OK)
613 LOG_SQLITE (nc->plugin, NULL,
614 GNUNET_ERROR_TYPE_ERROR |
615 GNUNET_ERROR_TYPE_BULK,
620 if (SQLITE_OK != sqlite3_bind_int (stmtd, 1, sizeof (GNUNET_HashCode)))
621 LOG_SQLITE (nc->plugin, NULL,
622 GNUNET_ERROR_TYPE_ERROR |
623 GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_int");
624 if (SQLITE_DONE != sqlite3_step (stmtd))
625 LOG_SQLITE (nc->plugin, NULL,
626 GNUNET_ERROR_TYPE_ERROR |
627 GNUNET_ERROR_TYPE_BULK, "sqlite3_step");
628 if (SQLITE_OK != sqlite3_finalize (stmtd))
629 LOG_SQLITE (nc->plugin, NULL,
630 GNUNET_ERROR_TYPE_ERROR |
631 GNUNET_ERROR_TYPE_BULK, "sqlite3_finalize");
635 priority = sqlite3_column_int (nc->stmt, 1);
636 anonymity = sqlite3_column_int (nc->stmt, 2);
637 expiration.abs_value = sqlite3_column_int64 (nc->stmt, 3);
638 key = sqlite3_column_blob (nc->stmt, 4);
639 nc->lastPriority = priority;
640 nc->lastExpiration = expiration;
641 memcpy (&nc->lastKey, key, sizeof(GNUNET_HashCode));
642 data = sqlite3_column_blob (nc->stmt, 5);
644 ret = nc->iter (nc->iter_cls,
654 if (ret == GNUNET_SYSERR)
656 nc->end_it = GNUNET_YES;
660 if (ret == GNUNET_NO)
661 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
663 "Asked to remove entry %llu (%u bytes)\n",
664 (unsigned long long) rowid,
665 size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
667 if ( (ret == GNUNET_NO) &&
668 (GNUNET_OK == delete_by_rowid (plugin, rowid)) )
670 plugin->env->duc (plugin->env->cls,
671 - (size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
673 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
675 "Removed entry %llu (%u bytes)\n",
676 (unsigned long long) rowid,
677 size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
684 * Function invoked on behalf of a "PluginIterator"
685 * asking the database plugin to call the iterator
686 * with the next item.
688 * @param next_cls whatever argument was given
689 * to the PluginIterator as "next_cls".
690 * @param end_it set to GNUNET_YES if we
691 * should terminate the iteration early
692 * (iterator should be still called once more
693 * to signal the end of the iteration).
696 sqlite_next_request (void *next_cls,
699 struct NextContext * nc= next_cls;
701 if (GNUNET_YES == end_it)
702 nc->end_it = GNUNET_YES;
703 nc->plugin->next_task_nc = nc;
704 nc->plugin->next_task = GNUNET_SCHEDULER_add_now (&sqlite_next_request_cont,
710 * Store an item in the datastore.
713 * @param key key for the item
714 * @param size number of bytes in data
715 * @param data content stored
716 * @param type type of the content
717 * @param priority priority of the content
718 * @param anonymity anonymity-level for the content
719 * @param replication replication-level for the content
720 * @param expiration expiration time for the content
721 * @param msg set to an error message
722 * @return GNUNET_OK on success
725 sqlite_plugin_put (void *cls,
726 const GNUNET_HashCode * key,
729 enum GNUNET_BLOCK_Type type,
732 uint32_t replication,
733 struct GNUNET_TIME_Absolute expiration,
736 struct Plugin *plugin = cls;
739 GNUNET_HashCode vhash;
742 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
744 "Storing in database block with type %u/key `%s'/priority %u/expiration %llu (%lld).\n",
748 (unsigned long long) GNUNET_TIME_absolute_get_remaining (expiration).rel_value,
749 (long long) expiration.abs_value);
751 GNUNET_CRYPTO_hash (data, size, &vhash);
752 stmt = plugin->insertContent;
753 if ((SQLITE_OK != sqlite3_bind_int (stmt, 1, replication)) ||
754 (SQLITE_OK != sqlite3_bind_int (stmt, 2, type)) ||
755 (SQLITE_OK != sqlite3_bind_int (stmt, 3, priority)) ||
756 (SQLITE_OK != sqlite3_bind_int (stmt, 4, anonymity)) ||
757 (SQLITE_OK != sqlite3_bind_int64 (stmt, 5, expiration.abs_value)) ||
759 sqlite3_bind_blob (stmt, 6, key, sizeof (GNUNET_HashCode),
760 SQLITE_TRANSIENT)) ||
762 sqlite3_bind_blob (stmt, 7, &vhash, sizeof (GNUNET_HashCode),
765 sqlite3_bind_blob (stmt, 8, data, size,
770 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX");
771 if (SQLITE_OK != sqlite3_reset (stmt))
772 LOG_SQLITE (plugin, NULL,
773 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
774 return GNUNET_SYSERR;
776 n = sqlite3_step (stmt);
777 if (n != SQLITE_DONE)
779 if (n == SQLITE_BUSY)
781 LOG_SQLITE (plugin, msg,
782 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_step");
783 sqlite3_reset (stmt);
787 LOG_SQLITE (plugin, msg,
788 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_step");
789 sqlite3_reset (stmt);
790 database_shutdown (plugin);
791 database_setup (plugin->env->cfg,
793 return GNUNET_SYSERR;
795 if (SQLITE_OK != sqlite3_reset (stmt))
796 LOG_SQLITE (plugin, NULL,
797 GNUNET_ERROR_TYPE_ERROR |
798 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
799 plugin->env->duc (plugin->env->cls,
800 size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
802 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
804 "Stored new entry (%u bytes)\n",
805 size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
812 * Update the priority for a particular key in the datastore. If
813 * the expiration time in value is different than the time found in
814 * the datastore, the higher value should be kept. For the
815 * anonymity level, the lower value is to be used. The specified
816 * priority should be added to the existing priority, ignoring the
819 * Note that it is possible for multiple values to match this put.
820 * In that case, all of the respective values are updated.
822 * @param cls the plugin context (state for this module)
823 * @param uid unique identifier of the datum
824 * @param delta by how much should the priority
825 * change? If priority + delta < 0 the
826 * priority should be set to 0 (never go
828 * @param expire new expiration time should be the
829 * MAX of any existing expiration time and
831 * @param msg set to an error message
832 * @return GNUNET_OK on success
835 sqlite_plugin_update (void *cls,
837 int delta, struct GNUNET_TIME_Absolute expire,
840 struct Plugin *plugin = cls;
843 sqlite3_bind_int (plugin->updPrio, 1, delta);
844 sqlite3_bind_int64 (plugin->updPrio, 2, expire.abs_value);
845 sqlite3_bind_int64 (plugin->updPrio, 3, uid);
846 n = sqlite3_step (plugin->updPrio);
847 if (n != SQLITE_DONE)
848 LOG_SQLITE (plugin, msg,
849 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
853 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
857 sqlite3_reset (plugin->updPrio);
859 if (n == SQLITE_BUSY)
861 return n == SQLITE_DONE ? GNUNET_OK : GNUNET_SYSERR;
866 * Internal context for an iteration.
873 sqlite3_stmt *stmt_1;
878 sqlite3_stmt *stmt_2;
898 int limit_nonanonymous;
901 * Desired type for blocks returned by this iterator.
903 enum GNUNET_BLOCK_Type type;
908 * Prepare our SQL query to obtain the next record from the database.
910 * @param cls our "struct IterContext"
911 * @param nc NULL to terminate the iteration, otherwise our context for
912 * getting the next result.
913 * @return GNUNET_OK on success, GNUNET_NO if there are no more results,
914 * GNUNET_SYSERR on error (or end of iteration)
917 iter_next_prepare (void *cls,
918 struct NextContext *nc)
920 struct IterContext *ic = cls;
921 struct Plugin *plugin;
927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
928 "Asked to clean up iterator state.\n");
930 sqlite3_finalize (ic->stmt_1);
931 sqlite3_finalize (ic->stmt_2);
932 return GNUNET_SYSERR;
934 sqlite3_reset (ic->stmt_1);
935 sqlite3_reset (ic->stmt_2);
940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
941 "Restricting to results larger than the last priority %u\n",
944 sqlite3_bind_int (ic->stmt_1, 1, nc->lastPriority);
945 sqlite3_bind_int (ic->stmt_2, 1, nc->lastPriority);
950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
951 "Restricting to results larger than the last expiration %llu\n",
952 (unsigned long long) nc->lastExpiration.abs_value);
954 sqlite3_bind_int64 (ic->stmt_1, 1, nc->lastExpiration.abs_value);
955 sqlite3_bind_int64 (ic->stmt_2, 1, nc->lastExpiration.abs_value);
958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
959 "Restricting to results larger than the last key `%s'\n",
960 GNUNET_h2s(&nc->lastKey));
962 sqlite3_bind_blob (ic->stmt_1, 2,
964 sizeof (GNUNET_HashCode),
966 if (SQLITE_ROW == (ret = sqlite3_step (ic->stmt_1)))
969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
970 "Result found using iterator 1\n");
972 nc->stmt = ic->stmt_1;
975 if (ret != SQLITE_DONE)
977 LOG_SQLITE (plugin, NULL,
978 GNUNET_ERROR_TYPE_ERROR |
979 GNUNET_ERROR_TYPE_BULK,
981 return GNUNET_SYSERR;
983 if (SQLITE_OK != sqlite3_reset (ic->stmt_1))
984 LOG_SQLITE (plugin, NULL,
985 GNUNET_ERROR_TYPE_ERROR |
986 GNUNET_ERROR_TYPE_BULK,
988 if (SQLITE_ROW == (ret = sqlite3_step (ic->stmt_2)))
991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
992 "Result found using iterator 2\n");
994 nc->stmt = ic->stmt_2;
997 if (ret != SQLITE_DONE)
999 LOG_SQLITE (plugin, NULL,
1000 GNUNET_ERROR_TYPE_ERROR |
1001 GNUNET_ERROR_TYPE_BULK,
1003 return GNUNET_SYSERR;
1005 if (SQLITE_OK != sqlite3_reset (ic->stmt_2))
1006 LOG_SQLITE (plugin, NULL,
1007 GNUNET_ERROR_TYPE_ERROR |
1008 GNUNET_ERROR_TYPE_BULK,
1011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1012 "No result found using either iterator\n");
1019 * Call a method for each key in the database and
1020 * call the callback method on it.
1022 * @param plugin our plugin context
1023 * @param type entries of which type should be considered?
1024 * @param is_asc are we iterating in ascending order?
1025 * @param is_prio are we iterating by priority (otherwise by expiration)
1026 * @param is_migr are we iterating in migration order?
1027 * @param limit_nonanonymous are we restricting results to those with anonymity
1029 * @param stmt_str_1 first SQL statement to execute
1030 * @param stmt_str_2 SQL statement to execute to get "more" results (inner iteration)
1031 * @param iter function to call on each matching value;
1032 * will be called once with a NULL value at the end
1033 * @param iter_cls closure for iter
1036 basic_iter (struct Plugin *plugin,
1037 enum GNUNET_BLOCK_Type type,
1041 int limit_nonanonymous,
1042 const char *stmt_str_1,
1043 const char *stmt_str_2,
1044 PluginIterator iter,
1047 struct NextContext *nc;
1048 struct IterContext *ic;
1049 sqlite3_stmt *stmt_1;
1050 sqlite3_stmt *stmt_2;
1053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1054 "At %llu, using queries `%s' and `%s'\n",
1055 (unsigned long long) GNUNET_TIME_absolute_get ().abs_value,
1059 if (sq_prepare (plugin->dbh, stmt_str_1, &stmt_1) != SQLITE_OK)
1061 LOG_SQLITE (plugin, NULL,
1062 GNUNET_ERROR_TYPE_ERROR |
1063 GNUNET_ERROR_TYPE_BULK, "sqlite3_prepare_v2");
1064 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1067 if (sq_prepare (plugin->dbh, stmt_str_2, &stmt_2) != SQLITE_OK)
1069 LOG_SQLITE (plugin, NULL,
1070 GNUNET_ERROR_TYPE_ERROR |
1071 GNUNET_ERROR_TYPE_BULK, "sqlite3_prepare_v2");
1072 sqlite3_finalize (stmt_1);
1073 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1076 nc = GNUNET_malloc (sizeof(struct NextContext) +
1077 sizeof(struct IterContext));
1078 nc->plugin = plugin;
1080 nc->iter_cls = iter_cls;
1082 ic = (struct IterContext*) &nc[1];
1083 ic->stmt_1 = stmt_1;
1084 ic->stmt_2 = stmt_2;
1086 ic->is_asc = is_asc;
1087 ic->is_prio = is_prio;
1088 ic->is_migr = is_migr;
1089 ic->limit_nonanonymous = limit_nonanonymous;
1090 nc->prep = &iter_next_prepare;
1094 nc->lastPriority = 0;
1095 nc->lastExpiration.abs_value = 0;
1096 memset (&nc->lastKey, 0, sizeof (GNUNET_HashCode));
1100 nc->lastPriority = 0x7FFFFFFF;
1101 nc->lastExpiration.abs_value = 0x7FFFFFFFFFFFFFFFLL;
1102 memset (&nc->lastKey, 255, sizeof (GNUNET_HashCode));
1104 sqlite_next_request (nc, GNUNET_NO);
1109 * Select a subset of the items in the datastore and call
1110 * the given iterator for each of them.
1112 * @param cls our plugin context
1113 * @param type entries of which type should be considered?
1114 * Use 0 for any type.
1115 * @param iter function to call on each matching value;
1116 * will be called once with a NULL value at the end
1117 * @param iter_cls closure for iter
1120 sqlite_plugin_iter_low_priority (void *cls,
1121 enum GNUNET_BLOCK_Type type,
1122 PluginIterator iter,
1127 GNUNET_YES, GNUNET_YES,
1128 GNUNET_NO, GNUNET_NO,
1129 SELECT_IT_LOW_PRIORITY_1,
1130 SELECT_IT_LOW_PRIORITY_2,
1136 * Select a subset of the items in the datastore and call
1137 * the given iterator for each of them.
1139 * @param cls our plugin context
1140 * @param type entries of which type should be considered?
1141 * Use 0 for any type.
1142 * @param iter function to call on each matching value;
1143 * will be called once with a NULL value at the end
1144 * @param iter_cls closure for iter
1147 sqlite_plugin_iter_zero_anonymity (void *cls,
1148 enum GNUNET_BLOCK_Type type,
1149 PluginIterator iter,
1152 struct GNUNET_TIME_Absolute now;
1156 now = GNUNET_TIME_absolute_get ();
1157 GNUNET_asprintf (&q1, SELECT_IT_NON_ANONYMOUS_1,
1158 (unsigned long long) now.abs_value);
1159 GNUNET_asprintf (&q2, SELECT_IT_NON_ANONYMOUS_2,
1160 (unsigned long long) now.abs_value);
1163 GNUNET_NO, GNUNET_YES,
1164 GNUNET_NO, GNUNET_YES,
1175 * Select a subset of the items in the datastore and call
1176 * the given iterator for each of them.
1178 * @param cls our plugin context
1179 * @param type entries of which type should be considered?
1180 * Use 0 for any type.
1181 * @param iter function to call on each matching value;
1182 * will be called once with a NULL value at the end
1183 * @param iter_cls closure for iter
1186 sqlite_plugin_iter_ascending_expiration (void *cls,
1187 enum GNUNET_BLOCK_Type type,
1188 PluginIterator iter,
1191 struct GNUNET_TIME_Absolute now;
1195 now = GNUNET_TIME_absolute_get ();
1196 GNUNET_asprintf (&q1, SELECT_IT_EXPIRATION_TIME_1,
1197 (unsigned long long) 0*now.abs_value);
1198 GNUNET_asprintf (&q2, SELECT_IT_EXPIRATION_TIME_2,
1199 (unsigned long long) 0*now.abs_value);
1202 GNUNET_YES, GNUNET_NO,
1203 GNUNET_NO, GNUNET_NO,
1212 * Select a subset of the items in the datastore and call
1213 * the given iterator for each of them.
1215 * @param cls our plugin context
1216 * @param type entries of which type should be considered?
1217 * Use 0 for any type.
1218 * @param iter function to call on each matching value;
1219 * will be called once with a NULL value at the end
1220 * @param iter_cls closure for iter
1223 sqlite_plugin_iter_migration_order (void *cls,
1224 enum GNUNET_BLOCK_Type type,
1225 PluginIterator iter,
1228 struct GNUNET_TIME_Absolute now;
1231 now = GNUNET_TIME_absolute_get ();
1232 GNUNET_asprintf (&q, SELECT_IT_MIGRATION_ORDER_2,
1233 (unsigned long long) now.abs_value);
1236 GNUNET_NO, GNUNET_NO,
1237 GNUNET_YES, GNUNET_NO,
1238 SELECT_IT_MIGRATION_ORDER_1,
1246 * Call sqlite using the already prepared query to get
1249 * @param cls context with the prepared query
1250 * @param nc context with the prepared query
1251 * @return GNUNET_OK on success, GNUNET_SYSERR on error, GNUNET_NO if
1252 * there are no more results
1255 all_next_prepare (void *cls,
1256 struct NextContext *nc)
1258 struct Plugin *plugin;
1264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1265 "Asked to clean up iterator state.\n");
1267 nc = (struct NextContext *)cls;
1269 sqlite3_finalize (nc->stmt);
1271 return GNUNET_SYSERR;
1273 plugin = nc->plugin;
1274 if (SQLITE_ROW == (ret = sqlite3_step (nc->stmt)))
1278 if (ret != SQLITE_DONE)
1280 LOG_SQLITE (plugin, NULL,
1281 GNUNET_ERROR_TYPE_ERROR |
1282 GNUNET_ERROR_TYPE_BULK,
1284 return GNUNET_SYSERR;
1291 * Select a subset of the items in the datastore and call
1292 * the given iterator for each of them.
1294 * @param cls our plugin context
1295 * @param type entries of which type should be considered?
1296 * Use 0 for any type.
1297 * @param iter function to call on each matching value;
1298 * will be called once with a NULL value at the end
1299 * @param iter_cls closure for iter
1302 sqlite_plugin_iter_all_now (void *cls,
1303 enum GNUNET_BLOCK_Type type,
1304 PluginIterator iter,
1307 struct Plugin *plugin = cls;
1308 struct NextContext *nc;
1311 if (sq_prepare (plugin->dbh,
1312 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090",
1313 &stmt) != SQLITE_OK)
1315 LOG_SQLITE (plugin, NULL,
1316 GNUNET_ERROR_TYPE_ERROR |
1317 GNUNET_ERROR_TYPE_BULK, "sqlite3_prepare_v2");
1318 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1321 nc = GNUNET_malloc (sizeof(struct NextContext));
1322 nc->plugin = plugin;
1324 nc->iter_cls = iter_cls;
1326 nc->prep = &all_next_prepare;
1328 sqlite_next_request (nc, GNUNET_NO);
1335 struct GetNextContext
1366 GNUNET_HashCode key;
1371 GNUNET_HashCode vhash;
1379 * @param cls our "struct GetNextContext*"
1381 * @return GNUNET_YES if there are more results,
1382 * GNUNET_NO if there are no more results,
1383 * GNUNET_SYSERR on internal error
1386 get_next_prepare (void *cls,
1387 struct NextContext *nc)
1389 struct GetNextContext *gnc = cls;
1396 sqlite3_finalize (gnc->stmt);
1397 return GNUNET_SYSERR;
1399 if (nc->count == gnc->total)
1401 if (nc->count + gnc->off == gnc->total)
1404 limit_off = gnc->off;
1408 sqlite3_reset (nc->stmt);
1409 ret = sqlite3_bind_blob (nc->stmt,
1412 sizeof (GNUNET_HashCode),
1414 if ((gnc->have_vhash) && (ret == SQLITE_OK))
1415 ret = sqlite3_bind_blob (nc->stmt,
1418 sizeof (GNUNET_HashCode), SQLITE_TRANSIENT);
1419 if ((gnc->type != 0) && (ret == SQLITE_OK))
1420 ret = sqlite3_bind_int (nc->stmt, sqoff++, gnc->type);
1421 if (ret == SQLITE_OK)
1422 ret = sqlite3_bind_int64 (nc->stmt, sqoff++, nc->last_rowid + 1);
1423 if (ret == SQLITE_OK)
1424 ret = sqlite3_bind_int (nc->stmt, sqoff++, limit_off);
1425 if (ret != SQLITE_OK)
1426 return GNUNET_SYSERR;
1427 if (SQLITE_ROW != sqlite3_step (nc->stmt))
1434 * Iterate over the results for a particular key
1437 * @param cls closure
1438 * @param key maybe NULL (to match all entries)
1439 * @param vhash hash of the value, maybe NULL (to
1440 * match all values that have the right key).
1441 * Note that for DBlocks there is no difference
1442 * betwen key and vhash, but for other blocks
1444 * @param type entries of which type are relevant?
1445 * Use 0 for any type.
1446 * @param iter function to call on each matching value;
1447 * will be called once with a NULL value at the end
1448 * @param iter_cls closure for iter
1451 sqlite_plugin_get (void *cls,
1452 const GNUNET_HashCode * key,
1453 const GNUNET_HashCode * vhash,
1454 enum GNUNET_BLOCK_Type type,
1455 PluginIterator iter, void *iter_cls)
1457 struct Plugin *plugin = cls;
1458 struct GetNextContext *gpc;
1459 struct NextContext *nc;
1466 GNUNET_assert (iter != NULL);
1469 sqlite_plugin_iter_low_priority (cls, type, iter, iter_cls);
1472 GNUNET_snprintf (scratch, sizeof (scratch),
1473 "SELECT count(*) FROM gn090 WHERE hash=:1%s%s",
1474 vhash == NULL ? "" : " AND vhash=:2",
1475 type == 0 ? "" : (vhash ==
1476 NULL) ? " AND type=:2" : " AND type=:3");
1477 if (sq_prepare (plugin->dbh, scratch, &stmt) != SQLITE_OK)
1479 LOG_SQLITE (plugin, NULL,
1480 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite_prepare");
1481 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1485 ret = sqlite3_bind_blob (stmt,
1487 key, sizeof (GNUNET_HashCode), SQLITE_TRANSIENT);
1488 if ((vhash != NULL) && (ret == SQLITE_OK))
1489 ret = sqlite3_bind_blob (stmt,
1492 sizeof (GNUNET_HashCode), SQLITE_TRANSIENT);
1493 if ((type != 0) && (ret == SQLITE_OK))
1494 ret = sqlite3_bind_int (stmt, sqoff++, type);
1495 if (SQLITE_OK != ret)
1497 LOG_SQLITE (plugin, NULL,
1498 GNUNET_ERROR_TYPE_ERROR, "sqlite_bind");
1499 sqlite3_reset (stmt);
1500 sqlite3_finalize (stmt);
1501 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1504 ret = sqlite3_step (stmt);
1505 if (ret != SQLITE_ROW)
1507 LOG_SQLITE (plugin, NULL,
1508 GNUNET_ERROR_TYPE_ERROR| GNUNET_ERROR_TYPE_BULK,
1510 sqlite3_reset (stmt);
1511 sqlite3_finalize (stmt);
1512 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1515 total = sqlite3_column_int (stmt, 0);
1516 sqlite3_reset (stmt);
1517 sqlite3_finalize (stmt);
1520 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1524 GNUNET_snprintf (scratch, sizeof (scratch),
1525 "SELECT type, prio, anonLevel, expire, hash, value, _ROWID_ "
1526 "FROM gn090 WHERE hash=:1%s%s AND _ROWID_ >= :%d "
1527 "ORDER BY _ROWID_ ASC LIMIT 1 OFFSET :d",
1528 vhash == NULL ? "" : " AND vhash=:2",
1529 type == 0 ? "" : (vhash ==
1530 NULL) ? " AND type=:2" : " AND type=:3",
1532 if (sq_prepare (plugin->dbh, scratch, &stmt) != SQLITE_OK)
1534 LOG_SQLITE (plugin, NULL,
1535 GNUNET_ERROR_TYPE_ERROR |
1536 GNUNET_ERROR_TYPE_BULK, "sqlite_prepare");
1537 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1540 nc = GNUNET_malloc (sizeof(struct NextContext) +
1541 sizeof(struct GetNextContext));
1542 nc->plugin = plugin;
1544 nc->iter_cls = iter_cls;
1546 gpc = (struct GetNextContext*) &nc[1];
1550 gpc->stmt = stmt; /* alias used for freeing at the end! */
1553 gpc->have_vhash = GNUNET_YES;
1554 gpc->vhash = *vhash;
1556 gpc->off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, total);
1557 nc->prep = &get_next_prepare;
1559 sqlite_next_request (nc, GNUNET_NO);
1564 * Get a random item for replication. Returns a single, not expired, random item
1565 * from those with the highest replication counters. The item's
1566 * replication counter is decremented by one IF it was positive before.
1567 * Call 'iter' with all values ZERO or NULL if the datastore is empty.
1569 * @param cls closure
1570 * @param iter function to call the value (once only).
1571 * @param iter_cls closure for iter
1574 sqlite_plugin_replication_get (void *cls,
1575 PluginIterator iter, void *iter_cls)
1577 struct Plugin *plugin = cls;
1580 struct GNUNET_TIME_Absolute expiration;
1581 unsigned long long rowid;
1584 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1586 "Getting random block based on replication order.\n");
1588 stmt = plugin->selRepl;
1589 if (SQLITE_OK != sqlite3_bind_int64 (stmt, 1, expiration.abs_value))
1591 LOG_SQLITE (plugin, NULL,
1592 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX");
1593 if (SQLITE_OK != sqlite3_reset (stmt))
1594 LOG_SQLITE (plugin, NULL,
1595 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
1596 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0,
1597 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1600 n = sqlite3_step (stmt);
1604 rowid = sqlite3_column_int64 (stmt, 6);
1605 if (sqlite3_column_bytes (stmt, 4) != sizeof (GNUNET_HashCode))
1607 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1609 _("Invalid data in database. Trying to fix (by deletion).\n"));
1610 if (SQLITE_OK != sqlite3_reset (stmt))
1611 LOG_SQLITE (plugin, NULL,
1612 GNUNET_ERROR_TYPE_ERROR |
1613 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
1614 delete_by_rowid (plugin, rowid);
1617 expiration.abs_value = sqlite3_column_int64 (stmt, 3);
1618 (void) iter (iter_cls,
1620 sqlite3_column_blob (stmt, 4) /* key */,
1621 sqlite3_column_bytes (stmt, 5) /* size of data */,
1622 sqlite3_column_blob (stmt, 5) /* data */,
1623 sqlite3_column_int (stmt, 0) /* type */,
1624 sqlite3_column_int (stmt, 1) /* priority */,
1625 sqlite3_column_int (stmt, 2) /* anonymity */,
1628 if (SQLITE_OK != sqlite3_reset (stmt))
1629 LOG_SQLITE (plugin, NULL,
1630 GNUNET_ERROR_TYPE_ERROR |
1631 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
1634 /* database must be empty */
1635 if (SQLITE_OK != sqlite3_reset (stmt))
1636 LOG_SQLITE (plugin, NULL,
1637 GNUNET_ERROR_TYPE_ERROR |
1638 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
1644 LOG_SQLITE (plugin, NULL,
1645 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1647 (void) sqlite3_reset (stmt);
1649 database_shutdown (plugin);
1650 database_setup (plugin->env->cfg,
1654 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0,
1655 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1662 * @param cls our plugin context
1665 sqlite_plugin_drop (void *cls)
1667 struct Plugin *plugin = cls;
1668 plugin->drop_on_shutdown = GNUNET_YES;
1672 static unsigned long long
1673 sqlite_plugin_get_size (void *cls)
1675 struct Plugin *plugin = cls;
1683 if (SQLITE_VERSION_NUMBER < 3006000)
1685 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1687 _("sqlite version to old to determine size, assuming zero\n"));
1691 sqlite3_exec (plugin->dbh,
1692 "VACUUM", NULL, NULL, ENULL));
1694 sqlite3_exec (plugin->dbh,
1695 "PRAGMA auto_vacuum=INCREMENTAL", NULL, NULL, ENULL));
1697 sq_prepare (plugin->dbh,
1698 "PRAGMA page_count",
1701 sqlite3_step (stmt))
1702 pages = sqlite3_column_int64 (stmt, 0);
1705 sqlite3_finalize (stmt);
1707 sq_prepare (plugin->dbh,
1710 CHECK (SQLITE_ROW ==
1711 sqlite3_step (stmt));
1712 page_size = sqlite3_column_int64 (stmt, 0);
1713 sqlite3_finalize (stmt);
1714 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1715 _("Using sqlite page utilization to estimate payload (%llu pages of size %llu bytes)\n"),
1716 (unsigned long long) pages,
1717 (unsigned long long) page_size);
1718 return pages * page_size;
1723 * Entry point for the plugin.
1725 * @param cls the "struct GNUNET_DATASTORE_PluginEnvironment*"
1726 * @return NULL on error, othrewise the plugin context
1729 libgnunet_plugin_datastore_sqlite_init (void *cls)
1731 static struct Plugin plugin;
1732 struct GNUNET_DATASTORE_PluginEnvironment *env = cls;
1733 struct GNUNET_DATASTORE_PluginFunctions *api;
1735 if (plugin.env != NULL)
1736 return NULL; /* can only initialize once! */
1737 memset (&plugin, 0, sizeof(struct Plugin));
1740 database_setup (env->cfg, &plugin))
1742 database_shutdown (&plugin);
1745 api = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_PluginFunctions));
1747 api->get_size = &sqlite_plugin_get_size;
1748 api->put = &sqlite_plugin_put;
1749 api->next_request = &sqlite_next_request;
1750 api->get = &sqlite_plugin_get;
1751 api->replication_get = &sqlite_plugin_replication_get;
1752 api->update = &sqlite_plugin_update;
1753 api->iter_low_priority = &sqlite_plugin_iter_low_priority;
1754 api->iter_zero_anonymity = &sqlite_plugin_iter_zero_anonymity;
1755 api->iter_ascending_expiration = &sqlite_plugin_iter_ascending_expiration;
1756 api->iter_migration_order = &sqlite_plugin_iter_migration_order;
1757 api->iter_all_now = &sqlite_plugin_iter_all_now;
1758 api->drop = &sqlite_plugin_drop;
1759 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1760 "sqlite", _("Sqlite database running\n"));
1766 * Exit point from the plugin.
1768 * @param cls the plugin context (as returned by "init")
1769 * @return always NULL
1772 libgnunet_plugin_datastore_sqlite_done (void *cls)
1775 struct GNUNET_DATASTORE_PluginFunctions *api = cls;
1776 struct Plugin *plugin = api->cls;
1779 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1781 "sqlite plugin is doneing\n");
1784 if (plugin->next_task != GNUNET_SCHEDULER_NO_TASK)
1787 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1789 "Canceling next task\n");
1791 GNUNET_SCHEDULER_cancel (plugin->next_task);
1792 plugin->next_task = GNUNET_SCHEDULER_NO_TASK;
1794 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1796 "Prep'ing next task\n");
1798 plugin->next_task_nc->prep (plugin->next_task_nc->prep_cls, NULL);
1799 GNUNET_free (plugin->next_task_nc);
1800 plugin->next_task_nc = NULL;
1803 if (plugin->drop_on_shutdown)
1804 fn = GNUNET_strdup (plugin->fn);
1806 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1808 "Shutting down database\n");
1810 database_shutdown (plugin);
1815 if (0 != UNLINK(fn))
1816 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1822 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1824 "sqlite plugin is finished doneing\n");
1829 /* end of plugin_datastore_sqlite.c */