Logo ROOT   6.10/09
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 
12 #include "ROOT/TBufferMerger.hxx"
13 
14 #include "TBufferFile.h"
15 #include "TError.h"
16 #include "TFileMerger.h"
17 #include "TROOT.h"
18 #include "TVirtualMutex.h"
19 
20 namespace ROOT {
21 namespace Experimental {
22 
23 TBufferMerger::TBufferMerger(const char *name, Option_t *option, Int_t compress)
24  : fName(name), fOption(option), fCompress(compress),
25  fMergingThread(new std::thread([&]() { this->WriteOutputFile(); }))
26 {
27 }
28 
30 {
31  for (auto f : fAttachedFiles)
32  if (!f.expired()) Fatal("TBufferMerger", " TBufferMergerFiles must be destroyed before the server");
33 
34  this->Push(nullptr);
35  fMergingThread->join();
36 }
37 
38 std::shared_ptr<TBufferMergerFile> TBufferMerger::GetFile()
39 {
41  std::shared_ptr<TBufferMergerFile> f(new TBufferMergerFile(*this));
42  gROOT->GetListOfFiles()->Remove(f.get());
43  fAttachedFiles.push_back(f);
44  return f;
45 }
46 
48 {
49  {
50  std::lock_guard<std::mutex> lock(fQueueMutex);
51  fQueue.push(buffer);
52  }
53  fDataAvailable.notify_one();
54 }
55 
57 {
59  std::unique_ptr<TMemFile> memfile;
60  std::unique_ptr<TBufferFile> buffer;
61  TFileMerger merger;
62 
63  merger.ResetBit(kMustCleanup);
64 
65  {
67  merger.OutputFile(fName.c_str(), fOption.c_str(), fCompress);
68  }
69 
70  while (true) {
71  std::unique_lock<std::mutex> lock(fQueueMutex);
72  fDataAvailable.wait(lock, [this]() { return !this->fQueue.empty(); });
73 
74  buffer.reset(fQueue.front());
75  fQueue.pop();
76  lock.unlock();
77 
78  if (!buffer) return;
79 
80  Long64_t length;
81  buffer->SetReadMode();
82  buffer->SetBufferOffset();
83  buffer->ReadLong64(length);
84 
85  {
87  {
89  memfile.reset(new TMemFile(fName.c_str(), buffer->Buffer() + buffer->Length(), length, "read"));
90  buffer->SetBufferOffset(buffer->Length() + length);
91  merger.AddFile(memfile.get(), false);
92  merger.PartialMerge();
93  }
94  merger.Reset();
95  }
96  }
97 }
98 
99 } // namespace Experimental
100 } // namespace ROOT
void SetBufferOffset(Int_t offset=0)
Definition: TBuffer.h:88
The concrete implementation of TBuffer for writing/reading to/from a ROOT file or socket...
Definition: TBufferFile.h:47
long long Long64_t
Definition: RtypesCore.h:69
std::vector< std::weak_ptr< TBufferMergerFile > > fAttachedFiles
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
void Fatal(const char *location, const char *msgfmt,...)
const char Option_t
Definition: RtypesCore.h:62
#define gROOT
Definition: TROOT.h:375
int Int_t
Definition: RtypesCore.h:41
R__EXTERN TVirtualMutex * gROOTMutex
Definition: TROOT.h:57
STL namespace.
void Push(TBufferFile *buffer)
A TMemFile is like a normal TFile except that it reads and writes only from memory.
Definition: TMemFile.h:17
Int_t Length() const
Definition: TBuffer.h:94
std::queue< TBufferFile * > fQueue
virtual void ReadLong64(Long64_t &l)
Definition: TBufferFile.h:483
char * Buffer() const
Definition: TBuffer.h:91
virtual Bool_t OutputFile(const char *url, Bool_t force)
Open merger output file.
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...
virtual ~TBufferMerger()
Destructor.
std::condition_variable fDataAvailable
This class provides file copy and merging services.
Definition: TFileMerger.h:24
void SetReadMode()
Set buffer in read mode.
Definition: TBuffer.cxx:271
#define R__LOCKGUARD2(mutex)
std::unique_ptr< std::thread > fMergingThread
double f(double x)
TBufferMerger()
TBufferMerger has no default constructor.
std::shared_ptr< TBufferMergerFile > GetFile()
Returns a TBufferMergerFile to which data can be written.
virtual Bool_t PartialMerge(Int_t type=kAll|kIncremental)
Merge the files.
void ResetBit(UInt_t f)
Definition: TObject.h:158
virtual void Reset()
Reset merger file list.