diff --git a/services/backup/docker-server/contents/server/dev/DatabaseSimulator.h b/services/backup/docker-server/contents/server/dev/DatabaseSimulator.h new file mode 100644 --- /dev/null +++ b/services/backup/docker-server/contents/server/dev/DatabaseSimulator.h @@ -0,0 +1,29 @@ +#pragma once + +#include "DatabaseEntitiesTools.h" + +#include + +#include +#include +#include +#include + +namespace comm { +namespace network { +namespace database { + +// thread-safe in-memory database +struct DatabaseSimulator { + // userID -> map(created -> item) + folly::ConcurrentHashMap< + std::string, + std::unique_ptr>> + backup; + // logID -> item + folly::ConcurrentHashMap> log; +}; + +} // namespace database +} // namespace network +} // namespace comm diff --git a/services/backup/docker-server/contents/server/src/DatabaseManager.dev.cpp b/services/backup/docker-server/contents/server/src/DatabaseManager.dev.cpp new file mode 100644 --- /dev/null +++ b/services/backup/docker-server/contents/server/src/DatabaseManager.dev.cpp @@ -0,0 +1,84 @@ +#include "DatabaseManager.h" +#include "Tools.h" + +#include +#include + +namespace comm { +namespace network { +namespace database { + +DatabaseManager &DatabaseManager::getInstance() { + static DatabaseManager instance; + return instance; +} + +void DatabaseManager::putBackupItem(const BackupItem &item) { + std::shared_ptr backupItem = std::make_shared( + item.getUserID(), + item.getBackupID(), + item.getCreated(), + item.getRecoveryData(), + item.getCompactionHolder(), + item.getAttachmentHolders()); + if (this->dbSimulator.backup.find(item.getUserID()) == + this->dbSimulator.backup.end()) { + this->dbSimulator.backup.insert( + item.getUserID(), std::make_unique>()); + } + this->dbSimulator.backup.find(item.getUserID()) + ->second->insert({backupItem->getCreated(), *backupItem}); +} + +std::shared_ptr +DatabaseManager::findLastBackupItem(const std::string &userID) { + if (this->dbSimulator.backup.find(userID) == this->dbSimulator.backup.end()) { + return nullptr; + } + if (this->dbSimulator.backup.find(userID)->second->empty()) { + return nullptr; + } + return std::make_shared( + (--this->dbSimulator.backup.find(userID)->second->end())->second); +} + +void DatabaseManager::removeBackupItem(std::shared_ptr item) { + if (this->dbSimulator.backup.find(item->getUserID()) == + this->dbSimulator.backup.end()) { + return; + } + this->dbSimulator.backup.find(item->getUserID()) + ->second->erase(item->getCreated()); +} + +void DatabaseManager::putLogItem(const LogItem &item) { + this->dbSimulator.log.insert( + item.getLogID(), + std::make_shared( + item.getBackupID(), + item.getLogID(), + item.getPersistedInBlob(), + item.getValue(), + item.getAttachmentHolders())); +} + +std::vector> +DatabaseManager::findLogItemsForBackup(const std::string &backupID) { + std::vector> result; + for (auto it = this->dbSimulator.log.begin(); + it != this->dbSimulator.log.end(); + ++it) { + if (it->second->getBackupID() == backupID) { + result.push_back(it->second); + } + } + return result; +} + +void DatabaseManager::removeLogItem(std::shared_ptr item) { + this->dbSimulator.log.erase(item->getLogID()); +} + +} // namespace database +} // namespace network +} // namespace comm diff --git a/services/backup/docker-server/contents/server/test/DatabaseManagerTest.cpp b/services/backup/docker-server/contents/server/test/DatabaseManagerTest.cpp --- a/services/backup/docker-server/contents/server/test/DatabaseManagerTest.cpp +++ b/services/backup/docker-server/contents/server/test/DatabaseManagerTest.cpp @@ -28,13 +28,13 @@ BackupItem generateBackupItem(const std::string &userID, const std::string &backupID) { - return BackupItem( - userID, - backupID, - comm::network::getCurrentTimestamp(), - "xxx", - "xxx", - {""}); + // this simulates operations that are going to occur one after another on the + // timeline. For the dev mode this was failing when reading the current time + // because operations on collections were fast and occuring at the same + // exact millisecond. In practice, this should never happen(grpc connection + // should cause the delay) + static uint64_t operationID = comm::network::getCurrentTimestamp(); + return BackupItem(userID, backupID, operationID++, "xxx", "xxx", {""}); } LogItem generateLogItem(const std::string &backupID, const std::string &logID) { @@ -86,7 +86,9 @@ EXPECT_EQ(items2.size(), 2); for (size_t i = 0; i < items1.size(); ++i) { - EXPECT_EQ(logIDs1.at(i), items1.at(i)->getLogID()); + EXPECT_NE( + std::find(logIDs1.begin(), logIDs1.end(), items1.at(i)->getLogID()), + logIDs1.end()); DatabaseManager::getInstance().removeLogItem(items1.at(i)); } EXPECT_EQ( @@ -94,7 +96,9 @@ 0); for (size_t i = 0; i < items2.size(); ++i) { - EXPECT_EQ(logIDs2.at(i), items2.at(i)->getLogID()); + EXPECT_NE( + std::find(logIDs2.begin(), logIDs2.end(), items2.at(i)->getLogID()), + logIDs2.end()); DatabaseManager::getInstance().removeLogItem(items2.at(i)); } EXPECT_EQ(