Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 {
22
23TBufferMerger::TBufferMerger(const char *name, Option_t *option, Int_t compress)
24{
25 // We cannot chain constructors or use in-place initialization here because
26 // instantiating a TBufferMerger should not alter gDirectory's state.
28 Init(std::unique_ptr<TFile>(TFile::Open(name, option, /* title */ name, compress)));
29}
30
31TBufferMerger::TBufferMerger(std::unique_ptr<TFile> output)
32{
33 Init(std::move(output));
34}
35
36void TBufferMerger::Init(std::unique_ptr<TFile> output)
37{
38 if (!output || !output->IsWritable() || output->IsZombie())
39 Error("TBufferMerger", "cannot write to output file");
40
41 fMerger.OutputFile(std::move(output));
42}
43
45{
46 for (const auto &f : fAttachedFiles)
47 if (!f.expired()) Fatal("TBufferMerger", " TBufferMergerFiles must be destroyed before the server");
48
49 if (!fQueue.empty())
50 Merge();
51
52 // Since we support purely incremental merging, Merge does not write the target objects
53 // that are attached to the file (TTree and histograms) and thus we need to write them
54 // now.
55 if (TFile *out = fMerger.GetOutputFile())
56 out->Write("",TObject::kOverwrite);
57}
58
59std::shared_ptr<TBufferMergerFile> TBufferMerger::GetFile()
60{
62 std::shared_ptr<TBufferMergerFile> f(new TBufferMergerFile(*this));
63 gROOT->GetListOfFiles()->Remove(f.get());
64 fAttachedFiles.push_back(f);
65 return f;
66}
67
69{
70 std::lock_guard<std::mutex> lock(fQueueMutex);
71 return fQueue.size();
72}
73
75{
76 {
77 std::lock_guard<std::mutex> lock(fQueueMutex);
78 fBuffered += buffer->BufferSize();
79 fQueue.push(buffer);
80 }
81
82 if (fBuffered > fAutoSave)
83 Merge();
84}
85
87{
88 return fAutoSave;
89}
90
92{
93 return fMerger.GetMergeOptions();
94}
95
96
98{
100}
101
103{
104 fMerger.SetMergeOptions(options);
105}
106
108{
109 if (fMergeMutex.try_lock()) {
110 MergeImpl();
111 fMergeMutex.unlock();
112 }
113}
114
116{
117 std::queue<TBufferFile *> queue;
118 {
119 std::lock_guard<std::mutex> q(fQueueMutex);
120 std::swap(queue, fQueue);
121 fBuffered = 0;
122 }
123
124 while (!queue.empty()) {
125 std::unique_ptr<TBufferFile> buffer{queue.front()};
126 fMerger.AddAdoptFile(new TMemFile(fMerger.GetOutputFileName(), std::move(buffer)));
127 queue.pop();
128 }
129
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 ROOT
#define f(i)
Definition RSha256.hxx:104
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
const char Option_t
Definition RtypesCore.h:66
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:185
void Fatal(const char *location, const char *msgfmt,...)
Use this function in case of a fatal error. It will abort the program.
Definition TError.cxx:244
Option_t Option_t option
char name[80]
Definition TGX11.cxx:110
float * q
R__EXTERN TVirtualMutex * gROOTMutex
Definition TROOT.h:63
#define gROOT
Definition TROOT.h:407
#define R__LOCKGUARD(mutex)
A TBufferMergerFile is similar to a TMemFile, but when data is written to it, it is appended to the T...
size_t GetAutoSave() const
Returns the current value of the auto save setting in bytes (default = 0).
std::atomic< size_t > fBuffered
bool TryMerge(TBufferMergerFile *memfile)
std::vector< std::weak_ptr< TBufferMergerFile > > fAttachedFiles
void SetAutoSave(size_t size)
By default, TBufferMerger will call TFileMerger::PartialMerge() for each buffer pushed onto its merge...
friend class TBufferMergerFile
std::queue< TBufferFile * > fQueue
virtual ~TBufferMerger()
Destructor.
void Push(TBufferFile *buffer)
std::shared_ptr< TBufferMergerFile > GetFile()
Returns a TBufferMergerFile to which data can be written.
const char * GetMergeOptions()
Returns the current merge options.
TBufferMerger()
TBufferMerger has no default constructor.
void SetMergeOptions(const TString &options)
Sets the merge options.
void Init(std::unique_ptr< TFile >)
size_t GetQueueSize() const
Returns the number of buffers currently in the queue.
The concrete implementation of TBuffer for writing/reading to/from a ROOT file or socket.
Definition TBufferFile.h:47
Int_t BufferSize() const
Definition TBuffer.h:98
TDirectory::TContext keeps track and restore the current directory.
Definition TDirectory.h:89
virtual Bool_t OutputFile(const char *url, Bool_t force)
Open merger output file.
TFile * GetOutputFile() const
Definition TFileMerger.h:92
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:90
const char * GetMergeOptions()
Definition TFileMerger.h:97
@ kAll
Merge all type of objects (default)
Definition TFileMerger.h:76
@ kIncremental
Merge the input file with the content of the output file (if already existing).
Definition TFileMerger.h:71
@ kKeepCompression
Keep compression level unchanged for each input files.
Definition TFileMerger.h:81
@ kDelayWrite
Delay the TFile write (to reduce the number of write when reusing the file)
Definition TFileMerger.h:74
void SetMergeOptions(const TString &options)
Definition TFileMerger.h:98
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 composed of a header, followed by consecutive data records (TKey instances) with a wel...
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:3778
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:4075
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:92
Basic string class.
Definition TString.h:139
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
static void output()