From 0c8761b3d76912f034a0cb819d72f0349a25bf4f Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 11 May 2018 17:10:39 +0200 Subject: Remove commit, maybe readd it later --- Scheme.md | 4 +- include/odhtdb/Database.hpp | 9 ---- include/odhtdb/StagedObject.hpp | 22 ---------- src/Database.cpp | 96 ++++++++++++----------------------------- tests/main.cpp | 1 - 5 files changed, 30 insertions(+), 102 deletions(-) delete mode 100644 include/odhtdb/StagedObject.hpp diff --git a/Scheme.md b/Scheme.md index c62c6f4..865339e 100644 --- a/Scheme.md +++ b/Scheme.md @@ -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 callbackFunc); void setOnAddNodeCallback(std::function 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); - // Throws CommitAddException on failure - void commitStagedAddObject(const std::unique_ptr &stagedObject); void deserializeCreateRequest(const std::shared_ptr &value, const Hash &hash, const std::shared_ptr encryptionKey); void deserializeAddRequest(const std::shared_ptr &value, const Hash &requestDataHash, const std::shared_ptr &nodeHash, 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 &requestDataHash, const std::shared_ptr nodeHash, const std::shared_ptr encryptionKey); private: dht::DhtRunner node; - std::vector> stagedCreateObjects; - std::vector> stagedAddObjects; DatabaseStorage databaseStorage; std::function onCreateNodeCallbackFunc; std::function 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 requestKey; - - StagedObject(DataView &_data, const std::shared_ptr &_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 hashRequestKey = make_shared(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(requestData, hashRequestKey)); assert(encryptedBody.getKey().size == ENCRYPTION_KEY_BYTE_SIZE); auto key = make_shared(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(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(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 &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(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) - { - 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) - { - 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(); -- cgit v1.2.3