Page MenuHomePhabricator

D3200.id9818.diff
No OneTemporary

D3200.id9818.diff

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 <folly/concurrency/ConcurrentHashMap.h>
+
+#include <functional>
+#include <map>
+#include <memory>
+#include <string>
+
+namespace comm {
+namespace network {
+namespace database {
+
+// thread-safe in-memory database
+struct DatabaseSimulator {
+ // userID -> map(created -> item)
+ folly::ConcurrentHashMap<
+ std::string,
+ std::unique_ptr<std::map<uint64_t, BackupItem>>>
+ backup;
+ // logID -> item
+ folly::ConcurrentHashMap<std::string, std::shared_ptr<LogItem>> 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 <iostream>
+#include <string>
+
+namespace comm {
+namespace network {
+namespace database {
+
+DatabaseManager &DatabaseManager::getInstance() {
+ static DatabaseManager instance;
+ return instance;
+}
+
+void DatabaseManager::putBackupItem(const BackupItem &item) {
+ std::shared_ptr<BackupItem> backupItem = std::make_shared<BackupItem>(
+ 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<std::map<uint64_t, BackupItem>>());
+ }
+ this->dbSimulator.backup.find(item.getUserID())
+ ->second->insert({backupItem->getCreated(), *backupItem});
+}
+
+std::shared_ptr<BackupItem>
+DatabaseManager::findLastBackupItem(const std::string &userID) {
+ auto it = this->dbSimulator.backup.find(userID);
+ if (it == this->dbSimulator.backup.end()) {
+ return nullptr;
+ }
+ if (it->second->empty()) {
+ return nullptr;
+ }
+ return std::make_shared<BackupItem>((--it->second->end())->second);
+}
+
+void DatabaseManager::removeBackupItem(std::shared_ptr<BackupItem> 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<LogItem>(
+ item.getBackupID(),
+ item.getLogID(),
+ item.getPersistedInBlob(),
+ item.getValue(),
+ item.getAttachmentHolders()));
+}
+
+std::vector<std::shared_ptr<LogItem>>
+DatabaseManager::findLogItemsForBackup(const std::string &backupID) {
+ std::vector<std::shared_ptr<LogItem>> 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<LogItem> 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(

File Metadata

Mime Type
text/plain
Expires
Sun, Nov 24, 12:55 PM (19 h, 37 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2575772
Default Alt Text
D3200.id9818.diff (5 KB)

Event Timeline