/
DatabaseManager.h
75 lines (62 loc) · 2.11 KB
/
DatabaseManager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#pragma once
#include "AwsTools.h"
#include "DatabaseEntitiesTools.h"
#include <aws/core/Aws.h>
#include <aws/dynamodb/model/AttributeDefinition.h>
#include <aws/dynamodb/model/DeleteItemRequest.h>
#include <aws/dynamodb/model/GetItemRequest.h>
#include <aws/dynamodb/model/PutItemRequest.h>
#include <memory>
#include <stdexcept>
#include <string>
#include <vector>
#ifdef COMM_SERVICES_DEV_MODE
#include "DatabaseSimulator.h"
#endif
namespace comm {
namespace network {
namespace database {
// this class should be thread-safe in case any shared resources appear
class DatabaseManager {
#ifdef COMM_SERVICES_DEV_MODE
DatabaseSimulator dbSimulator;
#endif
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);
public:
static DatabaseManager &getInstance();
void putBlobItem(const BlobItem &item);
std::shared_ptr<BlobItem> findBlobItem(const std::string &blobHash);
void removeBlobItem(const std::string &blobHash);
void putReverseIndexItem(const ReverseIndexItem &item);
std::shared_ptr<ReverseIndexItem>
findReverseIndexItemByHolder(const std::string &holder);
std::vector<std::shared_ptr<database::ReverseIndexItem>>
findReverseIndexItemsByHash(const std::string &blobHash);
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