From 7d83b94ee966984273f0806e1cf68a992c0ea35c Mon Sep 17 00:00:00 2001 From: Martijn Otto Date: Mon, 4 Apr 2016 09:52:56 +0200 Subject: [PATCH] Added the recompress option to the Yothalot\Output::flush method --- .gitignore | 1 + extension.cpp | 1 + output.h | 48 ++++++++++++++++++------------------------------ 3 files changed, 20 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index 9304a2b..3fd4edf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ *.so *.o +*.d *.sw* core vgcore.* diff --git a/extension.cpp b/extension.cpp index c1eccd9..6597ed4 100644 --- a/extension.cpp +++ b/extension.cpp @@ -152,6 +152,7 @@ extern "C" { Php::ByVal("value", Php::Type::Null) }).method("name", &Output::name, { }).method("flush", &Output::flush, { + Php::ByVal("recompress", Php::Type::Bool, false) }).method("size", &Output::size); // register input methods diff --git a/output.h b/output.h index 2cc2e08..23f3c1c 100644 --- a/output.h +++ b/output.h @@ -50,7 +50,7 @@ class Output : public Php::Base, private TupleHelper // read the params _name = params[0].stringValue(); _splitsize = (params.size() >= 2) ? params[1].numericValue() : 0; - + // prevent exceptions (C++ errors should not bubble up to PHP space) try { @@ -96,37 +96,25 @@ class Output : public Php::Base, private TupleHelper /** * Flush the output log - * @return Php::Value + * + * This will enforce that all bytes still in memory buffers are + * flushed to disk + * + * @note Recompressing may lead to a smaller filesize when previous + * flushes were done, but may be costly in terms of CPU and I/O + * + * @param params Array of parameters, the first (optional) parameter can for recompression + * @return PHP value wrapping the same object, for chaining */ - Php::Value flush() + Php::Value flush(Php::Parameters ¶ms) { - // prevent exceptions - try - { - // reconstruct the object - // @todo just call _impl->flush() (and fix this flush method in the Yothalot C++ library) - // @todo We should be able to actually flush the Output without closing it entirely etc - // Unfortunately this is not possible since the SLZ4 SplitCompressor hold internal - // state that cannot be flushed. - if (_splitsize > 0) - { - // The initial object needs to be destructed before we can - // construct the new object - _impl.reset(nullptr); - _impl.reset(new Yothalot::Output(_name.data(), _splitsize)); - } - else - { - // The initial object needs to be destructed before we can - // construct the new object - _impl.reset(nullptr); - _impl.reset(new Yothalot::Output(_name.data())); - } - } - catch (...) - { - // we ignore this error for now - } + // should we recompress? by default we don't do this, because + // this can be a pretty hefty operation, both in terms of CPU + // and in terms of I/O (the whole file is rewritten!) + bool recompress = !params.empty() && params[0]; + + // flush the file, optionally recompressing it + _impl->flush(recompress); // allow chaining return this;