2 This file is part of GNUnet
3 (C) 2009 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_statistics_service.h"
29 #include "plugin_datastore.h"
32 #define DEBUG_SQLITE GNUNET_NO
35 * After how many payload-changing operations
36 * do we sync our statistics?
38 #define MAX_STAT_SYNC_LAG 50
40 #define QUOTA_STAT_NAME gettext_noop ("# bytes used in file-sharing datastore")
43 * Log an error message at log-level 'level' that indicates
44 * a failure of the command 'cmd' on file 'filename'
45 * with the message given by strerror(errno).
47 #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)
49 #define SELECT_IT_LOW_PRIORITY_1 \
50 "SELECT size,type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn080 WHERE (prio = ? AND hash > ?) "\
51 "ORDER BY hash ASC LIMIT 1"
53 #define SELECT_IT_LOW_PRIORITY_2 \
54 "SELECT size,type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn080 WHERE (prio > ?) "\
55 "ORDER BY prio ASC, hash ASC LIMIT 1"
57 #define SELECT_IT_NON_ANONYMOUS_1 \
58 "SELECT size,type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn080 WHERE (prio = ? AND hash < ? AND anonLevel = 0 AND expire > %llu) "\
59 " ORDER BY hash DESC LIMIT 1"
61 #define SELECT_IT_NON_ANONYMOUS_2 \
62 "SELECT size,type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn080 WHERE (prio < ? AND anonLevel = 0 AND expire > %llu)"\
63 " ORDER BY prio DESC, hash DESC LIMIT 1"
65 #define SELECT_IT_EXPIRATION_TIME_1 \
66 "SELECT size,type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn080 WHERE (expire = ? AND hash > ?) "\
67 " ORDER BY hash ASC LIMIT 1"
69 #define SELECT_IT_EXPIRATION_TIME_2 \
70 "SELECT size,type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn080 WHERE (expire > ?) "\
71 " ORDER BY expire ASC, hash ASC LIMIT 1"
73 #define SELECT_IT_MIGRATION_ORDER_1 \
74 "SELECT size,type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn080 WHERE (expire = ? AND hash < ?) "\
75 " ORDER BY hash DESC LIMIT 1"
77 #define SELECT_IT_MIGRATION_ORDER_2 \
78 "SELECT size,type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn080 WHERE (expire < ? AND expire > %llu) "\
79 " ORDER BY expire DESC, hash DESC LIMIT 1"
82 * After how many ms "busy" should a DB operation fail for good?
83 * A low value makes sure that we are more responsive to requests
84 * (especially PUTs). A high value guarantees a higher success
85 * rate (SELECTs in iterate can take several seconds despite LIMIT=1).
87 * The default value of 250ms should ensure that users do not experience
88 * huge latencies while at the same time allowing operations to succeed
89 * with reasonable probability.
91 #define BUSY_TIMEOUT_MS 250
96 * Context for all functions in this plugin.
101 * Our execution environment.
103 struct GNUNET_DATASTORE_PluginEnvironment *env;
111 * Native SQLite database handle.
116 * Precompiled SQL for update.
118 sqlite3_stmt *updPrio;
121 * Precompiled SQL for insertion.
123 sqlite3_stmt *insertContent;
126 * Handle to the statistics service.
128 struct GNUNET_STATISTICS_Handle *statistics;
131 * Handle for pending get request.
133 struct GNUNET_STATISTICS_GetHandle *stat_get;
136 * Closure of the 'next_task' (must be freed if 'next_task' is cancelled).
138 struct NextContext *next_task_nc;
141 * Pending task with scheduler for running the next request.
143 GNUNET_SCHEDULER_TaskIdentifier next_task;
146 * How much data are we currently storing
149 unsigned long long payload;
152 * Number of updates that were made to the
153 * payload value since we last synchronized
154 * it with the statistics service.
156 unsigned int lastSync;
159 * Should the database be dropped on shutdown?
161 int drop_on_shutdown;
164 * Did we get an answer from statistics?
171 * @brief Prepare a SQL statement
173 * @param dbh handle to the database
174 * @param zSql SQL statement, UTF-8 encoded
175 * @param ppStmt set to the prepared statement
176 * @return 0 on success
179 sq_prepare (sqlite3 * dbh, const char *zSql,
180 sqlite3_stmt ** ppStmt)
183 return sqlite3_prepare_v2 (dbh,
185 strlen (zSql), ppStmt, (const char **) &dummy);
190 * Create our database indices.
192 * @param dbh handle to the database
195 create_indices (sqlite3 * dbh)
199 "CREATE INDEX idx_hash ON gn080 (hash)", NULL, NULL, NULL);
201 "CREATE INDEX idx_hash_vhash ON gn080 (hash,vhash)", NULL,
203 sqlite3_exec (dbh, "CREATE INDEX idx_prio ON gn080 (prio)", NULL, NULL,
205 sqlite3_exec (dbh, "CREATE INDEX idx_expire ON gn080 (expire)", NULL, NULL,
207 sqlite3_exec (dbh, "CREATE INDEX idx_comb3 ON gn080 (prio,anonLevel)", NULL,
209 sqlite3_exec (dbh, "CREATE INDEX idx_comb4 ON gn080 (prio,hash,anonLevel)",
211 sqlite3_exec (dbh, "CREATE INDEX idx_comb7 ON gn080 (expire,hash)", NULL,
218 #define CHECK(a) GNUNET_break(a)
222 #define ENULL_DEFINED 1
223 #define CHECK(a) if (! a) { GNUNET_log(GNUNET_ERROR_TYPE_ERRROR, "%s\n", e); sqlite3_free(e); }
230 * Initialize the database connections and associated
231 * data structures (create tables and indices
232 * as needed as well).
234 * @param cfg our configuration
235 * @param plugin the plugin context (state for this module)
236 * @return GNUNET_OK on success
239 database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg,
240 struct Plugin *plugin)
249 GNUNET_CONFIGURATION_get_value_filename (cfg,
254 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
256 _("Option `%s' in section `%s' missing in configuration!\n"),
259 return GNUNET_SYSERR;
261 if (GNUNET_OK != GNUNET_DISK_file_test (afsdir))
263 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (afsdir))
266 GNUNET_free (afsdir);
267 return GNUNET_SYSERR;
269 /* database is new or got deleted, reset payload to zero! */
270 if (plugin->stat_get != NULL)
272 GNUNET_STATISTICS_get_cancel (plugin->stat_get);
273 plugin->stat_get = NULL;
277 plugin->fn = GNUNET_STRINGS_to_utf8 (afsdir, strlen (afsdir),
279 nl_langinfo (CODESET)
281 "UTF-8" /* good luck */
284 GNUNET_free (afsdir);
286 /* Open database and precompile statements */
287 if (sqlite3_open (plugin->fn, &plugin->dbh) != SQLITE_OK)
289 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
291 _("Unable to initialize SQLite: %s.\n"),
292 sqlite3_errmsg (plugin->dbh));
293 return GNUNET_SYSERR;
296 sqlite3_exec (plugin->dbh,
297 "PRAGMA temp_store=MEMORY", NULL, NULL, ENULL));
299 sqlite3_exec (plugin->dbh,
300 "PRAGMA synchronous=OFF", NULL, NULL, ENULL));
302 sqlite3_exec (plugin->dbh,
303 "PRAGMA auto_vacuum=INCREMENTAL", NULL, NULL, ENULL));
305 sqlite3_exec (plugin->dbh,
306 "PRAGMA count_changes=OFF", NULL, NULL, ENULL));
308 sqlite3_exec (plugin->dbh,
309 "PRAGMA page_size=4092", NULL, NULL, ENULL));
311 CHECK (SQLITE_OK == sqlite3_busy_timeout (plugin->dbh, BUSY_TIMEOUT_MS));
314 /* We have to do it here, because otherwise precompiling SQL might fail */
316 sq_prepare (plugin->dbh,
317 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'gn080'",
319 if ( (sqlite3_step (stmt) == SQLITE_DONE) &&
320 (sqlite3_exec (plugin->dbh,
321 "CREATE TABLE gn080 ("
322 " size INT4 NOT NULL DEFAULT 0,"
323 " type INT4 NOT NULL DEFAULT 0,"
324 " prio INT4 NOT NULL DEFAULT 0,"
325 " anonLevel INT4 NOT NULL DEFAULT 0,"
326 " expire INT8 NOT NULL DEFAULT 0,"
327 " hash TEXT NOT NULL DEFAULT '',"
328 " vhash TEXT NOT NULL DEFAULT '',"
329 " value BLOB NOT NULL DEFAULT '')", NULL, NULL,
330 NULL) != SQLITE_OK) )
332 LOG_SQLITE (plugin, NULL,
333 GNUNET_ERROR_TYPE_ERROR,
335 sqlite3_finalize (stmt);
336 return GNUNET_SYSERR;
338 sqlite3_finalize (stmt);
339 create_indices (plugin->dbh);
342 sq_prepare (plugin->dbh,
343 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'gn071'",
345 if ( (sqlite3_step (stmt) == SQLITE_DONE) &&
346 (sqlite3_exec (plugin->dbh,
347 "CREATE TABLE gn071 ("
348 " key TEXT NOT NULL DEFAULT '',"
349 " value INTEGER NOT NULL DEFAULT 0)", NULL, NULL,
350 NULL) != SQLITE_OK) )
352 LOG_SQLITE (plugin, NULL,
353 GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec");
354 sqlite3_finalize (stmt);
355 return GNUNET_SYSERR;
357 sqlite3_finalize (stmt);
359 if ((sq_prepare (plugin->dbh,
360 "UPDATE gn080 SET prio = prio + ?, expire = MAX(expire,?) WHERE "
362 &plugin->updPrio) != SQLITE_OK) ||
363 (sq_prepare (plugin->dbh,
364 "INSERT INTO gn080 (size, type, prio, "
365 "anonLevel, expire, hash, vhash, value) VALUES "
366 "(?, ?, ?, ?, ?, ?, ?, ?)",
367 &plugin->insertContent) != SQLITE_OK))
369 LOG_SQLITE (plugin, NULL,
370 GNUNET_ERROR_TYPE_ERROR, "precompiling");
371 return GNUNET_SYSERR;
378 * Synchronize our utilization statistics with the
379 * statistics service.
380 * @param plugin the plugin context (state for this module)
383 sync_stats (struct Plugin *plugin)
385 GNUNET_STATISTICS_set (plugin->statistics,
389 plugin->lastSync = 0;
394 * Shutdown database connection and associate data
396 * @param plugin the plugin context (state for this module)
399 database_shutdown (struct Plugin *plugin)
401 if (plugin->lastSync > 0)
403 if (plugin->updPrio != NULL)
404 sqlite3_finalize (plugin->updPrio);
405 if (plugin->insertContent != NULL)
406 sqlite3_finalize (plugin->insertContent);
407 sqlite3_close (plugin->dbh);
408 GNUNET_free_non_null (plugin->fn);
413 * Get an estimate of how much space the database is
416 * @param cls our plugin context
417 * @return number of bytes used on disk
419 static unsigned long long sqlite_plugin_get_size (void *cls)
421 struct Plugin *plugin = cls;
422 return plugin->payload;
427 * Delete the database entry with the given
430 * @param plugin the plugin context (state for this module)
431 * @param rid the ID of the row to delete
434 delete_by_rowid (struct Plugin* plugin,
435 unsigned long long rid)
439 if (sq_prepare (plugin->dbh,
440 "DELETE FROM gn080 WHERE _ROWID_ = ?", &stmt) != SQLITE_OK)
442 LOG_SQLITE (plugin, NULL,
443 GNUNET_ERROR_TYPE_ERROR |
444 GNUNET_ERROR_TYPE_BULK, "sq_prepare");
445 return GNUNET_SYSERR;
447 sqlite3_bind_int64 (stmt, 1, rid);
448 if (SQLITE_DONE != sqlite3_step (stmt))
450 LOG_SQLITE (plugin, NULL,
451 GNUNET_ERROR_TYPE_ERROR |
452 GNUNET_ERROR_TYPE_BULK, "sqlite3_step");
453 sqlite3_finalize (stmt);
454 return GNUNET_SYSERR;
456 sqlite3_finalize (stmt);
462 * Context for the universal iterator.
467 * Type of a function that will prepare
468 * the next iteration.
471 * @param nc the next context; NULL for the last
472 * call which gives the callback a chance to
473 * clean up the closure
474 * @return GNUNET_OK on success, GNUNET_NO if there are
475 * no more values, GNUNET_SYSERR on error
477 typedef int (*PrepareFunction)(void *cls,
478 struct NextContext *nc);
482 * Context we keep for the "next request" callback.
489 struct Plugin *plugin;
492 * Function to call on the next value.
502 * Function to call to prepare the next
505 PrepareFunction prep;
513 * Statement that the iterator will get the data
514 * from (updated or set by prep).
519 * Row ID of the last result.
521 unsigned long long last_rowid;
524 * Key of the last result.
526 GNUNET_HashCode lastKey;
529 * Expiration time of the last value visited.
531 struct GNUNET_TIME_Absolute lastExpiration;
534 * Priority of the last value visited.
536 unsigned int lastPriority;
539 * Number of results processed so far.
544 * Set to GNUNET_YES if we must stop now.
551 * Continuation of "sqlite_next_request".
553 * @param cls the next context
554 * @param tc the task context (unused)
557 sqlite_next_request_cont (void *cls,
558 const struct GNUNET_SCHEDULER_TaskContext *tc)
560 struct NextContext * nc = cls;
561 struct Plugin *plugin;
562 unsigned long long rowid;
567 unsigned int priority;
568 unsigned int anonymity;
569 struct GNUNET_TIME_Absolute expiration;
570 const GNUNET_HashCode *key;
574 plugin->next_task = GNUNET_SCHEDULER_NO_TASK;
575 plugin->next_task_nc = NULL;
576 if ( (GNUNET_YES == nc->end_it) ||
577 (GNUNET_OK != (nc->prep(nc->prep_cls,
581 nc->iter (nc->iter_cls,
582 NULL, NULL, 0, NULL, 0, 0, 0,
583 GNUNET_TIME_UNIT_ZERO_ABS, 0);
584 nc->prep (nc->prep_cls, NULL);
589 rowid = sqlite3_column_int64 (nc->stmt, 7);
590 nc->last_rowid = rowid;
591 type = sqlite3_column_int (nc->stmt, 1);
592 size = sqlite3_column_bytes (nc->stmt, 6);
593 if (sqlite3_column_bytes (nc->stmt, 5) != sizeof (GNUNET_HashCode))
595 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
597 _("Invalid data in database. Trying to fix (by deletion).\n"));
598 if (SQLITE_OK != sqlite3_reset (nc->stmt))
599 LOG_SQLITE (nc->plugin, NULL,
600 GNUNET_ERROR_TYPE_ERROR |
601 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
604 "DELETE FROM gn080 WHERE NOT LENGTH(hash) = ?",
605 &stmtd) != SQLITE_OK)
607 LOG_SQLITE (nc->plugin, NULL,
608 GNUNET_ERROR_TYPE_ERROR |
609 GNUNET_ERROR_TYPE_BULK,
614 if (SQLITE_OK != sqlite3_bind_int (stmtd, 1, sizeof (GNUNET_HashCode)))
615 LOG_SQLITE (nc->plugin, NULL,
616 GNUNET_ERROR_TYPE_ERROR |
617 GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_int");
618 if (SQLITE_DONE != sqlite3_step (stmtd))
619 LOG_SQLITE (nc->plugin, NULL,
620 GNUNET_ERROR_TYPE_ERROR |
621 GNUNET_ERROR_TYPE_BULK, "sqlite3_step");
622 if (SQLITE_OK != sqlite3_finalize (stmtd))
623 LOG_SQLITE (nc->plugin, NULL,
624 GNUNET_ERROR_TYPE_ERROR |
625 GNUNET_ERROR_TYPE_BULK, "sqlite3_finalize");
629 priority = sqlite3_column_int (nc->stmt, 2);
630 anonymity = sqlite3_column_int (nc->stmt, 3);
631 expiration.value = sqlite3_column_int64 (nc->stmt, 4);
632 key = sqlite3_column_blob (nc->stmt, 5);
633 nc->lastPriority = priority;
634 nc->lastExpiration = expiration;
635 memcpy (&nc->lastKey, key, sizeof(GNUNET_HashCode));
636 data = sqlite3_column_blob (nc->stmt, 6);
638 ret = nc->iter (nc->iter_cls,
648 if (ret == GNUNET_SYSERR)
650 nc->end_it = GNUNET_YES;
654 if (ret == GNUNET_NO)
655 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
657 "Asked to remove entry %llu (%u bytes)\n",
658 (unsigned long long) rowid,
659 size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
661 if ( (ret == GNUNET_NO) &&
662 (GNUNET_OK == delete_by_rowid (plugin, rowid)) )
664 if (plugin->payload >= size + GNUNET_DATASTORE_ENTRY_OVERHEAD)
665 plugin->payload -= (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
667 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
668 _("Datastore payload inaccurate, please fix and restart!\n"));
671 if (ret == GNUNET_NO)
672 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
674 "Removed entry %llu (%u bytes), new payload is %llu\n",
675 (unsigned long long) rowid,
676 size + GNUNET_DATASTORE_ENTRY_OVERHEAD,
679 if (plugin->lastSync >= MAX_STAT_SYNC_LAG)
686 * Function invoked on behalf of a "PluginIterator"
687 * asking the database plugin to call the iterator
688 * with the next item.
690 * @param next_cls whatever argument was given
691 * to the PluginIterator as "next_cls".
692 * @param end_it set to GNUNET_YES if we
693 * should terminate the iteration early
694 * (iterator should be still called once more
695 * to signal the end of the iteration).
698 sqlite_next_request (void *next_cls,
701 struct NextContext * nc= next_cls;
703 if (GNUNET_YES == end_it)
704 nc->end_it = GNUNET_YES;
705 nc->plugin->next_task_nc = nc;
706 nc->plugin->next_task = GNUNET_SCHEDULER_add_now (nc->plugin->env->sched,
707 &sqlite_next_request_cont,
714 * Store an item in the datastore.
717 * @param key key for the item
718 * @param size number of bytes in data
719 * @param data content stored
720 * @param type type of the content
721 * @param priority priority of the content
722 * @param anonymity anonymity-level for the content
723 * @param expiration expiration time for the content
724 * @param msg set to an error message
725 * @return GNUNET_OK on success
728 sqlite_plugin_put (void *cls,
729 const GNUNET_HashCode * key,
732 enum GNUNET_BLOCK_Type type,
735 struct GNUNET_TIME_Absolute expiration,
738 struct Plugin *plugin = cls;
741 GNUNET_HashCode vhash;
744 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
746 "Storing in database block with type %u/key `%s'/priority %u/expiration %llu (%lld).\n",
750 (unsigned long long) GNUNET_TIME_absolute_get_remaining (expiration).value,
751 (long long) expiration.value);
753 GNUNET_CRYPTO_hash (data, size, &vhash);
754 stmt = plugin->insertContent;
755 if ((SQLITE_OK != sqlite3_bind_int (stmt, 1, size)) ||
756 (SQLITE_OK != sqlite3_bind_int (stmt, 2, type)) ||
757 (SQLITE_OK != sqlite3_bind_int (stmt, 3, priority)) ||
758 (SQLITE_OK != sqlite3_bind_int (stmt, 4, anonymity)) ||
759 (SQLITE_OK != sqlite3_bind_int64 (stmt, 5, (sqlite3_int64) expiration.value)) ||
761 sqlite3_bind_blob (stmt, 6, key, sizeof (GNUNET_HashCode),
762 SQLITE_TRANSIENT)) ||
764 sqlite3_bind_blob (stmt, 7, &vhash, sizeof (GNUNET_HashCode),
767 sqlite3_bind_blob (stmt, 8, data, size,
772 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX");
773 if (SQLITE_OK != sqlite3_reset (stmt))
774 LOG_SQLITE (plugin, NULL,
775 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
776 return GNUNET_SYSERR;
778 n = sqlite3_step (stmt);
779 if (n != SQLITE_DONE)
781 if (n == SQLITE_BUSY)
783 LOG_SQLITE (plugin, msg,
784 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_step");
785 sqlite3_reset (stmt);
789 LOG_SQLITE (plugin, msg,
790 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_step");
791 sqlite3_reset (stmt);
792 database_shutdown (plugin);
793 database_setup (plugin->env->cfg,
795 return GNUNET_SYSERR;
797 if (SQLITE_OK != sqlite3_reset (stmt))
798 LOG_SQLITE (plugin, NULL,
799 GNUNET_ERROR_TYPE_ERROR |
800 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
802 plugin->payload += size + GNUNET_DATASTORE_ENTRY_OVERHEAD;
804 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
806 "Stored new entry (%u bytes), new payload is %llu\n",
807 size + GNUNET_DATASTORE_ENTRY_OVERHEAD,
810 if (plugin->lastSync >= MAX_STAT_SYNC_LAG)
817 * Update the priority for a particular key in the datastore. If
818 * the expiration time in value is different than the time found in
819 * the datastore, the higher value should be kept. For the
820 * anonymity level, the lower value is to be used. The specified
821 * priority should be added to the existing priority, ignoring the
824 * Note that it is possible for multiple values to match this put.
825 * In that case, all of the respective values are updated.
827 * @param cls the plugin context (state for this module)
828 * @param uid unique identifier of the datum
829 * @param delta by how much should the priority
830 * change? If priority + delta < 0 the
831 * priority should be set to 0 (never go
833 * @param expire new expiration time should be the
834 * MAX of any existing expiration time and
836 * @param msg set to an error message
837 * @return GNUNET_OK on success
840 sqlite_plugin_update (void *cls,
842 int delta, struct GNUNET_TIME_Absolute expire,
845 struct Plugin *plugin = cls;
848 sqlite3_bind_int (plugin->updPrio, 1, delta);
849 sqlite3_bind_int64 (plugin->updPrio, 2, expire.value);
850 sqlite3_bind_int64 (plugin->updPrio, 3, uid);
851 n = sqlite3_step (plugin->updPrio);
852 if (n != SQLITE_DONE)
853 LOG_SQLITE (plugin, msg,
854 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
858 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
862 sqlite3_reset (plugin->updPrio);
864 if (n == SQLITE_BUSY)
866 return n == SQLITE_DONE ? GNUNET_OK : GNUNET_SYSERR;
871 * Internal context for an iteration.
878 sqlite3_stmt *stmt_1;
883 sqlite3_stmt *stmt_2;
903 int limit_nonanonymous;
906 * Desired type for blocks returned by this iterator.
908 enum GNUNET_BLOCK_Type type;
913 * Prepare our SQL query to obtain the next record from the database.
915 * @param cls our "struct IterContext"
916 * @param nc NULL to terminate the iteration, otherwise our context for
917 * getting the next result.
918 * @return GNUNET_OK on success, GNUNET_NO if there are no more results,
919 * GNUNET_SYSERR on error (or end of iteration)
922 iter_next_prepare (void *cls,
923 struct NextContext *nc)
925 struct IterContext *ic = cls;
926 struct Plugin *plugin;
932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
933 "Asked to clean up iterator state.\n");
935 sqlite3_finalize (ic->stmt_1);
936 sqlite3_finalize (ic->stmt_2);
937 return GNUNET_SYSERR;
939 sqlite3_reset (ic->stmt_1);
940 sqlite3_reset (ic->stmt_2);
945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
946 "Restricting to results larger than the last priority %u\n",
949 sqlite3_bind_int (ic->stmt_1, 1, nc->lastPriority);
950 sqlite3_bind_int (ic->stmt_2, 1, nc->lastPriority);
955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
956 "Restricting to results larger than the last expiration %llu\n",
957 (unsigned long long) nc->lastExpiration.value);
959 sqlite3_bind_int64 (ic->stmt_1, 1, nc->lastExpiration.value);
960 sqlite3_bind_int64 (ic->stmt_2, 1, nc->lastExpiration.value);
963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
964 "Restricting to results larger than the last key `%s'\n",
965 GNUNET_h2s(&nc->lastKey));
967 sqlite3_bind_blob (ic->stmt_1, 2,
969 sizeof (GNUNET_HashCode),
971 if (SQLITE_ROW == (ret = sqlite3_step (ic->stmt_1)))
974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
975 "Result found using iterator 1\n");
977 nc->stmt = ic->stmt_1;
980 if (ret != SQLITE_DONE)
982 LOG_SQLITE (plugin, NULL,
983 GNUNET_ERROR_TYPE_ERROR |
984 GNUNET_ERROR_TYPE_BULK,
986 return GNUNET_SYSERR;
988 if (SQLITE_OK != sqlite3_reset (ic->stmt_1))
989 LOG_SQLITE (plugin, NULL,
990 GNUNET_ERROR_TYPE_ERROR |
991 GNUNET_ERROR_TYPE_BULK,
993 if (SQLITE_ROW == (ret = sqlite3_step (ic->stmt_2)))
996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
997 "Result found using iterator 2\n");
999 nc->stmt = ic->stmt_2;
1002 if (ret != SQLITE_DONE)
1004 LOG_SQLITE (plugin, NULL,
1005 GNUNET_ERROR_TYPE_ERROR |
1006 GNUNET_ERROR_TYPE_BULK,
1008 return GNUNET_SYSERR;
1010 if (SQLITE_OK != sqlite3_reset (ic->stmt_2))
1011 LOG_SQLITE (plugin, NULL,
1012 GNUNET_ERROR_TYPE_ERROR |
1013 GNUNET_ERROR_TYPE_BULK,
1016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1017 "No result found using either iterator\n");
1024 * Call a method for each key in the database and
1025 * call the callback method on it.
1027 * @param plugin our plugin context
1028 * @param type entries of which type should be considered?
1029 * @param is_asc are we iterating in ascending order?
1030 * @param is_prio are we iterating by priority (otherwise by expiration)
1031 * @param is_migr are we iterating in migration order?
1032 * @param limit_nonanonymous are we restricting results to those with anonymity
1034 * @param stmt_str_1 first SQL statement to execute
1035 * @param stmt_str_2 SQL statement to execute to get "more" results (inner iteration)
1036 * @param iter function to call on each matching value;
1037 * will be called once with a NULL value at the end
1038 * @param iter_cls closure for iter
1041 basic_iter (struct Plugin *plugin,
1042 enum GNUNET_BLOCK_Type type,
1046 int limit_nonanonymous,
1047 const char *stmt_str_1,
1048 const char *stmt_str_2,
1049 PluginIterator iter,
1052 struct NextContext *nc;
1053 struct IterContext *ic;
1054 sqlite3_stmt *stmt_1;
1055 sqlite3_stmt *stmt_2;
1058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1059 "At %llu, using queries `%s' and `%s'\n",
1060 (unsigned long long) GNUNET_TIME_absolute_get ().value,
1064 if (sq_prepare (plugin->dbh, stmt_str_1, &stmt_1) != SQLITE_OK)
1066 LOG_SQLITE (plugin, NULL,
1067 GNUNET_ERROR_TYPE_ERROR |
1068 GNUNET_ERROR_TYPE_BULK, "sqlite3_prepare_v2");
1069 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1072 if (sq_prepare (plugin->dbh, stmt_str_2, &stmt_2) != SQLITE_OK)
1074 LOG_SQLITE (plugin, NULL,
1075 GNUNET_ERROR_TYPE_ERROR |
1076 GNUNET_ERROR_TYPE_BULK, "sqlite3_prepare_v2");
1077 sqlite3_finalize (stmt_1);
1078 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1081 nc = GNUNET_malloc (sizeof(struct NextContext) +
1082 sizeof(struct IterContext));
1083 nc->plugin = plugin;
1085 nc->iter_cls = iter_cls;
1087 ic = (struct IterContext*) &nc[1];
1088 ic->stmt_1 = stmt_1;
1089 ic->stmt_2 = stmt_2;
1091 ic->is_asc = is_asc;
1092 ic->is_prio = is_prio;
1093 ic->is_migr = is_migr;
1094 ic->limit_nonanonymous = limit_nonanonymous;
1095 nc->prep = &iter_next_prepare;
1099 nc->lastPriority = 0;
1100 nc->lastExpiration.value = 0;
1101 memset (&nc->lastKey, 0, sizeof (GNUNET_HashCode));
1105 nc->lastPriority = 0x7FFFFFFF;
1106 nc->lastExpiration.value = 0x7FFFFFFFFFFFFFFFLL;
1107 memset (&nc->lastKey, 255, sizeof (GNUNET_HashCode));
1109 sqlite_next_request (nc, GNUNET_NO);
1114 * Select a subset of the items in the datastore and call
1115 * the given iterator for each of them.
1117 * @param cls our plugin context
1118 * @param type entries of which type should be considered?
1119 * Use 0 for any type.
1120 * @param iter function to call on each matching value;
1121 * will be called once with a NULL value at the end
1122 * @param iter_cls closure for iter
1125 sqlite_plugin_iter_low_priority (void *cls,
1126 enum GNUNET_BLOCK_Type type,
1127 PluginIterator iter,
1132 GNUNET_YES, GNUNET_YES,
1133 GNUNET_NO, GNUNET_NO,
1134 SELECT_IT_LOW_PRIORITY_1,
1135 SELECT_IT_LOW_PRIORITY_2,
1141 * Select a subset of the items in the datastore and call
1142 * the given iterator for each of them.
1144 * @param cls our plugin context
1145 * @param type entries of which type should be considered?
1146 * Use 0 for any type.
1147 * @param iter function to call on each matching value;
1148 * will be called once with a NULL value at the end
1149 * @param iter_cls closure for iter
1152 sqlite_plugin_iter_zero_anonymity (void *cls,
1153 enum GNUNET_BLOCK_Type type,
1154 PluginIterator iter,
1157 struct GNUNET_TIME_Absolute now;
1161 now = GNUNET_TIME_absolute_get ();
1162 GNUNET_asprintf (&q1, SELECT_IT_NON_ANONYMOUS_1,
1163 (unsigned long long) now.value);
1164 GNUNET_asprintf (&q2, SELECT_IT_NON_ANONYMOUS_2,
1165 (unsigned long long) now.value);
1168 GNUNET_NO, GNUNET_YES,
1169 GNUNET_NO, GNUNET_YES,
1180 * Select a subset of the items in the datastore and call
1181 * the given iterator for each of them.
1183 * @param cls our plugin context
1184 * @param type entries of which type should be considered?
1185 * Use 0 for any type.
1186 * @param iter function to call on each matching value;
1187 * will be called once with a NULL value at the end
1188 * @param iter_cls closure for iter
1191 sqlite_plugin_iter_ascending_expiration (void *cls,
1192 enum GNUNET_BLOCK_Type type,
1193 PluginIterator iter,
1196 struct GNUNET_TIME_Absolute now;
1200 now = GNUNET_TIME_absolute_get ();
1201 GNUNET_asprintf (&q1, SELECT_IT_EXPIRATION_TIME_1,
1202 (unsigned long long) 0*now.value);
1203 GNUNET_asprintf (&q2, SELECT_IT_EXPIRATION_TIME_2,
1204 (unsigned long long) 0*now.value);
1207 GNUNET_YES, GNUNET_NO,
1208 GNUNET_NO, GNUNET_NO,
1217 * Select a subset of the items in the datastore and call
1218 * the given iterator for each of them.
1220 * @param cls our plugin context
1221 * @param type entries of which type should be considered?
1222 * Use 0 for any type.
1223 * @param iter function to call on each matching value;
1224 * will be called once with a NULL value at the end
1225 * @param iter_cls closure for iter
1228 sqlite_plugin_iter_migration_order (void *cls,
1229 enum GNUNET_BLOCK_Type type,
1230 PluginIterator iter,
1233 struct GNUNET_TIME_Absolute now;
1236 now = GNUNET_TIME_absolute_get ();
1237 GNUNET_asprintf (&q, SELECT_IT_MIGRATION_ORDER_2,
1238 (unsigned long long) now.value);
1241 GNUNET_NO, GNUNET_NO,
1242 GNUNET_YES, GNUNET_NO,
1243 SELECT_IT_MIGRATION_ORDER_1,
1251 * Call sqlite using the already prepared query to get
1254 * @param cls not used
1255 * @param nc context with the prepared query
1256 * @return GNUNET_OK on success, GNUNET_SYSERR on error, GNUNET_NO if
1257 * there are no more results
1260 all_next_prepare (void *cls,
1261 struct NextContext *nc)
1263 struct Plugin *plugin;
1269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1270 "Asked to clean up iterator state.\n");
1272 return GNUNET_SYSERR;
1274 plugin = nc->plugin;
1275 if (SQLITE_ROW == (ret = sqlite3_step (nc->stmt)))
1279 if (ret != SQLITE_DONE)
1281 LOG_SQLITE (plugin, NULL,
1282 GNUNET_ERROR_TYPE_ERROR |
1283 GNUNET_ERROR_TYPE_BULK,
1285 return GNUNET_SYSERR;
1292 * Select a subset of the items in the datastore and call
1293 * the given iterator for each of them.
1295 * @param cls our plugin context
1296 * @param type entries of which type should be considered?
1297 * Use 0 for any type.
1298 * @param iter function to call on each matching value;
1299 * will be called once with a NULL value at the end
1300 * @param iter_cls closure for iter
1303 sqlite_plugin_iter_all_now (void *cls,
1304 enum GNUNET_BLOCK_Type type,
1305 PluginIterator iter,
1308 struct Plugin *plugin = cls;
1309 struct NextContext *nc;
1312 if (sq_prepare (plugin->dbh,
1313 "SELECT size,type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn080",
1314 &stmt) != SQLITE_OK)
1316 LOG_SQLITE (plugin, NULL,
1317 GNUNET_ERROR_TYPE_ERROR |
1318 GNUNET_ERROR_TYPE_BULK, "sqlite3_prepare_v2");
1319 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1322 nc = GNUNET_malloc (sizeof(struct NextContext));
1323 nc->plugin = plugin;
1325 nc->iter_cls = iter_cls;
1327 nc->prep = &all_next_prepare;
1328 nc->prep_cls = NULL;
1329 sqlite_next_request (nc, GNUNET_NO);
1336 struct GetNextContext
1367 GNUNET_HashCode key;
1372 GNUNET_HashCode vhash;
1380 * @param cls our "struct GetNextContext*"
1382 * @return GNUNET_YES if there are more results,
1383 * GNUNET_NO if there are no more results,
1384 * GNUNET_SYSERR on internal error
1387 get_next_prepare (void *cls,
1388 struct NextContext *nc)
1390 struct GetNextContext *gnc = cls;
1397 sqlite3_finalize (gnc->stmt);
1398 return GNUNET_SYSERR;
1400 if (nc->count == gnc->total)
1402 if (nc->count + gnc->off == gnc->total)
1405 limit_off = gnc->off;
1409 sqlite3_reset (nc->stmt);
1410 ret = sqlite3_bind_blob (nc->stmt,
1413 sizeof (GNUNET_HashCode),
1415 if ((gnc->have_vhash) && (ret == SQLITE_OK))
1416 ret = sqlite3_bind_blob (nc->stmt,
1419 sizeof (GNUNET_HashCode), SQLITE_TRANSIENT);
1420 if ((gnc->type != 0) && (ret == SQLITE_OK))
1421 ret = sqlite3_bind_int (nc->stmt, sqoff++, gnc->type);
1422 if (ret == SQLITE_OK)
1423 ret = sqlite3_bind_int64 (nc->stmt, sqoff++, nc->last_rowid + 1);
1424 if (ret == SQLITE_OK)
1425 ret = sqlite3_bind_int (nc->stmt, sqoff++, limit_off);
1426 if (ret != SQLITE_OK)
1427 return GNUNET_SYSERR;
1428 if (SQLITE_ROW != sqlite3_step (nc->stmt))
1435 * Iterate over the results for a particular key
1438 * @param cls closure
1439 * @param key maybe NULL (to match all entries)
1440 * @param vhash hash of the value, maybe NULL (to
1441 * match all values that have the right key).
1442 * Note that for DBlocks there is no difference
1443 * betwen key and vhash, but for other blocks
1445 * @param type entries of which type are relevant?
1446 * Use 0 for any type.
1447 * @param iter function to call on each matching value;
1448 * will be called once with a NULL value at the end
1449 * @param iter_cls closure for iter
1452 sqlite_plugin_get (void *cls,
1453 const GNUNET_HashCode * key,
1454 const GNUNET_HashCode * vhash,
1455 enum GNUNET_BLOCK_Type type,
1456 PluginIterator iter, void *iter_cls)
1458 struct Plugin *plugin = cls;
1459 struct GetNextContext *gpc;
1460 struct NextContext *nc;
1467 GNUNET_assert (iter != NULL);
1470 sqlite_plugin_iter_low_priority (cls, type, iter, iter_cls);
1473 GNUNET_snprintf (scratch, sizeof (scratch),
1474 "SELECT count(*) FROM gn080 WHERE hash=:1%s%s",
1475 vhash == NULL ? "" : " AND vhash=:2",
1476 type == 0 ? "" : (vhash ==
1477 NULL) ? " AND type=:2" : " AND type=:3");
1478 if (sq_prepare (plugin->dbh, scratch, &stmt) != SQLITE_OK)
1480 LOG_SQLITE (plugin, NULL,
1481 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite_prepare");
1482 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1486 ret = sqlite3_bind_blob (stmt,
1488 key, sizeof (GNUNET_HashCode), SQLITE_TRANSIENT);
1489 if ((vhash != NULL) && (ret == SQLITE_OK))
1490 ret = sqlite3_bind_blob (stmt,
1493 sizeof (GNUNET_HashCode), SQLITE_TRANSIENT);
1494 if ((type != 0) && (ret == SQLITE_OK))
1495 ret = sqlite3_bind_int (stmt, sqoff++, type);
1496 if (SQLITE_OK != ret)
1498 LOG_SQLITE (plugin, NULL,
1499 GNUNET_ERROR_TYPE_ERROR, "sqlite_bind");
1500 sqlite3_reset (stmt);
1501 sqlite3_finalize (stmt);
1502 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1505 ret = sqlite3_step (stmt);
1506 if (ret != SQLITE_ROW)
1508 LOG_SQLITE (plugin, NULL,
1509 GNUNET_ERROR_TYPE_ERROR| GNUNET_ERROR_TYPE_BULK,
1511 sqlite3_reset (stmt);
1512 sqlite3_finalize (stmt);
1513 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1516 total = sqlite3_column_int (stmt, 0);
1517 sqlite3_reset (stmt);
1518 sqlite3_finalize (stmt);
1521 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1525 GNUNET_snprintf (scratch, sizeof (scratch),
1526 "SELECT size, type, prio, anonLevel, expire, hash, value, _ROWID_ "
1527 "FROM gn080 WHERE hash=:1%s%s AND _ROWID_ >= :%d "
1528 "ORDER BY _ROWID_ ASC LIMIT 1 OFFSET :d",
1529 vhash == NULL ? "" : " AND vhash=:2",
1530 type == 0 ? "" : (vhash ==
1531 NULL) ? " AND type=:2" : " AND type=:3",
1533 if (sq_prepare (plugin->dbh, scratch, &stmt) != SQLITE_OK)
1535 LOG_SQLITE (plugin, NULL,
1536 GNUNET_ERROR_TYPE_ERROR |
1537 GNUNET_ERROR_TYPE_BULK, "sqlite_prepare");
1538 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1541 nc = GNUNET_malloc (sizeof(struct NextContext) +
1542 sizeof(struct GetNextContext));
1543 nc->plugin = plugin;
1545 nc->iter_cls = iter_cls;
1547 gpc = (struct GetNextContext*) &nc[1];
1551 gpc->stmt = stmt; /* alias used for freeing at the end! */
1554 gpc->have_vhash = GNUNET_YES;
1555 gpc->vhash = *vhash;
1557 gpc->off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, total);
1558 nc->prep = &get_next_prepare;
1560 sqlite_next_request (nc, GNUNET_NO);
1567 * @param cls our plugin context
1570 sqlite_plugin_drop (void *cls)
1572 struct Plugin *plugin = cls;
1573 plugin->drop_on_shutdown = GNUNET_YES;
1578 * Callback function to process statistic values.
1580 * @param cls closure
1581 * @param subsystem name of subsystem that created the statistic
1582 * @param name the name of the datum
1583 * @param value the current value
1584 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
1585 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
1588 process_stat_in (void *cls,
1589 const char *subsystem,
1594 struct Plugin *plugin = cls;
1596 plugin->stats_worked = GNUNET_YES;
1597 plugin->payload += value;
1599 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1601 "Notification from statistics about existing payload (%llu), new payload is %llu\n",
1610 process_stat_done (void *cls,
1613 struct Plugin *plugin = cls;
1618 plugin->stat_get = NULL;
1619 if ( (plugin->stats_worked == GNUNET_NO) &&
1620 (SQLITE_VERSION_NUMBER >= 3006000) )
1623 sqlite3_exec (plugin->dbh,
1624 "VACUUM", NULL, NULL, ENULL));
1626 sqlite3_exec (plugin->dbh,
1627 "PRAGMA auto_vacuum=INCREMENTAL", NULL, NULL, ENULL));
1629 sq_prepare (plugin->dbh,
1630 "PRAGMA page_count",
1633 sqlite3_step (stmt))
1634 pages = sqlite3_column_int64 (stmt, 0);
1637 sqlite3_finalize (stmt);
1639 sq_prepare (plugin->dbh,
1642 CHECK (SQLITE_ROW ==
1643 sqlite3_step (stmt));
1644 page_size = sqlite3_column_int64 (stmt, 0);
1645 sqlite3_finalize (stmt);
1646 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1647 _("Using sqlite page utilization to estimate payload (%llu pages of size %llu bytes)\n"),
1648 (unsigned long long) pages,
1649 (unsigned long long) page_size);
1650 plugin->payload = pages * page_size;
1656 * Entry point for the plugin.
1658 * @param cls the "struct GNUNET_DATASTORE_PluginEnvironment*"
1659 * @return NULL on error, othrewise the plugin context
1662 libgnunet_plugin_datastore_sqlite_init (void *cls)
1664 static struct Plugin plugin;
1665 struct GNUNET_DATASTORE_PluginEnvironment *env = cls;
1666 struct GNUNET_DATASTORE_PluginFunctions *api;
1668 if (plugin.env != NULL)
1669 return NULL; /* can only initialize once! */
1670 memset (&plugin, 0, sizeof(struct Plugin));
1672 plugin.statistics = GNUNET_STATISTICS_create (env->sched,
1675 plugin.stat_get = GNUNET_STATISTICS_get (plugin.statistics,
1678 GNUNET_TIME_UNIT_SECONDS,
1683 database_setup (env->cfg, &plugin))
1685 database_shutdown (&plugin);
1688 api = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_PluginFunctions));
1690 api->get_size = &sqlite_plugin_get_size;
1691 api->put = &sqlite_plugin_put;
1692 api->next_request = &sqlite_next_request;
1693 api->get = &sqlite_plugin_get;
1694 api->update = &sqlite_plugin_update;
1695 api->iter_low_priority = &sqlite_plugin_iter_low_priority;
1696 api->iter_zero_anonymity = &sqlite_plugin_iter_zero_anonymity;
1697 api->iter_ascending_expiration = &sqlite_plugin_iter_ascending_expiration;
1698 api->iter_migration_order = &sqlite_plugin_iter_migration_order;
1699 api->iter_all_now = &sqlite_plugin_iter_all_now;
1700 api->drop = &sqlite_plugin_drop;
1701 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1702 "sqlite", _("Sqlite database running\n"));
1708 * Exit point from the plugin.
1710 * @param cls the plugin context (as returned by "init")
1711 * @return always NULL
1714 libgnunet_plugin_datastore_sqlite_done (void *cls)
1717 struct GNUNET_DATASTORE_PluginFunctions *api = cls;
1718 struct Plugin *plugin = api->cls;
1720 if (plugin->stat_get != NULL)
1722 GNUNET_STATISTICS_get_cancel (plugin->stat_get);
1723 plugin->stat_get = NULL;
1725 if (plugin->next_task != GNUNET_SCHEDULER_NO_TASK)
1727 GNUNET_SCHEDULER_cancel (plugin->env->sched,
1729 plugin->next_task = GNUNET_SCHEDULER_NO_TASK;
1730 plugin->next_task_nc->prep (plugin->next_task_nc->prep_cls, NULL);
1731 GNUNET_free (plugin->next_task_nc);
1732 plugin->next_task_nc = NULL;
1735 if (plugin->drop_on_shutdown)
1736 fn = GNUNET_strdup (plugin->fn);
1737 database_shutdown (plugin);
1738 GNUNET_STATISTICS_destroy (plugin->statistics,
1741 plugin->payload = 0;
1745 if (0 != UNLINK(fn))
1746 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1754 /* end of plugin_datastore_sqlite.c */