Merge branch 'master' of git+ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / psycstore / plugin_psycstore_sqlite.c
index 8f9a3ef638359892f2a48a18a3143fb0002dab63..69291b4406338b5588c19dcbb21507084dedb31a 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of GNUnet
- * Copyright (C) 2013 Christian Grothoff (and other contributing authors)
+ * Copyright (C) 2013 GNUnet e.V.
  *
  * GNUnet is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published
@@ -35,7 +35,7 @@
 #include "gnunet_psycstore_service.h"
 #include "gnunet_multicast_service.h"
 #include "gnunet_crypto_lib.h"
-#include "gnunet_env_lib.h"
+#include "gnunet_psyc_util_lib.h"
 #include "psycstore.h"
 #include <sqlite3.h>
 
@@ -348,7 +348,7 @@ database_setup (struct Plugin *plugin)
   sql_exec (plugin->dbh,
             "CREATE TABLE IF NOT EXISTS channels (\n"
             "  id INTEGER PRIMARY KEY,\n"
-            "  pub_key BLOB UNIQUE,\n"
+            "  pub_key BLOB(32) UNIQUE,\n"
             "  max_state_message_id INTEGER,\n" // last applied state message ID
             "  state_hash_message_id INTEGER\n" // last message ID with a state hash
             ");");
