2 This file is part of GNUnet
3 (C) 2006, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file 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"
31 #define DEBUG_DATACACHE_SQLITE GNUNET_NO
34 * How much overhead do we assume per entry in the
37 #define OVERHEAD (sizeof(GNUNET_HashCode) + 32)
40 * Context for all functions in this plugin.
45 * Our execution environment.
47 struct GNUNET_DATACACHE_PluginEnvironment *env;
50 * Handle to the sqlite database.
55 * Filename used for the DB.
62 * Log an error message at log-level 'level' that indicates
63 * a failure of the command 'cmd' on file 'filename'
64 * with the message given by strerror(errno).
66 #define LOG_SQLITE(db, level, cmd) do { GNUNET_log(level, _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, sqlite3_errmsg(db)); } while(0)
69 #define SQLITE3_EXEC(db, cmd) do { emsg = NULL; if (SQLITE_OK != sqlite3_exec(db, cmd, NULL, NULL, &emsg)) { GNUNET_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)
73 * @brief Prepare a SQL statement
76 sq_prepare (sqlite3 * dbh, const char *zSql, /* SQL statement, UTF-8 encoded */
77 sqlite3_stmt ** ppStmt)
78 { /* OUT: Statement handle */
80 return sqlite3_prepare (dbh,
82 strlen (zSql), ppStmt, (const char **) &dummy);
87 * Store an item in the datastore.
89 * @param cls closure (our "struct Plugin")
90 * @param key key to store data under
91 * @param size number of bytes in data
92 * @param data data to store
93 * @param type type of the value
94 * @param discard_time when to discard the value in any case
95 * @return 0 on error, number of bytes used otherwise
98 sqlite_plugin_put (void *cls,
99 const GNUNET_HashCode * key,
102 enum GNUNET_BLOCK_Type type,
103 struct GNUNET_TIME_Absolute discard_time)
105 struct Plugin *plugin = cls;
109 #if DEBUG_DATACACHE_SQLITE
110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
111 "Processing `%s' of %u bytes with key `%4s' and expiration %llums\n",
115 (unsigned long long) GNUNET_TIME_absolute_get_remaining (discard_time).rel_value);
117 dval = (int64_t) discard_time.abs_value;
120 if (sq_prepare (plugin->dbh,
122 "(type, expire, key, value) "
123 "VALUES (?, ?, ?, ?)", &stmt) != SQLITE_OK)
125 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
126 _("`%s' failed at %s:%d with error: %s\n"),
127 "sq_prepare", __FILE__, __LINE__,
128 sqlite3_errmsg (plugin->dbh));
131 if ( (SQLITE_OK != sqlite3_bind_int (stmt, 1, type)) ||
132 (SQLITE_OK != sqlite3_bind_int64 (stmt, 2, dval)) ||
133 (SQLITE_OK != sqlite3_bind_blob (stmt, 3, key, sizeof (GNUNET_HashCode),
134 SQLITE_TRANSIENT)) ||
135 (SQLITE_OK != sqlite3_bind_blob (stmt, 4, data, size, SQLITE_TRANSIENT)))
137 LOG_SQLITE (plugin->dbh,
138 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
140 sqlite3_finalize (stmt);
143 if (SQLITE_DONE != sqlite3_step (stmt))
145 LOG_SQLITE (plugin->dbh,
146 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
148 sqlite3_finalize (stmt);
151 if (SQLITE_OK != sqlite3_finalize (stmt))
152 LOG_SQLITE (plugin->dbh,
153 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
155 return size + OVERHEAD;
160 * Iterate over the results for a particular key
163 * @param cls closure (our "struct Plugin")
165 * @param type entries of which type are relevant?
166 * @param iter maybe NULL (to just count)
167 * @param iter_cls closure for iter
168 * @return the number of results found
171 sqlite_plugin_get (void *cls,
172 const GNUNET_HashCode * key,
173 enum GNUNET_BLOCK_Type type,
174 GNUNET_DATACACHE_Iterator iter,
177 struct Plugin *plugin = cls;
179 struct GNUNET_TIME_Absolute now;
180 struct GNUNET_TIME_Absolute exp;
189 now = GNUNET_TIME_absolute_get ();
190 #if DEBUG_DATACACHE_SQLITE
191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
192 "Processing `%s' for key `%4s'\n",
196 if (sq_prepare (plugin->dbh,
197 "SELECT count(*) FROM ds090 WHERE key=? AND type=? AND expire >= ?",
200 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
201 _("`%s' failed at %s:%d with error: %s\n"),
202 "sq_prepare", __FILE__, __LINE__,
203 sqlite3_errmsg (plugin->dbh));
206 ntime = (int64_t) now.abs_value;
207 GNUNET_assert (ntime >= 0);
209 sqlite3_bind_blob (stmt, 1, key, sizeof (GNUNET_HashCode),
210 SQLITE_TRANSIENT)) ||
212 sqlite3_bind_int (stmt, 2, type)) ||
214 sqlite3_bind_int64 (stmt, 3, now.abs_value)) )
216 LOG_SQLITE (plugin->dbh,
217 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
219 sqlite3_finalize (stmt);
223 if (SQLITE_ROW != sqlite3_step (stmt))
225 LOG_SQLITE (plugin->dbh,
226 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
228 sqlite3_finalize (stmt);
231 total = sqlite3_column_int (stmt, 0);
232 sqlite3_finalize (stmt);
233 if ( (total == 0) || (iter == NULL) )
237 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, total);
240 off = (off + 1) % total;
241 GNUNET_snprintf (scratch,
243 "SELECT value,expire FROM ds090 WHERE key=? AND type=? AND expire >= ? LIMIT 1 OFFSET %u",
245 if (sq_prepare (plugin->dbh, scratch, &stmt) != SQLITE_OK)
247 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
248 _("`%s' failed at %s:%d with error: %s\n"),
249 "sq_prepare", __FILE__, __LINE__,
250 sqlite3_errmsg (plugin->dbh));
254 sqlite3_bind_blob (stmt, 1, key, sizeof (GNUNET_HashCode),
255 SQLITE_TRANSIENT)) ||
257 sqlite3_bind_int (stmt, 2, type)) ||
259 sqlite3_bind_int64 (stmt, 3, now.abs_value)) )
261 LOG_SQLITE (plugin->dbh,
262 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
264 sqlite3_finalize (stmt);
267 if (sqlite3_step (stmt) != SQLITE_ROW)
269 size = sqlite3_column_bytes (stmt, 0);
270 dat = sqlite3_column_blob (stmt, 0);
271 exp.abs_value = sqlite3_column_int64 (stmt, 1);
272 ntime = (int64_t) exp.abs_value;
273 if (ntime == INT64_MAX)
274 exp = GNUNET_TIME_UNIT_FOREVER_ABS;
276 if (GNUNET_OK != iter (iter_cls,
283 sqlite3_finalize (stmt);
286 sqlite3_finalize (stmt);
293 * Delete the entry with the lowest expiration value
294 * from the datacache right now.
296 * @param cls closure (our "struct Plugin")
297 * @return GNUNET_OK on success, GNUNET_SYSERR on error
300 sqlite_plugin_del (void *cls)
302 struct Plugin *plugin = cls;
310 #if DEBUG_DATACACHE_SQLITE
311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317 if (sq_prepare (plugin->dbh,
318 "SELECT type, key, value FROM ds090 ORDER BY expire ASC LIMIT 1",
321 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
322 _("`%s' failed at %s:%d with error: %s\n"),
323 "sq_prepare", __FILE__, __LINE__, sqlite3_errmsg (plugin->dbh));
325 (void) sqlite3_finalize (stmt);
326 return GNUNET_SYSERR;
328 if (SQLITE_ROW != sqlite3_step (stmt))
330 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
331 _("`%s' failed at %s:%d with error: %s\n"),
332 "sqlite3_step", __FILE__, __LINE__,
333 sqlite3_errmsg (plugin->dbh));
334 (void) sqlite3_finalize (stmt);
335 return GNUNET_SYSERR;
337 dtype = sqlite3_column_int (stmt, 0);
338 GNUNET_break (sqlite3_column_bytes (stmt, 1) == sizeof (GNUNET_HashCode));
339 dsize = sqlite3_column_bytes (stmt, 2);
340 GNUNET_assert (dsize <= sizeof (blob));
341 memcpy (blob, sqlite3_column_blob (stmt, 2), dsize);
342 memcpy (&hc, sqlite3_column_blob (stmt, 1), sizeof (GNUNET_HashCode));
343 if (SQLITE_OK != sqlite3_finalize (stmt))
344 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
345 _("`%s' failed at %s:%d with error: %s\n"),
346 "sqlite3_step", __FILE__, __LINE__,
347 sqlite3_errmsg (plugin->dbh));
348 if (sq_prepare (plugin->dbh,
350 "WHERE key=? AND value=? AND type=?",
351 &dstmt) != SQLITE_OK)
353 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
354 _("`%s' failed at %s:%d with error: %s\n"),
355 "sq_prepare", __FILE__, __LINE__, sqlite3_errmsg (plugin->dbh));
357 (void) sqlite3_finalize (stmt);
358 return GNUNET_SYSERR;
361 sqlite3_bind_blob (dstmt,
363 sizeof (GNUNET_HashCode),
364 SQLITE_TRANSIENT)) ||
366 sqlite3_bind_blob (dstmt,
369 SQLITE_TRANSIENT)) ||
371 sqlite3_bind_int (dstmt, 3, dtype)) )
373 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
374 _("`%s' failed at %s:%d with error: %s\n"),
375 "sqlite3_bind", __FILE__, __LINE__,
376 sqlite3_errmsg (plugin->dbh));
377 (void) sqlite3_finalize (dstmt);
378 return GNUNET_SYSERR;
380 if (sqlite3_step (dstmt) != SQLITE_DONE)
382 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
383 _("`%s' failed at %s:%d with error: %s\n"),
384 "sqlite3_step", __FILE__, __LINE__,
385 sqlite3_errmsg (plugin->dbh));
386 (void) sqlite3_finalize (dstmt);
387 return GNUNET_SYSERR;
389 plugin->env->delete_notify (plugin->env->cls,
392 if (SQLITE_OK != sqlite3_finalize (dstmt))
393 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
394 _("`%s' failed at %s:%d with error: %s\n"),
395 "sqlite3_finalize", __FILE__, __LINE__,
396 sqlite3_errmsg (plugin->dbh));
402 * Entry point for the plugin.
404 * @param cls closure (the "struct GNUNET_DATACACHE_PluginEnvironmnet")
405 * @return the plugin's closure (our "struct Plugin")
408 libgnunet_plugin_datacache_sqlite_init (void *cls)
410 struct GNUNET_DATACACHE_PluginEnvironment *env = cls;
411 struct GNUNET_DATACACHE_PluginFunctions *api;
412 struct Plugin *plugin;
418 fn = GNUNET_DISK_mktemp ("gnunet-datacache");
425 fn_utf8 = GNUNET_STRINGS_to_utf8 (fn, strlen (fn), nl_langinfo (CODESET));
428 fn_utf8 = GNUNET_STRINGS_to_utf8 (fn, strlen (fn), "UTF-8");
430 if (SQLITE_OK != sqlite3_open (fn_utf8, &dbh))
433 GNUNET_free (fn_utf8);
438 SQLITE3_EXEC (dbh, "PRAGMA temp_store=MEMORY");
439 SQLITE3_EXEC (dbh, "PRAGMA locking_mode=EXCLUSIVE");
440 SQLITE3_EXEC (dbh, "PRAGMA journal_mode=OFF");
441 SQLITE3_EXEC (dbh, "PRAGMA synchronous=OFF");
442 SQLITE3_EXEC (dbh, "PRAGMA count_changes=OFF");
443 SQLITE3_EXEC (dbh, "PRAGMA page_size=4092");
445 "CREATE TABLE ds090 ("
446 " type INTEGER NOT NULL DEFAULT 0,"
447 " expire INTEGER NOT NULL DEFAULT 0,"
448 " key BLOB NOT NULL DEFAULT '',"
449 " value BLOB NOT NULL DEFAULT '')");
450 SQLITE3_EXEC (dbh, "CREATE INDEX idx_hashidx ON ds090 (key,type,expire)");
451 plugin = GNUNET_malloc (sizeof (struct Plugin));
454 plugin->fn = fn_utf8;
455 api = GNUNET_malloc (sizeof (struct GNUNET_DATACACHE_PluginFunctions));
457 api->get = &sqlite_plugin_get;
458 api->put = &sqlite_plugin_put;
459 api->del = &sqlite_plugin_del;
460 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
461 "sqlite", _("Sqlite datacache running\n"));
464 // explain SELECT type FROM gn090 WHERE NOT EXISTS (SELECT 1 from gn090 WHERE expire < 42 LIMIT 1) OR expire < 42 ORDER BY repl DESC, Random() LIMIT 1;
468 * Exit point from the plugin.
470 * @param cls closure (our "struct Plugin")
474 libgnunet_plugin_datacache_sqlite_done (void *cls)
476 struct GNUNET_DATACACHE_PluginFunctions *api = cls;
477 struct Plugin *plugin = api->cls;
479 #if SQLITE_VERSION_NUMBER >= 3007000
483 #if !WINDOWS || defined(__CYGWIN__)
484 if (0 != UNLINK (plugin->fn))
485 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
488 GNUNET_free (plugin->fn);
490 result = sqlite3_close (plugin->dbh);
491 #if SQLITE_VERSION_NUMBER >= 3007000
492 if (result == SQLITE_BUSY)
494 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
496 _("Tried to close sqlite without finalizing all prepared statements.\n"));
497 stmt = sqlite3_next_stmt(plugin->dbh, NULL);
501 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
502 "sqlite", "Closing statement %p\n", stmt);
504 result = sqlite3_finalize(stmt);
506 if (result != SQLITE_OK)
507 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
509 "Failed to close statement %p: %d\n", stmt, result);
511 stmt = sqlite3_next_stmt(plugin->dbh, NULL);
513 result = sqlite3_close(plugin->dbh);
516 if (SQLITE_OK != result)
517 LOG_SQLITE (plugin->dbh,
518 GNUNET_ERROR_TYPE_ERROR,
521 #if WINDOWS && !defined(__CYGWIN__)
522 if (0 != UNLINK (plugin->fn))
523 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
526 GNUNET_free (plugin->fn);
528 GNUNET_free (plugin);
535 /* end of plugin_datacache_sqlite.c */