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
54std::shared_ptr<TBufferMergerFile> TBufferMerger::GetFile()
55{
57 std::shared_ptr<TBufferMergerFile> f(new TBufferMergerFile(*this));
58 gROOT->GetListOfFiles()->Remove(f.get());
59 fAttachedFiles.push_back(f);
60 return f;
61}
62
64{
65 return fQueue.size();
66}
67
69{
70 {
71 std::lock_guard<std::mutex> lock(fQueueMutex);
72 fBuffered += buffer->BufferSize();
73 fQueue.push(buffer);
74 }
75
76 if (fBuffered > fAutoSave)
77 Merge();
78}
79
81{
82 return fAutoSave;
83}
84
86{
87 return fMerger.GetMergeOptions();
88}
89
90
92{
93 fAutoSave = size;
94}
95
97{
98 fMerger.SetMergeOptions(options);
99}
100
102{
103 if (fMergeMutex.try_lock()) {
104 std::queue<TBufferFile *> queue;
105 {
106 std::lock_guard<std::mutex> q(fQueueMutex);
107 std::swap(queue, fQueue);
108 fBuffered = 0;
109 }
110
111 while (!queue.empty()) {
112 std::unique_ptr<TBufferFile> buffer{queue.front()};
113 fMerger.AddAdoptFile(new TMemFile(fMerger.GetOutputFileName(), std::move(buffer)));
114 queue.pop();
115 }
116
118 fMerger.Reset();
119 fMergeMutex.unlock();
120 }
121}
122
123} // namespace Experimental
124} // namespace ROOT
#define f(i)
Definition: RSha256.hxx:104
int Int_t
Definition: RtypesCore.h:41
const char Option_t
Definition: RtypesCore.h:62
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:415
#define R__LOCKGUARD(mutex)
const char * GetMergeOptions()
Returns the current merge options.
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:41
virtual Bool_t OutputFile(const char *url, Bool_t force)
Open merger output file.
const char * GetOutputFileName() const
Definition: TFileMerger.h:84
const char * GetMergeOptions()
Definition: TFileMerger.h:91
void SetMergeOptions(const TString &options)
Definition: TFileMerger.h:92
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...
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:3923
A TMemFile is like a normal TFile except that it reads and writes only from memory.
Definition: TMemFile.h:19
Basic string class.
Definition: TString.h:131
void swap(RDirectoryEntry &e1, RDirectoryEntry &e2) noexcept
VSD Structures.
Definition: StringConv.hxx:21
static void output(int code)
Definition: gifencode.c:226