aboutsummaryrefslogtreecommitdiff
path: root/include/Database.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'include/Database.hpp')
-rw-r--r--include/Database.hpp121
1 files changed, 109 insertions, 12 deletions
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 <opendht/dhtrunner.h>
#include <vector>
#include <ntp/NtpClient.hpp>
#include <boost/filesystem/path.hpp>
+#include <stdexcept>
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<char*> &key, const std::shared_ptr<Hash> &hash);
+
+ const std::shared_ptr<char*> getNodeEncryptionKey() const;
+ const std::shared_ptr<Hash> getRequestHash() const;
+ private:
+ std::shared_ptr<char*> key;
+ std::shared_ptr<Hash> 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> hash, const std::shared_ptr<char*> encryptionKey);
+ // Throws DatabaseCreateException on failure.
+ std::unique_ptr<DatabaseCreateResponse> 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<StagedCreateObject> &stagedObject);
+ // Throws CommitAddException on failure
+ void commitStagedAddObject(const DataView &stagedObject);
ntp::NtpTimestamp getSyncedTimestampUtc() const;
- StagedCreateObject deserializeCreateRequest(const std::shared_ptr<dht::Value> &value);
- StagedAddObject deserializeAddRequest(const std::shared_ptr<dht::Value> &value);
- bool listenCreateData(std::shared_ptr<dht::Value> value);
- bool listenAddData(std::shared_ptr<dht::Value> value);
+ DatabaseCreateRequest deserializeCreateRequest(const std::shared_ptr<dht::Value> &value, const Hash &hash, const std::shared_ptr<char*> encryptionKey);
+ DatabaseAddRequest deserializeAddRequest(const std::shared_ptr<dht::Value> &value, const Hash &hash, const std::shared_ptr<char*> encryptionKey);
+ bool listenCreateData(std::shared_ptr<dht::Value> value, const Hash &hash, const std::shared_ptr<char*> encryptionKey);
+ bool listenAddData(std::shared_ptr<dht::Value> value, const Hash &hash, const std::shared_ptr<char*> encryptionKey);
private:
dht::DhtRunner node;
- std::vector<StagedCreateObject> stagedCreateObjects;
- std::vector<StagedAddObject> stagedAddObjects;
+ std::vector<std::unique_ptr<StagedCreateObject>> stagedCreateObjects;
+ std::vector<std::unique_ptr<DataView>> stagedAddObjects;
DatabaseStorage databaseStorage;
};
}