Page MenuHomePhabricator

D3956.diff
No OneTemporary

D3956.diff

diff --git a/services/backup/src/DatabaseManager.h b/services/backup/src/DatabaseManager.h
--- a/services/backup/src/DatabaseManager.h
+++ b/services/backup/src/DatabaseManager.h
@@ -1,6 +1,7 @@
#pragma once
#include "DatabaseEntitiesTools.h"
+#include "DatabaseManagerBase.h"
#include "DynamoDBTools.h"
#include <aws/core/Aws.h>
@@ -16,16 +17,7 @@
namespace database {
// this class should be thread-safe in case any shared resources appear
-class DatabaseManager {
- void innerPutItem(
- std::shared_ptr<Item> item,
- const Aws::DynamoDB::Model::PutItemRequest &request);
-
- template <typename T>
- std::shared_ptr<T>
- innerFindItem(Aws::DynamoDB::Model::GetItemRequest &request);
- void innerRemoveItem(const Item &item);
-
+class DatabaseManager : public DatabaseManagerBase {
public:
static DatabaseManager &getInstance();
@@ -41,24 +33,6 @@
void removeLogItem(std::shared_ptr<LogItem> item);
};
-template <typename T>
-std::shared_ptr<T>
-DatabaseManager::innerFindItem(Aws::DynamoDB::Model::GetItemRequest &request) {
- std::shared_ptr<T> item = createItemByType<T>();
- request.SetTableName(item->getTableName());
- const Aws::DynamoDB::Model::GetItemOutcome &outcome =
- getDynamoDBClient()->GetItem(request);
- if (!outcome.IsSuccess()) {
- throw std::runtime_error(outcome.GetError().GetMessage());
- }
- const AttributeValues &outcomeItem = outcome.GetResult().GetItem();
- if (!outcomeItem.size()) {
- return nullptr;
- }
- item->assignItemFromDatabase(outcomeItem);
- return item;
-}
-
} // namespace database
} // namespace network
} // namespace comm
diff --git a/services/backup/src/DatabaseManager.cpp b/services/backup/src/DatabaseManager.cpp
--- a/services/backup/src/DatabaseManager.cpp
+++ b/services/backup/src/DatabaseManager.cpp
@@ -19,37 +19,6 @@
return instance;
}
-void DatabaseManager::innerPutItem(
- std::shared_ptr<Item> item,
- const Aws::DynamoDB::Model::PutItemRequest &request) {
- const Aws::DynamoDB::Model::PutItemOutcome outcome =
- getDynamoDBClient()->PutItem(request);
- if (!outcome.IsSuccess()) {
- throw std::runtime_error(outcome.GetError().GetMessage());
- }
-}
-
-void DatabaseManager::innerRemoveItem(const Item &item) {
- Aws::DynamoDB::Model::DeleteItemRequest request;
- request.SetTableName(item.getTableName());
- PrimaryKey pk = item.getPrimaryKey();
- PrimaryKeyValue primaryKeyValue = item.getPrimaryKeyValue();
- request.AddKey(
- pk.partitionKey,
- Aws::DynamoDB::Model::AttributeValue(primaryKeyValue.partitionKey));
- if (pk.sortKey != nullptr && primaryKeyValue.sortKey != nullptr) {
- request.AddKey(
- *pk.sortKey,
- Aws::DynamoDB::Model::AttributeValue(*primaryKeyValue.sortKey));
- }
-
- const Aws::DynamoDB::Model::DeleteItemOutcome &outcome =
- getDynamoDBClient()->DeleteItem(request);
- if (!outcome.IsSuccess()) {
- throw std::runtime_error(outcome.GetError().GetMessage());
- }
-}
-
void DatabaseManager::putBackupItem(const BackupItem &item) {
Aws::DynamoDB::Model::PutItemRequest request;
request.SetTableName(BackupItem::tableName);
diff --git a/services/blob/src/DatabaseManager.h b/services/blob/src/DatabaseManager.h
--- a/services/blob/src/DatabaseManager.h
+++ b/services/blob/src/DatabaseManager.h
@@ -1,6 +1,7 @@
#pragma once
#include "DatabaseEntitiesTools.h"
+#include "DatabaseManagerBase.h"
#include "DynamoDBTools.h"
#include <aws/core/Aws.h>
@@ -19,17 +20,7 @@
namespace database {
// this class should be thread-safe in case any shared resources appear
-class DatabaseManager {
-
- void innerPutItem(
- std::shared_ptr<Item> item,
- const Aws::DynamoDB::Model::PutItemRequest &request);
-
- template <typename T>
- std::shared_ptr<T>
- innerFindItem(Aws::DynamoDB::Model::GetItemRequest &request);
- void innerRemoveItem(const Item &item);
-
+class DatabaseManager : public DatabaseManagerBase {
public:
static DatabaseManager &getInstance();
@@ -45,24 +36,6 @@
bool removeReverseIndexItem(const std::string &holder);
};
-template <typename T>
-std::shared_ptr<T>
-DatabaseManager::innerFindItem(Aws::DynamoDB::Model::GetItemRequest &request) {
- std::shared_ptr<T> item = createItemByType<T>();
- request.SetTableName(item->getTableName());
- const Aws::DynamoDB::Model::GetItemOutcome &outcome =
- getDynamoDBClient()->GetItem(request);
- if (!outcome.IsSuccess()) {
- throw std::runtime_error(outcome.GetError().GetMessage());
- }
- const AttributeValues &outcomeItem = outcome.GetResult().GetItem();
- if (!outcomeItem.size()) {
- return nullptr;
- }
- item->assignItemFromDatabase(outcomeItem);
- return std::move(item);
-}
-
} // namespace database
} // namespace network
} // namespace comm
diff --git a/services/blob/src/DatabaseManager.cpp b/services/blob/src/DatabaseManager.cpp
--- a/services/blob/src/DatabaseManager.cpp
+++ b/services/blob/src/DatabaseManager.cpp
@@ -18,37 +18,6 @@
return instance;
}
-void DatabaseManager::innerPutItem(
- std::shared_ptr<Item> item,
- const Aws::DynamoDB::Model::PutItemRequest &request) {
- const Aws::DynamoDB::Model::PutItemOutcome outcome =
- getDynamoDBClient()->PutItem(request);
- if (!outcome.IsSuccess()) {
- throw std::runtime_error(outcome.GetError().GetMessage());
- }
-}
-
-void DatabaseManager::innerRemoveItem(const Item &item) {
- Aws::DynamoDB::Model::DeleteItemRequest request;
- request.SetTableName(item.getTableName());
- PrimaryKey pk = item.getPrimaryKey();
- PrimaryKeyValue primaryKeyValue = item.getPrimaryKeyValue();
- request.AddKey(
- pk.partitionKey,
- Aws::DynamoDB::Model::AttributeValue(primaryKeyValue.partitionKey));
- if (pk.sortKey != nullptr && primaryKeyValue.sortKey != nullptr) {
- request.AddKey(
- *pk.sortKey,
- Aws::DynamoDB::Model::AttributeValue(*primaryKeyValue.sortKey));
- }
-
- const Aws::DynamoDB::Model::DeleteItemOutcome &outcome =
- getDynamoDBClient()->DeleteItem(request);
- if (!outcome.IsSuccess()) {
- throw std::runtime_error(outcome.GetError().GetMessage());
- }
-}
-
void DatabaseManager::putBlobItem(const BlobItem &item) {
Aws::DynamoDB::Model::PutItemRequest request;
request.SetTableName(BlobItem::tableName);
diff --git a/services/backup/src/DatabaseManager.h b/services/lib/src/DatabaseManagerBase.h
copy from services/backup/src/DatabaseManager.h
copy to services/lib/src/DatabaseManagerBase.h
--- a/services/backup/src/DatabaseManager.h
+++ b/services/lib/src/DatabaseManagerBase.h
@@ -4,19 +4,18 @@
#include "DynamoDBTools.h"
#include <aws/core/Aws.h>
-#include <aws/dynamodb/model/AttributeDefinition.h>
#include <aws/dynamodb/model/GetItemRequest.h>
#include <aws/dynamodb/model/PutItemRequest.h>
#include <memory>
-#include <string>
namespace comm {
namespace network {
namespace database {
// this class should be thread-safe in case any shared resources appear
-class DatabaseManager {
+class DatabaseManagerBase {
+protected:
void innerPutItem(
std::shared_ptr<Item> item,
const Aws::DynamoDB::Model::PutItemRequest &request);
@@ -24,26 +23,13 @@
template <typename T>
std::shared_ptr<T>
innerFindItem(Aws::DynamoDB::Model::GetItemRequest &request);
- void innerRemoveItem(const Item &item);
-
-public:
- static DatabaseManager &getInstance();
- void putBackupItem(const BackupItem &item);
- std::shared_ptr<BackupItem>
- findBackupItem(const std::string &userID, const std::string &backupID);
- std::shared_ptr<BackupItem> findLastBackupItem(const std::string &userID);
- void removeBackupItem(std::shared_ptr<BackupItem> item);
-
- void putLogItem(const LogItem &item);
- std::vector<std::shared_ptr<LogItem>>
- findLogItemsForBackup(const std::string &backupID);
- void removeLogItem(std::shared_ptr<LogItem> item);
+ void innerRemoveItem(const Item &item);
};
template <typename T>
-std::shared_ptr<T>
-DatabaseManager::innerFindItem(Aws::DynamoDB::Model::GetItemRequest &request) {
+std::shared_ptr<T> DatabaseManagerBase::innerFindItem(
+ Aws::DynamoDB::Model::GetItemRequest &request) {
std::shared_ptr<T> item = createItemByType<T>();
request.SetTableName(item->getTableName());
const Aws::DynamoDB::Model::GetItemOutcome &outcome =
@@ -56,7 +42,7 @@
return nullptr;
}
item->assignItemFromDatabase(outcomeItem);
- return item;
+ return std::move(item);
}
} // namespace database
diff --git a/services/lib/src/DatabaseManagerBase.cpp b/services/lib/src/DatabaseManagerBase.cpp
new file mode 100644
--- /dev/null
+++ b/services/lib/src/DatabaseManagerBase.cpp
@@ -0,0 +1,45 @@
+#include "DatabaseManagerBase.h"
+
+#include <aws/core/utils/Outcome.h>
+#include <aws/dynamodb/model/DeleteItemRequest.h>
+
+#include <iostream>
+
+namespace comm {
+namespace network {
+namespace database {
+
+void DatabaseManagerBase::innerPutItem(
+ std::shared_ptr<Item> item,
+ const Aws::DynamoDB::Model::PutItemRequest &request) {
+ const Aws::DynamoDB::Model::PutItemOutcome outcome =
+ getDynamoDBClient()->PutItem(request);
+ if (!outcome.IsSuccess()) {
+ throw std::runtime_error(outcome.GetError().GetMessage());
+ }
+}
+
+void DatabaseManagerBase::innerRemoveItem(const Item &item) {
+ Aws::DynamoDB::Model::DeleteItemRequest request;
+ request.SetTableName(item.getTableName());
+ PrimaryKey pk = item.getPrimaryKey();
+ PrimaryKeyValue primaryKeyValue = item.getPrimaryKeyValue();
+ request.AddKey(
+ pk.partitionKey,
+ Aws::DynamoDB::Model::AttributeValue(primaryKeyValue.partitionKey));
+ if (pk.sortKey != nullptr && primaryKeyValue.sortKey != nullptr) {
+ request.AddKey(
+ *pk.sortKey,
+ Aws::DynamoDB::Model::AttributeValue(*primaryKeyValue.sortKey));
+ }
+
+ const Aws::DynamoDB::Model::DeleteItemOutcome &outcome =
+ 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
Thu, Dec 26, 5:57 AM (12 h, 36 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2704019
Default Alt Text
D3956.diff (9 KB)

Event Timeline