Cache the local Entities file to avoid reading 2 or 3 times

This commit is contained in:
Simon Walton 2018-10-12 17:26:46 -07:00
parent ad9f7f3a1d
commit e86d1691ce
4 changed files with 54 additions and 29 deletions

View file

@ -18,30 +18,32 @@
#include <QJsonDocument>
#include <QFile>
// Reads octree file and parses it into a QJsonDocument. Handles both gzipped and non-gzipped files.
// Returns true if the file was successfully opened and parsed, otherwise false.
// Example failures: file does not exist, gzipped file cannot be unzipped, invalid JSON.
bool readOctreeFile(QString path, QJsonDocument* doc) {
QFile file(path);
if (!file.open(QIODevice::ReadOnly)) {
qCritical() << "Cannot open json file for reading: " << path;
return false;
}
namespace {
// Reads octree file and parses it into a QJsonDocument. Handles both gzipped and non-gzipped files.
// Returns true if the file was successfully opened and parsed, otherwise false.
// Example failures: file does not exist, gzipped file cannot be unzipped, invalid JSON.
bool readOctreeFile(QString path, QJsonDocument* doc) {
QFile file(path);
if (!file.open(QIODevice::ReadOnly)) {
qCritical() << "Cannot open json file for reading: " << path;
return false;
}
QByteArray data = file.readAll();
QByteArray jsonData;
QByteArray data = file.readAll();
QByteArray jsonData;
if (!gunzip(data, jsonData)) {
jsonData = data;
}
if (!gunzip(data, jsonData)) {
jsonData = data;
}
QJsonParseError parserError;
*doc = QJsonDocument::fromJson(jsonData, &parserError);
if (parserError.error != QJsonParseError::NoError) {
qWarning() << "Error reading JSON file" << path << "-" << parserError.errorString();
QJsonParseError parserError;
*doc = QJsonDocument::fromJson(jsonData, &parserError);
if (parserError.error != QJsonParseError::NoError) {
qWarning() << "Error reading JSON file" << path << "-" << parserError.errorString();
}
return !doc->isNull();
}
return !doc->isNull();
}
} // Anon namespace.
bool OctreeUtils::RawOctreeData::readOctreeDataInfoFromJSON(QJsonObject root) {
if (root.contains("Id") && root.contains("DataVersion") && root.contains("Version")) {

View file

@ -46,6 +46,7 @@ public:
};
class RawEntityData : public RawOctreeData {
public:
PacketType dataPacketType() const override;
void readSubclassData(const QJsonObject& root) override;
void writeSubclassData(QJsonObject& root) const override;

View file

@ -31,6 +31,7 @@
#include <NumericalConstants.h>
#include <PerfStat.h>
#include <PathUtils.h>
#include <Gzip.h>
#include "OctreeLogging.h"
#include "OctreeUtils.h"
@ -72,14 +73,27 @@ void OctreePersistThread::start() {
OctreeUtils::RawOctreeData data;
qCDebug(octree) << "Reading octree data from" << _filename;
if (data.readOctreeDataInfoFromFile(_filename)) {
qCDebug(octree) << "Current octree data: ID(" << data.id << ") DataVersion(" << data.version << ")";
packet->writePrimitive(true);
auto id = data.id.toRfc4122();
packet->write(id);
packet->writePrimitive(data.version);
QFile file(_filename);
if (file.open(QIODevice::ReadOnly)) {
QByteArray jsonData(file.readAll());
file.close();
if (!gunzip(jsonData, _cachedJSONData)) {
_cachedJSONData = jsonData;
}
if (data.readOctreeDataInfoFromData(_cachedJSONData)) {
qCDebug(octree) << "Current octree data: ID(" << data.id << ") DataVersion(" << data.version << ")";
packet->writePrimitive(true);
auto id = data.id.toRfc4122();
packet->write(id);
packet->writePrimitive(data.version);
} else {
_cachedJSONData.clear();
qCWarning(octree) << "No octree data found";
packet->writePrimitive(false);
}
} else {
qCWarning(octree) << "No octree data found";
qCWarning(octree) << "Couldn't access file" << _filename << file.errorString();
packet->writePrimitive(false);
}
@ -99,6 +113,7 @@ void OctreePersistThread::handleOctreeDataFileReply(QSharedPointer<ReceivedMessa
OctreeUtils::RawOctreeData data;
bool hasValidOctreeData { false };
if (includesNewData) {
_cachedJSONData.clear();
replacementData = message->readAll();
replaceData(replacementData);
hasValidOctreeData = data.readOctreeDataInfoFromFile(_filename);
@ -108,7 +123,7 @@ void OctreePersistThread::handleOctreeDataFileReply(QSharedPointer<ReceivedMessa
OctreeUtils::RawEntityData data;
qCDebug(octree) << "Reading octree data from" << _filename;
if (data.readOctreeDataInfoFromFile(_filename)) {
if (data.readOctreeDataInfoFromData(_cachedJSONData)) {
hasValidOctreeData = true;
if (data.id.isNull()) {
qCDebug(octree) << "Current octree data has a null id, updating";
@ -139,10 +154,16 @@ void OctreePersistThread::handleOctreeDataFileReply(QSharedPointer<ReceivedMessa
_tree->withWriteLock([&] {
PerformanceWarning warn(true, "Loading Octree File", true);
persistentFileRead = _tree->readFromFile(_filename.toLocal8Bit().constData());
if (_cachedJSONData.isEmpty()) {
persistentFileRead = _tree->readFromFile(_filename.toLocal8Bit().constData());
} else {
QDataStream jsonStream(_cachedJSONData);
persistentFileRead = _tree->readFromStream(-1, QDataStream(_cachedJSONData));
}
_tree->pruneTree();
});
_cachedJSONData.clear();
quint64 loadDone = usecTimestampNow();
_loadTimeUSecs = loadDone - loadStarted;

View file

@ -78,6 +78,7 @@ private:
quint64 _lastTimeDebug;
QString _persistAsFileType;
QByteArray _cachedJSONData;
};
#endif // hifi_OctreePersistThread_h