-fix dht flags
[oweals/gnunet.git] / src / datacache / plugin_datacache_sqlite.c
1 /*
2      This file is part of GNUnet
3      (C) 2006, 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file datacache/plugin_datacache_sqlite.c
23  * @brief sqlite for an implementation of a database backend for the datacache
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_datacache_plugin.h"
29 #include <sqlite3.h>
30
31 #define LOG(kind,...) GNUNET_log_from (kind, "datacache-sqlite", __VA_ARGS__)
32
33 #define LOG_STRERROR_FILE(kind,op,fn) GNUNET_log_from_strerror_file (kind, "datacache-sqlite", op, fn)
34
35
36 /**
37  * How much overhead do we assume per entry in the
38  * datacache?
39  */
40 #define OVERHEAD (sizeof(struct GNUNET_HashCode) + 32)
41
42 /**
43  * Context for all functions in this plugin.
44  */
45 struct Plugin
46 {
47   /**
48    * Our execution environment.
49    */
50   struct GNUNET_DATACACHE_PluginEnvironment *env;
51
52   /**
53    * Handle to the sqlite database.
54    */
55   sqlite3 *dbh;
56
57   /**
58    * Filename used for the DB.
59    */
60   char *fn;
61 };
62
63
64 /**
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).
68  */
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)
70
71
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)
73
74
75 /**
76  * @brief Prepare a SQL statement
77  */
78 static int
79 sq_prepare (sqlite3 * dbh, const char *zSql,    /* SQL statement, UTF-8 encoded */
80             sqlite3_stmt ** ppStmt)
81 {                               /* OUT: Statement handle */
82   char *dummy;
83
84   return sqlite3_prepare (dbh, zSql, strlen (zSql), ppStmt,
85                           (const char **) &dummy);
86 }
87
88
89 /**
90  * Store an item in the datastore.
91  *
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
99  */
100 static size_t
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)
104 {
105   struct Plugin *plugin = cls;
106   sqlite3_stmt *stmt;
107   int64_t dval;
108
109   LOG (GNUNET_ERROR_TYPE_DEBUG,
110        "Processing `%s' of %u bytes with key `%4s' and expiration %llums\n",
111        "PUT", (unsigned int) size, GNUNET_h2s (key),
112        (unsigned long long)
113        GNUNET_TIME_absolute_get_remaining (discard_time).rel_value);
114   dval = (int64_t) discard_time.abs_value;
115   if (dval < 0)
116     dval = INT64_MAX;
117   if (sq_prepare
118       (plugin->dbh,
119        "INSERT INTO ds090 (type, expire, key, value) VALUES (?, ?, ?, ?)",
120        &stmt) != SQLITE_OK)
121   {
122     LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
123                 "sq_prepare");
124     return 0;
125   }
126   if ((SQLITE_OK != sqlite3_bind_int (stmt, 1, type)) ||
127       (SQLITE_OK != sqlite3_bind_int64 (stmt, 2, dval)) ||
128       (SQLITE_OK !=
129        sqlite3_bind_blob (stmt, 3, key, sizeof (struct GNUNET_HashCode),
130                           SQLITE_TRANSIENT)) ||
131       (SQLITE_OK != sqlite3_bind_blob (stmt, 4, data, size, SQLITE_TRANSIENT)))
132   {
133     LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
134                 "sqlite3_bind_xxx");
135     sqlite3_finalize (stmt);
136     return 0;
137   }
138   if (SQLITE_DONE != sqlite3_step (stmt))
139   {
140     LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
141                 "sqlite3_step");
142     sqlite3_finalize (stmt);
143     return 0;
144   }
145   if (SQLITE_OK != sqlite3_finalize (stmt))
146     LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
147                 "sqlite3_finalize");
148   return size + OVERHEAD;
149 }
150
151
152 /**
153  * Iterate over the results for a particular key
154  * in the datastore.
155  *
156  * @param cls closure (our "struct Plugin")
157  * @param key
158  * @param type entries of which type are relevant?
159  * @param iter maybe NULL (to just count)
160  * @param iter_cls closure for iter
161  * @return the number of results found
162  */
163 static unsigned int
164 sqlite_plugin_get (void *cls, const struct GNUNET_HashCode * key,
165                    enum GNUNET_BLOCK_Type type, GNUNET_DATACACHE_Iterator iter,
166                    void *iter_cls)
167 {
168   struct Plugin *plugin = cls;
169   sqlite3_stmt *stmt;
170   struct GNUNET_TIME_Absolute now;
171   struct GNUNET_TIME_Absolute exp;
172   unsigned int size;
173   const char *dat;
174   unsigned int cnt;
175   unsigned int off;
176   unsigned int total;
177   char scratch[256];
178   int64_t ntime;
179
180   now = GNUNET_TIME_absolute_get ();
181   LOG (GNUNET_ERROR_TYPE_DEBUG, "Processing `%s' for key `%4s'\n", "GET",
182        GNUNET_h2s (key));
183   if (sq_prepare
184       (plugin->dbh,
185        "SELECT count(*) FROM ds090 WHERE key=? AND type=? AND expire >= ?",
186        &stmt) != SQLITE_OK)
187   {
188     LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
189                 "sq_prepare");
190     return 0;
191   }
192   ntime = (int64_t) now.abs_value;
193   GNUNET_assert (ntime >= 0);
194   if ((SQLITE_OK !=
195        sqlite3_bind_blob (stmt, 1, key, sizeof (struct GNUNET_HashCode),
196                           SQLITE_TRANSIENT)) ||
197       (SQLITE_OK != sqlite3_bind_int (stmt, 2, type)) ||
198       (SQLITE_OK != sqlite3_bind_int64 (stmt, 3, now.abs_value)))
199   {
200     LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
201                 "sqlite3_bind_xxx");
202     sqlite3_finalize (stmt);
203     return 0;
204   }
205
206   if (SQLITE_ROW != sqlite3_step (stmt))
207   {
208     LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
209                 "sqlite_step");
210     sqlite3_finalize (stmt);
211     LOG (GNUNET_ERROR_TYPE_DEBUG,
212          "No content found when processing `%s' for key `%4s'\n", "GET",
213          GNUNET_h2s (key));
214     return 0;
215   }
216   total = sqlite3_column_int (stmt, 0);
217   sqlite3_finalize (stmt);
218   if ((total == 0) || (iter == NULL))
219   {
220     if (0 == total)
221       LOG (GNUNET_ERROR_TYPE_DEBUG,
222            "No content found when processing `%s' for key `%4s'\n", "GET",
223            GNUNET_h2s (key));
224     return total;
225   }
226
227   cnt = 0;
228   off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, total);
229   while (cnt < total)
230   {
231     off = (off + 1) % total;
232     GNUNET_snprintf (scratch, sizeof (scratch),
233                      "SELECT value,expire FROM ds090 WHERE key=? AND type=? AND expire >= ? LIMIT 1 OFFSET %u",
234                      off);
235     if (sq_prepare (plugin->dbh, scratch, &stmt) != SQLITE_OK)
236     {
237       LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
238                   "sq_prepare");
239       return cnt;
240     }
241     if ((SQLITE_OK !=
242          sqlite3_bind_blob (stmt, 1, key, sizeof (struct GNUNET_HashCode),
243                             SQLITE_TRANSIENT)) ||
244         (SQLITE_OK != sqlite3_bind_int (stmt, 2, type)) ||
245         (SQLITE_OK != sqlite3_bind_int64 (stmt, 3, now.abs_value)))
246     {
247       LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
248                   "sqlite3_bind_xxx");
249       sqlite3_finalize (stmt);
250       return cnt;
251     }
252     if (sqlite3_step (stmt) != SQLITE_ROW)
253       break;
254     size = sqlite3_column_bytes (stmt, 0);
255     dat = sqlite3_column_blob (stmt, 0);
256     exp.abs_value = sqlite3_column_int64 (stmt, 1);
257     ntime = (int64_t) exp.abs_value;
258     if (ntime == INT64_MAX)
259       exp = GNUNET_TIME_UNIT_FOREVER_ABS;
260     cnt++;
261     LOG (GNUNET_ERROR_TYPE_DEBUG,
262          "Found %u-byte result when processing `%s' for key `%4s'\n",
263          (unsigned int) size, "GET", GNUNET_h2s (key));
264     if (GNUNET_OK != iter (iter_cls, exp, key, size, dat, type))
265     {
266       sqlite3_finalize (stmt);
267       break;
268     }
269     sqlite3_finalize (stmt);
270   }
271   return cnt;
272 }
273
274
275 /**
276  * Delete the entry with the lowest expiration value
277  * from the datacache right now.
278  *
279  * @param cls closure (our "struct Plugin")
280  * @return GNUNET_OK on success, GNUNET_SYSERR on error
281  */
282 static int
283 sqlite_plugin_del (void *cls)
284 {
285   struct Plugin *plugin = cls;
286   unsigned long long rowid;
287   unsigned int dsize;
288   sqlite3_stmt *stmt;
289   sqlite3_stmt *dstmt;
290   struct GNUNET_HashCode hc;
291
292   LOG (GNUNET_ERROR_TYPE_DEBUG, "Processing `%s'\n", "DEL");
293   stmt = NULL;
294   dstmt = NULL;
295   if (sq_prepare
296       (plugin->dbh,
297        "SELECT _ROWID_,key,value FROM ds090 ORDER BY expire ASC LIMIT 1",
298        &stmt) != SQLITE_OK)
299   {
300     LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
301                 "sq_prepare");
302     if (stmt != NULL)
303       (void) sqlite3_finalize (stmt);
304     return GNUNET_SYSERR;
305   }
306   if (SQLITE_ROW != sqlite3_step (stmt))
307   {
308     LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
309                 "sqlite3_step");
310     (void) sqlite3_finalize (stmt);
311     return GNUNET_SYSERR;
312   }
313   rowid = sqlite3_column_int64 (stmt, 0);
314   GNUNET_assert (sqlite3_column_bytes (stmt, 1) == sizeof (struct GNUNET_HashCode));
315   memcpy (&hc, sqlite3_column_blob (stmt, 1), sizeof (struct GNUNET_HashCode));
316   dsize = sqlite3_column_bytes (stmt, 2);
317   if (SQLITE_OK != sqlite3_finalize (stmt))
318     LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
319                 "sqlite3_step");
320   if (sq_prepare (plugin->dbh, "DELETE FROM ds090 WHERE _ROWID_=?", &dstmt) !=
321       SQLITE_OK)
322   {
323     LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
324                 "sq_prepare");
325     if (stmt != NULL)
326       (void) sqlite3_finalize (stmt);
327     return GNUNET_SYSERR;
328   }
329   if (SQLITE_OK != sqlite3_bind_int64 (dstmt, 1, rowid))
330   {
331     LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
332                 "sqlite3_bind");
333     (void) sqlite3_finalize (dstmt);
334     return GNUNET_SYSERR;
335   }
336   if (sqlite3_step (dstmt) != SQLITE_DONE)
337   {
338     LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
339                 "sqlite3_step");
340     (void) sqlite3_finalize (dstmt);
341     return GNUNET_SYSERR;
342   }
343   plugin->env->delete_notify (plugin->env->cls, &hc, dsize + OVERHEAD);
344   if (SQLITE_OK != sqlite3_finalize (dstmt))
345     LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
346                 "sqlite3_finalize");
347   return GNUNET_OK;
348 }
349
350
351 /**
352  * Entry point for the plugin.
353  *
354  * @param cls closure (the "struct GNUNET_DATACACHE_PluginEnvironmnet")
355  * @return the plugin's closure (our "struct Plugin")
356  */
357 void *
358 libgnunet_plugin_datacache_sqlite_init (void *cls)
359 {
360   struct GNUNET_DATACACHE_PluginEnvironment *env = cls;
361   struct GNUNET_DATACACHE_PluginFunctions *api;
362   struct Plugin *plugin;
363   char *fn;
364   char *fn_utf8;
365   sqlite3 *dbh;
366   char *emsg;
367
368   fn = GNUNET_DISK_mktemp ("gnunet-datacache");
369   if (fn == NULL)
370   {
371     GNUNET_break (0);
372     return NULL;
373   }
374 #ifdef ENABLE_NLS
375   fn_utf8 = GNUNET_STRINGS_to_utf8 (fn, strlen (fn), nl_langinfo (CODESET));
376 #else
377   /* good luck */
378   fn_utf8 = GNUNET_STRINGS_to_utf8 (fn, strlen (fn), "UTF-8");
379 #endif
380   if (SQLITE_OK != sqlite3_open (fn_utf8, &dbh))
381   {
382     GNUNET_free (fn);
383     GNUNET_free (fn_utf8);
384     return NULL;
385   }
386   GNUNET_free (fn);
387
388   SQLITE3_EXEC (dbh, "PRAGMA temp_store=MEMORY");
389   SQLITE3_EXEC (dbh, "PRAGMA locking_mode=EXCLUSIVE");
390   SQLITE3_EXEC (dbh, "PRAGMA journal_mode=OFF");
391   SQLITE3_EXEC (dbh, "PRAGMA synchronous=OFF");
392   SQLITE3_EXEC (dbh, "PRAGMA count_changes=OFF");
393   SQLITE3_EXEC (dbh, "PRAGMA page_size=4092");
394   SQLITE3_EXEC (dbh,
395                 "CREATE TABLE ds090 (" "  type INTEGER NOT NULL DEFAULT 0,"
396                 "  expire INTEGER NOT NULL DEFAULT 0,"
397                 "  key BLOB NOT NULL DEFAULT '',"
398                 "  value BLOB NOT NULL DEFAULT '')");
399   SQLITE3_EXEC (dbh, "CREATE INDEX idx_hashidx ON ds090 (key,type,expire)");
400   SQLITE3_EXEC (dbh, "CREATE INDEX idx_expire ON ds090 (expire)");
401   plugin = GNUNET_malloc (sizeof (struct Plugin));
402   plugin->env = env;
403   plugin->dbh = dbh;
404   plugin->fn = fn_utf8;
405   api = GNUNET_malloc (sizeof (struct GNUNET_DATACACHE_PluginFunctions));
406   api->cls = plugin;
407   api->get = &sqlite_plugin_get;
408   api->put = &sqlite_plugin_put;
409   api->del = &sqlite_plugin_del;
410   LOG (GNUNET_ERROR_TYPE_INFO, _("Sqlite datacache running\n"));
411   return api;
412 }
413
414
415 /**
416  * Exit point from the plugin.
417  *
418  * @param cls closure (our "struct Plugin")
419  * @return NULL
420  */
421 void *
422 libgnunet_plugin_datacache_sqlite_done (void *cls)
423 {
424   struct GNUNET_DATACACHE_PluginFunctions *api = cls;
425   struct Plugin *plugin = api->cls;
426   int result;
427
428 #if SQLITE_VERSION_NUMBER >= 3007000
429   sqlite3_stmt *stmt;
430 #endif
431
432 #if !WINDOWS || defined(__CYGWIN__)
433   if (0 != UNLINK (plugin->fn))
434     LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", plugin->fn);
435   GNUNET_free (plugin->fn);
436 #endif
437   result = sqlite3_close (plugin->dbh);
438 #if SQLITE_VERSION_NUMBER >= 3007000
439   if (result == SQLITE_BUSY)
440   {
441     LOG (GNUNET_ERROR_TYPE_WARNING,
442          _
443          ("Tried to close sqlite without finalizing all prepared statements.\n"));
444     stmt = sqlite3_next_stmt (plugin->dbh, NULL);
445     while (stmt != NULL)
446     {
447       LOG (GNUNET_ERROR_TYPE_DEBUG, "Closing statement %p\n", stmt);
448       result = sqlite3_finalize (stmt);
449       if (result != SQLITE_OK)
450         LOG (GNUNET_ERROR_TYPE_WARNING, _("Failed to close statement %p: %d\n"),
451              stmt, result);
452       stmt = sqlite3_next_stmt (plugin->dbh, NULL);
453     }
454     result = sqlite3_close (plugin->dbh);
455   }
456 #endif
457   if (SQLITE_OK != result)
458     LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR, "sqlite3_close");
459
460 #if WINDOWS && !defined(__CYGWIN__)
461   if (0 != UNLINK (plugin->fn))
462     LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", plugin->fn);
463   GNUNET_free (plugin->fn);
464 #endif
465   GNUNET_free (plugin);
466   GNUNET_free (api);
467   return NULL;
468 }
469
470
471
472 /* end of plugin_datacache_sqlite.c */