-implementting get_keys for mysql (2013)
authorChristian Grothoff <christian@grothoff.org>
Fri, 16 Dec 2011 23:31:38 +0000 (23:31 +0000)
committerChristian Grothoff <christian@grothoff.org>
Fri, 16 Dec 2011 23:31:38 +0000 (23:31 +0000)
src/datastore/plugin_datastore_mysql.c

index a6e65eeec4a7b96084f4ce1451677f6bb4053697..76d6ad7068bd3344ac5f1cee06cb823bdd6307bf 100644 (file)
@@ -1304,6 +1304,86 @@ mysql_plugin_get_replication (void *cls, PluginDatumProcessor proc,
 }
 
 
+/**
+ * Get all of the keys in the datastore.
+ *
+ * @param cls closure
+ * @param proc function to call on each key
+ * @param proc_cls closure for proc
+ */
+static void
+mysql_plugin_get_keys (void *cls,
+                       PluginKeyProcessor proc,
+                       void *proc_cls)
+{
+  struct Plugin *plugin = cls;
+  const char *query = "SELECT hash FROM gn090";
+  int ret;
+  MYSQL_STMT *statement;
+  GNUNET_HashCode key;
+  MYSQL_BIND cbind[1];
+  unsigned long length;
+  statement = mysql_stmt_init (plugin->dbf);
+  if (statement == NULL)
+  {
+    iclose (plugin);
+    return;
+  }
+  if (mysql_stmt_prepare (statement, query, strlen (query)))
+  {
+    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
+                     _("Failed to prepare statement `%s'\n"), query);
+    LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR, "mysql_stmt_prepare", plugin);
+    mysql_stmt_close (statement);
+    iclose (plugin);
+    return;
+  }
+  GNUNET_assert (proc != NULL);
+  if (mysql_stmt_execute (statement))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                _("`%s' for `%s' failed at %s:%d with error: %s\n"),
+                "mysql_stmt_execute", query, __FILE__, __LINE__,
+                mysql_stmt_error (statement));
+    mysql_stmt_close (statement);
+    iclose (plugin);
+    return;
+  }
+  memset (cbind, 0, sizeof (cbind));
+  cbind[0].buffer_type = MYSQL_TYPE_BLOB;
+  cbind[0].buffer = &key;
+  cbind[0].buffer_length = sizeof (key);
+  cbind[0].length = &length;
+  cbind[0].is_unsigned = GNUNET_NO;
+  if (mysql_stmt_bind_result (statement, cbind))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                _("`%s' failed at %s:%d with error: %s\n"),
+                "mysql_stmt_bind_result", __FILE__, __LINE__,
+                mysql_stmt_error (statement));
+    iclose (plugin);
+    return;
+  }
+  while (0 == (ret = mysql_stmt_fetch (statement)))
+  {
+    if (sizeof (GNUNET_HashCode) == length)
+      proc (proc_cls, &key, 1);    
+  }
+  if (ret != MYSQL_NO_DATA)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                _("`%s' failed at %s:%d with error: %s\n"),
+                    "mysql_stmt_fetch", __FILE__, __LINE__,
+                    mysql_stmt_error (statement));    
+    mysql_stmt_close (statement);
+    iclose (plugin);
+    return;
+  }
+  mysql_stmt_close (statement);
+}
+
+
 /**
  * Context for 'expi_proc' function.
  */
@@ -1495,6 +1575,7 @@ libgnunet_plugin_datastore_mysql_init (void *cls)
   api->get_replication = &mysql_plugin_get_replication;
   api->get_expiration = &mysql_plugin_get_expiration;
   api->get_zero_anonymity = &mysql_plugin_get_zero_anonymity;
+  api->get_keys = &mysql_plugin_get_keys;
   api->drop = &mysql_plugin_drop;
   GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "mysql",
                    _("Mysql database running\n"));