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"
32 * Enable or disable logging debug messages.
34 #define DEBUG_SQLITE GNUNET_NO
37 * We allocate items on the stack at times. To prevent a stack
38 * overflow, we impose a limit on the maximum size for the data per
39 * item. 64k should be enough.
41 #define MAX_ITEM_SIZE 65536
44 * After how many ms "busy" should a DB operation fail for good?
45 * A low value makes sure that we are more responsive to requests
46 * (especially PUTs). A high value guarantees a higher success
47 * rate (SELECTs in iterate can take several seconds despite LIMIT=1).
49 * The default value of 250ms should ensure that users do not experience
50 * huge latencies while at the same time allowing operations to succeed
51 * with reasonable probability.
53 #define BUSY_TIMEOUT_MS 250
57 * Log an error message at log-level 'level' that indicates
58 * a failure of the command 'cmd' on file 'filename'
59 * with the message given by strerror(errno).
61 #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)
66 * Context for all functions in this plugin.
71 * Our execution environment.
73 struct GNUNET_DATASTORE_PluginEnvironment *env;
81 * Native SQLite database handle.
86 * Precompiled SQL for deletion.
91 * Precompiled SQL for update.
93 sqlite3_stmt *updPrio;
96 * Precompiled SQL for replication decrement.
98 sqlite3_stmt *updRepl;
101 * Precompiled SQL for replication selection.
103 sqlite3_stmt *selRepl;
106 * Precompiled SQL for expiration selection.
108 sqlite3_stmt *selExpi;
111 * Precompiled SQL for insertion.
113 sqlite3_stmt *insertContent;
116 * Closure of the 'next_task' (must be freed if 'next_task' is cancelled).
118 struct NextContext *next_task_nc;
121 * Pending task with scheduler for running the next request.
123 GNUNET_SCHEDULER_TaskIdentifier next_task;
126 * Should the database be dropped on shutdown?
128 int drop_on_shutdown;
134 * @brief Prepare a SQL statement
136 * @param dbh handle to the database
137 * @param zSql SQL statement, UTF-8 encoded
138 * @param ppStmt set to the prepared statement
139 * @return 0 on success
142 sq_prepare (sqlite3 * dbh,
144 sqlite3_stmt ** ppStmt)
149 result = sqlite3_prepare_v2 (dbh,
153 (const char **) &dummy);
154 #if DEBUG_SQLITE && 0
155 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
157 "Prepared `%s' / %p: %d\n",
167 * Create our database indices.
169 * @param dbh handle to the database
172 create_indices (sqlite3 * dbh)
176 "CREATE INDEX idx_hash ON gn090 (hash)", NULL, NULL, NULL);
178 "CREATE INDEX idx_hash_vhash ON gn090 (hash,vhash)", NULL,
180 sqlite3_exec (dbh, "CREATE INDEX idx_expire_repl ON gn090 (expire ASC,repl DESC)", NULL, NULL,
182 sqlite3_exec (dbh, "CREATE INDEX idx_comb ON gn090 (anonLevel ASC,expire ASC,prio,type,hash)",
184 sqlite3_exec (dbh, "CREATE INDEX expire ON gn090 (expire)",
190 #define CHECK(a) GNUNET_break(a)
194 #define ENULL_DEFINED 1
195 #define CHECK(a) if (! a) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "%s\n", e); sqlite3_free(e); }
200 * Initialize the database connections and associated
201 * data structures (create tables and indices
202 * as needed as well).
204 * @param cfg our configuration
205 * @param plugin the plugin context (state for this module)
206 * @return GNUNET_OK on success
209 database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg,
210 struct Plugin *plugin)
219 GNUNET_CONFIGURATION_get_value_filename (cfg,
224 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
226 _("Option `%s' in section `%s' missing in configuration!\n"),
229 return GNUNET_SYSERR;
231 if (GNUNET_OK != GNUNET_DISK_file_test (afsdir))
233 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (afsdir))
236 GNUNET_free (afsdir);
237 return GNUNET_SYSERR;
239 /* database is new or got deleted, reset payload to zero! */
240 plugin->env->duc (plugin->env->cls, 0);
243 plugin->fn = GNUNET_STRINGS_to_utf8 (afsdir, strlen (afsdir),
244 nl_langinfo (CODESET));
246 plugin->fn = GNUNET_STRINGS_to_utf8 (afsdir, strlen (afsdir),
247 "UTF-8"); /* good luck */
249 GNUNET_free (afsdir);
251 /* Open database and precompile statements */
252 if (sqlite3_open (plugin->fn, &plugin->dbh) != SQLITE_OK)
254 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
256 _("Unable to initialize SQLite: %s.\n"),
257 sqlite3_errmsg (plugin->dbh));
258 return GNUNET_SYSERR;
261 sqlite3_exec (plugin->dbh,
262 "PRAGMA temp_store=MEMORY", NULL, NULL, ENULL));
264 sqlite3_exec (plugin->dbh,
265 "PRAGMA synchronous=OFF", NULL, NULL, ENULL));
267 sqlite3_exec (plugin->dbh,
268 "PRAGMA legacy_file_format=OFF", NULL, NULL, ENULL));
270 sqlite3_exec (plugin->dbh,
271 "PRAGMA auto_vacuum=INCREMENTAL", NULL, NULL, ENULL));
273 sqlite3_exec (plugin->dbh,
274 "PRAGMA locking_mode=EXCLUSIVE", NULL, NULL, ENULL));
276 sqlite3_exec (plugin->dbh,
277 "PRAGMA count_changes=OFF", NULL, NULL, ENULL));
279 sqlite3_exec (plugin->dbh,
280 "PRAGMA page_size=4092", NULL, NULL, ENULL));
282 CHECK (SQLITE_OK == sqlite3_busy_timeout (plugin->dbh, BUSY_TIMEOUT_MS));
285 /* We have to do it here, because otherwise precompiling SQL might fail */
287 sq_prepare (plugin->dbh,
288 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'gn090'",
290 if ( (sqlite3_step (stmt) == SQLITE_DONE) &&
291 (sqlite3_exec (plugin->dbh,
292 "CREATE TABLE gn090 ("
293 " repl INT4 NOT NULL DEFAULT 0,"
294 " type INT4 NOT NULL DEFAULT 0,"
295 " prio INT4 NOT NULL DEFAULT 0,"
296 " anonLevel INT4 NOT NULL DEFAULT 0,"
297 " expire INT8 NOT NULL DEFAULT 0,"
298 " hash TEXT NOT NULL DEFAULT '',"
299 " vhash TEXT NOT NULL DEFAULT '',"
300 " value BLOB NOT NULL DEFAULT '')", NULL, NULL,
301 NULL) != SQLITE_OK) )
303 LOG_SQLITE (plugin, NULL,
304 GNUNET_ERROR_TYPE_ERROR,
306 sqlite3_finalize (stmt);
307 return GNUNET_SYSERR;
309 sqlite3_finalize (stmt);
310 create_indices (plugin->dbh);
313 sq_prepare (plugin->dbh,
314 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'gn071'",
316 if ( (sqlite3_step (stmt) == SQLITE_DONE) &&
317 (sqlite3_exec (plugin->dbh,
318 "CREATE TABLE gn071 ("
319 " key TEXT NOT NULL DEFAULT '',"
320 " value INTEGER NOT NULL DEFAULT 0)", NULL, NULL,
321 NULL) != SQLITE_OK) )
323 LOG_SQLITE (plugin, NULL,
324 GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec");
325 sqlite3_finalize (stmt);
326 return GNUNET_SYSERR;
328 sqlite3_finalize (stmt);
330 if ((sq_prepare (plugin->dbh,
331 "UPDATE gn090 SET prio = prio + ?, expire = MAX(expire,?) WHERE _ROWID_ = ?",
332 &plugin->updPrio) != SQLITE_OK) ||
333 (sq_prepare (plugin->dbh,
334 "UPDATE gn090 SET repl = MAX (0, repl - 1) WHERE _ROWID_ = ?",
335 &plugin->updRepl) != SQLITE_OK) ||
336 (sq_prepare (plugin->dbh,
337 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090 WHERE expire > ?"
338 " ORDER BY repl DESC, Random() LIMIT 1",
339 &plugin->selRepl) != SQLITE_OK) ||
340 (sq_prepare (plugin->dbh,
341 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090 "
342 " WHERE NOT EXISTS (SELECT 1 FROM gn090 WHERE expire < ?1 LIMIT 1) OR expire < ?1 "
343 " ORDER BY prio ASC LIMIT 1",
344 &plugin->selExpi) != SQLITE_OK) ||
345 (sq_prepare (plugin->dbh,
346 "INSERT INTO gn090 (repl, type, prio, "
347 "anonLevel, expire, hash, vhash, value) "
348 "VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
349 &plugin->insertContent) != SQLITE_OK) ||
350 (sq_prepare (plugin->dbh,
351 "DELETE FROM gn090 WHERE _ROWID_ = ?",
352 &plugin->delRow) != SQLITE_OK))
354 LOG_SQLITE (plugin, NULL,
355 GNUNET_ERROR_TYPE_ERROR, "precompiling");
356 return GNUNET_SYSERR;
364 * Shutdown database connection and associate data
366 * @param plugin the plugin context (state for this module)
369 database_shutdown (struct Plugin *plugin)
372 #if SQLITE_VERSION_NUMBER >= 3007000
376 if (plugin->delRow != NULL)
377 sqlite3_finalize (plugin->delRow);
378 if (plugin->updPrio != NULL)
379 sqlite3_finalize (plugin->updPrio);
380 if (plugin->updRepl != NULL)
381 sqlite3_finalize (plugin->updRepl);
382 if (plugin->selRepl != NULL)
383 sqlite3_finalize (plugin->selRepl);
384 if (plugin->selExpi != NULL)
385 sqlite3_finalize (plugin->selExpi);
386 if (plugin->insertContent != NULL)
387 sqlite3_finalize (plugin->insertContent);
388 result = sqlite3_close(plugin->dbh);
389 #if SQLITE_VERSION_NUMBER >= 3007000
390 if (result == SQLITE_BUSY)
392 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
394 _("Tried to close sqlite without finalizing all prepared statements.\n"));
395 stmt = sqlite3_next_stmt(plugin->dbh, NULL);
399 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
400 "sqlite", "Closing statement %p\n", stmt);
402 result = sqlite3_finalize(stmt);
404 if (result != SQLITE_OK)
405 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
407 "Failed to close statement %p: %d\n", stmt, result);
409 stmt = sqlite3_next_stmt(plugin->dbh, NULL);
411 result = sqlite3_close(plugin->dbh);
414 if (SQLITE_OK != result)
415 LOG_SQLITE (plugin, NULL,
416 GNUNET_ERROR_TYPE_ERROR,
419 GNUNET_free_non_null (plugin->fn);
424 * Delete the database entry with the given
427 * @param plugin the plugin context (state for this module)
428 * @param rid the ID of the row to delete
431 delete_by_rowid (struct Plugin* plugin,
432 unsigned long long rid)
434 sqlite3_bind_int64 (plugin->delRow, 1, rid);
435 if (SQLITE_DONE != sqlite3_step (plugin->delRow))
437 LOG_SQLITE (plugin, NULL,
438 GNUNET_ERROR_TYPE_ERROR |
439 GNUNET_ERROR_TYPE_BULK, "sqlite3_step");
440 if (SQLITE_OK != sqlite3_reset (plugin->delRow))
441 LOG_SQLITE (plugin, NULL,
442 GNUNET_ERROR_TYPE_ERROR |
443 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
444 return GNUNET_SYSERR;
446 if (SQLITE_OK != sqlite3_reset (plugin->delRow))
447 LOG_SQLITE (plugin, NULL,
448 GNUNET_ERROR_TYPE_ERROR |
449 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
455 * Context for the universal iterator.
460 * Type of a function that will prepare
461 * the next iteration.
464 * @param nc the next context; NULL for the last
465 * call which gives the callback a chance to
466 * clean up the closure
467 * @return GNUNET_OK on success, GNUNET_NO if there are
468 * no more values, GNUNET_SYSERR on error
470 typedef int (*PrepareFunction)(void *cls,
471 struct NextContext *nc);
475 * Context we keep for the "next request" callback.
482 struct Plugin *plugin;
485 * Function to call on the next value.
495 * Function to call to prepare the next
498 PrepareFunction prep;
506 * Statement that the iterator will get the data
507 * from (updated or set by prep).
512 * Row ID of the last result.
514 unsigned long long last_rowid;
517 * Key of the last result.
519 GNUNET_HashCode lastKey;
522 * Priority of the last value visited.
524 unsigned int lastPriority;
527 * Number of results processed so far.
532 * Set to GNUNET_YES if we must stop now.
539 * Continuation of "sqlite_next_request".
541 * @param cls the 'struct NextContext*'
542 * @param tc the task context (unused)
545 sqlite_next_request_cont (void *cls,
546 const struct GNUNET_SCHEDULER_TaskContext *tc)
548 struct NextContext * nc = cls;
549 struct Plugin *plugin;
550 unsigned long long rowid;
556 enum GNUNET_BLOCK_Type type;
557 const GNUNET_HashCode *key;
558 struct GNUNET_TIME_Absolute expiration;
561 plugin->next_task = GNUNET_SCHEDULER_NO_TASK;
562 plugin->next_task_nc = NULL;
563 if ( (GNUNET_YES == nc->end_it) ||
564 (GNUNET_OK != (nc->prep(nc->prep_cls,
568 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
570 "Iteration completes after %u results\n",
574 nc->iter (nc->iter_cls,
575 NULL, NULL, 0, NULL, 0, 0, 0,
576 GNUNET_TIME_UNIT_ZERO_ABS, 0);
577 nc->prep (nc->prep_cls, NULL);
582 type = sqlite3_column_int (nc->stmt, 0);
583 priority = sqlite3_column_int (nc->stmt, 1);
584 anonymity = sqlite3_column_int (nc->stmt, 2);
585 expiration.abs_value = sqlite3_column_int64 (nc->stmt, 3);
586 hsize = sqlite3_column_bytes (nc->stmt, 4);
587 key = sqlite3_column_blob (nc->stmt, 4);
588 size = sqlite3_column_bytes (nc->stmt, 5);
589 rowid = sqlite3_column_int64 (nc->stmt, 6);
590 if (hsize != sizeof (GNUNET_HashCode))
593 if (SQLITE_OK != sqlite3_reset (nc->stmt))
594 LOG_SQLITE (plugin, NULL,
595 GNUNET_ERROR_TYPE_ERROR |
596 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
597 if (GNUNET_OK == delete_by_rowid (plugin, rowid))
598 plugin->env->duc (plugin->env->cls,
599 - (size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
603 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
605 "Iterator returns value with type %u/key `%s'/priority %u/expiration %llu (%lld).\n",
609 (unsigned long long) GNUNET_TIME_absolute_get_remaining (expiration).rel_value,
610 (long long) expiration.abs_value);
612 if (size > MAX_ITEM_SIZE)
615 if (SQLITE_OK != sqlite3_reset (nc->stmt))
616 LOG_SQLITE (plugin, NULL,
617 GNUNET_ERROR_TYPE_ERROR |
618 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
619 if (GNUNET_OK == delete_by_rowid (plugin, rowid))
620 plugin->env->duc (plugin->env->cls,
621 - (size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
627 memcpy (data, sqlite3_column_blob (nc->stmt, 5), size);
629 nc->last_rowid = rowid;
630 nc->lastPriority = priority;
632 if (SQLITE_OK != sqlite3_reset (nc->stmt))
633 LOG_SQLITE (plugin, NULL,
634 GNUNET_ERROR_TYPE_ERROR |
635 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
636 ret = nc->iter (nc->iter_cls, nc,
640 anonymity, expiration,
646 nc->end_it = GNUNET_YES;
649 if (GNUNET_OK == delete_by_rowid (plugin, rowid))
652 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
654 "Removed entry %llu (%u bytes)\n",
655 (unsigned long long) rowid,
656 size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
658 plugin->env->duc (plugin->env->cls,
659 - (size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
671 * Function invoked on behalf of a "PluginIterator" asking the
672 * database plugin to call the iterator with the next item.
674 * @param next_cls whatever argument was given
675 * to the PluginIterator as "next_cls".
676 * @param end_it set to GNUNET_YES if we
677 * should terminate the iteration early
678 * (iterator should be still called once more
679 * to signal the end of the iteration).
682 sqlite_next_request (void *next_cls,
685 struct NextContext * nc= next_cls;
687 if (GNUNET_YES == end_it)
688 nc->end_it = GNUNET_YES;
689 nc->plugin->next_task_nc = nc;
690 nc->plugin->next_task = GNUNET_SCHEDULER_add_now (&sqlite_next_request_cont,
696 * Store an item in the datastore.
699 * @param key key for the item
700 * @param size number of bytes in data
701 * @param data content stored
702 * @param type type of the content
703 * @param priority priority of the content
704 * @param anonymity anonymity-level for the content
705 * @param replication replication-level for the content
706 * @param expiration expiration time for the content
707 * @param msg set to an error message
708 * @return GNUNET_OK on success
711 sqlite_plugin_put (void *cls,
712 const GNUNET_HashCode *key,
715 enum GNUNET_BLOCK_Type type,
718 uint32_t replication,
719 struct GNUNET_TIME_Absolute expiration,
722 struct Plugin *plugin = cls;
726 GNUNET_HashCode vhash;
728 if (size > MAX_ITEM_SIZE)
729 return GNUNET_SYSERR;
731 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
733 "Storing in database block with type %u/key `%s'/priority %u/expiration %llu (%lld).\n",
737 (unsigned long long) GNUNET_TIME_absolute_get_remaining (expiration).rel_value,
738 (long long) expiration.abs_value);
740 GNUNET_CRYPTO_hash (data, size, &vhash);
741 stmt = plugin->insertContent;
742 if ((SQLITE_OK != sqlite3_bind_int (stmt, 1, replication)) ||
743 (SQLITE_OK != sqlite3_bind_int (stmt, 2, type)) ||
744 (SQLITE_OK != sqlite3_bind_int (stmt, 3, priority)) ||
745 (SQLITE_OK != sqlite3_bind_int (stmt, 4, anonymity)) ||
746 (SQLITE_OK != sqlite3_bind_int64 (stmt, 5, expiration.abs_value)) ||
748 sqlite3_bind_blob (stmt, 6, key, sizeof (GNUNET_HashCode),
749 SQLITE_TRANSIENT)) ||
751 sqlite3_bind_blob (stmt, 7, &vhash, sizeof (GNUNET_HashCode),
754 sqlite3_bind_blob (stmt, 8, data, size,
759 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX");
760 if (SQLITE_OK != sqlite3_reset (stmt))
761 LOG_SQLITE (plugin, NULL,
762 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
763 return GNUNET_SYSERR;
765 n = sqlite3_step (stmt);
769 plugin->env->duc (plugin->env->cls,
770 size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
772 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
774 "Stored new entry (%u bytes)\n",
775 size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
781 LOG_SQLITE (plugin, msg,
782 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
787 LOG_SQLITE (plugin, msg,
788 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
790 if (SQLITE_OK != sqlite3_reset (stmt))
791 LOG_SQLITE (plugin, NULL,
792 GNUNET_ERROR_TYPE_ERROR |
793 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
794 database_shutdown (plugin);
795 database_setup (plugin->env->cfg,
797 return GNUNET_SYSERR;
799 if (SQLITE_OK != sqlite3_reset (stmt))
800 LOG_SQLITE (plugin, NULL,
801 GNUNET_ERROR_TYPE_ERROR |
802 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
808 * Update the priority for a particular key in the datastore. If
809 * the expiration time in value is different than the time found in
810 * the datastore, the higher value should be kept. For the
811 * anonymity level, the lower value is to be used. The specified
812 * priority should be added to the existing priority, ignoring the
815 * Note that it is possible for multiple values to match this put.
816 * In that case, all of the respective values are updated.
818 * @param cls the plugin context (state for this module)
819 * @param uid unique identifier of the datum
820 * @param delta by how much should the priority
821 * change? If priority + delta < 0 the
822 * priority should be set to 0 (never go
824 * @param expire new expiration time should be the
825 * MAX of any existing expiration time and
827 * @param msg set to an error message
828 * @return GNUNET_OK on success
831 sqlite_plugin_update (void *cls,
833 int delta, struct GNUNET_TIME_Absolute expire,
836 struct Plugin *plugin = cls;
839 sqlite3_bind_int (plugin->updPrio, 1, delta);
840 sqlite3_bind_int64 (plugin->updPrio, 2, expire.abs_value);
841 sqlite3_bind_int64 (plugin->updPrio, 3, uid);
842 n = sqlite3_step (plugin->updPrio);
843 sqlite3_reset (plugin->updPrio);
848 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
854 LOG_SQLITE (plugin, msg,
855 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
859 LOG_SQLITE (plugin, msg,
860 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
862 return GNUNET_SYSERR;
868 * Internal context for an iteration.
870 struct ZeroIterContext
873 * First iterator statement for zero-anonymity iteration.
875 sqlite3_stmt *stmt_1;
878 * Second iterator statement for zero-anonymity iteration.
880 sqlite3_stmt *stmt_2;
883 * Desired type for blocks returned by this iterator.
885 enum GNUNET_BLOCK_Type type;
890 * Prepare our SQL query to obtain the next record from the database.
892 * @param cls our "struct ZeroIterContext"
893 * @param nc NULL to terminate the iteration, otherwise our context for
894 * getting the next result.
895 * @return GNUNET_OK on success, GNUNET_NO if there are no more results,
896 * GNUNET_SYSERR on error (or end of iteration)
899 zero_iter_next_prepare (void *cls,
900 struct NextContext *nc)
902 struct ZeroIterContext *ic = cls;
903 struct Plugin *plugin;
909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
910 "Asked to clean up iterator state.\n");
912 sqlite3_finalize (ic->stmt_1);
913 sqlite3_finalize (ic->stmt_2);
914 return GNUNET_SYSERR;
918 /* first try iter 1 */
920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
921 "Restricting to results larger than the last priority %u and key `%s'\n",
923 GNUNET_h2s (&nc->lastKey));
925 if ( (SQLITE_OK != sqlite3_bind_int (ic->stmt_1, 1, nc->lastPriority)) ||
926 (SQLITE_OK != sqlite3_bind_blob (ic->stmt_1, 2,
928 sizeof (GNUNET_HashCode),
931 LOG_SQLITE (plugin, NULL,
932 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX");
933 if (SQLITE_OK != sqlite3_reset (ic->stmt_1))
934 LOG_SQLITE (plugin, NULL,
935 GNUNET_ERROR_TYPE_ERROR |
936 GNUNET_ERROR_TYPE_BULK,
938 return GNUNET_SYSERR;
940 if (SQLITE_ROW == (ret = sqlite3_step (ic->stmt_1)))
943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
944 "Result found using iterator 1\n");
946 nc->stmt = ic->stmt_1;
949 if (ret != SQLITE_DONE)
951 LOG_SQLITE (plugin, NULL,
952 GNUNET_ERROR_TYPE_ERROR |
953 GNUNET_ERROR_TYPE_BULK,
955 if (SQLITE_OK != sqlite3_reset (ic->stmt_1))
956 LOG_SQLITE (plugin, NULL,
957 GNUNET_ERROR_TYPE_ERROR |
958 GNUNET_ERROR_TYPE_BULK,
960 return GNUNET_SYSERR;
962 if (SQLITE_OK != sqlite3_reset (ic->stmt_1))
963 LOG_SQLITE (plugin, NULL,
964 GNUNET_ERROR_TYPE_ERROR |
965 GNUNET_ERROR_TYPE_BULK,
969 if (SQLITE_OK != sqlite3_bind_int (ic->stmt_2, 1, nc->lastPriority))
971 LOG_SQLITE (plugin, NULL,
972 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX");
973 return GNUNET_SYSERR;
975 if (SQLITE_ROW == (ret = sqlite3_step (ic->stmt_2)))
978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
979 "Result found using iterator 2\n");
981 nc->stmt = ic->stmt_2;
984 if (ret != SQLITE_DONE)
986 LOG_SQLITE (plugin, NULL,
987 GNUNET_ERROR_TYPE_ERROR |
988 GNUNET_ERROR_TYPE_BULK,
990 if (SQLITE_OK != sqlite3_reset (ic->stmt_2))
991 LOG_SQLITE (plugin, NULL,
992 GNUNET_ERROR_TYPE_ERROR |
993 GNUNET_ERROR_TYPE_BULK,
995 return GNUNET_SYSERR;
997 if (SQLITE_OK != sqlite3_reset (ic->stmt_2))
998 LOG_SQLITE (plugin, NULL,
999 GNUNET_ERROR_TYPE_ERROR |
1000 GNUNET_ERROR_TYPE_BULK,
1003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1004 "No result found using either iterator\n");
1011 * Select a subset of the items in the datastore and call
1012 * the given iterator for each of them.
1014 * @param cls our plugin context
1015 * @param type entries of which type should be considered?
1016 * Use 0 for any type.
1017 * @param iter function to call on each matching value;
1018 * will be called once with a NULL value at the end
1019 * @param iter_cls closure for iter
1022 sqlite_plugin_iter_zero_anonymity (void *cls,
1023 enum GNUNET_BLOCK_Type type,
1024 PluginIterator iter,
1027 struct Plugin *plugin = cls;
1028 struct GNUNET_TIME_Absolute now;
1029 struct NextContext *nc;
1030 struct ZeroIterContext *ic;
1031 sqlite3_stmt *stmt_1;
1032 sqlite3_stmt *stmt_2;
1035 GNUNET_assert (type != GNUNET_BLOCK_TYPE_ANY);
1036 now = GNUNET_TIME_absolute_get ();
1037 GNUNET_asprintf (&q,
1038 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090 "
1039 "WHERE (anonLevel = 0 AND expire > %llu AND prio = ?1 AND type=%d AND hash < ?2) "
1040 "ORDER BY hash DESC LIMIT 1",
1041 (unsigned long long) now.abs_value,
1043 if (sq_prepare (plugin->dbh, q, &stmt_1) != SQLITE_OK)
1045 LOG_SQLITE (plugin, NULL,
1046 GNUNET_ERROR_TYPE_ERROR |
1047 GNUNET_ERROR_TYPE_BULK, "sqlite3_prepare_v2");
1048 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1053 GNUNET_asprintf (&q,
1054 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090 "
1055 "WHERE (anonLevel = 0 AND expire > %llu AND prio < ?1 AND type=%d) "
1056 "ORDER BY prio DESC, hash DESC LIMIT 1",
1057 (unsigned long long) now.abs_value,
1059 if (sq_prepare (plugin->dbh, q, &stmt_2) != SQLITE_OK)
1061 LOG_SQLITE (plugin, NULL,
1062 GNUNET_ERROR_TYPE_ERROR |
1063 GNUNET_ERROR_TYPE_BULK, "sqlite3_prepare_v2");
1064 sqlite3_finalize (stmt_1);
1065 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1070 nc = GNUNET_malloc (sizeof(struct NextContext) +
1071 sizeof(struct ZeroIterContext));
1072 nc->plugin = plugin;
1074 nc->iter_cls = iter_cls;
1076 ic = (struct ZeroIterContext*) &nc[1];
1077 ic->stmt_1 = stmt_1;
1078 ic->stmt_2 = stmt_2;
1080 nc->prep = &zero_iter_next_prepare;
1082 nc->lastPriority = INT32_MAX;
1083 memset (&nc->lastKey, 255, sizeof (GNUNET_HashCode));
1084 sqlite_next_request (nc, GNUNET_NO);
1089 * Context for get_next_prepare.
1091 struct GetNextContext
1095 * Our prepared statement.
1102 struct Plugin *plugin;
1105 * Key for the query.
1107 GNUNET_HashCode key;
1110 * Vhash for the query.
1112 GNUNET_HashCode vhash;
1115 * Expected total number of results.
1120 * Offset to add for the selected result.
1130 * Desired block type.
1132 enum GNUNET_BLOCK_Type type;
1138 * Prepare the stmt in 'nc' for the next round of execution, selecting the
1139 * next return value.
1141 * @param cls our "struct GetNextContext*"
1142 * @param nc the general context
1143 * @return GNUNET_YES if there are more results,
1144 * GNUNET_NO if there are no more results,
1145 * GNUNET_SYSERR on internal error
1148 get_next_prepare (void *cls,
1149 struct NextContext *nc)
1151 struct GetNextContext *gnc = cls;
1158 sqlite3_finalize (gnc->stmt);
1159 return GNUNET_SYSERR;
1161 if (nc->count == gnc->total)
1163 if (nc->count + gnc->off == gnc->total)
1166 limit_off = gnc->off;
1169 sqlite3_reset (nc->stmt);
1171 ret = sqlite3_bind_blob (nc->stmt,
1174 sizeof (GNUNET_HashCode),
1176 if ((gnc->have_vhash) && (ret == SQLITE_OK))
1177 ret = sqlite3_bind_blob (nc->stmt,
1180 sizeof (GNUNET_HashCode), SQLITE_TRANSIENT);
1181 if ((gnc->type != 0) && (ret == SQLITE_OK))
1182 ret = sqlite3_bind_int (nc->stmt, sqoff++, gnc->type);
1183 if (ret == SQLITE_OK)
1184 ret = sqlite3_bind_int64 (nc->stmt, sqoff++, limit_off);
1185 if (ret != SQLITE_OK)
1186 return GNUNET_SYSERR;
1188 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1190 "Preparing to GET for key `%s' with type %d at offset %u\n",
1191 GNUNET_h2s (&gnc->key),
1195 ret = sqlite3_step (nc->stmt);
1203 LOG_SQLITE (gnc->plugin, NULL,
1204 GNUNET_ERROR_TYPE_ERROR |
1205 GNUNET_ERROR_TYPE_BULK,
1207 return GNUNET_SYSERR;
1213 * Iterate over the results for a particular key
1216 * @param cls closure
1217 * @param key key to match, never NULL
1218 * @param vhash hash of the value, maybe NULL (to
1219 * match all values that have the right key).
1220 * Note that for DBlocks there is no difference
1221 * betwen key and vhash, but for other blocks
1223 * @param type entries of which type are relevant?
1224 * Use 0 for any type.
1225 * @param iter function to call on each matching value;
1226 * will be called once with a NULL value at the end
1227 * @param iter_cls closure for iter
1230 sqlite_plugin_get (void *cls,
1231 const GNUNET_HashCode *key,
1232 const GNUNET_HashCode *vhash,
1233 enum GNUNET_BLOCK_Type type,
1234 PluginIterator iter, void *iter_cls)
1236 struct Plugin *plugin = cls;
1237 struct GetNextContext *gnc;
1238 struct NextContext *nc;
1245 GNUNET_assert (iter != NULL);
1246 GNUNET_assert (key != NULL);
1247 GNUNET_snprintf (scratch, sizeof (scratch),
1248 "SELECT count(*) FROM gn090 WHERE hash=?%s%s",
1249 vhash == NULL ? "" : " AND vhash=?",
1250 type == 0 ? "" : " AND type=?");
1251 if (sq_prepare (plugin->dbh, scratch, &stmt) != SQLITE_OK)
1253 LOG_SQLITE (plugin, NULL,
1254 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite_prepare");
1255 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1259 ret = sqlite3_bind_blob (stmt, sqoff++,
1260 key, sizeof (GNUNET_HashCode), SQLITE_TRANSIENT);
1261 if ((vhash != NULL) && (ret == SQLITE_OK))
1262 ret = sqlite3_bind_blob (stmt, sqoff++,
1264 sizeof (GNUNET_HashCode), SQLITE_TRANSIENT);
1265 if ((type != 0) && (ret == SQLITE_OK))
1266 ret = sqlite3_bind_int (stmt, sqoff++, type);
1267 if (SQLITE_OK != ret)
1269 LOG_SQLITE (plugin, NULL,
1270 GNUNET_ERROR_TYPE_ERROR, "sqlite_bind");
1271 sqlite3_finalize (stmt);
1272 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1275 ret = sqlite3_step (stmt);
1276 if (ret != SQLITE_ROW)
1278 LOG_SQLITE (plugin, NULL,
1279 GNUNET_ERROR_TYPE_ERROR| GNUNET_ERROR_TYPE_BULK,
1281 sqlite3_finalize (stmt);
1282 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1285 total = sqlite3_column_int (stmt, 0);
1286 sqlite3_finalize (stmt);
1289 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1292 GNUNET_snprintf (scratch, sizeof (scratch),
1293 "SELECT type, prio, anonLevel, expire, hash, value, _ROWID_ "
1294 "FROM gn090 WHERE hash=?%s%s "
1295 "ORDER BY _ROWID_ ASC LIMIT 1 OFFSET ?",
1296 vhash == NULL ? "" : " AND vhash=?",
1297 type == 0 ? "" : " AND type=?");
1299 if (sq_prepare (plugin->dbh, scratch, &stmt) != SQLITE_OK)
1301 LOG_SQLITE (plugin, NULL,
1302 GNUNET_ERROR_TYPE_ERROR |
1303 GNUNET_ERROR_TYPE_BULK, "sqlite_prepare");
1304 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1307 nc = GNUNET_malloc (sizeof(struct NextContext) +
1308 sizeof(struct GetNextContext));
1309 nc->plugin = plugin;
1311 nc->iter_cls = iter_cls;
1313 gnc = (struct GetNextContext*) &nc[1];
1317 gnc->plugin = plugin;
1318 gnc->stmt = stmt; /* alias used for freeing at the end! */
1321 gnc->have_vhash = GNUNET_YES;
1322 gnc->vhash = *vhash;
1324 gnc->off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, total);
1325 nc->prep = &get_next_prepare;
1327 sqlite_next_request (nc, GNUNET_NO);
1332 * Execute statement that gets a row and call the callback
1333 * with the result. Resets the statement afterwards.
1335 * @param plugin the plugin
1336 * @param stmt the statement
1337 * @param iter iterator to call
1338 * @param iter_cls closure for 'iter'
1341 execute_get (struct Plugin *plugin,
1343 PluginIterator iter, void *iter_cls)
1346 struct GNUNET_TIME_Absolute expiration;
1347 unsigned long long rowid;
1351 n = sqlite3_step (stmt);
1355 size = sqlite3_column_bytes (stmt, 5);
1356 rowid = sqlite3_column_int64 (stmt, 6);
1357 if (sqlite3_column_bytes (stmt, 4) != sizeof (GNUNET_HashCode))
1359 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1361 _("Invalid data in database. Trying to fix (by deletion).\n"));
1362 if (SQLITE_OK != sqlite3_reset (stmt))
1363 LOG_SQLITE (plugin, NULL,
1364 GNUNET_ERROR_TYPE_ERROR |
1365 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
1366 if (GNUNET_OK == delete_by_rowid (plugin, rowid))
1367 plugin->env->duc (plugin->env->cls,
1368 - (size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
1371 expiration.abs_value = sqlite3_column_int64 (stmt, 3);
1372 ret = iter (iter_cls,
1374 sqlite3_column_blob (stmt, 4) /* key */,
1376 sqlite3_column_blob (stmt, 5) /* data */,
1377 sqlite3_column_int (stmt, 0) /* type */,
1378 sqlite3_column_int (stmt, 1) /* priority */,
1379 sqlite3_column_int (stmt, 2) /* anonymity */,
1382 if (SQLITE_OK != sqlite3_reset (stmt))
1383 LOG_SQLITE (plugin, NULL,
1384 GNUNET_ERROR_TYPE_ERROR |
1385 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
1386 if ( (GNUNET_NO == ret) &&
1387 (GNUNET_OK == delete_by_rowid (plugin, rowid)) )
1388 plugin->env->duc (plugin->env->cls,
1389 - (size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
1392 /* database must be empty */
1393 if (SQLITE_OK != sqlite3_reset (stmt))
1394 LOG_SQLITE (plugin, NULL,
1395 GNUNET_ERROR_TYPE_ERROR |
1396 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
1402 LOG_SQLITE (plugin, NULL,
1403 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1405 if (SQLITE_OK != sqlite3_reset (stmt))
1406 LOG_SQLITE (plugin, NULL,
1407 GNUNET_ERROR_TYPE_ERROR |
1408 GNUNET_ERROR_TYPE_BULK,
1411 database_shutdown (plugin);
1412 database_setup (plugin->env->cfg,
1416 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0,
1417 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1422 * Get a random item for replication. Returns a single, not expired, random item
1423 * from those with the highest replication counters. The item's
1424 * replication counter is decremented by one IF it was positive before.
1425 * Call 'iter' with all values ZERO or NULL if the datastore is empty.
1427 * @param cls closure
1428 * @param iter function to call the value (once only).
1429 * @param iter_cls closure for iter
1432 sqlite_plugin_replication_get (void *cls,
1433 PluginIterator iter, void *iter_cls)
1435 struct Plugin *plugin = cls;
1437 struct GNUNET_TIME_Absolute now;
1440 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1442 "Getting random block based on replication order.\n");
1444 stmt = plugin->selRepl;
1445 now = GNUNET_TIME_absolute_get ();
1446 if (SQLITE_OK != sqlite3_bind_int64 (stmt, 1, now.abs_value))
1448 LOG_SQLITE (plugin, NULL,
1449 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX");
1450 if (SQLITE_OK != sqlite3_reset (stmt))
1451 LOG_SQLITE (plugin, NULL,
1452 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
1453 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0,
1454 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1457 execute_get (plugin, stmt, iter, iter_cls);
1463 * Get a random item that has expired or has low priority.
1464 * Call 'iter' with all values ZERO or NULL if the datastore is empty.
1466 * @param cls closure
1467 * @param iter function to call the value (once only).
1468 * @param iter_cls closure for iter
1471 sqlite_plugin_expiration_get (void *cls,
1472 PluginIterator iter, void *iter_cls)
1474 struct Plugin *plugin = cls;
1476 struct GNUNET_TIME_Absolute now;
1479 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1481 "Getting random block based on expiration and priority order.\n");
1483 now = GNUNET_TIME_absolute_get ();
1484 stmt = plugin->selExpi;
1485 if (SQLITE_OK != sqlite3_bind_int64 (stmt, 1, now.abs_value))
1487 LOG_SQLITE (plugin, NULL,
1488 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX");
1489 if (SQLITE_OK != sqlite3_reset (stmt))
1490 LOG_SQLITE (plugin, NULL,
1491 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
1492 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0,
1493 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1496 execute_get (plugin, stmt, iter, iter_cls);
1503 * @param cls our plugin context
1506 sqlite_plugin_drop (void *cls)
1508 struct Plugin *plugin = cls;
1509 plugin->drop_on_shutdown = GNUNET_YES;
1514 * Get an estimate of how much space the database is
1517 * @param cls the 'struct Plugin'
1518 * @return the size of the database on disk (estimate)
1520 static unsigned long long
1521 sqlite_plugin_get_size (void *cls)
1523 struct Plugin *plugin = cls;
1531 if (SQLITE_VERSION_NUMBER < 3006000)
1533 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1535 _("sqlite version to old to determine size, assuming zero\n"));
1539 sqlite3_exec (plugin->dbh,
1540 "VACUUM", NULL, NULL, ENULL));
1542 sqlite3_exec (plugin->dbh,
1543 "PRAGMA auto_vacuum=INCREMENTAL", NULL, NULL, ENULL));
1545 sq_prepare (plugin->dbh,
1546 "PRAGMA page_count",
1549 sqlite3_step (stmt))
1550 pages = sqlite3_column_int64 (stmt, 0);
1553 sqlite3_finalize (stmt);
1555 sq_prepare (plugin->dbh,
1558 CHECK (SQLITE_ROW ==
1559 sqlite3_step (stmt));
1560 page_size = sqlite3_column_int64 (stmt, 0);
1561 sqlite3_finalize (stmt);
1562 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1563 _("Using sqlite page utilization to estimate payload (%llu pages of size %llu bytes)\n"),
1564 (unsigned long long) pages,
1565 (unsigned long long) page_size);
1566 return pages * page_size;
1571 * Entry point for the plugin.
1573 * @param cls the "struct GNUNET_DATASTORE_PluginEnvironment*"
1574 * @return NULL on error, othrewise the plugin context
1577 libgnunet_plugin_datastore_sqlite_init (void *cls)
1579 static struct Plugin plugin;
1580 struct GNUNET_DATASTORE_PluginEnvironment *env = cls;
1581 struct GNUNET_DATASTORE_PluginFunctions *api;
1583 if (plugin.env != NULL)
1584 return NULL; /* can only initialize once! */
1585 memset (&plugin, 0, sizeof(struct Plugin));
1588 database_setup (env->cfg, &plugin))
1590 database_shutdown (&plugin);
1593 api = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_PluginFunctions));
1595 api->get_size = &sqlite_plugin_get_size;
1596 api->put = &sqlite_plugin_put;
1597 api->next_request = &sqlite_next_request;
1598 api->get = &sqlite_plugin_get;
1599 api->replication_get = &sqlite_plugin_replication_get;
1600 api->expiration_get = &sqlite_plugin_expiration_get;
1601 api->update = &sqlite_plugin_update;
1602 api->iter_zero_anonymity = &sqlite_plugin_iter_zero_anonymity;
1603 api->drop = &sqlite_plugin_drop;
1604 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1605 "sqlite", _("Sqlite database running\n"));
1611 * Exit point from the plugin.
1613 * @param cls the plugin context (as returned by "init")
1614 * @return always NULL
1617 libgnunet_plugin_datastore_sqlite_done (void *cls)
1620 struct GNUNET_DATASTORE_PluginFunctions *api = cls;
1621 struct Plugin *plugin = api->cls;
1624 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1626 "sqlite plugin is doneing\n");
1629 if (plugin->next_task != GNUNET_SCHEDULER_NO_TASK)
1632 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1634 "Canceling next task\n");
1636 GNUNET_SCHEDULER_cancel (plugin->next_task);
1637 plugin->next_task = GNUNET_SCHEDULER_NO_TASK;
1639 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1641 "Prep'ing next task\n");
1643 plugin->next_task_nc->prep (plugin->next_task_nc->prep_cls, NULL);
1644 GNUNET_free (plugin->next_task_nc);
1645 plugin->next_task_nc = NULL;
1648 if (plugin->drop_on_shutdown)
1649 fn = GNUNET_strdup (plugin->fn);
1651 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1653 "Shutting down database\n");
1655 database_shutdown (plugin);
1660 if (0 != UNLINK(fn))
1661 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1667 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1669 "sqlite plugin is finished doneing\n");
1674 /* end of plugin_datastore_sqlite.c */