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 @@ -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, - const Aws::DynamoDB::Model::PutItemRequest &request); - - template - std::shared_ptr - 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 item); }; -template -std::shared_ptr -DatabaseManager::innerFindItem(Aws::DynamoDB::Model::GetItemRequest &request) { - std::shared_ptr item = createItemByType(); - 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, - 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 @@ -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, - const Aws::DynamoDB::Model::PutItemRequest &request); - - template - std::shared_ptr - 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 -std::shared_ptr -DatabaseManager::innerFindItem(Aws::DynamoDB::Model::GetItemRequest &request) { - std::shared_ptr item = createItemByType(); - 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, - 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/lib_src/DatabaseManagerBase.h copy from services/backup/src/DatabaseManager.h copy to services/lib/lib_src/DatabaseManagerBase.h --- a/services/backup/src/DatabaseManager.h +++ b/services/lib/lib_src/DatabaseManagerBase.h @@ -4,19 +4,18 @@ #include "DynamoDBTools.h" #include -#include #include #include #include -#include 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, const Aws::DynamoDB::Model::PutItemRequest &request); @@ -24,26 +23,13 @@ template std::shared_ptr innerFindItem(Aws::DynamoDB::Model::GetItemRequest &request); - void innerRemoveItem(const Item &item); - -public: - static DatabaseManager &getInstance(); - void putBackupItem(const BackupItem &item); - std::shared_ptr - findBackupItem(const std::string &userID, const std::string &backupID); - std::shared_ptr findLastBackupItem(const std::string &userID); - void removeBackupItem(std::shared_ptr item); - - void putLogItem(const LogItem &item); - std::vector> - findLogItemsForBackup(const std::string &backupID); - void removeLogItem(std::shared_ptr item); + void innerRemoveItem(const Item &item); }; template -std::shared_ptr -DatabaseManager::innerFindItem(Aws::DynamoDB::Model::GetItemRequest &request) { +std::shared_ptr DatabaseManagerBase::innerFindItem( + Aws::DynamoDB::Model::GetItemRequest &request) { std::shared_ptr item = createItemByType(); 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/lib_src/DatabaseManagerBase.cpp b/services/lib/lib_src/DatabaseManagerBase.cpp new file mode 100644 --- /dev/null +++ b/services/lib/lib_src/DatabaseManagerBase.cpp @@ -0,0 +1,45 @@ +#include "DatabaseManagerBase.h" + +#include +#include + +#include + +namespace comm { +namespace network { +namespace database { + +void DatabaseManagerBase::innerPutItem( + std::shared_ptr 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