diff options
-rw-r--r-- | Scheme.md | 4 | ||||
-rw-r--r-- | include/odhtdb/Database.hpp | 9 | ||||
-rw-r--r-- | include/odhtdb/StagedObject.hpp | 22 | ||||
-rw-r--r-- | src/Database.cpp | 96 | ||||
-rw-r--r-- | tests/main.cpp | 1 |
5 files changed, 30 insertions, 102 deletions
@@ -14,7 +14,7 @@ Packet Content (Signed with creator private key, verify with creator public key) Header packet structure version - timestamp + timestamp (not strictly accurate, mostly used for visual representation) operation type Body (Encrypted with node encryption key) data @@ -24,7 +24,7 @@ Packet Content (Signed with creator private key, verify with creator public key) Header packet structure version - timestamp + timestamp (not strictly accurate, mostly used for visual representation) operation type Body name (of the user to add to group, encrypted) diff --git a/include/odhtdb/Database.hpp b/include/odhtdb/Database.hpp index cf24c7e..12c618b 100644 --- a/include/odhtdb/Database.hpp +++ b/include/odhtdb/Database.hpp @@ -6,7 +6,6 @@ #include "DatabaseStorage.hpp" #include "Hash.hpp" #include "utils.hpp" -#include "StagedObject.hpp" #include "Signature.hpp" #include "Permission.hpp" #include "DatabaseNode.hpp" @@ -145,7 +144,6 @@ namespace odhtdb reponseKeyInfoHash = other.reponseKeyInfoHash; } }; - class Database { DISABLE_COPY(Database) @@ -166,7 +164,6 @@ namespace odhtdb void addData(const DatabaseNode &nodeInfo, const LocalUser *userToPerformActionWith, DataView dataToAdd); // Throws PermissionDeniedException if user @userToPerformActionWith is not allowed to add user @userToAdd to group @groupToAddUserTo void addUser(const DatabaseNode &nodeInfo, const LocalUser *userToPerformActionWith, const std::string &userToAddName, const Signature::PublicKey &userToAddPublicKey, Group *groupToAddUserTo); - void commit(); void setOnCreateNodeCallback(std::function<void(const DatabaseCreateNodeRequest&)> callbackFunc); void setOnAddNodeCallback(std::function<void(const DatabaseAddNodeRequest&)> callbackFunc); @@ -175,18 +172,12 @@ namespace odhtdb DatabaseStorage& getStorage(); ntp::NtpTimestamp getSyncedTimestampUtc() const; private: - // Throws CommitCreateException on failure - void commitStagedCreateObject(const std::unique_ptr<StagedObject> &stagedObject); - // Throws CommitAddException on failure - void commitStagedAddObject(const std::unique_ptr<StagedObject> &stagedObject); void deserializeCreateRequest(const std::shared_ptr<dht::Value> &value, const Hash &hash, const std::shared_ptr<OwnedMemory> encryptionKey); void deserializeAddRequest(const std::shared_ptr<dht::Value> &value, const Hash &requestDataHash, const std::shared_ptr<Hash> &nodeHash, const std::shared_ptr<OwnedMemory> encryptionKey); bool listenCreateData(std::shared_ptr<dht::Value> value, const Hash &hash, const std::shared_ptr<OwnedMemory> encryptionKey); bool listenAddData(std::shared_ptr<dht::Value> value, const Hash &requestDataHash, const std::shared_ptr<Hash> nodeHash, const std::shared_ptr<OwnedMemory> encryptionKey); private: dht::DhtRunner node; - std::vector<std::unique_ptr<StagedObject>> stagedCreateObjects; - std::vector<std::unique_ptr<StagedObject>> stagedAddObjects; DatabaseStorage databaseStorage; std::function<void(const DatabaseCreateNodeRequest&)> onCreateNodeCallbackFunc; std::function<void(const DatabaseAddNodeRequest&)> onAddNodeCallbackFunc; diff --git a/include/odhtdb/StagedObject.hpp b/include/odhtdb/StagedObject.hpp deleted file mode 100644 index 0c9b534..0000000 --- a/include/odhtdb/StagedObject.hpp +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#include "utils.hpp" -#include "Hash.hpp" -#include "DataView.hpp" - -namespace odhtdb -{ - struct StagedObject - { - DISABLE_COPY(StagedObject) - DataView data; - std::shared_ptr<Hash> requestKey; - - StagedObject(DataView &_data, const std::shared_ptr<Hash> &_requestKey) : - data(_data), - requestKey(_requestKey) - { - - } - }; -} diff --git a/src/Database.cpp b/src/Database.cpp index ee18b87..41d3798 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -321,12 +321,21 @@ namespace odhtdb shared_ptr<Hash> hashRequestKey = make_shared<Hash>(requestData.data, requestData.size); databaseStorage.setNodeDecryptionKey(*hashRequestKey, DataView(encryptedBody.getKey().data, encryptedBody.getKey().size)); databaseStorage.createStorage(*hashRequestKey, adminGroup, timestampCombined, (const u8*)requestData.data, requestData.size, serializer.getBuffer().size()); - - stagedCreateObjects.emplace_back(make_unique<StagedObject>(requestData, hashRequestKey)); assert(encryptedBody.getKey().size == ENCRYPTION_KEY_BYTE_SIZE); auto key = make_shared<OwnedMemory>(new char[encryptedBody.getKey().size], encryptedBody.getKey().size); memcpy(key->data, encryptedBody.getKey().data, encryptedBody.getKey().size); + + DhtKey dhtKey(*hashRequestKey); + Value createDataValue((u8*)requestData.data, requestData.size); + delete[] (char*)requestData.data; + node.put(dhtKey.getNewDataListenerKey(), move(createDataValue), [](bool ok) + { + // TODO: Handle failure to put data + if(!ok) + Log::warn("Failed to put: %s, what to do?", "Database::create"); + }); + return make_unique<DatabaseCreateResponse>(nodeAdminUser, move(key), hashRequestKey); } catch (EncryptionException &e) @@ -349,9 +358,7 @@ namespace odhtdb errMsg += " is not allowed to perform the operation: ADD_USER"; throw PermissionDeniedException(errMsg); } - - //u64 actionCounter = databaseStorage.increaseUserActionCounter(userToPerformActionWith->getPublicKey()); - + sibs::SafeSerializer serializer; serializer.add(DATABASE_ADD_PACKET_STRUCTURE_VERSION); u64 timestampCombined = getSyncedTimestampUtc().getCombined(); @@ -367,8 +374,16 @@ namespace odhtdb DataView encryptedDataView((char*)requestData.data + serializer.getBuffer().size(), requestData.size - serializer.getBuffer().size()); databaseStorage.appendStorage(*nodeInfo.getRequestHash(), requestDataHash, DatabaseOperation::ADD_DATA, userToPerformActionWith, timestampCombined, (u8*)stagedAddObject.data, stagedAddObject.size, encryptedDataView); delete[] (char*)requestData.data; - - stagedAddObjects.emplace_back(make_unique<StagedObject>(stagedAddObject, nodeInfo.getRequestHash())); + + DhtKey dhtKey(requestDataHash); + Value addDataValue((u8*)stagedAddObject.data, stagedAddObject.size); + delete[] (char*)stagedAddObject.data; + node.put(dhtKey.getNewDataListenerKey(), move(addDataValue), [](bool ok) + { + // TODO: Handle failure to put data + if(!ok) + Log::warn("Failed to put: %s, what to do?", "Database::addData"); + }); } Group* getGroupWithRightsToAddUserToGroup(const vector<Group*> &groups, Group *groupToAddUserTo) @@ -426,71 +441,16 @@ namespace odhtdb auto userToAdd = RemoteUser::create(userToAddPublicKey, userToAddName, groupToAddUserTo); databaseStorage.addUser(*nodeInfo.getRequestHash(), userToAdd); databaseStorage.appendStorage(*nodeInfo.getRequestHash(), requestDataHash, DatabaseOperation::ADD_USER, userToPerformActionWith, timestampCombined, (u8*)stagedAddObject.data, stagedAddObject.size, encryptedDataView); - - stagedAddObjects.emplace_back(make_unique<StagedObject>(stagedAddObject, nodeInfo.getRequestHash())); - } - - void Database::commit() - { - // TODO: Combine staged objects into one object for efficiency. - - try - { - Log::debug("Num objects to create: %zu", stagedCreateObjects.size()); - for(const auto &stagedObject : stagedCreateObjects) - { - commitStagedCreateObject(stagedObject); - } - - Log::debug("Num objects to add: %zu", stagedAddObjects.size()); - for(const auto &stagedObject : stagedAddObjects) - { - commitStagedAddObject(stagedObject); - } - } - catch (exception &e) - { - // TODO: Add rollback - Log::error("Failed to commit, reason: %s", e.what()); - } - - for(const auto &stagedObject : stagedCreateObjects) - { - delete[] (char*)stagedObject->data.data; - } - stagedCreateObjects.clear(); - for(const auto &stagedObject : stagedAddObjects) - { - delete[] (char*)stagedObject->data.data; - } - stagedAddObjects.clear(); - - // TODO: Add node.listen here to get notified when remote peers got the commit, then we can say we can return - } - - void Database::commitStagedCreateObject(const unique_ptr<StagedObject> &stagedObject) - { - DhtKey dhtKey(*stagedObject->requestKey); - Value createDataValue((u8*)stagedObject->data.data, stagedObject->data.size); - node.put(dhtKey.getNewDataListenerKey(), move(createDataValue), [](bool ok) + DhtKey dhtKey(requestDataHash); + Value addDataValue((u8*)stagedAddObject.data, stagedAddObject.size); + delete[] (char*)stagedAddObject.data; + node.put(dhtKey.getNewDataListenerKey(), move(addDataValue), [](bool ok) { // TODO: Handle failure to put data if(!ok) - Log::warn("Failed to put: %s, what to do?", "commitStagedCreateObject"); - }/* TODO: How to make this work?, time_point(), false*/); - } - - void Database::commitStagedAddObject(const unique_ptr<StagedObject> &stagedObject) - { - DhtKey dhtKey(*stagedObject->requestKey); - Value createDataValue((u8*)stagedObject->data.data, stagedObject->data.size); - node.put(dhtKey.getNewDataListenerKey(), move(createDataValue), [](bool ok) - { - // TODO: Handle failure to put data - if(!ok) - Log::warn("Failed to put: %s, what to do?", "commitStagedAddObject"); - }/* TODO: How to make this work?, time_point(), false*/); + Log::warn("Failed to put: %s, what to do?", "Database::addUser"); + }); } ntp::NtpTimestamp Database::getSyncedTimestampUtc() const diff --git a/tests/main.cpp b/tests/main.cpp index 104c9b8..8f19838 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -186,7 +186,6 @@ int main() database.addUser(databaseNode, adminUser, localUser->getName(), localUser->getPublicKey(), adminUser->getGroups()[0]); localUser->addToGroup(adminUser->getGroups()[0]); database.addData(databaseNode, localUser, DataView{ (void*)"hello, aaald!", 13 }); - database.commit(); database.seed(databaseNode); auto start = chrono::high_resolution_clock::now(); |