Skip to content

Commit 86076ba

Browse files
committed
[rfile] set default compression to 505 and add SetCompressionSettings
1 parent 514eb02 commit 86076ba

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

io/io/inc/ROOT/RFile.hxx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#ifndef ROOT7_RFile
99
#define ROOT7_RFile
1010

11+
#include <Compression.h>
1112
#include <ROOT/RError.hxx>
1213

1314
#include <deque>
@@ -375,6 +376,13 @@ public:
375376

376377
/// Prints the internal structure of this RFile to the given stream.
377378
void Print(std::ostream &out = std::cout) const;
379+
380+
/// Sets the compression algorithm and level used for all subsequent objects stored in this ROOT file.
381+
/// See core/zip/inc/Compression.h for the meaning of the `compression` argument.
382+
/// Default compression is 505 (ZSTD level 10).
383+
void SetCompressionSettings(int compression);
384+
/// \see SetCompressionSettings(int compression)
385+
void SetCompressionSettings(RCompressionSetting::EAlgorithm::EValues algorithm, int level);
378386
};
379387

380388
} // namespace Experimental

io/io/src/RFile.cxx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "ROOT/RFile.hxx"
99

1010
#include <ROOT/StringUtils.hxx>
11+
#include <ROOT/RLogger.hxx>
1112
#include <ROOT/RError.hxx>
1213

1314
#include <Byteswap.h>
@@ -30,6 +31,8 @@ ROOT::RLogChannel &ROOT::Experimental::Internal::RFileLog()
3031
using ROOT::Experimental::RFile;
3132
using ROOT::Experimental::Internal::RFileLog;
3233

34+
static constexpr int kDefaultCompression = ROOT::RCompressionSetting::EDefaults::kUseGeneralPurpose;
35+
3336
namespace {
3437
enum class ENameCycleError {
3538
kNoError,
@@ -224,7 +227,8 @@ std::unique_ptr<RFile> RFile::Update(std::string_view path)
224227
std::unique_ptr<RFile> RFile::Recreate(std::string_view path)
225228
{
226229
TDirectory::TContext ctx(nullptr); // XXX: probably not thread safe?
227-
auto tfile = std::unique_ptr<TFile>(TFile::Open(std::string(path).c_str(), "RECREATE_WITHOUT_GLOBALREGISTRATION"));
230+
auto tfile = std::unique_ptr<TFile>(
231+
TFile::Open(std::string(path).c_str(), "RECREATE_WITHOUT_GLOBALREGISTRATION", "", kDefaultCompression));
228232
EnsureFileOpenAndBinary(tfile.get(), path);
229233

230234
auto rfile = std::unique_ptr<RFile>(new RFile(std::move(tfile)));
@@ -235,6 +239,20 @@ RFile::RFile(std::unique_ptr<TFile> file) : fFile(std::move(file)) {}
235239

236240
RFile::~RFile() = default;
237241

242+
void RFile::SetCompressionSettings(int compression)
243+
{
244+
if (fFile->IsWritable())
245+
fFile->SetCompressionSettings(compression);
246+
else
247+
R__LOG_WARNING(RFileLog()) << "Tried to change compression settings on a non-writable file: this has no effect.";
248+
}
249+
250+
void RFile::SetCompressionSettings(ROOT::RCompressionSetting::EAlgorithm::EValues algorithm, int level)
251+
{
252+
int settings = ROOT::CompressionSettings(algorithm, level);
253+
SetCompressionSettings(settings);
254+
}
255+
238256
TKey *RFile::GetTKey(std::string_view path) const
239257
{
240258
// In RFile, differently from TFile, when dealing with a path like "a/b/c", we always consider it to mean

io/io/test/rfile.cxx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,3 +810,18 @@ TEST(RFile, TTreeNoDoubleFree)
810810
// destructed once during writing, once during reading.
811811
EXPECT_EQ(TTreeDestructorCounter::GetTimesDestructed(), 2);
812812
}
813+
814+
TEST(RFile, Compression)
815+
{
816+
FileRaii fileGuard("test_rfile_compression.root");
817+
818+
{
819+
auto file = RFile::Recreate(fileGuard.GetPath());
820+
std::string s = "foo";
821+
file->Put("foo", s);
822+
}
823+
{
824+
auto file = std::unique_ptr<TFile>(TFile::Open(fileGuard.GetPath().c_str()));
825+
EXPECT_EQ(file->GetCompressionSettings(), ROOT::RCompressionSetting::EDefaults::kUseGeneralPurpose);
826+
}
827+
}

0 commit comments

Comments
 (0)