From 8daf7b3165c65a932a7d8eae1f0a640199892ca9 Mon Sep 17 00:00:00 2001 From: dec05eba <0xdec05eba@gmail.com> Date: Mon, 14 May 2018 20:13:24 +0200 Subject: Only download nodes that we are missing --- include/odhtdb/DatabaseStorage.hpp | 14 +++++++++++--- include/odhtdb/sql/Sql.hpp | 37 +++++++++++++++++++++++++++++++++++++ include/odhtdb/sql/SqlExec.hpp | 33 +++++++++++++++++++++++++++++++++ include/odhtdb/sql/SqlQuery.hpp | 30 +----------------------------- 4 files changed, 82 insertions(+), 32 deletions(-) create mode 100644 include/odhtdb/sql/Sql.hpp create mode 100644 include/odhtdb/sql/SqlExec.hpp (limited to 'include') diff --git a/include/odhtdb/DatabaseStorage.hpp b/include/odhtdb/DatabaseStorage.hpp index 0d94c91..73b22b9 100644 --- a/include/odhtdb/DatabaseStorage.hpp +++ b/include/odhtdb/DatabaseStorage.hpp @@ -63,8 +63,10 @@ namespace odhtdb const int PASSWORD_SALT_LEN = 16; const int HASHED_PASSWORD_LEN = 32; - using FetchNodeRawCallbackFunc = std::function; - using FetchNodeAddDataRawCallbackFunc = std::function; + using FetchNodeRawCallbackFunc = std::function; + using FetchNodeAddDataRawCallbackFunc = std::function; + using FetchNodeUserActionGapsCallbackFunc = std::function; + using FetchNodeUserLatestActionCounterCallbackFunc = std::function; class DatabaseStorage { @@ -83,7 +85,7 @@ namespace odhtdb // Throws DatabaseStorageNotFound if data with @nodeHash hash has not been created yet. // Throws DatabaseStorageAlreadyExists if same data has been added before (hash of @data, in @dataHash) - void appendStorage(const Hash &nodeHash, const Hash &dataHash, DatabaseOperation operation, const Signature::PublicKey &creatorPublicKey, u64 timestamp, const void *data, usize size, const DataView &additionalDataView); + void appendStorage(const Hash &nodeHash, const Hash &dataHash, DatabaseOperation operation, u64 newUserActionCounter, const Signature::PublicKey &creatorPublicKey, u64 timestamp, const void *data, usize size, const DataView &additionalDataView); // Throws DatabaseStorageAlreadyExists if group already exists in node void addGroup(const Hash &nodeHash, const DataView &groupId, const Permission &permissions); @@ -96,9 +98,15 @@ namespace odhtdb void fetchNodeRaw(const Hash &nodeHash, FetchNodeRawCallbackFunc callbackFunc); void fetchNodeAddDataRaw(const Hash &nodeHash, FetchNodeAddDataRawCallbackFunc callbackFunc); + void fetchNodeUserActionGaps(const Hash &nodeHash, FetchNodeUserActionGapsCallbackFunc callbackFunc); + void fetchNodeUserLatestActionCounter(const Hash &nodeHash, FetchNodeUserLatestActionCounterCallbackFunc callbackFunc); + bool isUserAllowedToAddDataInNode(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const; bool isUserAllowedToAddUserToGroupInNode(const Hash &nodeHash, const Signature::PublicKey &userPublicKey, const DataView &groupToAddUserTo) const; + // Throws DatabaseStorageNotFound if user doesn't exist in node + u64 getUserActionCounter(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const; + // Username and key pair has to be unique, returns true on success //bool storeLocalUser(const std::string &username, const Signature::KeyPair &keyPair, const std::string &password); diff --git a/include/odhtdb/sql/Sql.hpp b/include/odhtdb/sql/Sql.hpp new file mode 100644 index 0000000..6c78360 --- /dev/null +++ b/include/odhtdb/sql/Sql.hpp @@ -0,0 +1,37 @@ +#pragma once + +#include "../DataView.hpp" + +class sqlite3; +class sqlite3_stmt; + +namespace odhtdb +{ + class SqlArg + { + public: + enum class Type : u8 + { + DATA_VIEW, + INT, + INT64, + UINT64 + }; + + SqlArg(const DataView &data) : dataView(data), type(Type::DATA_VIEW) {} + SqlArg(int data) : integer(data), type(Type::INT) {} + SqlArg(i64 data) : integer64(data), type(Type::INT64) {} + SqlArg(u64 data) : uinteger64(data), type(Type::UINT64) {} + + int bind(sqlite3_stmt *stmt, int paramIndex) const; + private: + union + { + const DataView dataView; + const int integer; + const i64 integer64; + const u64 uinteger64; + }; + const Type type; + }; +} diff --git a/include/odhtdb/sql/SqlExec.hpp b/include/odhtdb/sql/SqlExec.hpp new file mode 100644 index 0000000..07146ea --- /dev/null +++ b/include/odhtdb/sql/SqlExec.hpp @@ -0,0 +1,33 @@ +#pragma once + +#include "Sql.hpp" +#include +#include +#include + +namespace odhtdb +{ + class SqlExecException : public std::runtime_error + { + public: + SqlExecException(const std::string &errMsg) : std::runtime_error(errMsg) {} + }; + + class SqlExec + { + public: + // Throws SqlExecException on failure + SqlExec(sqlite3 *db, const char *sql); + ~SqlExec(); + + // Throws SqlExecException on failure + void execWithArgs(std::initializer_list args); + + // Throws SqlExecException on failure + void exec(); + private: + sqlite3 *db; + sqlite3_stmt *stmt; + std::mutex mutex; + }; +} diff --git a/include/odhtdb/sql/SqlQuery.hpp b/include/odhtdb/sql/SqlQuery.hpp index f26ee1a..615fa3f 100644 --- a/include/odhtdb/sql/SqlQuery.hpp +++ b/include/odhtdb/sql/SqlQuery.hpp @@ -1,12 +1,9 @@ #pragma once -#include "../DataView.hpp" +#include "Sql.hpp" #include #include -class sqlite3; -class sqlite3_stmt; - namespace odhtdb { class SqlQueryException : public std::runtime_error @@ -15,31 +12,6 @@ namespace odhtdb SqlQueryException(const std::string &errMsg) : std::runtime_error(errMsg) {} }; - class SqlArg - { - public: - enum class Type : u8 - { - DATA_VIEW, - INT, - INT64 - }; - - SqlArg(const DataView &data) : dataView(data), type(Type::DATA_VIEW) {} - SqlArg(int data) : integer(data), type(Type::INT) {} - SqlArg(i64 data) : integer64(data), type(Type::INT64) {} - - int bind(sqlite3_stmt *stmt, int paramIndex) const; - private: - union - { - const DataView dataView; - const int integer; - const i64 integer64; - }; - const Type type; - }; - class SqlQuery { public: -- cgit v1.2.3