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);
177 sqlite3_exec (dbh, "CREATE INDEX idx_prio ON gn090 (prio)", NULL, NULL,
179 sqlite3_exec (dbh, "CREATE INDEX idx_expire_prio ON gn090 (expire,prio)", NULL, NULL,
182 "CREATE INDEX idx_hash_vhash ON gn090 (hash,vhash)", NULL,
184 sqlite3_exec (dbh, "CREATE INDEX idx_comb ON gn090 (prio,expire,anonLevel,hash)",
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 auto_vacuum=INCREMENTAL", NULL, NULL, ENULL));
270 sqlite3_exec (plugin->dbh,
271 "PRAGMA count_changes=OFF", NULL, NULL, ENULL));
273 sqlite3_exec (plugin->dbh,
274 "PRAGMA page_size=4092", NULL, NULL, ENULL));
276 CHECK (SQLITE_OK == sqlite3_busy_timeout (plugin->dbh, BUSY_TIMEOUT_MS));
279 /* We have to do it here, because otherwise precompiling SQL might fail */
281 sq_prepare (plugin->dbh,
282 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'gn090'",
284 if ( (sqlite3_step (stmt) == SQLITE_DONE) &&
285 (sqlite3_exec (plugin->dbh,
286 "CREATE TABLE gn090 ("
287 " repl INT4 NOT NULL DEFAULT 0,"
288 " type INT4 NOT NULL DEFAULT 0,"
289 " prio INT4 NOT NULL DEFAULT 0,"
290 " anonLevel INT4 NOT NULL DEFAULT 0,"
291 " expire INT8 NOT NULL DEFAULT 0,"
292 " hash TEXT NOT NULL DEFAULT '',"
293 " vhash TEXT NOT NULL DEFAULT '',"
294 " value BLOB NOT NULL DEFAULT '')", NULL, NULL,
295 NULL) != SQLITE_OK) )
297 LOG_SQLITE (plugin, NULL,
298 GNUNET_ERROR_TYPE_ERROR,
300 sqlite3_finalize (stmt);
301 return GNUNET_SYSERR;
303 sqlite3_finalize (stmt);
304 create_indices (plugin->dbh);
307 sq_prepare (plugin->dbh,
308 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'gn071'",
310 if ( (sqlite3_step (stmt) == SQLITE_DONE) &&
311 (sqlite3_exec (plugin->dbh,
312 "CREATE TABLE gn071 ("
313 " key TEXT NOT NULL DEFAULT '',"
314 " value INTEGER NOT NULL DEFAULT 0)", NULL, NULL,
315 NULL) != SQLITE_OK) )
317 LOG_SQLITE (plugin, NULL,
318 GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec");
319 sqlite3_finalize (stmt);
320 return GNUNET_SYSERR;
322 sqlite3_finalize (stmt);
324 if ((sq_prepare (plugin->dbh,
325 "UPDATE gn090 SET prio = prio + ?, expire = MAX(expire,?) WHERE "
327 &plugin->updPrio) != SQLITE_OK) ||
328 (sq_prepare (plugin->dbh,
329 "UPDATE gn090 SET repl = MAX (0, repl - 1) WHERE "
331 &plugin->updRepl) != SQLITE_OK) ||
332 (sq_prepare (plugin->dbh,
333 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090 WHERE (expire > ?1) "
334 " ORDER BY repl DESC, Random() LIMIT 1",
335 &plugin->selRepl) != SQLITE_OK) ||
336 (sq_prepare (plugin->dbh,
337 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090 WHERE (expire < ?1) "
338 " OR NOT EXISTS (SELECT 1 from gn090 WHERE (expire < ?1)) "
339 " ORDER BY prio ASC LIMIT 1",
340 &plugin->selExpi) != SQLITE_OK) ||
341 (sq_prepare (plugin->dbh,
342 "INSERT INTO gn090 (repl, type, prio, "
343 "anonLevel, expire, hash, vhash, value) VALUES "
344 "(?, ?, ?, ?, ?, ?, ?, ?)",
345 &plugin->insertContent) != SQLITE_OK) ||
346 (sq_prepare (plugin->dbh,
347 "DELETE FROM gn090 WHERE _ROWID_ = ?",
348 &plugin->delRow) != SQLITE_OK))
350 LOG_SQLITE (plugin, NULL,
351 GNUNET_ERROR_TYPE_ERROR, "precompiling");
352 return GNUNET_SYSERR;
360 * Shutdown database connection and associate data
362 * @param plugin the plugin context (state for this module)
365 database_shutdown (struct Plugin *plugin)
368 #if SQLITE_VERSION_NUMBER >= 3007000
372 if (plugin->delRow != NULL)
373 sqlite3_finalize (plugin->delRow);
374 if (plugin->updPrio != NULL)
375 sqlite3_finalize (plugin->updPrio);
376 if (plugin->updRepl != NULL)
377 sqlite3_finalize (plugin->updRepl);
378 if (plugin->selRepl != NULL)
379 sqlite3_finalize (plugin->selRepl);
380 if (plugin->selExpi != NULL)
381 sqlite3_finalize (plugin->selExpi);
382 if (plugin->insertContent != NULL)
383 sqlite3_finalize (plugin->insertContent);
384 result = sqlite3_close(plugin->dbh);
385 #if SQLITE_VERSION_NUMBER >= 3007000
386 if (result == SQLITE_BUSY)
388 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
390 _("Tried to close sqlite without finalizing all prepared statements.\n"));
391 stmt = sqlite3_next_stmt(plugin->dbh, NULL);
395 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
396 "sqlite", "Closing statement %p\n", stmt);
398 result = sqlite3_finalize(stmt);
400 if (result != SQLITE_OK)
401 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
403 "Failed to close statement %p: %d\n", stmt, result);
405 stmt = sqlite3_next_stmt(plugin->dbh, NULL);
407 result = sqlite3_close(plugin->dbh);
410 if (SQLITE_OK != result)
411 LOG_SQLITE (plugin, NULL,
412 GNUNET_ERROR_TYPE_ERROR,
415 GNUNET_free_non_null (plugin->fn);
420 * Delete the database entry with the given
423 * @param plugin the plugin context (state for this module)
424 * @param rid the ID of the row to delete
427 delete_by_rowid (struct Plugin* plugin,
428 unsigned long long rid)
430 sqlite3_bind_int64 (plugin->delRow, 1, rid);
431 if (SQLITE_DONE != sqlite3_step (plugin->delRow))
433 LOG_SQLITE (plugin, NULL,
434 GNUNET_ERROR_TYPE_ERROR |
435 GNUNET_ERROR_TYPE_BULK, "sqlite3_step");
436 if (SQLITE_OK != sqlite3_reset (plugin->delRow))
437 LOG_SQLITE (plugin, NULL,
438 GNUNET_ERROR_TYPE_ERROR |
439 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
440 return GNUNET_SYSERR;
442 if (SQLITE_OK != sqlite3_reset (plugin->delRow))
443 LOG_SQLITE (plugin, NULL,
444 GNUNET_ERROR_TYPE_ERROR |
445 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
451 * Context for the universal iterator.
456 * Type of a function that will prepare
457 * the next iteration.
460 * @param nc the next context; NULL for the last
461 * call which gives the callback a chance to
462 * clean up the closure
463 * @return GNUNET_OK on success, GNUNET_NO if there are
464 * no more values, GNUNET_SYSERR on error
466 typedef int (*PrepareFunction)(void *cls,
467 struct NextContext *nc);
471 * Context we keep for the "next request" callback.
478 struct Plugin *plugin;
481 * Function to call on the next value.
491 * Function to call to prepare the next
494 PrepareFunction prep;
502 * Statement that the iterator will get the data
503 * from (updated or set by prep).
508 * Row ID of the last result.
510 unsigned long long last_rowid;
513 * Key of the last result.
515 GNUNET_HashCode lastKey;
518 * Priority of the last value visited.
520 unsigned int lastPriority;
523 * Number of results processed so far.
528 * Set to GNUNET_YES if we must stop now.
535 * Continuation of "sqlite_next_request".
537 * @param cls the 'struct NextContext*'
538 * @param tc the task context (unused)
541 sqlite_next_request_cont (void *cls,
542 const struct GNUNET_SCHEDULER_TaskContext *tc)
544 struct NextContext * nc = cls;
545 struct Plugin *plugin;
546 unsigned long long rowid;
552 enum GNUNET_BLOCK_Type type;
553 const GNUNET_HashCode *key;
554 struct GNUNET_TIME_Absolute expiration;
557 plugin->next_task = GNUNET_SCHEDULER_NO_TASK;
558 plugin->next_task_nc = NULL;
559 if ( (GNUNET_YES == nc->end_it) ||
560 (GNUNET_OK != (nc->prep(nc->prep_cls,
564 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
566 "Iteration completes after %u results\n",
570 nc->iter (nc->iter_cls,
571 NULL, NULL, 0, NULL, 0, 0, 0,
572 GNUNET_TIME_UNIT_ZERO_ABS, 0);
573 nc->prep (nc->prep_cls, NULL);
578 type = sqlite3_column_int (nc->stmt, 0);
579 priority = sqlite3_column_int (nc->stmt, 1);
580 anonymity = sqlite3_column_int (nc->stmt, 2);
581 expiration.abs_value = sqlite3_column_int64 (nc->stmt, 3);
582 hsize = sqlite3_column_bytes (nc->stmt, 4);
583 key = sqlite3_column_blob (nc->stmt, 4);
584 size = sqlite3_column_bytes (nc->stmt, 5);
585 rowid = sqlite3_column_int64 (nc->stmt, 6);
586 if (hsize != sizeof (GNUNET_HashCode))
589 if (SQLITE_OK != sqlite3_reset (nc->stmt))
590 LOG_SQLITE (plugin, NULL,
591 GNUNET_ERROR_TYPE_ERROR |
592 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
593 if (GNUNET_OK == delete_by_rowid (plugin, rowid))
594 plugin->env->duc (plugin->env->cls,
595 - (size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
599 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
601 "Iterator returns value with type %u/key `%s'/priority %u/expiration %llu (%lld).\n",
605 (unsigned long long) GNUNET_TIME_absolute_get_remaining (expiration).rel_value,
606 (long long) expiration.abs_value);
608 if (size > MAX_ITEM_SIZE)
611 if (SQLITE_OK != sqlite3_reset (nc->stmt))
612 LOG_SQLITE (plugin, NULL,
613 GNUNET_ERROR_TYPE_ERROR |
614 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
615 if (GNUNET_OK == delete_by_rowid (plugin, rowid))
616 plugin->env->duc (plugin->env->cls,
617 - (size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
623 memcpy (data, sqlite3_column_blob (nc->stmt, 5), size);
625 nc->last_rowid = rowid;
626 nc->lastPriority = priority;
628 if (SQLITE_OK != sqlite3_reset (nc->stmt))
629 LOG_SQLITE (plugin, NULL,
630 GNUNET_ERROR_TYPE_ERROR |
631 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
632 ret = nc->iter (nc->iter_cls, nc,
636 anonymity, expiration,
642 nc->end_it = GNUNET_YES;
645 if (GNUNET_OK == delete_by_rowid (plugin, rowid))
648 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
650 "Removed entry %llu (%u bytes)\n",
651 (unsigned long long) rowid,
652 size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
654 plugin->env->duc (plugin->env->cls,
655 - (size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
667 * Function invoked on behalf of a "PluginIterator" asking the
668 * database plugin to call the iterator with the next item.
670 * @param next_cls whatever argument was given
671 * to the PluginIterator as "next_cls".
672 * @param end_it set to GNUNET_YES if we
673 * should terminate the iteration early
674 * (iterator should be still called once more
675 * to signal the end of the iteration).
678 sqlite_next_request (void *next_cls,
681 struct NextContext * nc= next_cls;
683 if (GNUNET_YES == end_it)
684 nc->end_it = GNUNET_YES;
685 nc->plugin->next_task_nc = nc;
686 nc->plugin->next_task = GNUNET_SCHEDULER_add_now (&sqlite_next_request_cont,
692 * Store an item in the datastore.
695 * @param key key for the item
696 * @param size number of bytes in data
697 * @param data content stored
698 * @param type type of the content
699 * @param priority priority of the content
700 * @param anonymity anonymity-level for the content
701 * @param replication replication-level for the content
702 * @param expiration expiration time for the content
703 * @param msg set to an error message
704 * @return GNUNET_OK on success
707 sqlite_plugin_put (void *cls,
708 const GNUNET_HashCode *key,
711 enum GNUNET_BLOCK_Type type,
714 uint32_t replication,
715 struct GNUNET_TIME_Absolute expiration,
718 struct Plugin *plugin = cls;
722 GNUNET_HashCode vhash;
724 if (size > MAX_ITEM_SIZE)
725 return GNUNET_SYSERR;
727 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
729 "Storing in database block with type %u/key `%s'/priority %u/expiration %llu (%lld).\n",
733 (unsigned long long) GNUNET_TIME_absolute_get_remaining (expiration).rel_value,
734 (long long) expiration.abs_value);
736 GNUNET_CRYPTO_hash (data, size, &vhash);
737 stmt = plugin->insertContent;
738 if ((SQLITE_OK != sqlite3_bind_int (stmt, 1, replication)) ||
739 (SQLITE_OK != sqlite3_bind_int (stmt, 2, type)) ||
740 (SQLITE_OK != sqlite3_bind_int (stmt, 3, priority)) ||
741 (SQLITE_OK != sqlite3_bind_int (stmt, 4, anonymity)) ||
742 (SQLITE_OK != sqlite3_bind_int64 (stmt, 5, expiration.abs_value)) ||
744 sqlite3_bind_blob (stmt, 6, key, sizeof (GNUNET_HashCode),
745 SQLITE_TRANSIENT)) ||
747 sqlite3_bind_blob (stmt, 7, &vhash, sizeof (GNUNET_HashCode),
750 sqlite3_bind_blob (stmt, 8, data, size,
755 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX");
756 if (SQLITE_OK != sqlite3_reset (stmt))
757 LOG_SQLITE (plugin, NULL,
758 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
759 return GNUNET_SYSERR;
761 n = sqlite3_step (stmt);
765 plugin->env->duc (plugin->env->cls,
766 size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
768 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
770 "Stored new entry (%u bytes)\n",
771 size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
777 LOG_SQLITE (plugin, msg,
778 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
783 LOG_SQLITE (plugin, msg,
784 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
786 if (SQLITE_OK != sqlite3_reset (stmt))
787 LOG_SQLITE (plugin, NULL,
788 GNUNET_ERROR_TYPE_ERROR |
789 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
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");
804 * Update the priority for a particular key in the datastore. If
805 * the expiration time in value is different than the time found in
806 * the datastore, the higher value should be kept. For the
807 * anonymity level, the lower value is to be used. The specified
808 * priority should be added to the existing priority, ignoring the
811 * Note that it is possible for multiple values to match this put.
812 * In that case, all of the respective values are updated.
814 * @param cls the plugin context (state for this module)
815 * @param uid unique identifier of the datum
816 * @param delta by how much should the priority
817 * change? If priority + delta < 0 the
818 * priority should be set to 0 (never go
820 * @param expire new expiration time should be the
821 * MAX of any existing expiration time and
823 * @param msg set to an error message
824 * @return GNUNET_OK on success
827 sqlite_plugin_update (void *cls,
829 int delta, struct GNUNET_TIME_Absolute expire,
832 struct Plugin *plugin = cls;
835 sqlite3_bind_int (plugin->updPrio, 1, delta);
836 sqlite3_bind_int64 (plugin->updPrio, 2, expire.abs_value);
837 sqlite3_bind_int64 (plugin->updPrio, 3, uid);
838 n = sqlite3_step (plugin->updPrio);
839 sqlite3_reset (plugin->updPrio);
844 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
850 LOG_SQLITE (plugin, msg,
851 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
855 LOG_SQLITE (plugin, msg,
856 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
858 return GNUNET_SYSERR;
864 * Internal context for an iteration.
866 struct ZeroIterContext
869 * First iterator statement for zero-anonymity iteration.
871 sqlite3_stmt *stmt_1;
874 * Second iterator statement for zero-anonymity iteration.
876 sqlite3_stmt *stmt_2;
879 * Desired type for blocks returned by this iterator.
881 enum GNUNET_BLOCK_Type type;
886 * Prepare our SQL query to obtain the next record from the database.
888 * @param cls our "struct ZeroIterContext"
889 * @param nc NULL to terminate the iteration, otherwise our context for
890 * getting the next result.
891 * @return GNUNET_OK on success, GNUNET_NO if there are no more results,
892 * GNUNET_SYSERR on error (or end of iteration)
895 zero_iter_next_prepare (void *cls,
896 struct NextContext *nc)
898 struct ZeroIterContext *ic = cls;
899 struct Plugin *plugin;
905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
906 "Asked to clean up iterator state.\n");
908 sqlite3_finalize (ic->stmt_1);
909 sqlite3_finalize (ic->stmt_2);
910 return GNUNET_SYSERR;
914 /* first try iter 1 */
916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
917 "Restricting to results larger than the last priority %u and key `%s'\n",
919 GNUNET_h2s (&nc->lastKey));
921 if ( (SQLITE_OK != sqlite3_bind_int (ic->stmt_1, 1, nc->lastPriority)) ||
922 (SQLITE_OK != sqlite3_bind_blob (ic->stmt_1, 2,
924 sizeof (GNUNET_HashCode),
927 LOG_SQLITE (plugin, NULL,
928 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX");
929 if (SQLITE_OK != sqlite3_reset (ic->stmt_1))
930 LOG_SQLITE (plugin, NULL,
931 GNUNET_ERROR_TYPE_ERROR |
932 GNUNET_ERROR_TYPE_BULK,
934 return GNUNET_SYSERR;
936 if (SQLITE_ROW == (ret = sqlite3_step (ic->stmt_1)))
939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
940 "Result found using iterator 1\n");
942 nc->stmt = ic->stmt_1;
945 if (ret != SQLITE_DONE)
947 LOG_SQLITE (plugin, NULL,
948 GNUNET_ERROR_TYPE_ERROR |
949 GNUNET_ERROR_TYPE_BULK,
951 if (SQLITE_OK != sqlite3_reset (ic->stmt_1))
952 LOG_SQLITE (plugin, NULL,
953 GNUNET_ERROR_TYPE_ERROR |
954 GNUNET_ERROR_TYPE_BULK,
956 return GNUNET_SYSERR;
958 if (SQLITE_OK != sqlite3_reset (ic->stmt_1))
959 LOG_SQLITE (plugin, NULL,
960 GNUNET_ERROR_TYPE_ERROR |
961 GNUNET_ERROR_TYPE_BULK,
965 if (SQLITE_OK != sqlite3_bind_int (ic->stmt_2, 1, nc->lastPriority))
967 LOG_SQLITE (plugin, NULL,
968 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX");
969 return GNUNET_SYSERR;
971 if (SQLITE_ROW == (ret = sqlite3_step (ic->stmt_2)))
974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
975 "Result found using iterator 2\n");
977 nc->stmt = ic->stmt_2;
980 if (ret != SQLITE_DONE)
982 LOG_SQLITE (plugin, NULL,
983 GNUNET_ERROR_TYPE_ERROR |
984 GNUNET_ERROR_TYPE_BULK,
986 if (SQLITE_OK != sqlite3_reset (ic->stmt_2))
987 LOG_SQLITE (plugin, NULL,
988 GNUNET_ERROR_TYPE_ERROR |
989 GNUNET_ERROR_TYPE_BULK,
991 return GNUNET_SYSERR;
993 if (SQLITE_OK != sqlite3_reset (ic->stmt_2))
994 LOG_SQLITE (plugin, NULL,
995 GNUNET_ERROR_TYPE_ERROR |
996 GNUNET_ERROR_TYPE_BULK,
999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1000 "No result found using either iterator\n");
1007 * Select a subset of the items in the datastore and call
1008 * the given iterator for each of them.
1010 * @param cls our plugin context
1011 * @param type entries of which type should be considered?
1012 * Use 0 for any type.
1013 * @param iter function to call on each matching value;
1014 * will be called once with a NULL value at the end
1015 * @param iter_cls closure for iter
1018 sqlite_plugin_iter_zero_anonymity (void *cls,
1019 enum GNUNET_BLOCK_Type type,
1020 PluginIterator iter,
1023 struct Plugin *plugin = cls;
1024 struct GNUNET_TIME_Absolute now;
1025 struct NextContext *nc;
1026 struct ZeroIterContext *ic;
1027 sqlite3_stmt *stmt_1;
1028 sqlite3_stmt *stmt_2;
1031 now = GNUNET_TIME_absolute_get ();
1032 GNUNET_asprintf (&q,
1033 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090 "
1034 "WHERE (prio = ?1 AND expire > %llu AND anonLevel = 0 AND hash < ?2) "
1035 "ORDER BY hash DESC LIMIT 1",
1036 (unsigned long long) now.abs_value);
1037 if (sq_prepare (plugin->dbh, q, &stmt_1) != SQLITE_OK)
1039 LOG_SQLITE (plugin, NULL,
1040 GNUNET_ERROR_TYPE_ERROR |
1041 GNUNET_ERROR_TYPE_BULK, "sqlite3_prepare_v2");
1042 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1047 GNUNET_asprintf (&q,
1048 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090 "
1049 "WHERE (prio < ?1 AND expire > %llu AND anonLevel = 0) "
1050 "ORDER BY prio DESC, hash DESC LIMIT 1",
1051 (unsigned long long) now.abs_value);
1052 if (sq_prepare (plugin->dbh, q, &stmt_2) != SQLITE_OK)
1054 LOG_SQLITE (plugin, NULL,
1055 GNUNET_ERROR_TYPE_ERROR |
1056 GNUNET_ERROR_TYPE_BULK, "sqlite3_prepare_v2");
1057 sqlite3_finalize (stmt_1);
1058 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1063 nc = GNUNET_malloc (sizeof(struct NextContext) +
1064 sizeof(struct ZeroIterContext));
1065 nc->plugin = plugin;
1067 nc->iter_cls = iter_cls;
1069 ic = (struct ZeroIterContext*) &nc[1];
1070 ic->stmt_1 = stmt_1;
1071 ic->stmt_2 = stmt_2;
1073 nc->prep = &zero_iter_next_prepare;
1075 nc->lastPriority = INT32_MAX;
1076 memset (&nc->lastKey, 255, sizeof (GNUNET_HashCode));
1077 sqlite_next_request (nc, GNUNET_NO);
1082 * Closure for 'all_next_prepare'.
1084 struct IterateAllContext
1088 * Offset for the current result.
1093 * Requested block type.
1095 enum GNUNET_BLOCK_Type type;
1098 * Our prepared statement.
1105 * Call sqlite using the already prepared query to get
1108 * @param cls context with the prepared query (of type 'struct IterateAllContext')
1109 * @param nc generic context with the prepared query
1110 * @return GNUNET_OK on success, GNUNET_SYSERR on error, GNUNET_NO if
1111 * there are no more results
1114 all_next_prepare (void *cls,
1115 struct NextContext *nc)
1117 struct IterateAllContext *iac = cls;
1118 struct Plugin *plugin;
1125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1126 "Asked to clean up iterator state.\n");
1128 if (NULL != iac->stmt)
1130 sqlite3_finalize (iac->stmt);
1133 return GNUNET_SYSERR;
1135 plugin = nc->plugin;
1139 ret = sqlite3_bind_int (nc->stmt, sqoff++, iac->type);
1140 if (SQLITE_OK == ret)
1141 ret = sqlite3_bind_int64 (nc->stmt, sqoff++, iac->off++);
1142 if (ret != SQLITE_OK)
1144 LOG_SQLITE (plugin, NULL,
1145 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX");
1146 return GNUNET_SYSERR;
1148 ret = sqlite3_step (nc->stmt);
1156 LOG_SQLITE (plugin, NULL,
1157 GNUNET_ERROR_TYPE_ERROR |
1158 GNUNET_ERROR_TYPE_BULK,
1160 return GNUNET_SYSERR;
1166 * Select a subset of the items in the datastore and call
1167 * the given iterator for each of them.
1169 * @param cls our plugin context
1170 * @param type entries of which type should be considered?
1171 * Use 0 for any type.
1172 * @param iter function to call on each matching value;
1173 * will be called once with a NULL value at the end
1174 * @param iter_cls closure for iter
1177 sqlite_plugin_iter_all_now (void *cls,
1178 enum GNUNET_BLOCK_Type type,
1179 PluginIterator iter,
1182 struct Plugin *plugin = cls;
1183 struct NextContext *nc;
1184 struct IterateAllContext *iac;
1189 q = "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090 ORDER BY _ROWID_ ASC LIMIT 1 OFFSET ?";
1191 q = "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090 WHERE type=? ORDER BY _ROWID_ ASC LIMIT 1 OFFSET ?";
1192 if (sq_prepare (plugin->dbh, q, &stmt) != SQLITE_OK)
1194 LOG_SQLITE (plugin, NULL,
1195 GNUNET_ERROR_TYPE_ERROR |
1196 GNUNET_ERROR_TYPE_BULK, "sqlite3_prepare_v2");
1197 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1200 nc = GNUNET_malloc (sizeof(struct NextContext) +
1201 sizeof(struct IterateAllContext));
1202 iac = (struct IterateAllContext*) &nc[1];
1203 nc->plugin = plugin;
1205 nc->iter_cls = iter_cls;
1207 nc->prep = &all_next_prepare;
1211 iac->stmt = stmt; /* alias used for freeing at the end */
1212 sqlite_next_request (nc, GNUNET_NO);
1217 * Context for get_next_prepare.
1219 struct GetNextContext
1223 * Our prepared statement.
1230 struct Plugin *plugin;
1233 * Key for the query.
1235 GNUNET_HashCode key;
1238 * Vhash for the query.
1240 GNUNET_HashCode vhash;
1243 * Expected total number of results.
1248 * Offset to add for the selected result.
1258 * Desired block type.
1260 enum GNUNET_BLOCK_Type type;
1266 * Prepare the stmt in 'nc' for the next round of execution, selecting the
1267 * next return value.
1269 * @param cls our "struct GetNextContext*"
1270 * @param nc the general context
1271 * @return GNUNET_YES if there are more results,
1272 * GNUNET_NO if there are no more results,
1273 * GNUNET_SYSERR on internal error
1276 get_next_prepare (void *cls,
1277 struct NextContext *nc)
1279 struct GetNextContext *gnc = cls;
1286 sqlite3_finalize (gnc->stmt);
1287 return GNUNET_SYSERR;
1289 if (nc->count == gnc->total)
1291 if (nc->count + gnc->off == gnc->total)
1294 limit_off = gnc->off;
1297 sqlite3_reset (nc->stmt);
1299 ret = sqlite3_bind_blob (nc->stmt,
1302 sizeof (GNUNET_HashCode),
1304 if ((gnc->have_vhash) && (ret == SQLITE_OK))
1305 ret = sqlite3_bind_blob (nc->stmt,
1308 sizeof (GNUNET_HashCode), SQLITE_TRANSIENT);
1309 if ((gnc->type != 0) && (ret == SQLITE_OK))
1310 ret = sqlite3_bind_int (nc->stmt, sqoff++, gnc->type);
1311 if (ret == SQLITE_OK)
1312 ret = sqlite3_bind_int64 (nc->stmt, sqoff++, limit_off);
1313 if (ret != SQLITE_OK)
1314 return GNUNET_SYSERR;
1316 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1318 "Preparing to GET for key `%s' with type %d at offset %u\n",
1319 GNUNET_h2s (&gnc->key),
1323 ret = sqlite3_step (nc->stmt);
1331 LOG_SQLITE (gnc->plugin, NULL,
1332 GNUNET_ERROR_TYPE_ERROR |
1333 GNUNET_ERROR_TYPE_BULK,
1335 return GNUNET_SYSERR;
1341 * Iterate over the results for a particular key
1344 * @param cls closure
1345 * @param key key to match, never NULL
1346 * @param vhash hash of the value, maybe NULL (to
1347 * match all values that have the right key).
1348 * Note that for DBlocks there is no difference
1349 * betwen key and vhash, but for other blocks
1351 * @param type entries of which type are relevant?
1352 * Use 0 for any type.
1353 * @param iter function to call on each matching value;
1354 * will be called once with a NULL value at the end
1355 * @param iter_cls closure for iter
1358 sqlite_plugin_get (void *cls,
1359 const GNUNET_HashCode *key,
1360 const GNUNET_HashCode *vhash,
1361 enum GNUNET_BLOCK_Type type,
1362 PluginIterator iter, void *iter_cls)
1364 struct Plugin *plugin = cls;
1365 struct GetNextContext *gnc;
1366 struct NextContext *nc;
1373 GNUNET_assert (iter != NULL);
1374 GNUNET_assert (key != NULL);
1375 GNUNET_snprintf (scratch, sizeof (scratch),
1376 "SELECT count(*) FROM gn090 WHERE hash=?%s%s",
1377 vhash == NULL ? "" : " AND vhash=?",
1378 type == 0 ? "" : " AND type=?");
1379 if (sq_prepare (plugin->dbh, scratch, &stmt) != SQLITE_OK)
1381 LOG_SQLITE (plugin, NULL,
1382 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite_prepare");
1383 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1387 ret = sqlite3_bind_blob (stmt, sqoff++,
1388 key, sizeof (GNUNET_HashCode), SQLITE_TRANSIENT);
1389 if ((vhash != NULL) && (ret == SQLITE_OK))
1390 ret = sqlite3_bind_blob (stmt, sqoff++,
1392 sizeof (GNUNET_HashCode), SQLITE_TRANSIENT);
1393 if ((type != 0) && (ret == SQLITE_OK))
1394 ret = sqlite3_bind_int (stmt, sqoff++, type);
1395 if (SQLITE_OK != ret)
1397 LOG_SQLITE (plugin, NULL,
1398 GNUNET_ERROR_TYPE_ERROR, "sqlite_bind");
1399 sqlite3_finalize (stmt);
1400 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1403 ret = sqlite3_step (stmt);
1404 if (ret != SQLITE_ROW)
1406 LOG_SQLITE (plugin, NULL,
1407 GNUNET_ERROR_TYPE_ERROR| GNUNET_ERROR_TYPE_BULK,
1409 sqlite3_finalize (stmt);
1410 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1413 total = sqlite3_column_int (stmt, 0);
1414 sqlite3_finalize (stmt);
1417 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1420 GNUNET_snprintf (scratch, sizeof (scratch),
1421 "SELECT type, prio, anonLevel, expire, hash, value, _ROWID_ "
1422 "FROM gn090 WHERE hash=?%s%s "
1423 "ORDER BY _ROWID_ ASC LIMIT 1 OFFSET ?",
1424 vhash == NULL ? "" : " AND vhash=?",
1425 type == 0 ? "" : " AND type=?");
1427 if (sq_prepare (plugin->dbh, scratch, &stmt) != SQLITE_OK)
1429 LOG_SQLITE (plugin, NULL,
1430 GNUNET_ERROR_TYPE_ERROR |
1431 GNUNET_ERROR_TYPE_BULK, "sqlite_prepare");
1432 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1435 nc = GNUNET_malloc (sizeof(struct NextContext) +
1436 sizeof(struct GetNextContext));
1437 nc->plugin = plugin;
1439 nc->iter_cls = iter_cls;
1441 gnc = (struct GetNextContext*) &nc[1];
1445 gnc->plugin = plugin;
1446 gnc->stmt = stmt; /* alias used for freeing at the end! */
1449 gnc->have_vhash = GNUNET_YES;
1450 gnc->vhash = *vhash;
1452 gnc->off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, total);
1453 nc->prep = &get_next_prepare;
1455 sqlite_next_request (nc, GNUNET_NO);
1460 * Execute statement that gets a row and call the callback
1461 * with the result. Resets the statement afterwards.
1463 * @param plugin the plugin
1464 * @param stmt the statement
1465 * @param iter iterator to call
1466 * @param iter_cls closure for 'iter'
1469 execute_get (struct Plugin *plugin,
1471 PluginIterator iter, void *iter_cls)
1474 struct GNUNET_TIME_Absolute expiration;
1475 unsigned long long rowid;
1479 n = sqlite3_step (stmt);
1483 size = sqlite3_column_bytes (stmt, 5);
1484 rowid = sqlite3_column_int64 (stmt, 6);
1485 if (sqlite3_column_bytes (stmt, 4) != sizeof (GNUNET_HashCode))
1487 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1489 _("Invalid data in database. Trying to fix (by deletion).\n"));
1490 if (SQLITE_OK != sqlite3_reset (stmt))
1491 LOG_SQLITE (plugin, NULL,
1492 GNUNET_ERROR_TYPE_ERROR |
1493 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
1494 if (GNUNET_OK == delete_by_rowid (plugin, rowid))
1495 plugin->env->duc (plugin->env->cls,
1496 - (size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
1499 expiration.abs_value = sqlite3_column_int64 (stmt, 3);
1500 ret = iter (iter_cls,
1502 sqlite3_column_blob (stmt, 4) /* key */,
1504 sqlite3_column_blob (stmt, 5) /* data */,
1505 sqlite3_column_int (stmt, 0) /* type */,
1506 sqlite3_column_int (stmt, 1) /* priority */,
1507 sqlite3_column_int (stmt, 2) /* anonymity */,
1510 if (SQLITE_OK != sqlite3_reset (stmt))
1511 LOG_SQLITE (plugin, NULL,
1512 GNUNET_ERROR_TYPE_ERROR |
1513 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
1514 if ( (GNUNET_NO == ret) &&
1515 (GNUNET_OK == delete_by_rowid (plugin, rowid)) )
1516 plugin->env->duc (plugin->env->cls,
1517 - (size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
1520 /* database must be empty */
1521 if (SQLITE_OK != sqlite3_reset (stmt))
1522 LOG_SQLITE (plugin, NULL,
1523 GNUNET_ERROR_TYPE_ERROR |
1524 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
1530 LOG_SQLITE (plugin, NULL,
1531 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1533 if (SQLITE_OK != sqlite3_reset (stmt))
1534 LOG_SQLITE (plugin, NULL,
1535 GNUNET_ERROR_TYPE_ERROR |
1536 GNUNET_ERROR_TYPE_BULK,
1539 database_shutdown (plugin);
1540 database_setup (plugin->env->cfg,
1544 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0,
1545 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1550 * Get a random item for replication. Returns a single, not expired, random item
1551 * from those with the highest replication counters. The item's
1552 * replication counter is decremented by one IF it was positive before.
1553 * Call 'iter' with all values ZERO or NULL if the datastore is empty.
1555 * @param cls closure
1556 * @param iter function to call the value (once only).
1557 * @param iter_cls closure for iter
1560 sqlite_plugin_replication_get (void *cls,
1561 PluginIterator iter, void *iter_cls)
1563 struct Plugin *plugin = cls;
1565 struct GNUNET_TIME_Absolute now;
1568 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1570 "Getting random block based on replication order.\n");
1572 stmt = plugin->selRepl;
1573 now = GNUNET_TIME_absolute_get ();
1574 if (SQLITE_OK != sqlite3_bind_int64 (stmt, 1, now.abs_value))
1576 LOG_SQLITE (plugin, NULL,
1577 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX");
1578 if (SQLITE_OK != sqlite3_reset (stmt))
1579 LOG_SQLITE (plugin, NULL,
1580 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
1581 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0,
1582 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1585 execute_get (plugin, stmt, iter, iter_cls);
1591 * Get a random item that has expired or has low priority.
1592 * Call 'iter' with all values ZERO or NULL if the datastore is empty.
1594 * @param cls closure
1595 * @param iter function to call the value (once only).
1596 * @param iter_cls closure for iter
1599 sqlite_plugin_expiration_get (void *cls,
1600 PluginIterator iter, void *iter_cls)
1602 struct Plugin *plugin = cls;
1604 struct GNUNET_TIME_Absolute now;
1607 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1609 "Getting random block based on expiration and priority order.\n");
1611 now = GNUNET_TIME_absolute_get ();
1612 stmt = plugin->selExpi;
1613 if (SQLITE_OK != sqlite3_bind_int64 (stmt, 1, now.abs_value))
1615 LOG_SQLITE (plugin, NULL,
1616 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX");
1617 if (SQLITE_OK != sqlite3_reset (stmt))
1618 LOG_SQLITE (plugin, NULL,
1619 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
1620 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0,
1621 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1624 execute_get (plugin, stmt, iter, iter_cls);
1631 * @param cls our plugin context
1634 sqlite_plugin_drop (void *cls)
1636 struct Plugin *plugin = cls;
1637 plugin->drop_on_shutdown = GNUNET_YES;
1642 * Get an estimate of how much space the database is
1645 * @param cls the 'struct Plugin'
1646 * @return the size of the database on disk (estimate)
1648 static unsigned long long
1649 sqlite_plugin_get_size (void *cls)
1651 struct Plugin *plugin = cls;
1659 if (SQLITE_VERSION_NUMBER < 3006000)
1661 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1663 _("sqlite version to old to determine size, assuming zero\n"));
1667 sqlite3_exec (plugin->dbh,
1668 "VACUUM", NULL, NULL, ENULL))
1671 sqlite3_exec (plugin->dbh,
1672 "VACUUM", NULL, NULL, ENULL));
1674 sqlite3_exec (plugin->dbh,
1675 "PRAGMA auto_vacuum=INCREMENTAL", NULL, NULL, ENULL));
1677 sq_prepare (plugin->dbh,
1678 "PRAGMA page_count",
1681 sqlite3_step (stmt))
1682 pages = sqlite3_column_int64 (stmt, 0);
1685 sqlite3_finalize (stmt);
1687 sq_prepare (plugin->dbh,
1690 CHECK (SQLITE_ROW ==
1691 sqlite3_step (stmt));
1692 page_size = sqlite3_column_int64 (stmt, 0);
1693 sqlite3_finalize (stmt);
1694 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1695 _("Using sqlite page utilization to estimate payload (%llu pages of size %llu bytes)\n"),
1696 (unsigned long long) pages,
1697 (unsigned long long) page_size);
1698 return pages * page_size;
1703 * Entry point for the plugin.
1705 * @param cls the "struct GNUNET_DATASTORE_PluginEnvironment*"
1706 * @return NULL on error, othrewise the plugin context
1709 libgnunet_plugin_datastore_sqlite_init (void *cls)
1711 static struct Plugin plugin;
1712 struct GNUNET_DATASTORE_PluginEnvironment *env = cls;
1713 struct GNUNET_DATASTORE_PluginFunctions *api;
1715 if (plugin.env != NULL)
1716 return NULL; /* can only initialize once! */
1717 memset (&plugin, 0, sizeof(struct Plugin));
1720 database_setup (env->cfg, &plugin))
1722 database_shutdown (&plugin);
1725 api = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_PluginFunctions));
1727 api->get_size = &sqlite_plugin_get_size;
1728 api->put = &sqlite_plugin_put;
1729 api->next_request = &sqlite_next_request;
1730 api->get = &sqlite_plugin_get;
1731 api->replication_get = &sqlite_plugin_replication_get;
1732 api->expiration_get = &sqlite_plugin_expiration_get;
1733 api->update = &sqlite_plugin_update;
1734 api->iter_zero_anonymity = &sqlite_plugin_iter_zero_anonymity;
1735 api->iter_all_now = &sqlite_plugin_iter_all_now;
1736 api->drop = &sqlite_plugin_drop;
1737 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1738 "sqlite", _("Sqlite database running\n"));
1744 * Exit point from the plugin.
1746 * @param cls the plugin context (as returned by "init")
1747 * @return always NULL
1750 libgnunet_plugin_datastore_sqlite_done (void *cls)
1753 struct GNUNET_DATASTORE_PluginFunctions *api = cls;
1754 struct Plugin *plugin = api->cls;
1757 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1759 "sqlite plugin is doneing\n");
1762 if (plugin->next_task != GNUNET_SCHEDULER_NO_TASK)
1765 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1767 "Canceling next task\n");
1769 GNUNET_SCHEDULER_cancel (plugin->next_task);
1770 plugin->next_task = GNUNET_SCHEDULER_NO_TASK;
1772 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1774 "Prep'ing next task\n");
1776 plugin->next_task_nc->prep (plugin->next_task_nc->prep_cls, NULL);
1777 GNUNET_free (plugin->next_task_nc);
1778 plugin->next_task_nc = NULL;
1781 if (plugin->drop_on_shutdown)
1782 fn = GNUNET_strdup (plugin->fn);
1784 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1786 "Shutting down database\n");
1788 database_shutdown (plugin);
1793 if (0 != UNLINK(fn))
1794 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1800 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1802 "sqlite plugin is finished doneing\n");
1807 /* end of plugin_datastore_sqlite.c */