From 22db0c4b636f61a763435c6552a0ccdb6e8c0ffd Mon Sep 17 00:00:00 2001 From: Charles Otto Date: Sun, 1 Feb 2015 16:27:03 -0800 Subject: [PATCH] Use BlockCompression in loadstores and algorithm serialization --- openbr/core/core.cpp | 22 +++++++++++----------- openbr/plugins/meta.cpp | 34 ++++++++++++++++++++++++++-------- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/openbr/core/core.cpp b/openbr/core/core.cpp index 4d4bb01..a6f68bf 100644 --- a/openbr/core/core.cpp +++ b/openbr/core/core.cpp @@ -110,9 +110,11 @@ struct AlgorithmCore void store(const QString &model) const { - // Create stream - QByteArray data; - QDataStream out(&data, QFile::WriteOnly); + QtUtils::BlockCompression compressedWrite; + QFile outFile(model); + compressedWrite.setBasis(&outFile); + QDataStream out(&compressedWrite); + compressedWrite.open(QFile::WriteOnly); // Serialize algorithm to stream transform->serialize(out); @@ -131,18 +133,16 @@ struct AlgorithmCore if (mode == TransformCompare) comparison->serialize(out); - // Compress and save to file - QtUtils::writeFile(model, data, -1); + compressedWrite.close(); } void load(const QString &model) { - // Load from file and decompress - QByteArray data; - QtUtils::readFile(model, data, true); - - // Create stream - QDataStream in(&data, QFile::ReadOnly); + QtUtils::BlockCompression compressedRead; + QFile inFile(model); + compressedRead.setBasis(&inFile); + QDataStream in(&compressedRead); + compressedRead.open(QFile::ReadOnly); // Load algorithm transform = QSharedPointer(Transform::deserialize(in)); diff --git a/openbr/plugins/meta.cpp b/openbr/plugins/meta.cpp index df880d5..b441b7f 100644 --- a/openbr/plugins/meta.cpp +++ b/openbr/plugins/meta.cpp @@ -17,6 +17,8 @@ #include #include #include +#include + #include "openbr_internal.h" #include "openbr/core/common.h" #include "openbr/core/opencvutils.h" @@ -553,7 +555,7 @@ private: return transform->timeVarying(); } - void train(const TemplateList &data) + void train(const QList &data) { if (QFileInfo(getFileName()).exists()) return; @@ -561,11 +563,20 @@ private: transform->train(data); qDebug("Storing %s", qPrintable(baseName)); - QByteArray byteArray; - QDataStream stream(&byteArray, QFile::WriteOnly); - stream << transform->description(); + QtUtils::BlockCompression compressedOut; + QFile fout(baseName); + QtUtils::touchDir(fout); + compressedOut.setBasis(&fout); + + QDataStream stream(&compressedOut); + QString desc = transform->description(); + + if (!compressedOut.open(QFile::WriteOnly)) + qFatal("Failed to open %s for writing.", qPrintable(file)); + + stream << desc; transform->store(stream); - QtUtils::writeFile(baseName, byteArray, -1); + compressedOut.close(); } void project(const Template &src, Template &dst) const @@ -606,12 +617,19 @@ private: if (file.isEmpty()) return false; qDebug("Loading %s", qPrintable(file)); - QByteArray data; - QtUtils::readFile(file, data, true); - QDataStream stream(&data, QFile::ReadOnly); + QFile fin(file); + QtUtils::BlockCompression reader(&fin); + if (!reader.open(QIODevice::ReadOnly)) { + if (QFileInfo(file).exists()) qFatal("Unable to open %s for reading. Check file permissions.", qPrintable(file)); + else qFatal("Unable to open %s for reading. File does not exist.", qPrintable(file)); + } + + QDataStream stream(&reader); stream >> transformString; + transform = Transform::make(transformString); transform->load(stream); + return true; } }; -- libgit2 0.21.4