From eda9a7bbefc5587bf1ff895a9214f450e64575fa Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 5 Mar 2018 22:45:56 +0100 Subject: Implement 'create' operation, add seeding Seeding is currently only done on the key you specify, in the future the user should request data that it can seed. --- include/Database.hpp | 121 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 109 insertions(+), 12 deletions(-) (limited to 'include/Database.hpp') diff --git a/include/Database.hpp b/include/Database.hpp index e8b35bb..d160133 100644 --- a/include/Database.hpp +++ b/include/Database.hpp @@ -2,17 +2,110 @@ #include "types.hpp" #include "Key.hpp" -#include "StagedObject.hpp" #include "DataView.hpp" #include "DatabaseStorage.hpp" +#include "Hash.hpp" +#include "utils.hpp" +#include "StagedObject.hpp" +#include "Signature.hpp" #include #include #include #include +#include namespace odhtdb { class LocalUser; + class Group; + + class CommitCreateException : public std::runtime_error + { + public: + CommitCreateException(const std::string &errMsg) : std::runtime_error(errMsg) {} + }; + + class CommitAddException : public std::runtime_error + { + public: + CommitAddException(const std::string &errMsg) : std::runtime_error(errMsg) {} + }; + + class DatabaseCreateException : public std::runtime_error + { + public: + DatabaseCreateException(const std::string &errMsg) : std::runtime_error(errMsg) {} + }; + + class DatabaseAddException : public std::runtime_error + { + public: + DatabaseAddException(const std::string &errMsg) : std::runtime_error(errMsg) {} + }; + + struct DatabaseCreateRequest + { + DISABLE_COPY(DatabaseCreateRequest) + + u64 timestamp; // In microseconds + Group *creatorGroup; + std::string name; + + DatabaseCreateRequest(u64 _timestamp, Group *_creatorGroup, std::string &&_name) : + timestamp(_timestamp), + creatorGroup(_creatorGroup), + name(std::move(_name)) + { + + } + + DatabaseCreateRequest(DatabaseCreateRequest &&other) + { + timestamp = other.timestamp; + creatorGroup = other.creatorGroup; + name = std::move(other.name); + + other.timestamp = 0; + other.creatorGroup = nullptr; + } + }; + + struct DatabaseAddRequest + { + DISABLE_COPY(DatabaseAddRequest) + + u16 packetStructureVersion; + u64 timestamp; // In microseconds + Signature::PublicKey creatorPublicKey; + DataView data; + + DatabaseAddRequest(u16 _packetStructureVersion, u64 _timestamp, Signature::PublicKey &&_creatorPublicKey, DataView &_data) : + packetStructureVersion(_packetStructureVersion), + timestamp(_timestamp), + creatorPublicKey(std::move(_creatorPublicKey)), + data(_data) + { + + } + + ~DatabaseAddRequest() + { + free(data.data); + data = DataView(); + } + }; + + class DatabaseCreateResponse + { + public: + DatabaseCreateResponse(const std::shared_ptr &key, const std::shared_ptr &hash); + + const std::shared_ptr getNodeEncryptionKey() const; + const std::shared_ptr getRequestHash() const; + private: + std::shared_ptr key; + std::shared_ptr hash; + }; class Database { @@ -20,22 +113,26 @@ namespace odhtdb Database(const char *bootstrapNodeAddr, u16 port, boost::filesystem::path storageDir); ~Database(); - void seed(); - void create(LocalUser *owner, const Key &key); - void add(LocalUser *owner, const Key &key, DataView data); + void seed(const std::shared_ptr hash, const std::shared_ptr encryptionKey); + // Throws DatabaseCreateException on failure. + std::unique_ptr create(const LocalUser *owner, const std::string &name); + // Throws DatabaseAddException on failure + void add(const LocalUser *owner, const Key &key, DataView data); void commit(); private: - void commitStagedCreateObject(const StagedCreateObject &stagedObject); - void commitStagedAddObject(const StagedAddObject &stagedObject); + // Throws CommitCreateException on failure + void commitStagedCreateObject(const std::unique_ptr &stagedObject); + // Throws CommitAddException on failure + void commitStagedAddObject(const DataView &stagedObject); ntp::NtpTimestamp getSyncedTimestampUtc() const; - StagedCreateObject deserializeCreateRequest(const std::shared_ptr &value); - StagedAddObject deserializeAddRequest(const std::shared_ptr &value); - bool listenCreateData(std::shared_ptr value); - bool listenAddData(std::shared_ptr value); + DatabaseCreateRequest deserializeCreateRequest(const std::shared_ptr &value, const Hash &hash, const std::shared_ptr encryptionKey); + DatabaseAddRequest deserializeAddRequest(const std::shared_ptr &value, const Hash &hash, const std::shared_ptr encryptionKey); + bool listenCreateData(std::shared_ptr value, const Hash &hash, const std::shared_ptr encryptionKey); + bool listenAddData(std::shared_ptr value, const Hash &hash, const std::shared_ptr encryptionKey); private: dht::DhtRunner node; - std::vector stagedCreateObjects; - std::vector stagedAddObjects; + std::vector> stagedCreateObjects; + std::vector> stagedAddObjects; DatabaseStorage databaseStorage; }; } -- cgit v1.2.3