remove send on connect
[oweals/gnunet.git] / src / datastore / plugin_datastore_sqlite.c
1  /*
2      This file is part of GNUnet
3      (C) 2009, 2011 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 datastore/plugin_datastore_sqlite.c
23  * @brief sqlite-based datastore backend
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_datastore_plugin.h"
29 #include <sqlite3.h>
30
31 /**
32  * Enable or disable logging debug messages.
33  */
34 #define DEBUG_SQLITE GNUNET_NO
35
36 /**
37  * We allocate items on the stack at times.  To prevent a stack
38  * overflow, we impose a limit on the maximum size for the data per
39  * item.  64k should be enough.
40  */
41 #define MAX_ITEM_SIZE 65536
42
43 /**
44  * After how many ms "busy" should a DB operation fail for good?
45  * A low value makes sure that we are more responsive to requests
46  * (especially PUTs).  A high value guarantees a higher success
47  * rate (SELECTs in iterate can take several seconds despite LIMIT=1).
48  *
49  * The default value of 250ms should ensure that users do not experience
50  * huge latencies while at the same time allowing operations to succeed
51  * with reasonable probability.
52  */
53 #define BUSY_TIMEOUT_MS 250
54
55
56 /**
57  * Log an error message at log-level 'level' that indicates
58  * a failure of the command 'cmd' on file 'filename'
59  * with the message given by strerror(errno).
60  */
61 #define LOG_SQLITE(db, msg, level, cmd) do { GNUNET_log_from (level, "sqlite", _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, sqlite3_errmsg(db->dbh)); if (msg != NULL) GNUNET_asprintf(msg, _("`%s' failed at %s:%u with error: %s"), cmd, __FILE__, __LINE__, sqlite3_errmsg(db->dbh)); } while(0)
62
63
64
65 /**
66  * Context for all functions in this plugin.
67  */
68 struct Plugin 
69 {
70   /**
71    * Our execution environment.
72    */
73   struct GNUNET_DATASTORE_PluginEnvironment *env;
74
75   /**
76    * Database filename.
77    */
78   char *fn;
79
80   /**
81    * Native SQLite database handle.
82    */
83   sqlite3 *dbh;
84
85   /**
86    * Precompiled SQL for deletion.
87    */
88   sqlite3_stmt *delRow;
89
90   /**
91    * Precompiled SQL for update.
92    */
93   sqlite3_stmt *updPrio;
94
95   /**
96    * Precompiled SQL for replication decrement.
97    */
98   sqlite3_stmt *updRepl;
99
100   /**
101    * Precompiled SQL for replication selection.
102    */
103   sqlite3_stmt *selRepl;
104
105   /**
106    * Precompiled SQL for expiration selection.
107    */
108   sqlite3_stmt *selExpi;
109
110   /**
111    * Precompiled SQL for expiration selection.
112    */
113   sqlite3_stmt *selZeroAnon;
114
115   /**
116    * Precompiled SQL for insertion.
117    */
118   sqlite3_stmt *insertContent;
119
120   /**
121    * Should the database be dropped on shutdown?
122    */
123   int drop_on_shutdown;
124
125 };
126
127
128 /**
129  * @brief Prepare a SQL statement
130  *
131  * @param dbh handle to the database
132  * @param zSql SQL statement, UTF-8 encoded
133  * @param ppStmt set to the prepared statement
134  * @return 0 on success
135  */
136 static int
137 sq_prepare (sqlite3 * dbh, 
138             const char *zSql,
139             sqlite3_stmt ** ppStmt)
140 {
141   char *dummy;
142   int result;
143
144   result = sqlite3_prepare_v2 (dbh,
145                                zSql,
146                                strlen (zSql), 
147                                ppStmt,
148                                (const char **) &dummy);
149 #if DEBUG_SQLITE && 0
150   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
151                    "sqlite",
152                    "Prepared `%s' / %p: %d\n",
153                    zSql,
154                    *ppStmt, 
155                    result);
156 #endif
157   return result;
158 }
159
160
161 /**
162  * Create our database indices.
163  * 
164  * @param dbh handle to the database
165  */
166 static void
167 create_indices (sqlite3 * dbh)
168 {
169   /* create indices */
170   sqlite3_exec (dbh,
171                 "CREATE INDEX idx_hash ON gn090 (hash)", NULL, NULL, NULL);
172   sqlite3_exec (dbh,
173                 "CREATE INDEX idx_hash_vhash ON gn090 (hash,vhash)", NULL,
174                 NULL, NULL);
175   sqlite3_exec (dbh, "CREATE INDEX idx_expire_repl ON gn090 (expire ASC,repl DESC)", NULL, NULL,
176                 NULL);
177   sqlite3_exec (dbh, "CREATE INDEX idx_comb ON gn090 (anonLevel ASC,expire ASC,prio,type,hash)",
178                 NULL, NULL, NULL);
179   sqlite3_exec (dbh, "CREATE INDEX idx_expire ON gn090 (expire)",
180                 NULL, NULL, NULL);
181   sqlite3_exec (dbh, "CREATE INDEX idx_repl_rvalue ON gn090 (repl,rvalue)",
182                 NULL, NULL, NULL);
183 }
184
185
186 #if 0
187 #define CHECK(a) GNUNET_break(a)
188 #define ENULL NULL
189 #else
190 #define ENULL &e
191 #define ENULL_DEFINED 1
192 #define CHECK(a) if (! a) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "%s\n", e); sqlite3_free(e); }
193 #endif
194
195
196 /**
197  * Initialize the database connections and associated
198  * data structures (create tables and indices
199  * as needed as well).
200  *
201  * @param cfg our configuration
202  * @param plugin the plugin context (state for this module)
203  * @return GNUNET_OK on success
204  */
205 static int
206 database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg,
207                 struct Plugin *plugin)
208 {
209   sqlite3_stmt *stmt;
210   char *afsdir;
211 #if ENULL_DEFINED
212   char *e;
213 #endif
214   
215   if (GNUNET_OK != 
216       GNUNET_CONFIGURATION_get_value_filename (cfg,
217                                                "datastore-sqlite",
218                                                "FILENAME",
219                                                &afsdir))
220     {
221       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
222                        "sqlite",
223                        _("Option `%s' in section `%s' missing in configuration!\n"),
224                        "FILENAME",
225                        "datastore-sqlite");
226       return GNUNET_SYSERR;
227     }
228   if (GNUNET_OK != GNUNET_DISK_file_test (afsdir))
229     {
230       if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (afsdir))
231         {
232           GNUNET_break (0);
233           GNUNET_free (afsdir);
234           return GNUNET_SYSERR;
235         }
236       /* database is new or got deleted, reset payload to zero! */
237       plugin->env->duc (plugin->env->cls, 0);
238     }
239 #ifdef ENABLE_NLS
240   plugin->fn = GNUNET_STRINGS_to_utf8 (afsdir, strlen (afsdir),
241                                        nl_langinfo (CODESET));
242 #else
243   plugin->fn = GNUNET_STRINGS_to_utf8 (afsdir, strlen (afsdir),
244                                        "UTF-8");   /* good luck */
245 #endif
246   GNUNET_free (afsdir);
247   
248   /* Open database and precompile statements */
249   if (sqlite3_open (plugin->fn, &plugin->dbh) != SQLITE_OK)
250     {
251       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
252                        "sqlite",
253                        _("Unable to initialize SQLite: %s.\n"),
254                        sqlite3_errmsg (plugin->dbh));
255       return GNUNET_SYSERR;
256     }
257   CHECK (SQLITE_OK ==
258          sqlite3_exec (plugin->dbh,
259                        "PRAGMA temp_store=MEMORY", NULL, NULL, ENULL));
260   CHECK (SQLITE_OK ==
261          sqlite3_exec (plugin->dbh,
262                        "PRAGMA synchronous=OFF", NULL, NULL, ENULL));
263   CHECK (SQLITE_OK ==
264          sqlite3_exec (plugin->dbh,
265                        "PRAGMA legacy_file_format=OFF", NULL, NULL, ENULL));
266   CHECK (SQLITE_OK ==
267          sqlite3_exec (plugin->dbh,
268                        "PRAGMA auto_vacuum=INCREMENTAL", NULL, NULL, ENULL));
269   CHECK (SQLITE_OK ==
270          sqlite3_exec (plugin->dbh,
271                        "PRAGMA locking_mode=EXCLUSIVE", NULL, NULL, ENULL));
272   CHECK (SQLITE_OK ==
273          sqlite3_exec (plugin->dbh,
274                        "PRAGMA count_changes=OFF", NULL, NULL, ENULL));
275   CHECK (SQLITE_OK ==
276          sqlite3_exec (plugin->dbh, 
277                        "PRAGMA page_size=4092", NULL, NULL, ENULL));
278
279   CHECK (SQLITE_OK == sqlite3_busy_timeout (plugin->dbh, BUSY_TIMEOUT_MS));
280
281
282   /* We have to do it here, because otherwise precompiling SQL might fail */
283   CHECK (SQLITE_OK ==
284          sq_prepare (plugin->dbh,
285                      "SELECT 1 FROM sqlite_master WHERE tbl_name = 'gn090'",
286                      &stmt));
287   if ( (sqlite3_step (stmt) == SQLITE_DONE) &&
288        (sqlite3_exec (plugin->dbh,
289                       "CREATE TABLE gn090 ("
290                       "  repl INT4 NOT NULL DEFAULT 0,"
291                       "  type INT4 NOT NULL DEFAULT 0,"
292                       "  prio INT4 NOT NULL DEFAULT 0,"
293                       "  anonLevel INT4 NOT NULL DEFAULT 0,"
294                       "  expire INT8 NOT NULL DEFAULT 0,"
295                       "  rvalue INT8 NOT NULL,"
296                       "  hash TEXT NOT NULL DEFAULT '',"
297                       "  vhash TEXT NOT NULL DEFAULT '',"
298                       "  value BLOB NOT NULL DEFAULT '')", NULL, NULL,
299                       NULL) != SQLITE_OK) )
300     {
301       LOG_SQLITE (plugin, NULL,
302                   GNUNET_ERROR_TYPE_ERROR, 
303                   "sqlite3_exec");
304       sqlite3_finalize (stmt);
305       return GNUNET_SYSERR;
306     }
307   sqlite3_finalize (stmt);
308   create_indices (plugin->dbh);
309
310   if ((sq_prepare (plugin->dbh,
311                    "UPDATE gn090 SET prio = prio + ?, expire = MAX(expire,?) WHERE _ROWID_ = ?",
312                    &plugin->updPrio) != SQLITE_OK) ||
313       (sq_prepare (plugin->dbh,
314                    "UPDATE gn090 SET repl = MAX (0, repl - 1) WHERE _ROWID_ = ?",
315                    &plugin->updRepl) != SQLITE_OK) ||
316       (sq_prepare (plugin->dbh,
317                    "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090"
318                    " ORDER BY repl DESC, Random() LIMIT 1",
319                    &plugin->selRepl) != SQLITE_OK) ||
320       (sq_prepare (plugin->dbh,
321                    "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090 "
322                    " WHERE NOT EXISTS (SELECT 1 FROM gn090 WHERE expire < ?1 LIMIT 1) OR expire < ?1 "
323                    " ORDER BY prio ASC LIMIT 1",
324                    &plugin->selExpi) != SQLITE_OK) ||
325       (sq_prepare (plugin->dbh, 
326                    "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090 "
327                    "WHERE (anonLevel = 0 AND type=?1) "
328                    "ORDER BY hash DESC LIMIT 1 OFFSET ?2",
329                    &plugin->selZeroAnon) != SQLITE_OK) ||
330       (sq_prepare (plugin->dbh,
331                    "INSERT INTO gn090 (repl, type, prio, "
332                    "anonLevel, expire, rvalue, hash, vhash, value) "
333                    "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
334                    &plugin->insertContent) != SQLITE_OK) ||
335       (sq_prepare (plugin->dbh,
336                    "DELETE FROM gn090 WHERE _ROWID_ = ?",
337                    &plugin->delRow) != SQLITE_OK))
338     {
339       LOG_SQLITE (plugin, NULL,
340                   GNUNET_ERROR_TYPE_ERROR, "precompiling");
341       return GNUNET_SYSERR;
342     }
343
344   return GNUNET_OK;
345 }
346
347
348 /**
349  * Shutdown database connection and associate data
350  * structures.
351  * @param plugin the plugin context (state for this module)
352  */
353 static void
354 database_shutdown (struct Plugin *plugin)
355 {
356   int result;
357 #if SQLITE_VERSION_NUMBER >= 3007000
358   sqlite3_stmt *stmt;
359 #endif
360
361   if (plugin->delRow != NULL)
362     sqlite3_finalize (plugin->delRow);
363   if (plugin->updPrio != NULL)
364     sqlite3_finalize (plugin->updPrio);
365   if (plugin->updRepl != NULL)
366     sqlite3_finalize (plugin->updRepl);
367   if (plugin->selRepl != NULL)
368     sqlite3_finalize (plugin->selRepl);
369   if (plugin->selExpi != NULL)
370     sqlite3_finalize (plugin->selExpi);
371   if (plugin->selZeroAnon != NULL)
372     sqlite3_finalize (plugin->selZeroAnon);
373   if (plugin->insertContent != NULL)
374     sqlite3_finalize (plugin->insertContent);
375   result = sqlite3_close(plugin->dbh);
376 #if SQLITE_VERSION_NUMBER >= 3007000
377   if (result == SQLITE_BUSY)
378     {
379       GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, 
380                        "sqlite",
381                        _("Tried to close sqlite without finalizing all prepared statements.\n"));
382       stmt = sqlite3_next_stmt(plugin->dbh, NULL); 
383       while (stmt != NULL)
384         {
385 #if DEBUG_SQLITE
386           GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
387                      "sqlite", "Closing statement %p\n", stmt);
388 #endif
389           result = sqlite3_finalize(stmt);
390 #if DEBUG_SQLITE
391           if (result != SQLITE_OK)
392               GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
393                                "sqlite",
394                                "Failed to close statement %p: %d\n", stmt, result);
395 #endif
396           stmt = sqlite3_next_stmt(plugin->dbh, NULL);
397         }
398       result = sqlite3_close(plugin->dbh);
399     }
400 #endif
401   if (SQLITE_OK != result)
402       LOG_SQLITE (plugin, NULL,
403                   GNUNET_ERROR_TYPE_ERROR, 
404                   "sqlite3_close");
405
406   GNUNET_free_non_null (plugin->fn);
407 }
408
409
410 /**
411  * Delete the database entry with the given
412  * row identifier.
413  *
414  * @param plugin the plugin context (state for this module)
415  * @param rid the ID of the row to delete
416  */
417 static int
418 delete_by_rowid (struct Plugin* plugin, 
419                  unsigned long long rid)
420 {
421   if (SQLITE_OK !=
422       sqlite3_bind_int64 (plugin->delRow, 1, rid))
423     {
424       LOG_SQLITE (plugin, NULL,
425                   GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX");
426       if (SQLITE_OK != sqlite3_reset (plugin->delRow))
427         LOG_SQLITE (plugin, NULL,
428                     GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
429       return GNUNET_SYSERR;
430     }
431   if (SQLITE_DONE != sqlite3_step (plugin->delRow))
432     {
433       LOG_SQLITE (plugin, NULL,
434                   GNUNET_ERROR_TYPE_ERROR |
435                   GNUNET_ERROR_TYPE_BULK, "sqlite3_step");
436       if (SQLITE_OK != sqlite3_reset (plugin->delRow))
437           LOG_SQLITE (plugin, NULL,
438                       GNUNET_ERROR_TYPE_ERROR |
439                       GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
440       return GNUNET_SYSERR;
441     }
442   if (SQLITE_OK != sqlite3_reset (plugin->delRow))
443     LOG_SQLITE (plugin, NULL,
444                 GNUNET_ERROR_TYPE_ERROR |
445                 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
446   return GNUNET_OK;
447 }
448
449
450 /**
451  * Store an item in the datastore.
452  *
453  * @param cls closure
454  * @param key key for the item
455  * @param size number of bytes in data
456  * @param data content stored
457  * @param type type of the content
458  * @param priority priority of the content
459  * @param anonymity anonymity-level for the content
460  * @param replication replication-level for the content
461  * @param expiration expiration time for the content
462  * @param msg set to an error message
463  * @return GNUNET_OK on success
464  */
465 static int
466 sqlite_plugin_put (void *cls,
467                    const GNUNET_HashCode *key,
468                    uint32_t size,
469                    const void *data,
470                    enum GNUNET_BLOCK_Type type,
471                    uint32_t priority,
472                    uint32_t anonymity,
473                    uint32_t replication,
474                    struct GNUNET_TIME_Absolute expiration,
475                    char ** msg)
476 {
477   struct Plugin *plugin = cls;
478   int n;
479   int ret;
480   sqlite3_stmt *stmt;
481   GNUNET_HashCode vhash;
482   uint64_t rvalue;
483
484   if (size > MAX_ITEM_SIZE)
485     return GNUNET_SYSERR;
486 #if DEBUG_SQLITE
487   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
488                    "sqlite",
489                    "Storing in database block with type %u/key `%s'/priority %u/expiration in %llu ms (%lld).\n",
490                    type, 
491                    GNUNET_h2s(key),
492                    priority,
493                    (unsigned long long) GNUNET_TIME_absolute_get_remaining (expiration).rel_value,
494                    (long long) expiration.abs_value);
495 #endif
496   GNUNET_CRYPTO_hash (data, size, &vhash);
497   stmt = plugin->insertContent;
498   rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
499   if ((SQLITE_OK != sqlite3_bind_int (stmt, 1, replication)) ||
500       (SQLITE_OK != sqlite3_bind_int (stmt, 2, type)) ||
501       (SQLITE_OK != sqlite3_bind_int (stmt, 3, priority)) ||
502       (SQLITE_OK != sqlite3_bind_int (stmt, 4, anonymity)) ||
503       (SQLITE_OK != sqlite3_bind_int64 (stmt, 5, expiration.abs_value)) ||
504       (SQLITE_OK != sqlite3_bind_int64 (stmt, 6, rvalue)) ||
505       (SQLITE_OK !=
506        sqlite3_bind_blob (stmt, 7, key, sizeof (GNUNET_HashCode),
507                           SQLITE_TRANSIENT)) ||
508       (SQLITE_OK !=
509        sqlite3_bind_blob (stmt, 8, &vhash, sizeof (GNUNET_HashCode),
510                           SQLITE_TRANSIENT))
511       || (SQLITE_OK !=
512           sqlite3_bind_blob (stmt, 9, data, size,
513                              SQLITE_TRANSIENT)))
514     {
515       LOG_SQLITE (plugin,
516                   msg,
517                   GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX");
518       if (SQLITE_OK != sqlite3_reset (stmt))
519         LOG_SQLITE (plugin, NULL,
520                     GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
521       return GNUNET_SYSERR;
522     }
523   n = sqlite3_step (stmt);
524   switch (n)
525     {
526     case SQLITE_DONE:
527       plugin->env->duc (plugin->env->cls,
528                         size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
529 #if DEBUG_SQLITE
530       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
531                        "sqlite",
532                        "Stored new entry (%u bytes)\n",
533                        size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
534 #endif
535       ret = GNUNET_OK;
536       break;
537     case SQLITE_BUSY:      
538       GNUNET_break (0);
539       LOG_SQLITE (plugin, msg,
540                   GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 
541                   "sqlite3_step");
542       ret = GNUNET_SYSERR;
543       break;
544     default:
545       LOG_SQLITE (plugin, msg,
546                   GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 
547                   "sqlite3_step");
548       if (SQLITE_OK != sqlite3_reset (stmt))
549         LOG_SQLITE (plugin, NULL,
550                     GNUNET_ERROR_TYPE_ERROR |
551                     GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
552       database_shutdown (plugin);
553       database_setup (plugin->env->cfg,
554                       plugin);
555       return GNUNET_SYSERR;    
556     }
557   if (SQLITE_OK != sqlite3_reset (stmt))
558     LOG_SQLITE (plugin, NULL,
559                 GNUNET_ERROR_TYPE_ERROR |
560                 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
561   return ret;
562 }
563
564
565 /**
566  * Update the priority for a particular key in the datastore.  If
567  * the expiration time in value is different than the time found in
568  * the datastore, the higher value should be kept.  For the
569  * anonymity level, the lower value is to be used.  The specified
570  * priority should be added to the existing priority, ignoring the
571  * priority in value.
572  *
573  * Note that it is possible for multiple values to match this put.
574  * In that case, all of the respective values are updated.
575  *
576  * @param cls the plugin context (state for this module)
577  * @param uid unique identifier of the datum
578  * @param delta by how much should the priority
579  *     change?  If priority + delta < 0 the
580  *     priority should be set to 0 (never go
581  *     negative).
582  * @param expire new expiration time should be the
583  *     MAX of any existing expiration time and
584  *     this value
585  * @param msg set to an error message
586  * @return GNUNET_OK on success
587  */
588 static int
589 sqlite_plugin_update (void *cls,
590                       uint64_t uid,
591                       int delta, struct GNUNET_TIME_Absolute expire,
592                       char **msg)
593 {
594   struct Plugin *plugin = cls;
595   int n;
596
597   if ( (SQLITE_OK !=
598         sqlite3_bind_int (plugin->updPrio, 1, delta)) ||
599        (SQLITE_OK !=
600         sqlite3_bind_int64 (plugin->updPrio, 2, expire.abs_value)) ||
601        (SQLITE_OK !=
602         sqlite3_bind_int64 (plugin->updPrio, 3, uid)) )
603     {
604       LOG_SQLITE (plugin, msg,
605                   GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX");
606       if (SQLITE_OK != sqlite3_reset (plugin->updPrio))
607         LOG_SQLITE (plugin, NULL,
608                     GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
609       return GNUNET_SYSERR;
610
611     }
612   n = sqlite3_step (plugin->updPrio);
613   if (SQLITE_OK != sqlite3_reset (plugin->updPrio))
614     LOG_SQLITE (plugin, NULL,
615                 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
616    switch (n)
617     {
618     case SQLITE_DONE:
619 #if DEBUG_SQLITE
620       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
621                        "sqlite",
622                        "Block updated\n");
623 #endif
624       return GNUNET_OK;
625     case SQLITE_BUSY:
626       LOG_SQLITE (plugin, msg,
627                   GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
628                   "sqlite3_step");
629       return GNUNET_NO;
630     default:
631       LOG_SQLITE (plugin, msg,
632                   GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
633                   "sqlite3_step");
634       return GNUNET_SYSERR;
635     }
636 }
637
638
639 /**
640  * Execute statement that gets a row and call the callback
641  * with the result.  Resets the statement afterwards.
642  *
643  * @param plugin the plugin
644  * @param stmt the statement
645  * @param proc processor to call
646  * @param proc_cls closure for 'proc'
647  */
648 static void
649 execute_get (struct Plugin *plugin,
650              sqlite3_stmt *stmt,
651              PluginDatumProcessor proc, void *proc_cls)
652 {
653   int n;
654   struct GNUNET_TIME_Absolute expiration;
655   unsigned long long rowid;
656   unsigned int size;
657   int ret;
658
659   n = sqlite3_step (stmt);
660   switch (n)
661     {
662     case SQLITE_ROW:
663       size = sqlite3_column_bytes (stmt, 5);
664       rowid = sqlite3_column_int64 (stmt, 6);
665       if (sqlite3_column_bytes (stmt, 4) != sizeof (GNUNET_HashCode))
666         {
667           GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, 
668                            "sqlite",
669                            _("Invalid data in database.  Trying to fix (by deletion).\n"));
670           if (SQLITE_OK != sqlite3_reset (stmt))
671             LOG_SQLITE (plugin, NULL,
672                         GNUNET_ERROR_TYPE_ERROR |
673                         GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
674           if (GNUNET_OK == delete_by_rowid (plugin, rowid))
675             plugin->env->duc (plugin->env->cls,
676                               - (size + GNUNET_DATASTORE_ENTRY_OVERHEAD));        
677           break;
678         }
679       expiration.abs_value = sqlite3_column_int64 (stmt, 3);
680 #if DEBUG_SQLITE
681       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 
682                        "sqlite",
683                        "Found reply in database with expiration %llu\n",
684                        (unsigned long long) expiration.abs_value);
685 #endif
686       ret = proc (proc_cls,
687                   sqlite3_column_blob (stmt, 4) /* key */,
688                   size,
689                   sqlite3_column_blob (stmt, 5) /* data */, 
690                   sqlite3_column_int (stmt, 0) /* type */,
691                   sqlite3_column_int (stmt, 1) /* priority */,
692                   sqlite3_column_int (stmt, 2) /* anonymity */,
693                   expiration,
694                   rowid);
695       if (SQLITE_OK != sqlite3_reset (stmt))
696         LOG_SQLITE (plugin, NULL,
697                     GNUNET_ERROR_TYPE_ERROR |
698                     GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
699       if ( (GNUNET_NO == ret) &&
700            (GNUNET_OK == delete_by_rowid (plugin, rowid)) )
701         plugin->env->duc (plugin->env->cls,
702                           - (size + GNUNET_DATASTORE_ENTRY_OVERHEAD));  
703       return;
704     case SQLITE_DONE:
705       /* database must be empty */
706       if (SQLITE_OK != sqlite3_reset (stmt))
707         LOG_SQLITE (plugin, NULL,
708                     GNUNET_ERROR_TYPE_ERROR |
709                     GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
710       break;
711     case SQLITE_BUSY:    
712     case SQLITE_ERROR:
713     case SQLITE_MISUSE:
714     default:
715       LOG_SQLITE (plugin, NULL,
716                   GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 
717                   "sqlite3_step");
718       if (SQLITE_OK != sqlite3_reset (stmt))
719         LOG_SQLITE (plugin, NULL,
720                     GNUNET_ERROR_TYPE_ERROR |
721                     GNUNET_ERROR_TYPE_BULK,
722                     "sqlite3_reset");
723       GNUNET_break (0);
724       database_shutdown (plugin);
725       database_setup (plugin->env->cfg,
726                       plugin);
727       break;
728     }
729   if (SQLITE_OK != sqlite3_reset (stmt))
730     LOG_SQLITE (plugin, NULL,
731                 GNUNET_ERROR_TYPE_ERROR |
732                 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
733   proc (proc_cls, NULL, 0, NULL, 0, 0, 0,           
734         GNUNET_TIME_UNIT_ZERO_ABS, 0);
735 }
736
737
738
739 /**
740  * Select a subset of the items in the datastore and call
741  * the given processor for the item.
742  *
743  * @param cls our plugin context
744  * @param offset offset of the result (modulo num-results); 
745  *               specific ordering does not matter for the offset
746  * @param type entries of which type should be considered?
747  *        Use 0 for any type.
748  * @param proc function to call on each matching value;
749  *        will be called once with a NULL value at the end
750  * @param proc_cls closure for proc
751  */
752 static void
753 sqlite_plugin_get_zero_anonymity (void *cls,
754                                   uint64_t offset,
755                                   enum GNUNET_BLOCK_Type type,
756                                   PluginDatumProcessor proc,
757                                   void *proc_cls)
758 {
759   struct Plugin *plugin = cls;
760   sqlite3_stmt *stmt;
761
762   GNUNET_assert (type != GNUNET_BLOCK_TYPE_ANY);
763   stmt = plugin->selZeroAnon;
764   if ( (SQLITE_OK != sqlite3_bind_int (stmt, 1, type)) ||
765        (SQLITE_OK != sqlite3_bind_int64 (stmt, 2, offset)) )
766     {
767       LOG_SQLITE (plugin, NULL,
768                   GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 
769                   "sqlite3_bind_XXXX");
770       if (SQLITE_OK != sqlite3_reset (stmt))
771         LOG_SQLITE (plugin, NULL,
772                     GNUNET_ERROR_TYPE_ERROR | 
773                     GNUNET_ERROR_TYPE_BULK, 
774                     "sqlite3_reset");
775       proc (proc_cls, NULL, 0, NULL, 0, 0, 0,       
776             GNUNET_TIME_UNIT_ZERO_ABS, 0);
777       return;
778     }
779   execute_get (plugin, stmt, proc, proc_cls);
780 }
781
782
783
784 /**
785  * Get results for a particular key in the datastore.
786  *
787  * @param cls closure
788  * @param offset offset (mod count).
789  * @param key key to match, never NULL
790  * @param vhash hash of the value, maybe NULL (to
791  *        match all values that have the right key).
792  *        Note that for DBlocks there is no difference
793  *        betwen key and vhash, but for other blocks
794  *        there may be!
795  * @param type entries of which type are relevant?
796  *     Use 0 for any type.
797  * @param proc function to call on each matching value;
798  *        will be called once with a NULL value at the end
799  * @param proc_cls closure for proc
800  */
801 static void
802 sqlite_plugin_get_key (void *cls,
803                        uint64_t offset,
804                        const GNUNET_HashCode *key,
805                        const GNUNET_HashCode *vhash,
806                        enum GNUNET_BLOCK_Type type,
807                        PluginDatumProcessor proc, void *proc_cls)
808 {
809   struct Plugin *plugin = cls;
810   int ret;
811   int total;
812   int limit_off;
813   unsigned int sqoff;
814   sqlite3_stmt *stmt;
815   char scratch[256];
816
817   GNUNET_assert (proc != NULL);
818   GNUNET_assert (key != NULL);
819   GNUNET_snprintf (scratch, sizeof (scratch),
820                    "SELECT count(*) FROM gn090 WHERE hash=?%s%s",
821                    vhash == NULL ? "" : " AND vhash=?",
822                    type  == 0    ? "" : " AND type=?");
823   if (sq_prepare (plugin->dbh, scratch, &stmt) != SQLITE_OK)
824     {
825       LOG_SQLITE (plugin, NULL,
826                   GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite_prepare");
827       proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
828       return;
829     }
830   sqoff = 1;
831   ret = sqlite3_bind_blob (stmt, sqoff++,
832                            key, sizeof (GNUNET_HashCode), SQLITE_TRANSIENT);
833   if ((vhash != NULL) && (ret == SQLITE_OK))
834     ret = sqlite3_bind_blob (stmt, sqoff++,
835                              vhash,
836                              sizeof (GNUNET_HashCode), SQLITE_TRANSIENT);
837   if ((type != 0) && (ret == SQLITE_OK))
838     ret = sqlite3_bind_int (stmt, sqoff++, type);
839   if (SQLITE_OK != ret)
840     {
841       LOG_SQLITE (plugin, NULL,
842                   GNUNET_ERROR_TYPE_ERROR, "sqlite_bind");
843       sqlite3_finalize (stmt);
844       proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
845       return;
846     }
847   ret = sqlite3_step (stmt);
848   if (ret != SQLITE_ROW)
849     {
850       LOG_SQLITE (plugin, NULL,
851                   GNUNET_ERROR_TYPE_ERROR| GNUNET_ERROR_TYPE_BULK, 
852                   "sqlite_step");
853       sqlite3_finalize (stmt);
854       proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
855       return;
856     }
857   total = sqlite3_column_int (stmt, 0);
858   sqlite3_finalize (stmt);
859   if (0 == total)
860     {
861       proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
862       return;
863     }
864   limit_off = (int) (offset % total);
865   if (limit_off < 0)
866     limit_off += total;
867   GNUNET_snprintf (scratch, sizeof (scratch),
868                    "SELECT type, prio, anonLevel, expire, hash, value, _ROWID_ "
869                    "FROM gn090 WHERE hash=?%s%s "
870                    "ORDER BY _ROWID_ ASC LIMIT 1 OFFSET ?",
871                    vhash == NULL ? "" : " AND vhash=?",
872                    type == 0 ? "" : " AND type=?");
873   if (sq_prepare (plugin->dbh, scratch, &stmt) != SQLITE_OK)
874     {
875       LOG_SQLITE (plugin, NULL,
876                   GNUNET_ERROR_TYPE_ERROR |
877                   GNUNET_ERROR_TYPE_BULK, "sqlite_prepare");
878       proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
879       return;
880     }
881   sqoff = 1;
882   ret = sqlite3_bind_blob (stmt,
883                            sqoff++,
884                            key, 
885                            sizeof (GNUNET_HashCode),
886                            SQLITE_TRANSIENT);
887   if ((vhash != NULL) && (ret == SQLITE_OK))
888     ret = sqlite3_bind_blob (stmt,
889                              sqoff++,
890                              vhash,
891                              sizeof (GNUNET_HashCode), SQLITE_TRANSIENT);
892   if ((type != 0) && (ret == SQLITE_OK))
893     ret = sqlite3_bind_int (stmt, sqoff++, type);
894   if (ret == SQLITE_OK)
895     ret = sqlite3_bind_int64 (stmt, sqoff++, limit_off);
896   if (ret != SQLITE_OK)
897     {
898       LOG_SQLITE (plugin, NULL,
899                   GNUNET_ERROR_TYPE_ERROR |
900                   GNUNET_ERROR_TYPE_BULK, "sqlite_bind");
901       proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
902       return;
903     }
904   execute_get (plugin, stmt, proc, proc_cls);
905   sqlite3_finalize (stmt);
906 }
907
908
909
910 /**
911  * Context for 'repl_proc' function.
912  */
913 struct ReplCtx
914 {
915   
916   /**
917    * Function to call for the result (or the NULL).
918    */
919   PluginDatumProcessor proc;
920   
921   /**
922    * Closure for proc.
923    */
924   void *proc_cls;
925
926   /**
927    * UID to use.
928    */
929   uint64_t uid;
930
931   /**
932    * Yes if UID was set.
933    */
934   int have_uid;
935 };
936
937
938 /**
939  * Wrapper for the processor for 'sqlite_plugin_replication_get'.
940  * Decrements the replication counter and calls the original
941  * processor.
942  *
943  * @param cls closure
944  * @param key key for the content
945  * @param size number of bytes in data
946  * @param data content stored
947  * @param type type of the content
948  * @param priority priority of the content
949  * @param anonymity anonymity-level for the content
950  * @param expiration expiration time for the content
951  * @param uid unique identifier for the datum;
952  *        maybe 0 if no unique identifier is available
953  *
954  * @return GNUNET_OK for normal return,
955  *         GNUNET_NO to delete the item
956  */
957 static int
958 repl_proc (void *cls,
959            const GNUNET_HashCode *key,
960            uint32_t size,
961            const void *data,
962            enum GNUNET_BLOCK_Type type,
963            uint32_t priority,
964            uint32_t anonymity,
965            struct GNUNET_TIME_Absolute expiration, 
966            uint64_t uid)
967 {
968   struct ReplCtx *rc = cls;
969   int ret;
970
971   ret = rc->proc (rc->proc_cls,
972                   key,
973                   size, data, 
974                   type, priority, anonymity, expiration,
975                   uid);
976   if (key != NULL)
977     {
978       rc->uid = uid;
979       rc->have_uid = GNUNET_YES;
980     }
981   return ret;
982 }
983
984
985 /**
986  * Get a random item for replication.  Returns a single random item
987  * from those with the highest replication counters.  The item's 
988  * replication counter is decremented by one IF it was positive before.
989  * Call 'proc' with all values ZERO or NULL if the datastore is empty.
990  *
991  * @param cls closure
992  * @param proc function to call the value (once only).
993  * @param proc_cls closure for proc
994  */
995 static void
996 sqlite_plugin_get_replication (void *cls,
997                                PluginDatumProcessor proc, void *proc_cls)
998 {
999   struct Plugin *plugin = cls;
1000   struct ReplCtx rc;
1001
1002 #if DEBUG_SQLITE
1003   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1004                    "sqlite",
1005                    "Getting random block based on replication order.\n");
1006 #endif
1007   rc.have_uid = GNUNET_NO;
1008   rc.proc = proc;
1009   rc.proc_cls = proc_cls;
1010   execute_get (plugin, plugin->selRepl, &repl_proc, &rc); 
1011   if (GNUNET_YES == rc.have_uid)
1012     {
1013       if (SQLITE_OK !=
1014           sqlite3_bind_int64 (plugin->updRepl, 1, rc.uid))
1015         {
1016           LOG_SQLITE (plugin, NULL,
1017                       GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX");
1018           if (SQLITE_OK != sqlite3_reset (plugin->updRepl))
1019             LOG_SQLITE (plugin, NULL,
1020                         GNUNET_ERROR_TYPE_ERROR |
1021                         GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
1022           return;
1023         }
1024       if (SQLITE_DONE != sqlite3_step (plugin->updRepl))        
1025         LOG_SQLITE (plugin, NULL,
1026                     GNUNET_ERROR_TYPE_ERROR |
1027                     GNUNET_ERROR_TYPE_BULK, "sqlite3_step");
1028       if (SQLITE_OK != sqlite3_reset (plugin->updRepl))
1029         LOG_SQLITE (plugin, NULL,
1030                     GNUNET_ERROR_TYPE_ERROR |
1031                     GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
1032     }
1033 }
1034
1035
1036
1037 /**
1038  * Get a random item that has expired or has low priority.
1039  * Call 'proc' with all values ZERO or NULL if the datastore is empty.
1040  *
1041  * @param cls closure
1042  * @param proc function to call the value (once only).
1043  * @param proc_cls closure for proc
1044  */
1045 static void
1046 sqlite_plugin_get_expiration (void *cls,
1047                               PluginDatumProcessor proc, void *proc_cls)
1048 {
1049   struct Plugin *plugin = cls;
1050   sqlite3_stmt *stmt;
1051   struct GNUNET_TIME_Absolute now;
1052
1053 #if DEBUG_SQLITE
1054   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1055                    "sqlite",
1056                    "Getting random block based on expiration and priority order.\n");
1057 #endif
1058   now = GNUNET_TIME_absolute_get ();
1059   stmt = plugin->selExpi;
1060   if (SQLITE_OK != sqlite3_bind_int64 (stmt, 1, now.abs_value))
1061     {
1062       LOG_SQLITE (plugin, NULL,           
1063                   GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX");
1064       if (SQLITE_OK != sqlite3_reset (stmt))
1065         LOG_SQLITE (plugin, NULL,
1066                     GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
1067       proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 
1068             GNUNET_TIME_UNIT_ZERO_ABS, 0);
1069       return;
1070     }
1071   execute_get (plugin, stmt, proc, proc_cls);
1072 }
1073
1074
1075 /**
1076  * Drop database.
1077  *
1078  * @param cls our plugin context
1079  */
1080 static void 
1081 sqlite_plugin_drop (void *cls)
1082 {
1083   struct Plugin *plugin = cls;
1084   plugin->drop_on_shutdown = GNUNET_YES;
1085 }
1086
1087
1088 /**
1089  * Get an estimate of how much space the database is
1090  * currently using.
1091  *
1092  * @param cls the 'struct Plugin'
1093  * @return the size of the database on disk (estimate)
1094  */
1095 static unsigned long long
1096 sqlite_plugin_estimate_size (void *cls)
1097 {
1098   struct Plugin *plugin = cls;
1099   sqlite3_stmt *stmt;
1100   uint64_t pages;
1101   uint64_t page_size;
1102 #if ENULL_DEFINED
1103   char *e;
1104 #endif
1105
1106   if (SQLITE_VERSION_NUMBER < 3006000)
1107     {
1108       GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1109                        "datastore-sqlite",
1110                        _("sqlite version to old to determine size, assuming zero\n"));
1111       return 0;
1112     }
1113   CHECK (SQLITE_OK ==
1114          sqlite3_exec (plugin->dbh,
1115                        "VACUUM", NULL, NULL, ENULL));
1116   CHECK (SQLITE_OK ==
1117          sqlite3_exec (plugin->dbh,
1118                        "PRAGMA auto_vacuum=INCREMENTAL", NULL, NULL, ENULL));
1119   CHECK (SQLITE_OK ==
1120          sq_prepare (plugin->dbh,
1121                      "PRAGMA page_count",
1122                      &stmt));
1123   if (SQLITE_ROW ==
1124       sqlite3_step (stmt))
1125     pages = sqlite3_column_int64 (stmt, 0);
1126   else
1127     pages = 0;
1128   sqlite3_finalize (stmt);
1129   CHECK (SQLITE_OK ==
1130          sq_prepare (plugin->dbh,
1131                      "PRAGMA page_size",
1132                      &stmt));
1133   CHECK (SQLITE_ROW ==
1134          sqlite3_step (stmt));
1135   page_size = sqlite3_column_int64 (stmt, 0);
1136   sqlite3_finalize (stmt);
1137   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1138               _("Using sqlite page utilization to estimate payload (%llu pages of size %llu bytes)\n"),
1139               (unsigned long long) pages,
1140               (unsigned long long) page_size);
1141   return  pages * page_size;
1142 }
1143                                          
1144
1145 /**
1146  * Entry point for the plugin.
1147  *
1148  * @param cls the "struct GNUNET_DATASTORE_PluginEnvironment*"
1149  * @return NULL on error, othrewise the plugin context
1150  */
1151 void *
1152 libgnunet_plugin_datastore_sqlite_init (void *cls)
1153 {
1154   static struct Plugin plugin;
1155   struct GNUNET_DATASTORE_PluginEnvironment *env = cls;
1156   struct GNUNET_DATASTORE_PluginFunctions *api;
1157
1158   if (plugin.env != NULL)
1159     return NULL; /* can only initialize once! */
1160   memset (&plugin, 0, sizeof(struct Plugin));
1161   plugin.env = env;
1162   if (GNUNET_OK !=
1163       database_setup (env->cfg, &plugin))
1164     {
1165       database_shutdown (&plugin);
1166       return NULL;
1167     }
1168   api = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_PluginFunctions));
1169   api->cls = &plugin;
1170   api->estimate_size = &sqlite_plugin_estimate_size;
1171   api->put = &sqlite_plugin_put;
1172   api->update = &sqlite_plugin_update;
1173   api->get_key = &sqlite_plugin_get_key;
1174   api->get_replication = &sqlite_plugin_get_replication;
1175   api->get_expiration = &sqlite_plugin_get_expiration;
1176   api->get_zero_anonymity = &sqlite_plugin_get_zero_anonymity;
1177   api->drop = &sqlite_plugin_drop;
1178   GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1179                    "sqlite", _("Sqlite database running\n"));
1180   return api;
1181 }
1182
1183
1184 /**
1185  * Exit point from the plugin.
1186  *
1187  * @param cls the plugin context (as returned by "init")
1188  * @return always NULL
1189  */
1190 void *
1191 libgnunet_plugin_datastore_sqlite_done (void *cls)
1192 {
1193   char *fn;
1194   struct GNUNET_DATASTORE_PluginFunctions *api = cls;
1195   struct Plugin *plugin = api->cls;
1196
1197 #if DEBUG_SQLITE
1198   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1199                    "sqlite",
1200                    "sqlite plugin is done\n");
1201 #endif
1202
1203   fn = NULL;
1204   if (plugin->drop_on_shutdown)
1205     fn = GNUNET_strdup (plugin->fn);
1206 #if DEBUG_SQLITE
1207   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1208                    "sqlite",
1209                    "Shutting down database\n");
1210 #endif
1211   database_shutdown (plugin);
1212   plugin->env = NULL; 
1213   GNUNET_free (api);
1214   if (fn != NULL)
1215     {
1216       if (0 != UNLINK(fn))
1217         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1218                                   "unlink",
1219                                   fn);
1220       GNUNET_free (fn);
1221     }
1222 #if DEBUG_SQLITE
1223   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1224                    "sqlite",
1225                    "sqlite plugin is finished\n");
1226 #endif
1227   return NULL;
1228 }
1229
1230 /* end of plugin_datastore_sqlite.c */