diff --git a/services/backup/src/DatabaseEntities/BackupItem.cpp b/services/backup/src/DatabaseEntities/BackupItem.cpp index 4380eeef4..037444625 100644 --- a/services/backup/src/DatabaseEntities/BackupItem.cpp +++ b/services/backup/src/DatabaseEntities/BackupItem.cpp @@ -1,116 +1,122 @@ #include "BackupItem.h" #include "Constants.h" +#include "GlobalConstants.h" namespace comm { namespace network { namespace database { const std::string BackupItem::FIELD_USER_ID = "userID"; const std::string BackupItem::FIELD_BACKUP_ID = "backupID"; const std::string BackupItem::FIELD_CREATED = "created"; const std::string BackupItem::FIELD_RECOVERY_DATA = "recoveryData"; const std::string BackupItem::FIELD_COMPACTION_HOLDER = "compactionHolder"; const std::string BackupItem::FIELD_ATTACHMENT_HOLDERS = "attachmentHolders"; std::string BackupItem::tableName = BACKUP_TABLE_NAME; BackupItem::BackupItem( std::string userID, std::string backupID, uint64_t created, std::string recoveryData, std::string compactionHolder, - std::vector attachmentHolders) + std::string attachmentHolders) : userID(userID), backupID(backupID), created(created), recoveryData(recoveryData), compactionHolder(compactionHolder), attachmentHolders(attachmentHolders) { this->validate(); } BackupItem::BackupItem(const AttributeValues &itemFromDB) { this->assignItemFromDatabase(itemFromDB); } void BackupItem::validate() const { if (!this->userID.size()) { throw std::runtime_error("userID empty"); } if (!this->backupID.size()) { throw std::runtime_error("backupID empty"); } if (!this->created) { throw std::runtime_error("created not provided"); } if (!this->recoveryData.size()) { throw std::runtime_error("recoveryData empty"); } } void BackupItem::assignItemFromDatabase(const AttributeValues &itemFromDB) { try { this->userID = itemFromDB.at(BackupItem::FIELD_USER_ID).GetS(); this->backupID = itemFromDB.at(BackupItem::FIELD_BACKUP_ID).GetS(); this->created = std::stoll( std::string(itemFromDB.at(BackupItem::FIELD_CREATED).GetS()).c_str()); this->recoveryData = itemFromDB.at(BackupItem::FIELD_RECOVERY_DATA).GetS(); auto compactionHolder = itemFromDB.find(BackupItem::FIELD_COMPACTION_HOLDER); if (compactionHolder != itemFromDB.end()) { this->compactionHolder = compactionHolder->second.GetS(); } auto attachmentsHolders = itemFromDB.find(BackupItem::FIELD_ATTACHMENT_HOLDERS); if (attachmentsHolders != itemFromDB.end()) { - this->attachmentHolders = attachmentsHolders->second.GetSS(); + this->attachmentHolders = attachmentsHolders->second.GetS(); } } catch (std::logic_error &e) { throw std::runtime_error( "invalid backup item provided, " + std::string(e.what())); } this->validate(); } std::string BackupItem::getTableName() const { return BackupItem::tableName; } PrimaryKeyDescriptor BackupItem::getPrimaryKeyDescriptor() const { return PrimaryKeyDescriptor( BackupItem::FIELD_USER_ID, BackupItem::FIELD_BACKUP_ID); } PrimaryKeyValue BackupItem::getPrimaryKeyValue() const { return PrimaryKeyValue(this->userID, this->backupID); } std::string BackupItem::getUserID() const { return this->userID; } std::string BackupItem::getBackupID() const { return this->backupID; } uint64_t BackupItem::getCreated() const { return this->created; } std::string BackupItem::getRecoveryData() const { return this->recoveryData; } std::string BackupItem::getCompactionHolder() const { return this->compactionHolder; } -std::vector BackupItem::getAttachmentHolders() const { +std::string BackupItem::getAttachmentHolders() const { return this->attachmentHolders; } +void BackupItem::appendAttachmentHolder(const std::string &attachmentHolder) { + this->attachmentHolders += attachmentHolder; + this->attachmentHolders += ATTACHMENT_DELIMITER; +} + } // namespace database } // namespace network } // namespace comm diff --git a/services/backup/src/DatabaseEntities/BackupItem.h b/services/backup/src/DatabaseEntities/BackupItem.h index 73cb484d0..01c6f3ba9 100644 --- a/services/backup/src/DatabaseEntities/BackupItem.h +++ b/services/backup/src/DatabaseEntities/BackupItem.h @@ -1,80 +1,81 @@ #pragma once #include "Item.h" #include -#include namespace comm { namespace network { namespace database { /** * backup - backups assigned to users along with the data necessary to * decrypt * `created` - when the backup was created. This is a search key because * we want to be able to perform effective queries based on this info * (for example get me the latest backup, get me backup from some day) * `attachmentHolders` - this is a list of attachment references * `recoveryData` - data serialized with protobuf which is described by * one of the following structures: * { authType: 'password', pakePasswordCiphertext: string, nonce: string } * { authType: 'wallet', walletAddress: string, rawMessage: string } * * this class is used for representing two things: the rows in the main table, * and also the rows in the secondary index * * Needs userID(pk)-created(sk)-index that projects: * userID * backupID * created * recoveryData */ class BackupItem : public Item { std::string userID; std::string backupID; uint64_t created; std::string recoveryData; std::string compactionHolder; - std::vector attachmentHolders; + std::string attachmentHolders; void validate() const override; public: static std::string tableName; static const std::string FIELD_USER_ID; static const std::string FIELD_BACKUP_ID; static const std::string FIELD_CREATED; static const std::string FIELD_RECOVERY_DATA; static const std::string FIELD_COMPACTION_HOLDER; static const std::string FIELD_ATTACHMENT_HOLDERS; BackupItem() { } BackupItem( std::string userID, std::string backupID, uint64_t created, std::string recoveryData, std::string compactionHolder, - std::vector attachmentHolders); + std::string attachmentHolders); BackupItem(const AttributeValues &itemFromDB); void assignItemFromDatabase(const AttributeValues &itemFromDB) override; std::string getTableName() const override; PrimaryKeyDescriptor getPrimaryKeyDescriptor() const override; PrimaryKeyValue getPrimaryKeyValue() const override; std::string getUserID() const; std::string getBackupID() const; uint64_t getCreated() const; std::string getRecoveryData() const; std::string getCompactionHolder() const; - std::vector getAttachmentHolders() const; + std::string getAttachmentHolders() const; + + void appendAttachmentHolder(const std::string &attachmentHolder); }; } // namespace database } // namespace network } // namespace comm diff --git a/services/backup/src/DatabaseEntities/LogItem.cpp b/services/backup/src/DatabaseEntities/LogItem.cpp index eee3e5654..eac84f5c2 100644 --- a/services/backup/src/DatabaseEntities/LogItem.cpp +++ b/services/backup/src/DatabaseEntities/LogItem.cpp @@ -1,109 +1,115 @@ #include "LogItem.h" #include "Constants.h" +#include "GlobalConstants.h" #include 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"; std::string LogItem::tableName = LOG_TABLE_NAME; LogItem::LogItem( const std::string backupID, const std::string logID, const bool persistedInBlob, const std::string value, - std::vector attachmentHolders) + std::string attachmentHolders) : backupID(backupID), logID(logID), persistedInBlob(persistedInBlob), value(value), attachmentHolders(attachmentHolders) { 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"); } if (!this->persistedInBlob && this->value.size() > 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"); } } 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.GetSS(); + this->attachmentHolders = attachmentsHolders->second.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::tableName; } 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::vector LogItem::getAttachmentHolders() const { +std::string LogItem::getAttachmentHolders() const { return this->attachmentHolders; } +void LogItem::appendAttachmentHolder(const std::string &attachmentHolder) { + this->attachmentHolders += attachmentHolder; + this->attachmentHolders += ATTACHMENT_DELIMITER; +} + } // namespace database } // namespace network } // namespace comm diff --git a/services/backup/src/DatabaseEntities/LogItem.h b/services/backup/src/DatabaseEntities/LogItem.h index cf24c3abd..78388bfb8 100644 --- a/services/backup/src/DatabaseEntities/LogItem.h +++ b/services/backup/src/DatabaseEntities/LogItem.h @@ -1,63 +1,64 @@ #pragma once #include "Item.h" #include -#include namespace comm { namespace network { namespace database { /* * log - a single log record * `backupID` - id of the backup that this log is assigned to * `value` - either the value itself which is a dump of a single operation (if * `persistedInBlob` is false) or the holder to blob (if `persistedInBlob` is * true) * `attachmentHolders` - this is a list of attachment references */ class LogItem : public Item { std::string backupID; std::string logID; bool persistedInBlob; std::string value; - std::vector attachmentHolders; + std::string attachmentHolders; void validate() const override; public: static std::string tableName; static const std::string FIELD_BACKUP_ID; static const std::string FIELD_LOG_ID; static const std::string FIELD_PERSISTED_IN_BLOB; static const std::string FIELD_VALUE; static const std::string FIELD_ATTACHMENT_HOLDERS; LogItem() { } LogItem( const std::string backupID, const std::string logID, const bool persistedInBlob, const std::string value, - std::vector attachmentHolders); + std::string attachmentHolders); LogItem(const AttributeValues &itemFromDB); void assignItemFromDatabase(const AttributeValues &itemFromDB) override; std::string getTableName() const override; PrimaryKeyDescriptor getPrimaryKeyDescriptor() const override; PrimaryKeyValue getPrimaryKeyValue() const override; std::string getBackupID() const; std::string getLogID() const; bool getPersistedInBlob() const; std::string getValue() const; - std::vector getAttachmentHolders() const; + std::string getAttachmentHolders() const; + + void appendAttachmentHolder(const std::string &attachmentHolder); }; } // namespace database } // namespace network } // namespace comm