removing "unused variable" error
[oweals/gnunet.git] / src / datastore / plugin_datastore_sqlite.c
index 0a18b9b2f2ef0b7015c7611994b7b16f3ad9d873..4799b6bcaea7639c831fe5a3e36838a5fa5eae86 100644 (file)
@@ -25,7 +25,7 @@
  */
 
 #include "platform.h"
-#include "plugin_datastore.h"
+#include "gnunet_datastore_plugin.h"
 #include <sqlite3.h>
 
 #define DEBUG_SQLITE GNUNET_NO
@@ -104,6 +104,11 @@ struct Plugin
    */
   sqlite3 *dbh;
 
+  /**
+   * Precompiled SQL for deletion.
+   */
+  sqlite3_stmt *delRow;
+
   /**
    * Precompiled SQL for update.
    */
@@ -145,9 +150,16 @@ sq_prepare (sqlite3 * dbh, const char *zSql,
             sqlite3_stmt ** ppStmt)
 {
   char *dummy;
-  return sqlite3_prepare_v2 (dbh,
-                            zSql,
-                            strlen (zSql), ppStmt, (const char **) &dummy);
+  int result;
+  result = sqlite3_prepare_v2 (dbh,
+                              zSql,
+                              strlen (zSql), ppStmt, (const char **) &dummy);
+#if DEBUG_SQLITE
+  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
+                  "sqlite",
+                   "Prepared %p: %d\n", *ppStmt, result);
+#endif
+  return result;
 }
 
 
@@ -234,13 +246,13 @@ database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg,
       /* database is new or got deleted, reset payload to zero! */
       plugin->env->duc (plugin->env->cls, 0);
     }
-  plugin->fn = GNUNET_STRINGS_to_utf8 (afsdir, strlen (afsdir),
 #ifdef ENABLE_NLS
-                                             nl_langinfo (CODESET)
+  plugin->fn = GNUNET_STRINGS_to_utf8 (afsdir, strlen (afsdir),
+                                      nl_langinfo (CODESET));
 #else
-                                             "UTF-8"   /* good luck */
+  plugin->fn = GNUNET_STRINGS_to_utf8 (afsdir, strlen (afsdir),
+                                      "UTF-8");   /* good luck */
 #endif
-                                             );
   GNUNET_free (afsdir);
   
   /* Open database and precompile statements */
@@ -324,12 +336,16 @@ database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg,
                    "INSERT INTO gn080 (size, type, prio, "
                    "anonLevel, expire, hash, vhash, value) VALUES "
                    "(?, ?, ?, ?, ?, ?, ?, ?)",
-                   &plugin->insertContent) != SQLITE_OK))
+                   &plugin->insertContent) != SQLITE_OK) ||
+      (sq_prepare (plugin->dbh,
+                   "DELETE FROM gn080 WHERE _ROWID_ = ?",
+                   &plugin->delRow) != SQLITE_OK))
     {
       LOG_SQLITE (plugin, NULL,
                   GNUNET_ERROR_TYPE_ERROR, "precompiling");
       return GNUNET_SYSERR;
     }
+
   return GNUNET_OK;
 }
 
@@ -342,11 +358,47 @@ database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg,
 static void
 database_shutdown (struct Plugin *plugin)
 {
+  int result;
+  sqlite3_stmt *stmt;
+  stmt = NULL;
+
+  if (plugin->delRow != NULL)
+    sqlite3_finalize (plugin->delRow);
   if (plugin->updPrio != NULL)
     sqlite3_finalize (plugin->updPrio);
   if (plugin->insertContent != NULL)
     sqlite3_finalize (plugin->insertContent);
-  sqlite3_close (plugin->dbh);
+  result = sqlite3_close(plugin->dbh);
+#if SQLITE_VERSION_NUMBER >= 3007000
+  if (result == SQLITE_BUSY)
+    {
+      GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, 
+                      "sqlite",
+                      _("Tried to close sqlite without finalizing all prepared statements.\n"));
+      stmt = sqlite3_next_stmt(plugin->dbh, NULL); 
+      while (stmt != NULL)
+        {
+#if DEBUG_SQLITE
+          GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
+                    "sqlite", "Closing statement %p\n", stmt);
+#endif
+          result = sqlite3_finalize(stmt);
+#if DEBUG_SQLITE
+          if (result != SQLITE_OK)
+              GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
+                              "sqlite",
+                               "Failed to close statement %p: %d\n", stmt, result);
+#endif
+         stmt = sqlite3_next_stmt(plugin->dbh, NULL);
+        }
+      result = sqlite3_close(plugin->dbh);
+    }
+#endif
+  if (SQLITE_OK != result)
+      LOG_SQLITE (plugin, NULL,
+                  GNUNET_ERROR_TYPE_ERROR, 
+                 "sqlite3_close");
+
   GNUNET_free_non_null (plugin->fn);
 }
 
