/*
* 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
#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>
enum Transactions {
TRANSACTION_NONE = 0,
- TRANSACTION_STATE_MODIFY
+ TRANSACTION_STATE_MODIFY,
+ TRANSACTION_STATE_SYNC,
};
/**
" 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,
"SELECT name, value_current\n"
"FROM state\n"
"WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n"
- " AND (name = ? OR name LIKE ?);",
+ " AND (name = ? OR substr(name, 1, ?) = ? || '_');",
&plugin->select_state_prefix);
sql_prepare (plugin->dbh,
* @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;
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)
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
+ : -1))
{
LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
"sqlite3_bind");
uint64_t max_state_message_id = 0;
int ret = counters_state_get (plugin, channel_key, &max_state_message_id);
- if (GNUNET_OK != ret)
+ switch (ret)
+ {
+ case GNUNET_OK:
+ case GNUNET_NO: // no state yet
+ ret = GNUNET_OK;
+ break;
+ default:
return ret;
+ }
- if (message_id - state_delta != max_state_message_id)
- return GNUNET_NO;
+ if (max_state_message_id < message_id - state_delta)
+ return GNUNET_NO; /* some stateful messages not yet applied */
+ else if (message_id - state_delta < max_state_message_id)
+ return GNUNET_NO; /* changes already applied */
}
- // Make sure no other transaction is going on.
if (TRANSACTION_NONE != plugin->transaction)
- if (GNUNET_OK != transaction_rollback (plugin))
- return GNUNET_SYSERR;
-
+ {
+ /** @todo FIXME: wait for other transaction to finish */
+ return GNUNET_SYSERR;
+ }
return transaction_begin (plugin, TRANSACTION_STATE_MODIFY);
}
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;
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);
- /// @todo implement more state operations
- default:
+ default: /** @todo implement more state operations */
+ GNUNET_break (0);
return GNUNET_SYSERR;
}
}
struct Plugin *plugin = cls;
int ret = GNUNET_SYSERR;
- GNUNET_OK == transaction_begin (plugin, TRANSACTION_NONE)
+ if (TRANSACTION_NONE != plugin->transaction)
+ {
+ /** @todo FIXME: wait for other transaction to finish */
+ return GNUNET_SYSERR;
+ }
+
+ GNUNET_OK == transaction_begin (plugin, TRANSACTION_STATE_SYNC)
&& GNUNET_OK == exec_channel (plugin, plugin->delete_state, channel_key)
&& GNUNET_OK == exec_channel (plugin, plugin->insert_state_from_sync,
channel_key)
int ret = GNUNET_SYSERR;
sqlite3_stmt *stmt = plugin->select_state_prefix;
size_t name_len = strlen (name);
- char *name_prefix;
- GNUNET_asprintf (&name_prefix,
- "%s_%%",
- name);
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_text (stmt, 3, name_prefix, name_len + 2,
- SQLITE_STATIC))
+ || SQLITE_OK != sqlite3_bind_int (stmt, 3, name_len + 1)
+ || SQLITE_OK != sqlite3_bind_text (stmt, 4, name, name_len, SQLITE_STATIC))
{
LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
"sqlite3_bind");
LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
"sqlite3_reset");
}
- GNUNET_free (name_prefix);
return ret;
}
}
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;