Logo ROOT  
Reference Guide
TBufferMerger.cxx
Go to the documentation of this file.
1// @(#)root/io:$Id$
2// Author: Philippe Canal, Witold Pokorski, and Guilherme Amadio
3
4/*************************************************************************
5 * Copyright (C) 1995-2017, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
13
14#include "TBufferFile.h"
15#include "TError.h"
16#include "TROOT.h"
17#include "TVirtualMutex.h"
18
19#include <utility>
20
21namespace ROOT {
22namespace Experimental {
23
24TBufferMerger::TBufferMerger(const char *name, Option_t *option, Int_t compress)
25{
26 // We cannot chain constructors or use in-place initialization here because
27 // instantiating a TBufferMerger should not alter gDirectory's state.
29 Init(std::unique_ptr<TFile>(TFile::Open(name, option, /* title */ name, compress)));
30}
31
32TBufferMerger::TBufferMerger(std::unique_ptr<TFile> output)
33{
34 Init(std::move(output));
35}
36
37void TBufferMerger::Init(std::unique_ptr<TFile> output)
38{
39 if (!output || !output->IsWritable() || output->IsZombie())
40 Error("TBufferMerger", "cannot write to output file");
41
42 fMerger.OutputFile(std::move(output));
43}
44
46{
47 for (const auto &f : fAttachedFiles)
48 if (!f.expired()) Fatal("TBufferMerger", " TBufferMergerFiles must be destroyed before the server");
49
50 if (!fQueue.empty())
51 Merge();
52
53 // Since we support purely incremental merging, Merge does not write the target objects
54 // that are attached to the file (TTree and histograms) and thus we need to write them
55 // now.
56 if (TFile *out = fMerger.GetOutputFile())
57 out->Write("",TObject::kOverwrite);
58}
59
60std::shared_ptr<TBufferMergerFile> TBufferMerger::GetFile()
61{
63 std::shared_ptr<TBufferMergerFile> f(new TBufferMergerFile(*this));
64 gROOT->GetListOfFiles()->Remove(f.get());
65 fAttachedFiles.push_back(f);
66 return f;
67}
68
70{
71 std::lock_guard<std::mutex> lock(fQueueMutex);
72 return fQueue.size();
73}
74
76{
77 {
78 std::lock_guard<std::mutex> lock(fQueueMutex);
79 fBuffered += buffer->BufferSize();
80 fQueue.push(buffer);
81 }
82
83 if (fBuffered > fAutoSave)
84 Merge();
85}
86
88{
89 return fAutoSave;
90}
91
93{
94 return fMerger.GetMergeOptions();
95}
96
97
99{
100 fAutoSave = size;
101}
102
104{
105 fMerger.SetMergeOptions(options);
106}
107
109{
110 if (fMergeMutex.try_lock()) {
111 MergeImpl();
112 fMergeMutex.unlock();
113 }
114}
115
117{
118 std::queue<TBufferFile *> queue;
119 {
120 std::lock_guard<std::mutex> q(fQueueMutex);
121 std::swap(queue, fQueue);
122 fBuffered = 0;
123 }
124
125 while (!queue.empty()) {
126 std::unique_ptr<TBufferFile> buffer{queue.front()};
127 fMerger.AddAdoptFile(new TMemFile(fMerger.GetOutputFileName(), std::move(buffer)));
128 queue.pop();
129 }
130
132 fMerger.Reset();
133}
134
136{
137 if (fMergeMutex.try_lock()) {
138 memfile->WriteStreamerInfo();
139 fMerger.AddFile(memfile);
140 MergeImpl();
141 fMergeMutex.unlock();
142 return true;
143 } else
144 return false;
145}
146
147} // namespace Experimental
148} // namespace ROOT
#define f(i)
Definition: RSha256.hxx:104
int Int_t
Definition: RtypesCore.h:43
const char Option_t
Definition: RtypesCore.h:64
void Error(const char *location, const char *msgfmt,...)
void Fatal(const char *location, const char *msgfmt,...)
char name[80]
Definition: TGX11.cxx:109
float * q
Definition: THbookFile.cxx:87
R__EXTERN TVirtualMutex * gROOTMutex
Definition: TROOT.h:59
#define gROOT
Definition: TROOT.h:406
#define R__LOCKGUARD(mutex)
const char * GetMergeOptions()
Returns the current merge options.
bool TryMerge(TBufferMergerFile *memfile)
void Push(TBufferFile *buffer)
std::shared_ptr< TBufferMergerFile > GetFile()
Returns a TBufferMergerFile to which data can be written.
std::queue< TBufferFile * > fQueue
size_t GetQueueSize() const
Returns the number of buffers currently in the queue.
void SetMergeOptions(const TString &options)
Sets the merge options.
void Init(std::unique_ptr< TFile >)
virtual ~TBufferMerger()
Destructor.
size_t GetAutoSave() const
Returns the current value of the auto save setting in bytes (default = 0).
void SetAutoSave(size_t size)
By default, TBufferMerger will call TFileMerger::PartialMerge() for each buffer pushed onto its merge...
TBufferMerger()
TBufferMerger has no default constructor.
std::vector< std::weak_ptr< TBufferMergerFile > > fAttachedFiles
The concrete implementation of TBuffer for writing/reading to/from a ROOT file or socket.
Definition: TBufferFile.h:46
Int_t BufferSize() const
Definition: TBuffer.h:97
Small helper to keep current directory context.
Definition: TDirectory.h:47
virtual Bool_t OutputFile(const char *url, Bool_t force)
Open merger output file.
TFile * GetOutputFile() const
Definition: TFileMerger.h:94
virtual Bool_t AddFile(TFile *source, Bool_t own, Bool_t cpProgress)
Add the TFile to this file merger and give ownership of the TFile to this object (unless kFALSE is re...
const char * GetOutputFileName() const
Definition: TFileMerger.h:92
const char * GetMergeOptions()
Definition: TFileMerger.h:99
@ kAll
Merge all type of objects (default)
Definition: TFileMerger.h:78
@ kIncremental
Merge the input file with the content of the output file (if already exising).
Definition: TFileMerger.h:73
@ kKeepCompression
Keep compression level unchanged for each input files.
Definition: TFileMerger.h:83
@ kDelayWrite
Delay the TFile write (to reduce the number of write when reusing the file)
Definition: TFileMerger.h:76
void SetMergeOptions(const TString &options)
Definition: TFileMerger.h:100
virtual Bool_t PartialMerge(Int_t type=kAll|kIncremental)
Merge the files.
virtual void Reset()
Reset merger file list.
virtual Bool_t AddAdoptFile(TFile *source, Bool_t cpProgress=kTRUE)
Add the TFile to this file merger and give ownership of the TFile to this object (unless kFALSE is re...
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:53
virtual void WriteStreamerInfo()
Write the list of TStreamerInfo as a single object in this file The class Streamer description for al...
Definition: TFile.cxx:3651
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3942
A TMemFile is like a normal TFile except that it reads and writes only from memory.
Definition: TMemFile.h:19
@ kOverwrite
overwrite existing object with same name
Definition: TObject.h:88
Basic string class.
Definition: TString.h:131
void swap(RDirectoryEntry &e1, RDirectoryEntry &e2) noexcept
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
Definition: StringConv.hxx:21
static void output(int code)
Definition: gifencode.c:226