Page MenuHomePhabricator

D3160.id9728.diff
No OneTemporary

D3160.id9728.diff

diff --git a/services/backup/docker-server/contents/server/src/DatabaseManager.h b/services/backup/docker-server/contents/server/src/DatabaseManager.h
--- a/services/backup/docker-server/contents/server/src/DatabaseManager.h
+++ b/services/backup/docker-server/contents/server/src/DatabaseManager.h
@@ -36,6 +36,10 @@
public:
static DatabaseManager &getInstance();
+
+ void putBackupItem(const BackupItem &item);
+ std::shared_ptr<BackupItem> findLastBackupItem(const std::string &userID);
+ void removeBackupItem(std::shared_ptr<BackupItem> item);
};
template <typename T>
diff --git a/services/backup/docker-server/contents/server/src/DatabaseManager.cpp b/services/backup/docker-server/contents/server/src/DatabaseManager.cpp
--- a/services/backup/docker-server/contents/server/src/DatabaseManager.cpp
+++ b/services/backup/docker-server/contents/server/src/DatabaseManager.cpp
@@ -1,10 +1,11 @@
#include "DatabaseManager.h"
#include "Constants.h"
+#include "Tools.h"
#include <aws/core/utils/Outcome.h>
+#include <aws/dynamodb/model/DeleteItemRequest.h>
#include <aws/dynamodb/model/QueryRequest.h>
#include <aws/dynamodb/model/ScanRequest.h>
-#include <aws/dynamodb/model/DeleteItemRequest.h>
#include <iostream>
@@ -42,6 +43,78 @@
}
}
+void DatabaseManager::putBackupItem(const BackupItem &item) {
+ Aws::DynamoDB::Model::PutItemRequest request;
+ request.SetTableName(BackupItem::tableName);
+ request.AddItem(
+ BackupItem::FIELD_USER_ID,
+ Aws::DynamoDB::Model::AttributeValue(item.getUserID()));
+ request.AddItem(
+ BackupItem::FIELD_CREATED,
+ Aws::DynamoDB::Model::AttributeValue(
+ std::to_string(getCurrentTimestamp())));
+ request.AddItem(
+ BackupItem::FIELD_BACKUP_ID,
+ Aws::DynamoDB::Model::AttributeValue(item.getBackupID()));
+ request.AddItem(
+ BackupItem::FIELD_RECOVERY_DATA,
+ Aws::DynamoDB::Model::AttributeValue(item.getRecoveryData()));
+ request.AddItem(
+ BackupItem::FIELD_COMPACTION_HOLDER,
+ Aws::DynamoDB::Model::AttributeValue(item.getCompactionHolder()));
+ request.AddItem(
+ BackupItem::FIELD_ATTACHMENT_HOLDERS,
+ Aws::DynamoDB::Model::AttributeValue(item.getAttachmentHolders()));
+
+ this->innerPutItem(std::make_shared<BackupItem>(item), request);
+}
+
+std::shared_ptr<BackupItem>
+DatabaseManager::findLastBackupItem(const std::string &userID) {
+ std::shared_ptr<BackupItem> item = createItemByType<BackupItem>();
+
+ Aws::DynamoDB::Model::QueryRequest req;
+ req.SetTableName(BackupItem::tableName);
+ req.SetKeyConditionExpression(item->getPrimaryKey() + " = :valueToMatch");
+
+ AttributeValues attributeValues;
+ attributeValues.emplace(":valueToMatch", userID);
+
+ req.SetExpressionAttributeValues(attributeValues);
+ req.SetIndexName("userID-created-index");
+
+ req.SetLimit(1);
+ req.SetScanIndexForward(false);
+
+ const Aws::DynamoDB::Model::QueryOutcome &outcome =
+ getDynamoDBClient()->Query(req);
+ if (!outcome.IsSuccess()) {
+ throw std::runtime_error(outcome.GetError().GetMessage());
+ }
+ const Aws::Vector<AttributeValues> &items = outcome.GetResult().GetItems();
+ if (items.empty()) {
+ return nullptr;
+ }
+ return std::make_shared<database::BackupItem>(items[0]);
+}
+
+void DatabaseManager::removeBackupItem(std::shared_ptr<BackupItem> item) {
+ Aws::DynamoDB::Model::DeleteItemRequest request;
+ request.SetTableName(item->getTableName());
+ request.AddKey(
+ BackupItem::FIELD_USER_ID,
+ Aws::DynamoDB::Model::AttributeValue(item->getUserID()));
+ request.AddKey(
+ BackupItem::FIELD_BACKUP_ID,
+ Aws::DynamoDB::Model::AttributeValue(item->getBackupID()));
+
+ const Aws::DynamoDB::Model::DeleteItemOutcome &outcome =
+ comm::network::getDynamoDBClient()->DeleteItem(request);
+ if (!outcome.IsSuccess()) {
+ throw std::runtime_error(outcome.GetError().GetMessage());
+ }
+}
+
} // namespace database
} // namespace network
} // namespace comm

File Metadata

Mime Type
text/plain
Expires
Sun, Nov 24, 2:00 PM (21 h, 30 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2576160
Default Alt Text
D3160.id9728.diff (3 KB)

Event Timeline