@@ -362,26 +414,23 @@ static int
 delete_by_rowid (struct Plugin* plugin, 
                 unsigned long long rid)
 {
-  sqlite3_stmt *stmt;
 
-  if (sq_prepare (plugin->dbh,
-                  "DELETE FROM gn080 WHERE _ROWID_ = ?", &stmt) != SQLITE_OK)
+  sqlite3_bind_int64 (plugin->delRow, 1, rid);
+  if (SQLITE_DONE != sqlite3_step (plugin->delRow))
     {
       LOG_SQLITE (plugin, NULL,
                   GNUNET_ERROR_TYPE_ERROR |
-                  GNUNET_ERROR_TYPE_BULK, "sq_prepare");
+                  GNUNET_ERROR_TYPE_BULK, "sqlite3_step");
+      if (SQLITE_OK != sqlite3_reset (plugin->delRow))
+          LOG_SQLITE (plugin, NULL,
+                      GNUNET_ERROR_TYPE_ERROR |
+                      GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
       return GNUNET_SYSERR;
     }
-  sqlite3_bind_int64 (stmt, 1, rid);
-  if (SQLITE_DONE != sqlite3_step (stmt))
-    {
+  if (SQLITE_OK != sqlite3_reset (plugin->delRow))
       LOG_SQLITE (plugin, NULL,
                   GNUNET_ERROR_TYPE_ERROR |
-                  GNUNET_ERROR_TYPE_BULK, "sqlite3_step");
-      sqlite3_finalize (stmt);
-      return GNUNET_SYSERR;
-    }
-  sqlite3_finalize (stmt);
+                  GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
   return GNUNET_OK;
 }
 
