-
Notifications
You must be signed in to change notification settings - Fork 28
/
LogItem.cpp
148 lines (124 loc) · 4.2 KB
/
LogItem.cpp
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "LogItem.h"
#include "Constants.h"
#include "Tools.h"
#include <stdexcept>
namespace comm {
namespace network {
namespace database {
const std::string LogItem::FIELD_BACKUP_ID = "backupID";
const std::string LogItem::FIELD_LOG_ID = "logID";
const std::string LogItem::FIELD_PERSISTED_IN_BLOB = "persistedInBlob";
const std::string LogItem::FIELD_VALUE = "value";
const std::string LogItem::FIELD_ATTACHMENT_HOLDERS = "attachmentHolders";
const std::string LogItem::FIELD_DATA_HASH = "dataHash";
const std::string LogItem::TABLE_NAME = LOG_TABLE_NAME;
LogItem::LogItem(
const std::string backupID,
const std::string logID,
const bool persistedInBlob,
const std::string value,
std::string attachmentHolders,
const std::string dataHash)
: backupID(backupID),
logID(logID),
persistedInBlob(persistedInBlob),
value(value),
attachmentHolders(attachmentHolders),
dataHash(dataHash) {
this->validate();
}
LogItem::LogItem(const AttributeValues &itemFromDB) {
this->assignItemFromDatabase(itemFromDB);
}
void LogItem::validate() const {
if (!this->backupID.size()) {
throw std::runtime_error("backupID empty");
}
if (!this->logID.size()) {
throw std::runtime_error("logID empty");
}
if (!this->value.size()) {
throw std::runtime_error("value empty");
}
const size_t itemSize = LogItem::getItemSize(this);
if (!this->persistedInBlob && itemSize > LOG_DATA_SIZE_DATABASE_LIMIT) {
throw std::runtime_error(
"the value of this log is too big to be stored in the database, it "
"should be stored in the blob instead (" +
std::to_string(itemSize) + "/" +
std::to_string(LOG_DATA_SIZE_DATABASE_LIMIT) + ")");
}
if (!this->dataHash.size()) {
throw std::runtime_error("data hash empty");
}
}
void LogItem::assignItemFromDatabase(const AttributeValues &itemFromDB) {
try {
this->backupID = itemFromDB.at(LogItem::FIELD_BACKUP_ID).GetS();
this->logID = itemFromDB.at(LogItem::FIELD_LOG_ID).GetS();
this->persistedInBlob = std::stoi(
std::string(itemFromDB.at(LogItem::FIELD_PERSISTED_IN_BLOB).GetS())
.c_str());
this->value = itemFromDB.at(LogItem::FIELD_VALUE).GetS();
auto attachmentsHolders =
itemFromDB.find(LogItem::FIELD_ATTACHMENT_HOLDERS);
if (attachmentsHolders != itemFromDB.end()) {
this->attachmentHolders = attachmentsHolders->second.GetS();
}
this->dataHash = itemFromDB.at(LogItem::FIELD_DATA_HASH).GetS();
} catch (std::logic_error &e) {
throw std::runtime_error(
"invalid log item provided, " + std::string(e.what()));
}
this->validate();
}
std::string LogItem::getTableName() const {
return LogItem::TABLE_NAME;
}
PrimaryKeyDescriptor LogItem::getPrimaryKeyDescriptor() const {
return PrimaryKeyDescriptor(LogItem::FIELD_BACKUP_ID, LogItem::FIELD_LOG_ID);
}
PrimaryKeyValue LogItem::getPrimaryKeyValue() const {
return PrimaryKeyValue(this->backupID, this->logID);
}
std::string LogItem::getBackupID() const {
return this->backupID;
}
std::string LogItem::getLogID() const {
return this->logID;
}
bool LogItem::getPersistedInBlob() const {
return this->persistedInBlob;
}
std::string LogItem::getValue() const {
return this->value;
}
std::string LogItem::getAttachmentHolders() const {
return this->attachmentHolders;
}
std::string LogItem::getDataHash() const {
return this->dataHash;
}
void LogItem::addAttachmentHolders(const std::string &attachmentHolders) {
this->attachmentHolders +=
tools::validateAttachmentHolders(attachmentHolders);
}
size_t LogItem::getItemSize(const LogItem *item) {
size_t size = 0;
size += LogItem::FIELD_BACKUP_ID.size();
size += LogItem::FIELD_LOG_ID.size();
size += LogItem::FIELD_PERSISTED_IN_BLOB.size();
size += LogItem::FIELD_VALUE.size();
size += LogItem::FIELD_ATTACHMENT_HOLDERS.size();
size += LogItem::FIELD_DATA_HASH.size();
size += item->getBackupID().size();
size += item->getLogID().size();
size += std::to_string(item->getPersistedInBlob()).size();
size += item->getValue().size();
size += item->getAttachmentHolders().size();
size += item->getDataHash().size();
return size;
}
} // namespace database
} // namespace network
} // namespace comm