mirror of
https://github.com/AleziaKurdis/overte.git
synced 2025-04-08 06:32:35 +02:00
Simplify BackupHandler pattern
This commit is contained in:
parent
84761c003d
commit
ce93b9a1f4
5 changed files with 22 additions and 65 deletions
|
@ -18,68 +18,22 @@
|
|||
|
||||
#include <quazip5/quazip.h>
|
||||
|
||||
class BackupHandler {
|
||||
class BackupHandlerInterface {
|
||||
public:
|
||||
template <typename T>
|
||||
BackupHandler(T* x) : _self(new Model<T>(x)) {}
|
||||
virtual ~BackupHandlerInterface() = default;
|
||||
|
||||
void loadBackup(QuaZip& zip) {
|
||||
_self->loadBackup(zip);
|
||||
}
|
||||
void createBackup(QuaZip& zip) {
|
||||
_self->createBackup(zip);
|
||||
}
|
||||
void recoverBackup(QuaZip& zip) {
|
||||
_self->recoverBackup(zip);
|
||||
}
|
||||
void deleteBackup(QuaZip& zip) {
|
||||
_self->deleteBackup(zip);
|
||||
}
|
||||
void consolidateBackup(QuaZip& zip) {
|
||||
_self->consolidateBackup(zip);
|
||||
}
|
||||
|
||||
private:
|
||||
struct Concept {
|
||||
virtual ~Concept() = default;
|
||||
|
||||
virtual void loadBackup(QuaZip& zip) = 0;
|
||||
virtual void createBackup(QuaZip& zip) = 0;
|
||||
virtual void recoverBackup(QuaZip& zip) = 0;
|
||||
virtual void deleteBackup(QuaZip& zip) = 0;
|
||||
virtual void consolidateBackup(QuaZip& zip) = 0;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct Model : Concept {
|
||||
Model(T* x) : data(x) {}
|
||||
|
||||
void loadBackup(QuaZip& zip) override {
|
||||
data->loadBackup(zip);
|
||||
}
|
||||
void createBackup(QuaZip& zip) override {
|
||||
data->createBackup(zip);
|
||||
}
|
||||
void recoverBackup(QuaZip& zip) override {
|
||||
data->recoverBackup(zip);
|
||||
}
|
||||
void deleteBackup(QuaZip& zip) override {
|
||||
data->deleteBackup(zip);
|
||||
}
|
||||
void consolidateBackup(QuaZip& zip) override {
|
||||
data->consolidateBackup(zip);
|
||||
}
|
||||
|
||||
std::unique_ptr<T> data;
|
||||
};
|
||||
|
||||
std::unique_ptr<Concept> _self;
|
||||
virtual void loadBackup(QuaZip& zip) = 0;
|
||||
virtual void createBackup(QuaZip& zip) = 0;
|
||||
virtual void recoverBackup(QuaZip& zip) = 0;
|
||||
virtual void deleteBackup(QuaZip& zip) = 0;
|
||||
virtual void consolidateBackup(QuaZip& zip) = 0;
|
||||
};
|
||||
using BackupHandlerPointer = std::unique_ptr<BackupHandlerInterface>;
|
||||
|
||||
#include <quazip5/quazipfile.h>
|
||||
#include <OctreeUtils.h>
|
||||
|
||||
class EntitiesBackupHandler {
|
||||
class EntitiesBackupHandler : public BackupHandlerInterface {
|
||||
public:
|
||||
EntitiesBackupHandler(QString entitiesFilePath, QString entitiesReplacementFilePath) :
|
||||
_entitiesFilePath(entitiesFilePath),
|
||||
|
|
|
@ -24,6 +24,8 @@
|
|||
|
||||
#include <ReceivedMessage.h>
|
||||
|
||||
#include "BackupHandler.h"
|
||||
|
||||
class QuaZip;
|
||||
|
||||
struct AssetServerBackup {
|
||||
|
@ -32,7 +34,7 @@ struct AssetServerBackup {
|
|||
bool corruptedBackup;
|
||||
};
|
||||
|
||||
class BackupSupervisor : public QObject {
|
||||
class BackupSupervisor : public QObject, public BackupHandlerInterface {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
|
|
@ -39,7 +39,8 @@ static const QString DATETIME_FORMAT { "yyyy-MM-dd_HH-mm-ss" };
|
|||
static const QString DATETIME_FORMAT_RE { "\\d{4}-\\d{2}-\\d{2}_\\d{2}-\\d{2}-\\d{2}" };
|
||||
static const QString AUTOMATIC_BACKUP_PREFIX { "autobackup-" };
|
||||
static const QString MANUAL_BACKUP_PREFIX { "backup-" };
|
||||
void DomainContentBackupManager::addBackupHandler(BackupHandler handler) {
|
||||
|
||||
void DomainContentBackupManager::addBackupHandler(BackupHandlerPointer handler) {
|
||||
_backupHandlers.push_back(std::move(handler));
|
||||
}
|
||||
|
||||
|
@ -238,7 +239,7 @@ void DomainContentBackupManager::recoverFromBackup(MiniPromise::Promise promise,
|
|||
success = false;
|
||||
} else {
|
||||
for (auto& handler : _backupHandlers) {
|
||||
handler.recoverBackup(zip);
|
||||
handler->recoverBackup(zip);
|
||||
}
|
||||
|
||||
qDebug() << "Successfully recovered from " << backupName;
|
||||
|
@ -339,7 +340,7 @@ void DomainContentBackupManager::load() {
|
|||
}
|
||||
|
||||
for (auto& handler : _backupHandlers) {
|
||||
handler.loadBackup(zip);
|
||||
handler->loadBackup(zip);
|
||||
}
|
||||
|
||||
zip.close();
|
||||
|
@ -402,7 +403,7 @@ void DomainContentBackupManager::consolidate(QString fileName) {
|
|||
}
|
||||
|
||||
for (auto& handler : _backupHandlers) {
|
||||
handler.consolidateBackup(zip);
|
||||
handler->consolidateBackup(zip);
|
||||
}
|
||||
|
||||
zip.close();
|
||||
|
@ -437,7 +438,7 @@ std::pair<bool, QString> DomainContentBackupManager::createBackup(const QString&
|
|||
}
|
||||
|
||||
for (auto& handler : _backupHandlers) {
|
||||
handler.createBackup(zip);
|
||||
handler->createBackup(zip);
|
||||
}
|
||||
|
||||
zip.close();
|
||||
|
|
|
@ -50,7 +50,7 @@ public:
|
|||
int persistInterval = DEFAULT_PERSIST_INTERVAL,
|
||||
bool debugTimestampNow = false);
|
||||
|
||||
void addBackupHandler(BackupHandler handler);
|
||||
void addBackupHandler(BackupHandlerPointer handler);
|
||||
std::vector<BackupItemInfo> getAllBackups();
|
||||
|
||||
void aboutToFinish(); /// call this to inform the persist thread that the owner is about to finish to support final persist
|
||||
|
@ -82,7 +82,7 @@ protected:
|
|||
|
||||
private:
|
||||
const QString _backupDirectory;
|
||||
std::vector<BackupHandler> _backupHandlers;
|
||||
std::vector<BackupHandlerPointer> _backupHandlers;
|
||||
int _persistInterval { 0 };
|
||||
|
||||
int64_t _lastCheck { 0 };
|
||||
|
|
|
@ -296,8 +296,8 @@ DomainServer::DomainServer(int argc, char* argv[]) :
|
|||
maybeHandleReplacementEntityFile();
|
||||
|
||||
_contentManager.reset(new DomainContentBackupManager(getContentBackupDir(), _settingsManager.settingsResponseObjectForType("6")["entity_server_settings"].toObject()));
|
||||
_contentManager->addBackupHandler(new EntitiesBackupHandler(getEntitiesFilePath(), getEntitiesReplacementFilePath()));
|
||||
_contentManager->addBackupHandler(new BackupSupervisor(getContentBackupDir()));
|
||||
_contentManager->addBackupHandler(BackupHandlerPointer(new EntitiesBackupHandler(getEntitiesFilePath(), getEntitiesReplacementFilePath())));
|
||||
_contentManager->addBackupHandler(BackupHandlerPointer(new BackupSupervisor(getContentBackupDir())));
|
||||
_contentManager->initialize(true);
|
||||
|
||||
qDebug() << "Existing backups:";
|
||||
|
|
Loading…
Reference in a new issue