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