Skip to content

Commit

Permalink
Added the recompress option to the Yothalot\Output::flush method
Browse files Browse the repository at this point in the history
  • Loading branch information
Martijn Otto committed Apr 4, 2016
1 parent 0cdf6f1 commit 7d83b94
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.so
*.o
*.d
*.sw*
core
vgcore.*
1 change: 1 addition & 0 deletions extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 18 additions & 30 deletions output.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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 &params)
{
// 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;
Expand Down

0 comments on commit 7d83b94

Please sign in to comment.