diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Database.cpp | 96 |
1 files changed, 28 insertions, 68 deletions
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 |