Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3276407
D3990.id12572.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
D3990.id12572.diff
View Options
diff --git a/services/tunnelbroker/src/Database/DatabaseManager.h b/services/tunnelbroker/src/Database/DatabaseManager.h
--- a/services/tunnelbroker/src/Database/DatabaseManager.h
+++ b/services/tunnelbroker/src/Database/DatabaseManager.h
@@ -3,6 +3,7 @@
#include "AwsTools.h"
#include "Constants.h"
#include "DatabaseEntitiesTools.h"
+#include "DatabaseManagerBase.h"
#include "Tools.h"
#include <aws/core/Aws.h>
@@ -22,16 +23,7 @@
namespace network {
namespace database {
-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, const std::string &key);
-
+class DatabaseManager : public DatabaseManagerBase {
public:
static DatabaseManager &getInstance();
bool isTableAvailable(const std::string &tableName);
diff --git a/services/tunnelbroker/src/Database/DatabaseManager.cpp b/services/tunnelbroker/src/Database/DatabaseManager.cpp
--- a/services/tunnelbroker/src/Database/DatabaseManager.cpp
+++ b/services/tunnelbroker/src/Database/DatabaseManager.cpp
@@ -20,50 +20,6 @@
return result.IsSuccess();
}
-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());
- }
-}
-
-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);
-}
-
-void DatabaseManager::innerRemoveItem(
- const Item &item,
- const std::string &key) {
- Aws::DynamoDB::Model::DeleteItemRequest request;
- request.SetTableName(item.getTableName());
- request.AddKey(
- item.getPrimaryKey().partitionKey,
- Aws::DynamoDB::Model::AttributeValue(key));
-
- const Aws::DynamoDB::Model::DeleteItemOutcome &outcome =
- getDynamoDBClient()->DeleteItem(request);
- if (!outcome.IsSuccess()) {
- throw std::runtime_error(outcome.GetError().GetMessage());
- }
-}
-
void DatabaseManager::putSessionItem(const DeviceSessionItem &item) {
Aws::DynamoDB::Model::PutItemRequest request;
request.SetTableName(item.getTableName());
@@ -109,7 +65,11 @@
}
void DatabaseManager::removeSessionItem(const std::string &sessionID) {
- this->innerRemoveItem(*(createItemByType<DeviceSessionItem>()), sessionID);
+ std::shared_ptr<DeviceSessionItem> item = this->findSessionItem(sessionID);
+ if (item == nullptr) {
+ throw std::runtime_error("removing item failed: item not found");
+ }
+ this->innerRemoveItem(*item);
}
void DatabaseManager::putSessionSignItem(const SessionSignItem &item) {
@@ -138,7 +98,11 @@
}
void DatabaseManager::removeSessionSignItem(const std::string &deviceID) {
- this->innerRemoveItem(*(createItemByType<SessionSignItem>()), deviceID);
+ std::shared_ptr<SessionSignItem> item = this->findSessionSignItem(deviceID);
+ if (item == nullptr) {
+ throw std::runtime_error("removing item failed: item not found");
+ }
+ this->innerRemoveItem(*item);
}
void DatabaseManager::putPublicKeyItem(const PublicKeyItem &item) {
@@ -163,7 +127,11 @@
}
void DatabaseManager::removePublicKeyItem(const std::string &deviceID) {
- this->innerRemoveItem(*(createItemByType<PublicKeyItem>()), deviceID);
+ std::shared_ptr<PublicKeyItem> item = this->findPublicKeyItem(deviceID);
+ if (item == nullptr) {
+ throw std::runtime_error("removing item failed: item not found");
+ }
+ this->innerRemoveItem(*item);
}
void DatabaseManager::putMessageItem(const MessageItem &item) {
@@ -233,7 +201,11 @@
}
void DatabaseManager::removeMessageItem(const std::string &messageID) {
- this->innerRemoveItem(*(createItemByType<MessageItem>()), messageID);
+ std::shared_ptr<MessageItem> item = this->findMessageItem(messageID);
+ if (item == nullptr) {
+ throw std::runtime_error("removing item failed: item not found");
+ }
+ this->innerRemoveItem(*item);
}
} // namespace database
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Nov 17, 7:28 AM (19 h, 2 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2518180
Default Alt Text
D3990.id12572.diff (4 KB)
Attached To
Mode
D3990: [services] Lib - Tunnelbroker - Use database manager
Attached
Detach File
Event Timeline
Log In to Comment