Move EntitiesBackupHandler to its own file

This commit is contained in:
Atlante45 2018-02-14 18:11:55 -08:00
parent d8d05fe045
commit 9fca92facd
7 changed files with 124 additions and 81 deletions

View file

@ -21,13 +21,10 @@
#include <QJsonObject>
#include <AssetUtils.h>
#include <ReceivedMessage.h>
#include "BackupHandler.h"
class QuaZip;
class AssetsBackupHandler : public QObject, public BackupHandlerInterface {
Q_OBJECT

View file

@ -14,9 +14,7 @@
#include <memory>
#include <QDebug>
#include <quazip5/quazip.h>
class QuaZip;
class BackupHandlerInterface {
public:
@ -28,80 +26,7 @@ public:
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 : public BackupHandlerInterface {
public:
EntitiesBackupHandler(QString entitiesFilePath, QString entitiesReplacementFilePath) :
_entitiesFilePath(entitiesFilePath),
_entitiesReplacementFilePath(entitiesReplacementFilePath) {}
void loadBackup(QuaZip& zip) {}
// Create a skeleton backup
void createBackup(QuaZip& zip) {
QFile entitiesFile { _entitiesFilePath };
if (entitiesFile.open(QIODevice::ReadOnly)) {
QuaZipFile zipFile { &zip };
zipFile.open(QIODevice::WriteOnly, QuaZipNewInfo("models.json.gz", _entitiesFilePath));
zipFile.write(entitiesFile.readAll());
zipFile.close();
if (zipFile.getZipError() != UNZ_OK) {
qCritical() << "Failed to zip models.json.gz: " << zipFile.getZipError();
}
}
}
// Recover from a full backup
void recoverBackup(QuaZip& zip) {
if (!zip.setCurrentFile("models.json.gz")) {
qWarning() << "Failed to find models.json.gz while recovering backup";
return;
}
QuaZipFile zipFile { &zip };
if (!zipFile.open(QIODevice::ReadOnly)) {
qCritical() << "Failed to open models.json.gz in backup";
return;
}
auto rawData = zipFile.readAll();
zipFile.close();
OctreeUtils::RawOctreeData data;
if (!OctreeUtils::readOctreeDataInfoFromData(rawData, &data)) {
qCritical() << "Unable to parse octree data during backup recovery";
return;
}
data.resetIdAndVersion();
if (zipFile.getZipError() != UNZ_OK) {
qCritical() << "Failed to unzip models.json.gz: " << zipFile.getZipError();
return;
}
QFile entitiesFile { _entitiesReplacementFilePath };
if (entitiesFile.open(QIODevice::WriteOnly)) {
entitiesFile.write(data.toGzippedByteArray());
}
}
// Delete a skeleton backup
void deleteBackup(QuaZip& zip) {
}
// Create a full backup
void consolidateBackup(QuaZip& zip) {
}
private:
QString _entitiesFilePath;
QString _entitiesReplacementFilePath;
};
#endif /* hifi_BackupHandler_h */

View file

@ -9,6 +9,8 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include "DomainContentBackupManager.h"
#include <chrono>
#include <thread>
@ -25,13 +27,15 @@
#include <QJsonObject>
#include <QJsonDocument>
#include <quazip5/quazip.h>
#include <NumericalConstants.h>
#include <PerfStat.h>
#include <PathUtils.h>
#include <shared/QtHelpers.h>
#include "DomainServer.h"
#include "DomainContentBackupManager.h"
const int DomainContentBackupManager::DEFAULT_PERSIST_INTERVAL = 1000 * 30; // every 30 seconds
// Backup format looks like: daily_backup-TIMESTAMP.zip

View file

@ -16,6 +16,7 @@
#include <QString>
#include <QVector>
#include <QDateTime>
#include <GenericThread.h>

View file

@ -47,6 +47,7 @@
#include "AssetsBackupHandler.h"
#include "DomainServerNodeData.h"
#include "EntitiesBackupHandler.h"
#include "NodeConnectionData.h"
#include <Gzip.h>

View file

@ -0,0 +1,73 @@
//
// EntitiesBackupHandler.cpp
// domain-server/src
//
// Created by Clement Brisset on 2/14/18.
// Copyright 2018 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include "EntitiesBackupHandler.h"
#include <QDebug>
#include <quazip5/quazip.h>
#include <quazip5/quazipfile.h>
#include <OctreeUtils.h>
EntitiesBackupHandler::EntitiesBackupHandler(QString entitiesFilePath, QString entitiesReplacementFilePath) :
_entitiesFilePath(entitiesFilePath),
_entitiesReplacementFilePath(entitiesReplacementFilePath)
{
}
void EntitiesBackupHandler::createBackup(QuaZip& zip) {
QFile entitiesFile { _entitiesFilePath };
if (entitiesFile.open(QIODevice::ReadOnly)) {
QuaZipFile zipFile { &zip };
zipFile.open(QIODevice::WriteOnly, QuaZipNewInfo("models.json.gz", _entitiesFilePath));
zipFile.write(entitiesFile.readAll());
zipFile.close();
if (zipFile.getZipError() != UNZ_OK) {
qCritical() << "Failed to zip models.json.gz: " << zipFile.getZipError();
}
}
}
void EntitiesBackupHandler::recoverBackup(QuaZip& zip) {
if (!zip.setCurrentFile("models.json.gz")) {
qWarning() << "Failed to find models.json.gz while recovering backup";
return;
}
QuaZipFile zipFile { &zip };
if (!zipFile.open(QIODevice::ReadOnly)) {
qCritical() << "Failed to open models.json.gz in backup";
return;
}
auto rawData = zipFile.readAll();
zipFile.close();
OctreeUtils::RawOctreeData data;
if (!OctreeUtils::readOctreeDataInfoFromData(rawData, &data)) {
qCritical() << "Unable to parse octree data during backup recovery";
return;
}
data.resetIdAndVersion();
if (zipFile.getZipError() != UNZ_OK) {
qCritical() << "Failed to unzip models.json.gz: " << zipFile.getZipError();
return;
}
QFile entitiesFile { _entitiesReplacementFilePath };
if (entitiesFile.open(QIODevice::WriteOnly)) {
entitiesFile.write(data.toGzippedByteArray());
}
}

View file

@ -0,0 +1,42 @@
//
// EntitiesBackupHandler.h
// domain-server/src
//
// Created by Clement Brisset on 2/14/18.
// Copyright 2018 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#ifndef hifi_EntitiesBackupHandler_h
#define hifi_EntitiesBackupHandler_h
#include <QString>
#include "BackupHandler.h"
class EntitiesBackupHandler : public BackupHandlerInterface {
public:
EntitiesBackupHandler(QString entitiesFilePath, QString entitiesReplacementFilePath);
void loadBackup(QuaZip& zip) {}
// Create a skeleton backup
void createBackup(QuaZip& zip);
// Recover from a full backup
void recoverBackup(QuaZip& zip);
// Delete a skeleton backup
void deleteBackup(QuaZip& zip) {}
// Create a full backup
void consolidateBackup(QuaZip& zip) {}
private:
QString _entitiesFilePath;
QString _entitiesReplacementFilePath;
};
#endif /* hifi_EntitiesBackupHandler_h */