mirror of
https://github.com/overte-org/overte.git
synced 2025-07-26 06:29:50 +02:00
Cache the local Entities file to avoid reading 2 or 3 times
This commit is contained in:
parent
ad9f7f3a1d
commit
e86d1691ce
4 changed files with 54 additions and 29 deletions
|
@ -18,30 +18,32 @@
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
|
|
||||||
// Reads octree file and parses it into a QJsonDocument. Handles both gzipped and non-gzipped files.
|
namespace {
|
||||||
// Returns true if the file was successfully opened and parsed, otherwise false.
|
// Reads octree file and parses it into a QJsonDocument. Handles both gzipped and non-gzipped files.
|
||||||
// Example failures: file does not exist, gzipped file cannot be unzipped, invalid JSON.
|
// Returns true if the file was successfully opened and parsed, otherwise false.
|
||||||
bool readOctreeFile(QString path, QJsonDocument* doc) {
|
// Example failures: file does not exist, gzipped file cannot be unzipped, invalid JSON.
|
||||||
QFile file(path);
|
bool readOctreeFile(QString path, QJsonDocument* doc) {
|
||||||
if (!file.open(QIODevice::ReadOnly)) {
|
QFile file(path);
|
||||||
qCritical() << "Cannot open json file for reading: " << path;
|
if (!file.open(QIODevice::ReadOnly)) {
|
||||||
return false;
|
qCritical() << "Cannot open json file for reading: " << path;
|
||||||
}
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
QByteArray data = file.readAll();
|
QByteArray data = file.readAll();
|
||||||
QByteArray jsonData;
|
QByteArray jsonData;
|
||||||
|
|
||||||
if (!gunzip(data, jsonData)) {
|
if (!gunzip(data, jsonData)) {
|
||||||
jsonData = data;
|
jsonData = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
QJsonParseError parserError;
|
QJsonParseError parserError;
|
||||||
*doc = QJsonDocument::fromJson(jsonData, &parserError);
|
*doc = QJsonDocument::fromJson(jsonData, &parserError);
|
||||||
if (parserError.error != QJsonParseError::NoError) {
|
if (parserError.error != QJsonParseError::NoError) {
|
||||||
qWarning() << "Error reading JSON file" << path << "-" << parserError.errorString();
|
qWarning() << "Error reading JSON file" << path << "-" << parserError.errorString();
|
||||||
|
}
|
||||||
|
return !doc->isNull();
|
||||||
}
|
}
|
||||||
return !doc->isNull();
|
} // Anon namespace.
|
||||||
}
|
|
||||||
|
|
||||||
bool OctreeUtils::RawOctreeData::readOctreeDataInfoFromJSON(QJsonObject root) {
|
bool OctreeUtils::RawOctreeData::readOctreeDataInfoFromJSON(QJsonObject root) {
|
||||||
if (root.contains("Id") && root.contains("DataVersion") && root.contains("Version")) {
|
if (root.contains("Id") && root.contains("DataVersion") && root.contains("Version")) {
|
||||||
|
|
|
@ -46,6 +46,7 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
class RawEntityData : public RawOctreeData {
|
class RawEntityData : public RawOctreeData {
|
||||||
|
public:
|
||||||
PacketType dataPacketType() const override;
|
PacketType dataPacketType() const override;
|
||||||
void readSubclassData(const QJsonObject& root) override;
|
void readSubclassData(const QJsonObject& root) override;
|
||||||
void writeSubclassData(QJsonObject& root) const override;
|
void writeSubclassData(QJsonObject& root) const override;
|
||||||
|
|
|
@ -31,6 +31,7 @@
|
||||||
#include <NumericalConstants.h>
|
#include <NumericalConstants.h>
|
||||||
#include <PerfStat.h>
|
#include <PerfStat.h>
|
||||||
#include <PathUtils.h>
|
#include <PathUtils.h>
|
||||||
|
#include <Gzip.h>
|
||||||
|
|
||||||
#include "OctreeLogging.h"
|
#include "OctreeLogging.h"
|
||||||
#include "OctreeUtils.h"
|
#include "OctreeUtils.h"
|
||||||
|
@ -72,14 +73,27 @@ void OctreePersistThread::start() {
|
||||||
|
|
||||||
OctreeUtils::RawOctreeData data;
|
OctreeUtils::RawOctreeData data;
|
||||||
qCDebug(octree) << "Reading octree data from" << _filename;
|
qCDebug(octree) << "Reading octree data from" << _filename;
|
||||||
if (data.readOctreeDataInfoFromFile(_filename)) {
|
QFile file(_filename);
|
||||||
qCDebug(octree) << "Current octree data: ID(" << data.id << ") DataVersion(" << data.version << ")";
|
if (file.open(QIODevice::ReadOnly)) {
|
||||||
packet->writePrimitive(true);
|
QByteArray jsonData(file.readAll());
|
||||||
auto id = data.id.toRfc4122();
|
file.close();
|
||||||
packet->write(id);
|
if (!gunzip(jsonData, _cachedJSONData)) {
|
||||||
packet->writePrimitive(data.version);
|
_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 {
|
} else {
|
||||||
qCWarning(octree) << "No octree data found";
|
qCWarning(octree) << "Couldn't access file" << _filename << file.errorString();
|
||||||
packet->writePrimitive(false);
|
packet->writePrimitive(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,6 +113,7 @@ void OctreePersistThread::handleOctreeDataFileReply(QSharedPointer<ReceivedMessa
|
||||||
OctreeUtils::RawOctreeData data;
|
OctreeUtils::RawOctreeData data;
|
||||||
bool hasValidOctreeData { false };
|
bool hasValidOctreeData { false };
|
||||||
if (includesNewData) {
|
if (includesNewData) {
|
||||||
|
_cachedJSONData.clear();
|
||||||
replacementData = message->readAll();
|
replacementData = message->readAll();
|
||||||
replaceData(replacementData);
|
replaceData(replacementData);
|
||||||
hasValidOctreeData = data.readOctreeDataInfoFromFile(_filename);
|
hasValidOctreeData = data.readOctreeDataInfoFromFile(_filename);
|
||||||
|
@ -108,7 +123,7 @@ void OctreePersistThread::handleOctreeDataFileReply(QSharedPointer<ReceivedMessa
|
||||||
|
|
||||||
OctreeUtils::RawEntityData data;
|
OctreeUtils::RawEntityData data;
|
||||||
qCDebug(octree) << "Reading octree data from" << _filename;
|
qCDebug(octree) << "Reading octree data from" << _filename;
|
||||||
if (data.readOctreeDataInfoFromFile(_filename)) {
|
if (data.readOctreeDataInfoFromData(_cachedJSONData)) {
|
||||||
hasValidOctreeData = true;
|
hasValidOctreeData = true;
|
||||||
if (data.id.isNull()) {
|
if (data.id.isNull()) {
|
||||||
qCDebug(octree) << "Current octree data has a null id, updating";
|
qCDebug(octree) << "Current octree data has a null id, updating";
|
||||||
|
@ -139,10 +154,16 @@ void OctreePersistThread::handleOctreeDataFileReply(QSharedPointer<ReceivedMessa
|
||||||
_tree->withWriteLock([&] {
|
_tree->withWriteLock([&] {
|
||||||
PerformanceWarning warn(true, "Loading Octree File", true);
|
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();
|
_tree->pruneTree();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
_cachedJSONData.clear();
|
||||||
quint64 loadDone = usecTimestampNow();
|
quint64 loadDone = usecTimestampNow();
|
||||||
_loadTimeUSecs = loadDone - loadStarted;
|
_loadTimeUSecs = loadDone - loadStarted;
|
||||||
|
|
||||||
|
|
|
@ -78,6 +78,7 @@ private:
|
||||||
quint64 _lastTimeDebug;
|
quint64 _lastTimeDebug;
|
||||||
|
|
||||||
QString _persistAsFileType;
|
QString _persistAsFileType;
|
||||||
|
QByteArray _cachedJSONData;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // hifi_OctreePersistThread_h
|
#endif // hifi_OctreePersistThread_h
|
||||||
|
|
Loading…
Reference in a new issue