@@ -356,7 +356,7 @@ database_setup (struct Plugin *plugin)
   sql_exec (plugin->dbh,
             "CREATE TABLE IF NOT EXISTS slaves (\n"
             "  id INTEGER PRIMARY KEY,\n"
-            "  pub_key BLOB UNIQUE\n"
+            "  pub_key BLOB(32) UNIQUE\n"
             ");");
 
   sql_exec (plugin->dbh,
@@ -472,7 +472,8 @@ database_setup (struct Plugin *plugin)
                "       multicast_flags, psycstore_flags, data\n"
                "FROM messages\n"
                "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
-               "      AND ? <= message_id AND message_id <= ?;",
+               "      AND ? <= message_id AND message_id <= ?"
+               "LIMIT ?;",
                &plugin->select_messages);
 
   sql_prepare (plugin->dbh,
@@ -598,7 +599,7 @@ database_setup (struct Plugin *plugin)
                "SELECT name, value_current\n"
                "FROM state\n"
                "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
-               "      AND (name = ? OR substr(name, 1, ?) = ? || '_');",
+               "      AND (name = ? OR substr(name, 1, ?) = ?);",
                &plugin->select_state_prefix);
 
   sql_prepare (plugin->dbh,
@@ -812,13 +813,13 @@ slave_key_store (struct Plugin *plugin,
  * @return #GNUNET_OK on success, else #GNUNET_SYSERR
  */
 static int
-membership_store (void *cls,
-                  const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
-                  const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
-                  int did_join,
-                  uint64_t announced_at,
-                  uint64_t effective_since,
-                  uint64_t group_generation)
+sqlite_membership_store (void *cls,
+                         const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
+                         const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
+                         int did_join,
+                         uint64_t announced_at,
+                         uint64_t effective_since,
+                         uint64_t group_generation)
 {
   struct Plugin *plugin = cls;
   sqlite3_stmt *stmt = plugin->insert_membership;
@@ -997,7 +998,7 @@ static int
 message_add_flags (void *cls,
                    const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
                    uint64_t message_id,
-                   uint64_t psycstore_flags)
+                   uint32_t psycstore_flags)
 {
   struct Plugin *plugin = cls;
   sqlite3_stmt *stmt = plugin->update_message_flags;
@@ -1045,10 +1046,10 @@ fragment_row (sqlite3_stmt *stmt, GNUNET_PSYCSTORE_FragmentCallback cb,
   msg->header.size = htons (sizeof (*msg) + data_size);
   msg->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
   msg->hop_counter = htonl ((uint32_t) sqlite3_column_int64 (stmt, 0));
-  memcpy (&msg->signature,
+  GNUNET_memcpy (&msg->signature,
           sqlite3_column_blob (stmt, 1),
           sqlite3_column_bytes (stmt, 1));
-  memcpy (&msg->purpose,
+  GNUNET_memcpy (&msg->purpose,
           sqlite3_column_blob (stmt, 2),
           sqlite3_column_bytes (stmt, 2));
   msg->fragment_id = GNUNET_htonll (sqlite3_column_int64 (stmt, 3));
@@ -1056,7 +1057,7 @@ fragment_row (sqlite3_stmt *stmt, GNUNET_PSYCSTORE_FragmentCallback cb,
   msg->message_id = GNUNET_htonll (sqlite3_column_int64 (stmt, 5));
   msg->group_generation = GNUNET_htonll (sqlite3_column_int64 (stmt, 6));
   msg->flags = htonl (sqlite3_column_int64 (stmt, 7));
-  memcpy (&msg[1], sqlite3_column_blob (stmt, 9), data_size);
+  GNUNET_memcpy (&msg[1], sqlite3_column_blob (stmt, 9), data_size);
 
   return cb (cb_cls, (void *) msg, sqlite3_column_int64 (stmt, 8));
 }
@@ -1195,6 +1196,7 @@ message_get (void *cls,
              const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
              uint64_t first_message_id,
              uint64_t last_message_id,
+             uint64_t fragment_limit,
              uint64_t *returned_fragments,
              GNUNET_PSYCSTORE_FragmentCallback cb,
              void *cb_cls)
@@ -1208,7 +1210,11 @@ message_get (void *cls,
                                       sizeof (*channel_key),
                                       SQLITE_STATIC)
       || SQLITE_OK != sqlite3_bind_int64 (stmt, 2, first_message_id)
-      || SQLITE_OK != sqlite3_bind_int64 (stmt, 3, last_message_id))
+      || SQLITE_OK != sqlite3_bind_int64 (stmt, 3, last_message_id)
+      || SQLITE_OK != sqlite3_bind_int64 (stmt, 4,
+                                          (0 != fragment_limit)
+                                          ? fragment_limit
+                                          : INT64_MAX))
   {
     LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
                 "sqlite3_bind");
@@ -1558,7 +1564,7 @@ state_modify_begin (void *cls,
 static int
 state_modify_op (void *cls,
                  const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
-                 enum GNUNET_ENV_Operator op,
+                 enum GNUNET_PSYC_Operator op,
                  const char *name, const void *value, size_t value_size)
 {
   struct Plugin *plugin = cls;
@@ -1566,7 +1572,7 @@ state_modify_op (void *cls,
 
   switch (op)
   {
-  case GNUNET_ENV_OP_ASSIGN:
+  case GNUNET_PSYC_OP_ASSIGN:
     return state_assign (plugin, plugin->insert_state_current, channel_key,
                          name, value, value_size);
 
@@ -1767,7 +1773,7 @@ state_get_prefix (void *cls, const struct GNUNET_CRYPTO_EddsaPublicKey *channel_
   if (SQLITE_OK != sqlite3_bind_blob (stmt, 1, channel_key,
                                       sizeof (*channel_key), SQLITE_STATIC)
       || SQLITE_OK != sqlite3_bind_text (stmt, 2, name, name_len, SQLITE_STATIC)
-      || SQLITE_OK != sqlite3_bind_int (stmt, 3, name_len + 1)
+      || SQLITE_OK != sqlite3_bind_int (stmt, 3, name_len)
       || SQLITE_OK != sqlite3_bind_text (stmt, 4, name, name_len, SQLITE_STATIC))
   {
     LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
@@ -1892,7 +1898,7 @@ libgnunet_plugin_psycstore_sqlite_init (void *cls)
   }
   api = GNUNET_new (struct GNUNET_PSYCSTORE_PluginFunctions);
   api->cls = &plugin;
-  api->membership_store = &membership_store;
+  api->membership_store = &sqlite_membership_store;
   api->membership_test = &membership_test;
   api->fragment_store = &fragment_store;
   api->message_add_flags = &message_add_flags;