diff --git a/services/backup/docker-server/contents/server/src/Constants.h b/services/backup/docker-server/contents/server/src/Constants.h index ac049429d..0d52ac443 100644 --- a/services/backup/docker-server/contents/server/src/Constants.h +++ b/services/backup/docker-server/contents/server/src/Constants.h @@ -1,32 +1,34 @@ #pragma once #include namespace comm { namespace network { // 4MB limit // WARNING: use keeping in mind that grpc adds its own headers to messages // https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md // so the message that actually is being sent over the network looks like this // [Compressed-Flag] [Message-Length] [Message] // [Compressed-Flag] 1 byte - added by grpc // [Message-Length] 4 bytes - added by grpc // [Message] N bytes - actual data // so for every message we get 5 additional bytes of data // as mentioned here // https://github.com/grpc/grpc/issues/15734#issuecomment-396962671 // grpc stream may contain more than one message const size_t GRPC_CHUNK_SIZE_LIMIT = 4 * 1024 * 1024; const size_t GRPC_METADATA_SIZE_PER_MESSAGE = 5; const std::string AWS_REGION = "us-east-2"; #ifdef COMM_TEST_SERVICES const std::string LOG_TABLE_NAME = "backup-service-log-test"; +const std::string BACKUP_TABLE_NAME = "backup-service-backup-test"; #else const std::string LOG_TABLE_NAME = "backup-service-log"; +const std::string BACKUP_TABLE_NAME = "backup-service-backup"; #endif } // namespace network } // namespace comm diff --git a/services/backup/docker-server/contents/server/src/DatabaseEntities/BackupItem.cpp b/services/backup/docker-server/contents/server/src/DatabaseEntities/BackupItem.cpp new file mode 100644 index 000000000..b5c6840f7 --- /dev/null +++ b/services/backup/docker-server/contents/server/src/DatabaseEntities/BackupItem.cpp @@ -0,0 +1,111 @@ +#include "BackupItem.h" + +#include "Constants.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) + : 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(); + } + } 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; +} + +std::string BackupItem::getPrimaryKey() const { + return BackupItem::FIELD_USER_ID; +} + +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 { + return this->attachmentHolders; +} + +} // namespace database +} // namespace network +} // namespace comm diff --git a/services/backup/docker-server/contents/server/src/DatabaseEntities/BackupItem.h b/services/backup/docker-server/contents/server/src/DatabaseEntities/BackupItem.h new file mode 100644 index 000000000..5fc157780 --- /dev/null +++ b/services/backup/docker-server/contents/server/src/DatabaseEntities/BackupItem.h @@ -0,0 +1,58 @@ +#pragma once + +#include "Item.h" + +#include +#include + +namespace comm { +namespace network { +namespace database { + +class BackupItem : public Item { + + std::string userID; + std::string backupID; + uint64_t created; + std::string recoveryData; + std::string compactionHolder; + std::vector 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); + BackupItem(const AttributeValues &itemFromDB); + + void assignItemFromDatabase(const AttributeValues &itemFromDB) override; + + std::string getTableName() const override; + std::string getPrimaryKey() 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; +}; + +} // namespace database +} // namespace network +} // namespace comm diff --git a/services/backup/docker-server/contents/server/src/DatabaseEntities/DatabaseEntitiesTools.h b/services/backup/docker-server/contents/server/src/DatabaseEntities/DatabaseEntitiesTools.h index ad0473960..49d6ad100 100644 --- a/services/backup/docker-server/contents/server/src/DatabaseEntities/DatabaseEntitiesTools.h +++ b/services/backup/docker-server/contents/server/src/DatabaseEntities/DatabaseEntitiesTools.h @@ -1,21 +1,22 @@ #pragma once #include "Item.h" +#include "BackupItem.h" #include "LogItem.h" #include #include namespace comm { namespace network { namespace database { template std::shared_ptr createItemByType() { static_assert(std::is_base_of::value, "T must inherit from Item"); return std::make_shared(); } } // namespace database } // namespace network } // namespace comm