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 LOG(kind,...) GNUNET_log_from (kind, "datacache-sqlite", __VA_ARGS__)
33 #define LOG_STRERROR_FILE(kind,op,fn) GNUNET_log_from_strerror_file (kind, "datacache-sqlite", op, fn)
37 * How much overhead do we assume per entry in the
40 #define OVERHEAD (sizeof(struct GNUNET_HashCode) + 32)
43 * Context for all functions in this plugin.
48 * Our execution environment.
50 struct GNUNET_DATACACHE_PluginEnvironment *env;
53 * Handle to the sqlite database.
58 * Filename used for the DB.
65 * Log an error message at log-level 'level' that indicates
66 * a failure of the command 'cmd' on file 'filename'
67 * with the message given by strerror(errno).
69 #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)
72 #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)
76 * @brief Prepare a SQL statement
79 sq_prepare (sqlite3 * dbh, const char *zSql, /* SQL statement, UTF-8 encoded */
80 sqlite3_stmt ** ppStmt)
81 { /* OUT: Statement handle */
84 return sqlite3_prepare (dbh, zSql, strlen (zSql), ppStmt,
85 (const char **) &dummy);
90 * Store an item in the datastore.
92 * @param cls closure (our "struct Plugin")
93 * @param key key to store data under
94 * @param size number of bytes in data
95 * @param data data to store
96 * @param type type of the value
97 * @param discard_time when to discard the value in any case
98 * @return 0 on error, number of bytes used otherwise
101 sqlite_plugin_put (void *cls, const struct GNUNET_HashCode * key, size_t size,
102 const char *data, enum GNUNET_BLOCK_Type type,
103 struct GNUNET_TIME_Absolute discard_time)
105 struct Plugin *plugin = cls;
109 LOG (GNUNET_ERROR_TYPE_DEBUG,
110 "Processing `%s' of %u bytes with key `%4s' and expiration %s\n",
111 "PUT", (unsigned int) size, GNUNET_h2s (key),
112 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (discard_time), GNUNET_YES));
113 dval = (int64_t) discard_time.abs_value;
118 "INSERT INTO ds090 (type, expire, key, value) VALUES (?, ?, ?, ?)",
121 LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
125 if ((SQLITE_OK != sqlite3_bind_int (stmt, 1, type)) ||
126 (SQLITE_OK != sqlite3_bind_int64 (stmt, 2, dval)) ||
128 sqlite3_bind_blob (stmt, 3, key, sizeof (struct GNUNET_HashCode),
129 SQLITE_TRANSIENT)) ||
130 (SQLITE_OK != sqlite3_bind_blob (stmt, 4, data, size, SQLITE_TRANSIENT)))
132 LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
134 sqlite3_finalize (stmt);
137 if (SQLITE_DONE != sqlite3_step (stmt))
139 LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
141 sqlite3_finalize (stmt);
144 if (SQLITE_OK != sqlite3_finalize (stmt))
145 LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
147 return size + OVERHEAD;
152 * Iterate over the results for a particular key
155 * @param cls closure (our "struct Plugin")
157 * @param type entries of which type are relevant?
158 * @param iter maybe NULL (to just count)
159 * @param iter_cls closure for iter
160 * @return the number of results found
163 sqlite_plugin_get (void *cls, const struct GNUNET_HashCode * key,
164 enum GNUNET_BLOCK_Type type, GNUNET_DATACACHE_Iterator iter,
167 struct Plugin *plugin = cls;
169 struct GNUNET_TIME_Absolute now;
170 struct GNUNET_TIME_Absolute exp;
179 now = GNUNET_TIME_absolute_get ();
180 LOG (GNUNET_ERROR_TYPE_DEBUG, "Processing `%s' for key `%4s'\n", "GET",
184 "SELECT count(*) FROM ds090 WHERE key=? AND type=? AND expire >= ?",
187 LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
191 ntime = (int64_t) now.abs_value;
192 GNUNET_assert (ntime >= 0);
194 sqlite3_bind_blob (stmt, 1, key, sizeof (struct GNUNET_HashCode),
195 SQLITE_TRANSIENT)) ||
196 (SQLITE_OK != sqlite3_bind_int (stmt, 2, type)) ||
197 (SQLITE_OK != sqlite3_bind_int64 (stmt, 3, now.abs_value)))
199 LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
201 sqlite3_finalize (stmt);
205 if (SQLITE_ROW != sqlite3_step (stmt))
207 LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
209 sqlite3_finalize (stmt);
210 LOG (GNUNET_ERROR_TYPE_DEBUG,
211 "No content found when processing `%s' for key `%4s'\n", "GET",
215 total = sqlite3_column_int (stmt, 0);
216 sqlite3_finalize (stmt);
217 if ((total == 0) || (iter == NULL))
220 LOG (GNUNET_ERROR_TYPE_DEBUG,
221 "No content found when processing `%s' for key `%4s'\n", "GET",
227 off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, total);
230 off = (off + 1) % total;
231 GNUNET_snprintf (scratch, sizeof (scratch),
232 "SELECT value,expire FROM ds090 WHERE key=? AND type=? AND expire >= ? LIMIT 1 OFFSET %u",
234 if (sq_prepare (plugin->dbh, scratch, &stmt) != SQLITE_OK)
236 LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
241 sqlite3_bind_blob (stmt, 1, key, sizeof (struct GNUNET_HashCode),
242 SQLITE_TRANSIENT)) ||
243 (SQLITE_OK != sqlite3_bind_int (stmt, 2, type)) ||
244 (SQLITE_OK != sqlite3_bind_int64 (stmt, 3, now.abs_value)))
246 LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
248 sqlite3_finalize (stmt);
251 if (sqlite3_step (stmt) != SQLITE_ROW)
253 size = sqlite3_column_bytes (stmt, 0);
254 dat = sqlite3_column_blob (stmt, 0);
255 exp.abs_value = sqlite3_column_int64 (stmt, 1);
256 ntime = (int64_t) exp.abs_value;
257 if (ntime == INT64_MAX)
258 exp = GNUNET_TIME_UNIT_FOREVER_ABS;
260 LOG (GNUNET_ERROR_TYPE_DEBUG,
261 "Found %u-byte result when processing `%s' for key `%4s'\n",
262 (unsigned int) size, "GET", GNUNET_h2s (key));
263 if (GNUNET_OK != iter (iter_cls, exp, key, size, dat, type))
265 sqlite3_finalize (stmt);
268 sqlite3_finalize (stmt);
275 * Delete the entry with the lowest expiration value
276 * from the datacache right now.
278 * @param cls closure (our "struct Plugin")
279 * @return GNUNET_OK on success, GNUNET_SYSERR on error
282 sqlite_plugin_del (void *cls)
284 struct Plugin *plugin = cls;
285 unsigned long long rowid;
289 struct GNUNET_HashCode hc;
291 LOG (GNUNET_ERROR_TYPE_DEBUG, "Processing `%s'\n", "DEL");
296 "SELECT _ROWID_,key,value FROM ds090 ORDER BY expire ASC LIMIT 1",
299 LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
302 (void) sqlite3_finalize (stmt);
303 return GNUNET_SYSERR;
305 if (SQLITE_ROW != sqlite3_step (stmt))
307 LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
309 (void) sqlite3_finalize (stmt);
310 return GNUNET_SYSERR;
312 rowid = sqlite3_column_int64 (stmt, 0);
313 GNUNET_assert (sqlite3_column_bytes (stmt, 1) == sizeof (struct GNUNET_HashCode));
314 memcpy (&hc, sqlite3_column_blob (stmt, 1), sizeof (struct GNUNET_HashCode));
315 dsize = sqlite3_column_bytes (stmt, 2);
316 if (SQLITE_OK != sqlite3_finalize (stmt))
317 LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
319 if (sq_prepare (plugin->dbh, "DELETE FROM ds090 WHERE _ROWID_=?", &dstmt) !=
322 LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
325 (void) sqlite3_finalize (stmt);
326 return GNUNET_SYSERR;
328 if (SQLITE_OK != sqlite3_bind_int64 (dstmt, 1, rowid))
330 LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
332 (void) sqlite3_finalize (dstmt);
333 return GNUNET_SYSERR;
335 if (sqlite3_step (dstmt) != SQLITE_DONE)
337 LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
339 (void) sqlite3_finalize (dstmt);
340 return GNUNET_SYSERR;
342 plugin->env->delete_notify (plugin->env->cls, &hc, dsize + OVERHEAD);
343 if (SQLITE_OK != sqlite3_finalize (dstmt))
344 LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
351 * Entry point for the plugin.
353 * @param cls closure (the "struct GNUNET_DATACACHE_PluginEnvironmnet")
354 * @return the plugin's closure (our "struct Plugin")
357 libgnunet_plugin_datacache_sqlite_init (void *cls)
359 struct GNUNET_DATACACHE_PluginEnvironment *env = cls;
360 struct GNUNET_DATACACHE_PluginFunctions *api;
361 struct Plugin *plugin;
367 fn = GNUNET_DISK_mktemp ("gnunet-datacache");
373 /* fn should be UTF-8-encoded. If it isn't, it's a bug. */
374 fn_utf8 = GNUNET_strdup (fn);
375 if (SQLITE_OK != sqlite3_open (fn_utf8, &dbh))
378 GNUNET_free (fn_utf8);
383 SQLITE3_EXEC (dbh, "PRAGMA temp_store=MEMORY");
384 SQLITE3_EXEC (dbh, "PRAGMA locking_mode=EXCLUSIVE");
385 SQLITE3_EXEC (dbh, "PRAGMA journal_mode=OFF");
386 SQLITE3_EXEC (dbh, "PRAGMA synchronous=OFF");
387 SQLITE3_EXEC (dbh, "PRAGMA count_changes=OFF");
388 SQLITE3_EXEC (dbh, "PRAGMA page_size=4092");
390 "CREATE TABLE ds090 (" " type INTEGER NOT NULL DEFAULT 0,"
391 " expire INTEGER NOT NULL DEFAULT 0,"
392 " key BLOB NOT NULL DEFAULT '',"
393 " value BLOB NOT NULL DEFAULT '')");
394 SQLITE3_EXEC (dbh, "CREATE INDEX idx_hashidx ON ds090 (key,type,expire)");
395 SQLITE3_EXEC (dbh, "CREATE INDEX idx_expire ON ds090 (expire)");
396 plugin = GNUNET_malloc (sizeof (struct Plugin));
399 plugin->fn = fn_utf8;
400 api = GNUNET_malloc (sizeof (struct GNUNET_DATACACHE_PluginFunctions));
402 api->get = &sqlite_plugin_get;
403 api->put = &sqlite_plugin_put;
404 api->del = &sqlite_plugin_del;
405 LOG (GNUNET_ERROR_TYPE_INFO, _("Sqlite datacache running\n"));
411 * Exit point from the plugin.
413 * @param cls closure (our "struct Plugin")
417 libgnunet_plugin_datacache_sqlite_done (void *cls)
419 struct GNUNET_DATACACHE_PluginFunctions *api = cls;
420 struct Plugin *plugin = api->cls;
423 #if SQLITE_VERSION_NUMBER >= 3007000
427 #if !WINDOWS || defined(__CYGWIN__)
428 if (0 != UNLINK (plugin->fn))
429 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", plugin->fn);
430 GNUNET_free (plugin->fn);
432 result = sqlite3_close (plugin->dbh);
433 #if SQLITE_VERSION_NUMBER >= 3007000
434 if (result == SQLITE_BUSY)
436 LOG (GNUNET_ERROR_TYPE_WARNING,
438 ("Tried to close sqlite without finalizing all prepared statements.\n"));
439 stmt = sqlite3_next_stmt (plugin->dbh, NULL);
442 LOG (GNUNET_ERROR_TYPE_DEBUG, "Closing statement %p\n", stmt);
443 result = sqlite3_finalize (stmt);
444 if (result != SQLITE_OK)
445 LOG (GNUNET_ERROR_TYPE_WARNING, _("Failed to close statement %p: %d\n"),
447 stmt = sqlite3_next_stmt (plugin->dbh, NULL);
449 result = sqlite3_close (plugin->dbh);
452 if (SQLITE_OK != result)
453 LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR, "sqlite3_close");
455 #if WINDOWS && !defined(__CYGWIN__)
456 if (0 != UNLINK (plugin->fn))
457 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", plugin->fn);
458 GNUNET_free (plugin->fn);
460 GNUNET_free (plugin);
467 /* end of plugin_datacache_sqlite.c */