2 This file is part of GNUnet
3 Copyright (C) 2006, 2009, 2015 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file datacache/plugin_datacache_sqlite.c
23 * @brief sqlite for an implementation of a database backend for the datacache
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_datacache_plugin.h"
29 #include "gnunet_sq_lib.h"
32 #define LOG(kind,...) GNUNET_log_from (kind, "datacache-sqlite", __VA_ARGS__)
34 #define LOG_STRERROR_FILE(kind,op,fn) GNUNET_log_from_strerror_file (kind, "datacache-sqlite", op, fn)
38 * How much overhead do we assume per entry in the
41 #define OVERHEAD (sizeof(struct GNUNET_HashCode) + 32)
44 * Context for all functions in this plugin.
49 * Our execution environment.
51 struct GNUNET_DATACACHE_PluginEnvironment *env;
54 * Handle to the sqlite database.
59 * Filename used for the DB.
64 * Prepared statement for #sqlite_plugin_put.
66 sqlite3_stmt *insert_stmt;
69 * Prepared statement for #sqlite_plugin_get.
71 sqlite3_stmt *get_count_stmt;
74 * Prepared statement for #sqlite_plugin_get.
76 sqlite3_stmt *get_stmt;
79 * Prepared statement for #sqlite_plugin_del.
81 sqlite3_stmt *del_select_stmt;
84 * Prepared statement for #sqlite_plugin_del.
86 sqlite3_stmt *del_stmt;
89 * Prepared statement for #sqlite_plugin_get_random.
91 sqlite3_stmt *get_random_stmt;
94 * Prepared statement for #sqlite_plugin_get_closest.
96 sqlite3_stmt *get_closest_stmt;
99 * Number of key-value pairs in the database.
101 unsigned int num_items;
106 * Log an error message at log-level @a level that indicates
107 * a failure of the command @a cmd with the error from the database @a db
109 * @param db database handle
110 * @param level log level
111 * @param cmd failed command
113 #define LOG_SQLITE(db, level, cmd) do { LOG (level, _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, sqlite3_errmsg(db)); } while(0)
117 * Execute SQL statement.
119 * @param db database handle
120 * @param cmd SQL command to execute
122 #define SQLITE3_EXEC(db, cmd) do { emsg = NULL; if (SQLITE_OK != sqlite3_exec(db, cmd, NULL, NULL, &emsg)) { LOG (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, _("`%s' failed at %s:%d with error: %s\n"), "sqlite3_exec", __FILE__, __LINE__, emsg); sqlite3_free(emsg); } } while(0)
126 * @brief Prepare a SQL statement
128 * @param dbh database handle
129 * @param zsql SQL statement text
130 * @param[out] ppStmt set to the prepared statement
131 * @return 0 on success
134 sq_prepare (sqlite3 *dbh,
135 const char *zSql, /* SQL statement, UTF-8 encoded */
136 sqlite3_stmt **ppStmt)
137 { /* OUT: Statement handle */
140 return sqlite3_prepare (dbh,
143 (const char **) &dummy);
148 * Store an item in the datastore.
150 * @param cls closure (our `struct Plugin`)
151 * @param key key to store @a data under
152 * @param size number of bytes in @a data
153 * @param data data to store
154 * @param type type of the value
155 * @param discard_time when to discard the value in any case
156 * @param path_info_len number of entries in @a path_info
157 * @param path_info array of peers that have processed the request
158 * @return 0 if duplicate, -1 on error, number of bytes used otherwise
161 sqlite_plugin_put (void *cls,
162 const struct GNUNET_HashCode *key,
165 enum GNUNET_BLOCK_Type type,
166 struct GNUNET_TIME_Absolute discard_time,
167 unsigned int path_info_len,
168 const struct GNUNET_PeerIdentity *path_info)
170 struct Plugin *plugin = cls;
171 uint32_t type32 = type;
172 struct GNUNET_SQ_QueryParam params[] = {
173 GNUNET_SQ_query_param_uint32 (&type32),
174 GNUNET_SQ_query_param_absolute_time (&discard_time),
175 GNUNET_SQ_query_param_auto_from_type (key),
176 GNUNET_SQ_query_param_fixed_size (data, size),
177 GNUNET_SQ_query_param_fixed_size (path_info,
178 path_info_len * sizeof (struct GNUNET_PeerIdentity)),
179 GNUNET_SQ_query_param_end
182 LOG (GNUNET_ERROR_TYPE_DEBUG,
183 "Processing PUT of %u bytes with key `%s' and expiration %s\n",
186 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (discard_time),
189 GNUNET_SQ_bind (plugin->insert_stmt,
192 LOG_SQLITE (plugin->dbh,
193 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
195 GNUNET_SQ_reset (plugin->dbh,
196 plugin->insert_stmt);
200 sqlite3_step (plugin->insert_stmt))
202 LOG_SQLITE (plugin->dbh,
203 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
205 GNUNET_SQ_reset (plugin->dbh,
206 plugin->insert_stmt);
210 GNUNET_SQ_reset (plugin->dbh,
211 plugin->insert_stmt);
212 return size + OVERHEAD;
217 * Iterate over the results for a particular key
220 * @param cls closure (our `struct Plugin`)
222 * @param type entries of which type are relevant?
223 * @param iter maybe NULL (to just count)
224 * @param iter_cls closure for @a iter
225 * @return the number of results found
228 sqlite_plugin_get (void *cls,
229 const struct GNUNET_HashCode *key,
230 enum GNUNET_BLOCK_Type type,
231 GNUNET_DATACACHE_Iterator iter,
234 struct Plugin *plugin = cls;
235 uint32_t type32 = type;
236 struct GNUNET_TIME_Absolute now;
237 struct GNUNET_TIME_Absolute exp;
244 struct GNUNET_PeerIdentity *path;
245 struct GNUNET_SQ_QueryParam params_count[] = {
246 GNUNET_SQ_query_param_auto_from_type (key),
247 GNUNET_SQ_query_param_uint32 (&type32),
248 GNUNET_SQ_query_param_absolute_time (&now),
249 GNUNET_SQ_query_param_end
251 struct GNUNET_SQ_QueryParam params_select[] = {
252 GNUNET_SQ_query_param_auto_from_type (key),
253 GNUNET_SQ_query_param_uint32 (&type32),
254 GNUNET_SQ_query_param_absolute_time (&now),
255 GNUNET_SQ_query_param_uint32 (&off),
256 GNUNET_SQ_query_param_end
258 struct GNUNET_SQ_ResultSpec rs[] = {
259 GNUNET_SQ_result_spec_variable_size (&dat,
261 GNUNET_SQ_result_spec_absolute_time (&exp),
262 GNUNET_SQ_result_spec_variable_size ((void **) &path,
264 GNUNET_SQ_result_spec_end
267 now = GNUNET_TIME_absolute_get ();
268 LOG (GNUNET_ERROR_TYPE_DEBUG,
269 "Processing GET for key `%s'\n",
273 GNUNET_SQ_bind (plugin->get_count_stmt,
276 LOG_SQLITE (plugin->dbh,
277 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
279 GNUNET_SQ_reset (plugin->dbh,
280 plugin->get_count_stmt);
284 sqlite3_step (plugin->get_count_stmt))
286 LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
288 GNUNET_SQ_reset (plugin->dbh,
289 plugin->get_count_stmt);
290 LOG (GNUNET_ERROR_TYPE_DEBUG,
291 "No content found when processing GET for key `%s'\n",
295 total = sqlite3_column_int (plugin->get_count_stmt,
297 GNUNET_SQ_reset (plugin->dbh,
298 plugin->get_count_stmt);
303 LOG (GNUNET_ERROR_TYPE_DEBUG,
304 "No content found when processing GET for key `%s'\n",
310 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
314 off = (off + 1) % total;
316 GNUNET_SQ_bind (plugin->get_stmt,
319 LOG_SQLITE (plugin->dbh,
320 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
322 GNUNET_SQ_reset (plugin->dbh,
327 sqlite3_step (plugin->get_stmt))
330 GNUNET_SQ_extract_result (plugin->get_stmt,
334 GNUNET_SQ_reset (plugin->dbh,
338 if (0 != psize % sizeof (struct GNUNET_PeerIdentity))
344 psize /= sizeof (struct GNUNET_PeerIdentity);
346 LOG (GNUNET_ERROR_TYPE_DEBUG,
347 "Found %u-byte result when processing GET for key `%s'\n",
350 if (GNUNET_OK != iter (iter_cls,
359 GNUNET_SQ_cleanup_result (rs);
360 GNUNET_SQ_reset (plugin->dbh,
364 GNUNET_SQ_cleanup_result (rs);
365 GNUNET_SQ_reset (plugin->dbh,
368 GNUNET_SQ_reset (plugin->dbh,
375 * Delete the entry with the lowest expiration value
376 * from the datacache right now.
378 * @param cls closure (our `struct Plugin`)
379 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
382 sqlite_plugin_del (void *cls)
384 struct Plugin *plugin = cls;
388 struct GNUNET_HashCode hc;
389 struct GNUNET_SQ_ResultSpec rs[] = {
390 GNUNET_SQ_result_spec_uint64 (&rowid),
391 GNUNET_SQ_result_spec_auto_from_type (&hc),
392 GNUNET_SQ_result_spec_variable_size ((void **) &data,
394 GNUNET_SQ_result_spec_end
396 struct GNUNET_SQ_QueryParam params[] = {
397 GNUNET_SQ_query_param_uint64 (&rowid),
398 GNUNET_SQ_query_param_end
401 LOG (GNUNET_ERROR_TYPE_DEBUG,
404 sqlite3_step (plugin->del_select_stmt))
406 LOG_SQLITE (plugin->dbh,
407 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
409 GNUNET_SQ_reset (plugin->dbh,
410 plugin->del_select_stmt);
411 return GNUNET_SYSERR;
414 GNUNET_SQ_extract_result (plugin->del_select_stmt,
418 GNUNET_SQ_reset (plugin->dbh,
419 plugin->del_select_stmt);
420 return GNUNET_SYSERR;
422 GNUNET_SQ_cleanup_result (rs);
423 GNUNET_SQ_reset (plugin->dbh,
424 plugin->del_select_stmt);
426 GNUNET_SQ_bind (plugin->del_stmt,
429 LOG_SQLITE (plugin->dbh,
430 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
432 GNUNET_SQ_reset (plugin->dbh,
434 return GNUNET_SYSERR;
437 sqlite3_step (plugin->del_stmt))
439 LOG_SQLITE (plugin->dbh,
440 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
442 GNUNET_SQ_reset (plugin->dbh,
444 return GNUNET_SYSERR;
447 plugin->env->delete_notify (plugin->env->cls,
450 GNUNET_SQ_reset (plugin->dbh,
457 * Obtain a random key-value pair from the datacache.
459 * @param cls closure (our `struct Plugin`)
460 * @param iter maybe NULL (to just count)
461 * @param iter_cls closure for @a iter
462 * @return the number of results found, zero (datacache empty) or one
465 sqlite_plugin_get_random (void *cls,
466 GNUNET_DATACACHE_Iterator iter,
469 struct Plugin *plugin = cls;
470 struct GNUNET_TIME_Absolute exp;
476 struct GNUNET_PeerIdentity *path;
477 struct GNUNET_HashCode key;
478 struct GNUNET_SQ_QueryParam params[] = {
479 GNUNET_SQ_query_param_uint32 (&off),
480 GNUNET_SQ_query_param_end
482 struct GNUNET_SQ_ResultSpec rs[] = {
483 GNUNET_SQ_result_spec_variable_size (&dat,
485 GNUNET_SQ_result_spec_absolute_time (&exp),
486 GNUNET_SQ_result_spec_variable_size ((void **) &path,
488 GNUNET_SQ_result_spec_auto_from_type (&key),
489 GNUNET_SQ_result_spec_uint32 (&type),
490 GNUNET_SQ_result_spec_end
493 if (0 == plugin->num_items)
497 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
500 GNUNET_SQ_bind (plugin->get_random_stmt,
506 sqlite3_step (plugin->get_random_stmt))
509 GNUNET_SQ_reset (plugin->dbh,
510 plugin->get_random_stmt);
514 GNUNET_SQ_extract_result (plugin->get_random_stmt,
518 GNUNET_SQ_reset (plugin->dbh,
519 plugin->get_random_stmt);
522 if (0 != psize % sizeof (struct GNUNET_PeerIdentity))
528 psize /= sizeof (struct GNUNET_PeerIdentity);
529 LOG (GNUNET_ERROR_TYPE_DEBUG,
530 "Found %u-byte result with key %s when processing GET-RANDOM\n",
533 (void) iter (iter_cls,
537 (enum GNUNET_BLOCK_Type) type,
541 GNUNET_SQ_cleanup_result (rs);
542 GNUNET_SQ_reset (plugin->dbh,
543 plugin->get_random_stmt);
549 * Iterate over the results that are "close" to a particular key in
550 * the datacache. "close" is defined as numerically larger than @a
551 * key (when interpreted as a circular address space), with small
554 * @param cls closure (internal context for the plugin)
555 * @param key area of the keyspace to look into
556 * @param num_results number of results that should be returned to @a iter
557 * @param iter maybe NULL (to just count)
558 * @param iter_cls closure for @a iter
559 * @return the number of results found
562 sqlite_plugin_get_closest (void *cls,
563 const struct GNUNET_HashCode *key,
564 unsigned int num_results,
565 GNUNET_DATACACHE_Iterator iter,
568 struct Plugin *plugin = cls;
569 uint32_t num_results32 = num_results;
570 struct GNUNET_TIME_Absolute now;
571 struct GNUNET_TIME_Absolute exp;
577 struct GNUNET_HashCode hc;
578 struct GNUNET_PeerIdentity *path;
579 struct GNUNET_SQ_QueryParam params[] = {
580 GNUNET_SQ_query_param_auto_from_type (key),
581 GNUNET_SQ_query_param_absolute_time (&now),
582 GNUNET_SQ_query_param_uint32 (&num_results32),
583 GNUNET_SQ_query_param_end
585 struct GNUNET_SQ_ResultSpec rs[] = {
586 GNUNET_SQ_result_spec_variable_size (&dat,
588 GNUNET_SQ_result_spec_absolute_time (&exp),
589 GNUNET_SQ_result_spec_variable_size ((void **) &path,
591 GNUNET_SQ_result_spec_uint32 (&type),
592 GNUNET_SQ_result_spec_auto_from_type (&hc),
593 GNUNET_SQ_result_spec_end
596 now = GNUNET_TIME_absolute_get ();
597 LOG (GNUNET_ERROR_TYPE_DEBUG,
598 "Processing GET_CLOSEST for key `%s'\n",
601 GNUNET_SQ_bind (plugin->get_closest_stmt,
604 LOG_SQLITE (plugin->dbh,
605 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
607 GNUNET_SQ_reset (plugin->dbh,
608 plugin->get_closest_stmt);
613 sqlite3_step (plugin->get_closest_stmt))
616 GNUNET_SQ_extract_result (plugin->get_closest_stmt,
622 if (0 != psize % sizeof (struct GNUNET_PeerIdentity))
628 psize /= sizeof (struct GNUNET_PeerIdentity);
630 LOG (GNUNET_ERROR_TYPE_DEBUG,
631 "Found %u-byte result at %s when processing GET_CLOSE\n",
634 if (GNUNET_OK != iter (iter_cls,
643 GNUNET_SQ_cleanup_result (rs);
646 GNUNET_SQ_cleanup_result (rs);
648 GNUNET_SQ_reset (plugin->dbh,
649 plugin->get_closest_stmt);
655 * Entry point for the plugin.
657 * @param cls closure (the `struct GNUNET_DATACACHE_PluginEnvironment`)
658 * @return the plugin's closure (our `struct Plugin`)
661 libgnunet_plugin_datacache_sqlite_init (void *cls)
663 struct GNUNET_DATACACHE_PluginEnvironment *env = cls;
664 struct GNUNET_DATACACHE_PluginFunctions *api;
665 struct Plugin *plugin;
672 GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
676 if (SQLITE_OK != sqlite3_open (":memory:", &dbh))
682 fn = GNUNET_DISK_mktemp ("gnunet-datacache");
688 /* fn should be UTF-8-encoded. If it isn't, it's a bug. */
689 fn_utf8 = GNUNET_strdup (fn);
690 if (SQLITE_OK != sqlite3_open (fn_utf8, &dbh))
693 GNUNET_free (fn_utf8);
699 SQLITE3_EXEC (dbh, "PRAGMA temp_store=MEMORY");
700 SQLITE3_EXEC (dbh, "PRAGMA locking_mode=EXCLUSIVE");
701 SQLITE3_EXEC (dbh, "PRAGMA journal_mode=OFF");
702 SQLITE3_EXEC (dbh, "PRAGMA synchronous=OFF");
703 SQLITE3_EXEC (dbh, "PRAGMA page_size=4092");
705 GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
708 SQLITE3_EXEC (dbh, "PRAGMA sqlite_temp_store=3");
711 "CREATE TABLE ds090 (" " type INTEGER NOT NULL DEFAULT 0,"
712 " expire INTEGER NOT NULL DEFAULT 0,"
713 " key BLOB NOT NULL DEFAULT '',"
714 " value BLOB NOT NULL DEFAULT '',"
715 " path BLOB DEFAULT '')");
716 SQLITE3_EXEC (dbh, "CREATE INDEX idx_hashidx ON ds090 (key,type,expire)");
717 SQLITE3_EXEC (dbh, "CREATE INDEX idx_expire ON ds090 (expire)");
718 plugin = GNUNET_new (struct Plugin);
721 plugin->fn = fn_utf8;
724 sq_prepare (plugin->dbh,
725 "INSERT INTO ds090 (type, expire, key, value, path) "
726 "VALUES (?, ?, ?, ?, ?)",
727 &plugin->insert_stmt)) ||
729 sq_prepare (plugin->dbh,
730 "SELECT count(*) FROM ds090 "
731 "WHERE key=? AND type=? AND expire >= ?",
732 &plugin->get_count_stmt)) ||
734 sq_prepare (plugin->dbh,
735 "SELECT value,expire,path FROM ds090 "
736 "WHERE key=? AND type=? AND expire >= ? LIMIT 1 OFFSET ?",
737 &plugin->get_stmt)) ||
739 sq_prepare (plugin->dbh,
740 "SELECT _ROWID_,key,value FROM ds090 ORDER BY expire ASC LIMIT 1",
741 &plugin->del_select_stmt)) ||
743 sq_prepare (plugin->dbh,
744 "DELETE FROM ds090 WHERE _ROWID_=?",
745 &plugin->del_stmt)) ||
747 sq_prepare (plugin->dbh,
748 "SELECT value,expire,path,key,type FROM ds090 "
749 "ORDER BY key LIMIT 1 OFFSET ?",
750 &plugin->get_random_stmt)) ||
752 sq_prepare (plugin->dbh,
753 "SELECT value,expire,path,type,key FROM ds090 "
754 "WHERE key>=? AND expire >= ? ORDER BY KEY ASC LIMIT ?",
755 &plugin->get_closest_stmt))
758 LOG_SQLITE (plugin->dbh,
759 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
761 GNUNET_break (SQLITE_OK ==
762 sqlite3_close (plugin->dbh));
763 GNUNET_free (plugin);
767 api = GNUNET_new (struct GNUNET_DATACACHE_PluginFunctions);
769 api->get = &sqlite_plugin_get;
770 api->put = &sqlite_plugin_put;
771 api->del = &sqlite_plugin_del;
772 api->get_random = &sqlite_plugin_get_random;
773 api->get_closest = &sqlite_plugin_get_closest;
774 LOG (GNUNET_ERROR_TYPE_INFO,
775 "Sqlite datacache running\n");
781 * Exit point from the plugin.
783 * @param cls closure (our `struct Plugin`)
787 libgnunet_plugin_datacache_sqlite_done (void *cls)
789 struct GNUNET_DATACACHE_PluginFunctions *api = cls;
790 struct Plugin *plugin = api->cls;
793 #if SQLITE_VERSION_NUMBER >= 3007000
797 #if !WINDOWS || defined(__CYGWIN__)
798 if ( (NULL != plugin->fn) &&
799 (0 != UNLINK (plugin->fn)) )
800 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
803 GNUNET_free_non_null (plugin->fn);
805 sqlite3_finalize (plugin->insert_stmt);
806 sqlite3_finalize (plugin->get_count_stmt);
807 sqlite3_finalize (plugin->get_stmt);
808 sqlite3_finalize (plugin->del_select_stmt);
809 sqlite3_finalize (plugin->del_stmt);
810 sqlite3_finalize (plugin->get_random_stmt);
811 sqlite3_finalize (plugin->get_closest_stmt);
812 result = sqlite3_close (plugin->dbh);
813 #if SQLITE_VERSION_NUMBER >= 3007000
814 if (SQLITE_BUSY == result)
816 LOG (GNUNET_ERROR_TYPE_WARNING,
817 _("Tried to close sqlite without finalizing all prepared statements.\n"));
818 stmt = sqlite3_next_stmt (plugin->dbh, NULL);
821 result = sqlite3_finalize (stmt);
822 if (result != SQLITE_OK)
823 LOG (GNUNET_ERROR_TYPE_WARNING,
824 "Failed to close statement %p: %d\n",
827 stmt = sqlite3_next_stmt (plugin->dbh, NULL);
829 result = sqlite3_close (plugin->dbh);
832 if (SQLITE_OK != result)
833 LOG_SQLITE (plugin->dbh,
834 GNUNET_ERROR_TYPE_ERROR,
837 #if WINDOWS && !defined(__CYGWIN__)
838 if ( (NULL != plugin->fn) &&
839 (0 != UNLINK (plugin->fn)) )
840 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
843 GNUNET_free_non_null (plugin->fn);
845 GNUNET_free (plugin);
852 /* end of plugin_datacache_sqlite.c */