@@ -556,7 +605,7 @@ sqlite_next_request_cont (void *cls,
 
   priority = sqlite3_column_int (nc->stmt, 2);
   anonymity = sqlite3_column_int (nc->stmt, 3);
-  expiration.value = sqlite3_column_int64 (nc->stmt, 4);
+  expiration.abs_value = sqlite3_column_int64 (nc->stmt, 4);
   key = sqlite3_column_blob (nc->stmt, 5);
   nc->lastPriority = priority;
   nc->lastExpiration = expiration;
@@ -623,8 +672,7 @@ sqlite_next_request (void *next_cls,
   if (GNUNET_YES == end_it)
     nc->end_it = GNUNET_YES;
   nc->plugin->next_task_nc = nc;
-  nc->plugin->next_task = GNUNET_SCHEDULER_add_now (nc->plugin->env->sched,
-                                                   &sqlite_next_request_cont,
+  nc->plugin->next_task = GNUNET_SCHEDULER_add_now (&sqlite_next_request_cont,
                                                    nc);
 }
 
@@ -667,8 +715,8 @@ sqlite_plugin_put (void *cls,
                   type, 
                   GNUNET_h2s(key),
                   priority,
-                  (unsigned long long) GNUNET_TIME_absolute_get_remaining (expiration).value,
-                  (long long) expiration.value);
+                  (unsigned long long) GNUNET_TIME_absolute_get_remaining (expiration).rel_value,
+                  (long long) expiration.abs_value);
 #endif
   GNUNET_CRYPTO_hash (data, size, &vhash);
   stmt = plugin->insertContent;
@@ -676,7 +724,7 @@ sqlite_plugin_put (void *cls,
       (SQLITE_OK != sqlite3_bind_int (stmt, 2, type)) ||
       (SQLITE_OK != sqlite3_bind_int (stmt, 3, priority)) ||
       (SQLITE_OK != sqlite3_bind_int (stmt, 4, anonymity)) ||
-      (SQLITE_OK != sqlite3_bind_int64 (stmt, 5, expiration.value)) ||
+      (SQLITE_OK != sqlite3_bind_int64 (stmt, 5, expiration.abs_value)) ||
       (SQLITE_OK !=
        sqlite3_bind_blob (stmt, 6, key, sizeof (GNUNET_HashCode),
                           SQLITE_TRANSIENT)) ||
@@ -763,7 +811,7 @@ sqlite_plugin_update (void *cls,
   int n;
 
   sqlite3_bind_int (plugin->updPrio, 1, delta);
-  sqlite3_bind_int64 (plugin->updPrio, 2, expire.value);
+  sqlite3_bind_int64 (plugin->updPrio, 2, expire.abs_value);
   sqlite3_bind_int64 (plugin->updPrio, 3, uid);
   n = sqlite3_step (plugin->updPrio);
   if (n != SQLITE_DONE)
@@ -871,10 +919,10 @@ iter_next_prepare (void *cls,
 #if DEBUG_SQLITE
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Restricting to results larger than the last expiration %llu\n",
-                 (unsigned long long) nc->lastExpiration.value);
+                 (unsigned long long) nc->lastExpiration.abs_value);
 #endif
-      sqlite3_bind_int64 (ic->stmt_1, 1, nc->lastExpiration.value);
-      sqlite3_bind_int64 (ic->stmt_2, 1, nc->lastExpiration.value);
+      sqlite3_bind_int64 (ic->stmt_1, 1, nc->lastExpiration.abs_value);
+      sqlite3_bind_int64 (ic->stmt_2, 1, nc->lastExpiration.abs_value);
     }
 #if DEBUG_SQLITE
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -974,7 +1022,7 @@ basic_iter (struct Plugin *plugin,
 #if DEBUG_SQLITE
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "At %llu, using queries `%s' and `%s'\n",
-             (unsigned long long) GNUNET_TIME_absolute_get ().value,
+             (unsigned long long) GNUNET_TIME_absolute_get ().abs_value,
              stmt_str_1,
              stmt_str_2);
 #endif
@@ -1014,13 +1062,13 @@ basic_iter (struct Plugin *plugin,
   if (is_asc)
     {
       nc->lastPriority = 0;
-      nc->lastExpiration.value = 0;
+      nc->lastExpiration.abs_value = 0;
       memset (&nc->lastKey, 0, sizeof (GNUNET_HashCode));
     }
   else
     {
       nc->lastPriority = 0x7FFFFFFF;
-      nc->lastExpiration.value = 0x7FFFFFFFFFFFFFFFLL;
+      nc->lastExpiration.abs_value = 0x7FFFFFFFFFFFFFFFLL;
       memset (&nc->lastKey, 255, sizeof (GNUNET_HashCode));
     }
   sqlite_next_request (nc, GNUNET_NO);
@@ -1077,9 +1125,9 @@ sqlite_plugin_iter_zero_anonymity (void *cls,
 
   now = GNUNET_TIME_absolute_get ();
   GNUNET_asprintf (&q1, SELECT_IT_NON_ANONYMOUS_1,
-                  (unsigned long long) now.value);
+                  (unsigned long long) now.abs_value);
   GNUNET_asprintf (&q2, SELECT_IT_NON_ANONYMOUS_2,
-                  (unsigned long long) now.value);
+                  (unsigned long long) now.abs_value);
   basic_iter (cls,
              type, 
              GNUNET_NO, GNUNET_YES, 
@@ -1116,9 +1164,9 @@ sqlite_plugin_iter_ascending_expiration (void *cls,
 
   now = GNUNET_TIME_absolute_get ();
   GNUNET_asprintf (&q1, SELECT_IT_EXPIRATION_TIME_1,
-                  (unsigned long long) 0*now.value);
+                  (unsigned long long) 0*now.abs_value);
   GNUNET_asprintf (&q2, SELECT_IT_EXPIRATION_TIME_2,
-                  (unsigned long long) 0*now.value);
+                  (unsigned long long) 0*now.abs_value);
   basic_iter (cls,
              type, 
              GNUNET_YES, GNUNET_NO, 
@@ -1152,7 +1200,7 @@ sqlite_plugin_iter_migration_order (void *cls,
 
   now = GNUNET_TIME_absolute_get ();
   GNUNET_asprintf (&q, SELECT_IT_MIGRATION_ORDER_2,
-                  (unsigned long long) now.value);
+                  (unsigned long long) now.abs_value);
   basic_iter (cls,
              type, 
              GNUNET_NO, GNUNET_NO, 
@@ -1168,7 +1216,7 @@ sqlite_plugin_iter_migration_order (void *cls,
  * Call sqlite using the already prepared query to get
  * the next result.
  *
- * @param cls not used
+ * @param cls context with the prepared query
  * @param nc context with the prepared query
  * @return GNUNET_OK on success, GNUNET_SYSERR on error, GNUNET_NO if
  *        there are no more results 
@@ -1186,6 +1234,10 @@ all_next_prepare (void *cls,
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Asked to clean up iterator state.\n");
 #endif
+      nc = (struct NextContext *)cls;
+      if (nc->stmt)
+          sqlite3_finalize (nc->stmt);
+      nc->stmt = NULL;
       return GNUNET_SYSERR;
     }
   plugin = nc->plugin;
@@ -1242,7 +1294,7 @@ sqlite_plugin_iter_all_now (void *cls,
   nc->iter_cls = iter_cls;
   nc->stmt = stmt;
   nc->prep = &all_next_prepare;
-  nc->prep_cls = NULL;
+  nc->prep_cls = nc;
   sqlite_next_request (nc, GNUNET_NO);
 }
 
@@ -1596,11 +1648,26 @@ libgnunet_plugin_datastore_sqlite_done (void *cls)
   struct GNUNET_DATASTORE_PluginFunctions *api = cls;
   struct Plugin *plugin = api->cls;
 
+#if DEBUG_SQLITE
+  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
+                  "sqlite",
+                  "sqlite plugin is doneing\n");
+#endif
+
   if (plugin->next_task != GNUNET_SCHEDULER_NO_TASK)
     {
-      GNUNET_SCHEDULER_cancel (plugin->env->sched,
-                              plugin->next_task);
+#if DEBUG_SQLITE
+      GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
+                      "sqlite",
+                      "Canceling next task\n");
+#endif
+      GNUNET_SCHEDULER_cancel (plugin->next_task);
       plugin->next_task = GNUNET_SCHEDULER_NO_TASK;
+#if DEBUG_SQLITE
+      GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
+                      "sqlite",
+                      "Prep'ing next task\n");
+#endif
       plugin->next_task_nc->prep (plugin->next_task_nc->prep_cls, NULL);
       GNUNET_free (plugin->next_task_nc);
       plugin->next_task_nc = NULL;
@@ -1608,6 +1675,11 @@ libgnunet_plugin_datastore_sqlite_done (void *cls)
   fn = NULL;
   if (plugin->drop_on_shutdown)
     fn = GNUNET_strdup (plugin->fn);
+#if DEBUG_SQLITE
+  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
+                  "sqlite",
+                  "Shutting down database\n");
+#endif
   database_shutdown (plugin);
   plugin->env = NULL; 
   GNUNET_free (api);
@@ -1619,6 +1691,11 @@ libgnunet_plugin_datastore_sqlite_done (void *cls)
                                  fn);
       GNUNET_free (fn);
     }
+#if DEBUG_SQLITE
+  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
+                  "sqlite",
+                  "sqlite plugin is finished doneing\n");
+#endif
   return NULL;
 }