Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RMiniFile.hxx
Go to the documentation of this file.
1/// \file ROOT/RMiniFile.hxx
2/// \ingroup NTuple ROOT7
3/// \author Jakob Blomer <jblomer@cern.ch>
4/// \date 2019-12-22
5/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
6/// is welcome!
7
8/*************************************************************************
9 * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
10 * All rights reserved. *
11 * *
12 * For the licensing terms see $ROOTSYS/LICENSE. *
13 * For the list of contributors see $ROOTSYS/README/CREDITS. *
14 *************************************************************************/
15
16#ifndef ROOT7_RMiniFile
17#define ROOT7_RMiniFile
18
19#include <ROOT/RError.hxx>
20#include <ROOT/RNTuple.hxx>
22#include <ROOT/RSpan.hxx>
23#include <string_view>
24
25#include <cstdint>
26#include <cstdio>
27#include <memory>
28#include <string>
29
30class TDirectory;
31class TFileMergeInfo;
33
34namespace ROOT {
35
36namespace Internal {
37class RRawFile;
38}
39
40namespace Experimental {
41
42class RNTupleWriteOptions;
43
44namespace Internal {
45/// Holds status information of an open ROOT file during writing
46struct RTFileControlBlock;
47
48// clang-format off
49/**
50\class ROOT::Experimental::Internal::RMiniFileReader
51\ingroup NTuple
52\brief Read RNTuple data blocks from a TFile container, provided by a RRawFile
53
54A RRawFile is used for the byte access. The class implements a minimal subset of TFile, enough to extract
55RNTuple data keys.
56*/
57// clang-format on
59private:
60 /// The raw file used to read byte ranges
62 /// Indicates whether the file is a TFile container or an RNTuple bare file
63 bool fIsBare = false;
64 /// If `fMaxKeySize > 0` and ReadBuffer attempts to read `nbytes > maxKeySize`, it will assume the
65 /// blob being read is chunked and read all the chunks into the buffer. This is symmetrical to
66 /// what happens in `RNTupleFileWriter::WriteBlob()`.
67 std::uint64_t fMaxKeySize = 0;
68
69 /// Used when the file container turns out to be a bare file
70 RResult<RNTuple> GetNTupleBare(std::string_view ntupleName);
71 /// Used when the file turns out to be a TFile container. The ntuplePath variable is either the ntuple name
72 /// or an ntuple name preceded by a directory (`myNtuple` or `foo/bar/myNtuple` or `/foo/bar/myNtuple`)
73 RResult<RNTuple> GetNTupleProper(std::string_view ntuplePath);
74
75 /// Searches for a key with the given name and type in the key index of the directory starting at offsetDir.
76 /// The offset points to the start of the TDirectory DATA section, without the key and without the name and title
77 /// of the TFile record (the root directory).
78 /// Return 0 if the key was not found. Otherwise returns the offset of found key.
79 std::uint64_t SearchInDirectory(std::uint64_t &offsetDir, std::string_view keyName, std::string_view typeName);
80
81public:
82 RMiniFileReader() = default;
83 /// Uses the given raw file to read byte ranges
85 /// Extracts header and footer location for the RNTuple identified by ntupleName
86 RResult<RNTuple> GetNTuple(std::string_view ntupleName);
87 /// Reads a given byte range from the file into the provided memory buffer.
88 /// If `nbytes > fMaxKeySize` it will perform chunked read from multiple blobs,
89 /// whose addresses are listed at the end of the first chunk.
90 void ReadBuffer(void *buffer, size_t nbytes, std::uint64_t offset);
91
92 std::uint64_t GetMaxKeySize() const { return fMaxKeySize; }
93 /// If the reader is not used to retrieve the anchor, we need to set the max key size manually
94 void SetMaxKeySize(std::uint64_t maxKeySize) { fMaxKeySize = maxKeySize; }
95};
96
97// clang-format off
98/**
99\class ROOT::Experimental::Internal::RNTupleFileWriter
100\ingroup NTuple
101\brief Write RNTuple data blocks in a TFile or a bare file container
102
103The writer can create a new TFile container for an RNTuple or add an RNTuple to an existing TFile.
104Creating a single RNTuple in a new TFile container can be done with a C file stream without a TFile class.
105Updating an existing TFile requires a proper TFile object. Also, writing a remote file requires a proper TFile object.
106A stand-alone version of RNTuple can remove the TFile based writer.
107*/
108// clang-format on
110public:
111 /// The key length of a blob. It is always a big key (version > 1000) with class name RBlob.
112 static constexpr std::size_t kBlobKeyLen = 42;
113
114private:
115 struct RFileProper {
116 /// A sub directory in fFile or nullptr if the data is stored in the root directory of the file
118 /// Low-level writing using a TFile
119 void Write(const void *buffer, size_t nbytes, std::int64_t offset);
120 /// Reserves an RBlob opaque key as data record and returns the offset of the record. If keyBuffer is specified,
121 /// it must be written *before* the returned offset. (Note that the array type is purely documentation, the
122 /// argument is actually just a pointer.)
123 std::uint64_t ReserveBlobKey(size_t nbytes, size_t len, unsigned char keyBuffer[kBlobKeyLen] = nullptr);
124 operator bool() const { return fDirectory; }
125 };
126
127 struct RFileSimple {
128 /// Direct I/O requires that all buffers and write lengths are aligned. It seems 512 byte alignment is the minimum
129 /// for Direct I/O to work, but further testing showed that it results in worse performance than 4kB.
130 static constexpr int kBlockAlign = 4096;
131 /// During commit, WriteTFileKeysList() updates fNBytesKeys and fSeekKeys of the RTFFile located at
132 /// fSeekFileRecord. Given that the TFile key starts at offset 100 and the file name, which is written twice,
133 /// is shorter than 255 characters, we should need at most ~600 bytes. However, the header also needs to be
134 /// aligned to kBlockAlign...
135 static constexpr std::size_t kHeaderBlockSize = 4096;
136
137 // fHeaderBlock and fBlock are raw pointers because we have to manually call operator new and delete.
138 unsigned char *fHeaderBlock = nullptr;
139 std::size_t fBlockSize = 0;
140 std::uint64_t fBlockOffset = 0;
141 unsigned char *fBlock = nullptr;
142
143 /// For the simplest cases, a C file stream can be used for writing
144 FILE *fFile = nullptr;
145 /// Whether the C file stream has been opened with Direct I/O, introducing alignment requirements.
146 bool fDirectIO = false;
147 /// Keeps track of the seek offset
148 std::uint64_t fFilePos = 0;
149 /// Keeps track of the next key offset
150 std::uint64_t fKeyOffset = 0;
151 /// Keeps track of TFile control structures, which need to be updated on committing the data set
152 std::unique_ptr<ROOT::Experimental::Internal::RTFileControlBlock> fControlBlock;
153
155 RFileSimple(const RFileSimple &other) = delete;
156 RFileSimple(RFileSimple &&other) = delete;
157 RFileSimple &operator=(const RFileSimple &other) = delete;
159 ~RFileSimple();
160
161 void AllocateBuffers(std::size_t bufferSize);
162 void Flush();
163
164 /// Writes bytes in the open stream, either at fFilePos or at the given offset
165 void Write(const void *buffer, size_t nbytes, std::int64_t offset = -1);
166 /// Writes a TKey including the data record, given by buffer, into fFile; returns the file offset to the payload.
167 /// The payload is already compressed
168 std::uint64_t WriteKey(const void *buffer, std::size_t nbytes, std::size_t len, std::int64_t offset = -1,
169 std::uint64_t directoryOffset = 100, const std::string &className = "",
170 const std::string &objectName = "", const std::string &title = "");
171 /// Reserves an RBlob opaque key as data record and returns the offset of the record. If keyBuffer is specified,
172 /// it must be written *before* the returned offset. (Note that the array type is purely documentation, the
173 /// argument is actually just a pointer.)
174 std::uint64_t ReserveBlobKey(std::size_t nbytes, std::size_t len, unsigned char keyBuffer[kBlobKeyLen] = nullptr);
175 operator bool() const { return fFile; }
176 };
177
178 // TODO(jblomer): wrap in an std::variant with C++17
179 /// For updating existing files and for storing more than just an RNTuple in the file
181 /// For simple use cases, survives without libRIO dependency
183 /// A simple file can either be written as TFile container or as NTuple bare file
184 bool fIsBare = false;
185 /// The identifier of the RNTuple; A single writer object can only write a single RNTuple but multiple
186 /// writers can operate on the same file if (and only if) they use a proper TFile object for writing.
187 std::string fNTupleName;
188 /// The file name without parent directory; only required when writing with a C file stream
189 std::string fFileName;
190 /// Header and footer location of the ntuple, written on Commit()
192 /// Set of streamer info records that should be written to the file.
193 /// The RNTuple class description is always present.
195
196 explicit RNTupleFileWriter(std::string_view name, std::uint64_t maxKeySize);
197
198 /// For a TFile container written by a C file stream, write the header and TFile object
199 void WriteTFileSkeleton(int defaultCompression);
200 /// The only key that will be visible in file->ls()
201 void WriteTFileNTupleKey();
202 /// Write the TList with the RNTuple key
203 void WriteTFileKeysList();
204 /// Write the compressed streamer info record with the description of the RNTuple class
206 /// Last record in the file
207 void WriteTFileFreeList();
208 /// For a bare file, which is necessarily written by a C file stream, write file header
209 void WriteBareFileSkeleton(int defaultCompression);
210
211public:
212 /// For testing purposes, RNTuple data can be written into a bare file container instead of a ROOT file
213 enum class EContainerFormat {
214 kTFile, // ROOT TFile
215 kBare, // A thin envelope supporting a single RNTuple only
216 };
217
218 /// Create or truncate the local file given by path with the new empty RNTuple identified by ntupleName.
219 /// Uses a C stream for writing
220 static std::unique_ptr<RNTupleFileWriter> Recreate(std::string_view ntupleName, std::string_view path,
221 EContainerFormat containerFormat,
222 const RNTupleWriteOptions &options);
223 /// The directory parameter can also be a TFile object (TFile inherits from TDirectory).
224 static std::unique_ptr<RNTupleFileWriter>
225 Append(std::string_view ntupleName, TDirectory &fileOrDirectory, std::uint64_t maxKeySize);
226
227 RNTupleFileWriter(const RNTupleFileWriter &other) = delete;
232
233 /// Writes the compressed header and registeres its location; lenHeader is the size of the uncompressed header.
234 std::uint64_t WriteNTupleHeader(const void *data, size_t nbytes, size_t lenHeader);
235 /// Writes the compressed footer and registeres its location; lenFooter is the size of the uncompressed footer.
236 std::uint64_t WriteNTupleFooter(const void *data, size_t nbytes, size_t lenFooter);
237 /// Writes a new record as an RBlob key into the file
238 std::uint64_t WriteBlob(const void *data, size_t nbytes, size_t len);
239 /// Reserves a new record as an RBlob key in the file. If keyBuffer is specified, it must be written *before* the
240 /// returned offset. (Note that the array type is purely documentation, the argument is actually just a pointer.)
241 std::uint64_t ReserveBlob(size_t nbytes, size_t len, unsigned char keyBuffer[kBlobKeyLen] = nullptr);
242 /// Write into a reserved record; the caller is responsible for making sure that the written byte range is in the
243 /// previously reserved key.
244 void WriteIntoReservedBlob(const void *buffer, size_t nbytes, std::int64_t offset);
245 /// Ensures that the streamer info records passed as argument are written to the file
247 /// Writes the RNTuple key to the file so that the header and footer keys can be found
248 void Commit();
249};
250
251} // namespace Internal
252} // namespace Experimental
253} // namespace ROOT
254
255#endif
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
char name[80]
Definition TGX11.cxx:110
Read RNTuple data blocks from a TFile container, provided by a RRawFile.
Definition RMiniFile.hxx:58
bool fIsBare
Indicates whether the file is a TFile container or an RNTuple bare file.
Definition RMiniFile.hxx:63
void SetMaxKeySize(std::uint64_t maxKeySize)
If the reader is not used to retrieve the anchor, we need to set the max key size manually.
Definition RMiniFile.hxx:94
std::uint64_t SearchInDirectory(std::uint64_t &offsetDir, std::string_view keyName, std::string_view typeName)
Searches for a key with the given name and type in the key index of the directory starting at offsetD...
RResult< RNTuple > GetNTupleBare(std::string_view ntupleName)
Used when the file container turns out to be a bare file.
std::uint64_t fMaxKeySize
If fMaxKeySize > 0 and ReadBuffer attempts to read nbytes > maxKeySize, it will assume the blob being...
Definition RMiniFile.hxx:67
RResult< RNTuple > GetNTuple(std::string_view ntupleName)
Extracts header and footer location for the RNTuple identified by ntupleName.
ROOT::Internal::RRawFile * fRawFile
The raw file used to read byte ranges.
Definition RMiniFile.hxx:61
RResult< RNTuple > GetNTupleProper(std::string_view ntuplePath)
Used when the file turns out to be a TFile container.
void ReadBuffer(void *buffer, size_t nbytes, std::uint64_t offset)
Reads a given byte range from the file into the provided memory buffer.
Write RNTuple data blocks in a TFile or a bare file container.
std::uint64_t WriteBlob(const void *data, size_t nbytes, size_t len)
Writes a new record as an RBlob key into the file.
std::string fNTupleName
The identifier of the RNTuple; A single writer object can only write a single RNTuple but multiple wr...
std::string fFileName
The file name without parent directory; only required when writing with a C file stream.
std::uint64_t WriteNTupleFooter(const void *data, size_t nbytes, size_t lenFooter)
Writes the compressed footer and registeres its location; lenFooter is the size of the uncompressed f...
void WriteTFileKeysList()
Write the TList with the RNTuple key.
void UpdateStreamerInfos(const RNTupleSerializer::StreamerInfoMap_t &streamerInfos)
Ensures that the streamer info records passed as argument are written to the file.
void Commit()
Writes the RNTuple key to the file so that the header and footer keys can be found.
RFileProper fFileProper
For updating existing files and for storing more than just an RNTuple in the file.
RFileSimple fFileSimple
For simple use cases, survives without libRIO dependency.
RNTupleFileWriter(const RNTupleFileWriter &other)=delete
void WriteTFileNTupleKey()
The only key that will be visible in file->ls()
void WriteTFileFreeList()
Last record in the file.
EContainerFormat
For testing purposes, RNTuple data can be written into a bare file container instead of a ROOT file.
std::uint64_t ReserveBlob(size_t nbytes, size_t len, unsigned char keyBuffer[kBlobKeyLen]=nullptr)
Reserves a new record as an RBlob key in the file.
RNTupleFileWriter & operator=(const RNTupleFileWriter &other)=delete
bool fIsBare
A simple file can either be written as TFile container or as NTuple bare file.
static std::unique_ptr< RNTupleFileWriter > Recreate(std::string_view ntupleName, std::string_view path, EContainerFormat containerFormat, const RNTupleWriteOptions &options)
Create or truncate the local file given by path with the new empty RNTuple identified by ntupleName.
static std::unique_ptr< RNTupleFileWriter > Append(std::string_view ntupleName, TDirectory &fileOrDirectory, std::uint64_t maxKeySize)
The directory parameter can also be a TFile object (TFile inherits from TDirectory).
void WriteTFileSkeleton(int defaultCompression)
For a TFile container written by a C file stream, write the header and TFile object.
void WriteBareFileSkeleton(int defaultCompression)
For a bare file, which is necessarily written by a C file stream, write file header.
void WriteTFileStreamerInfo()
Write the compressed streamer info record with the description of the RNTuple class.
static constexpr std::size_t kBlobKeyLen
The key length of a blob. It is always a big key (version > 1000) with class name RBlob.
std::uint64_t WriteNTupleHeader(const void *data, size_t nbytes, size_t lenHeader)
Writes the compressed header and registeres its location; lenHeader is the size of the uncompressed h...
RNTuple fNTupleAnchor
Header and footer location of the ntuple, written on Commit()
RNTupleFileWriter(RNTupleFileWriter &&other)=delete
RNTupleFileWriter & operator=(RNTupleFileWriter &&other)=delete
void WriteIntoReservedBlob(const void *buffer, size_t nbytes, std::int64_t offset)
Write into a reserved record; the caller is responsible for making sure that the written byte range i...
RNTupleSerializer::StreamerInfoMap_t fStreamerInfoMap
Set of streamer info records that should be written to the file.
std::map< Int_t, TVirtualStreamerInfo * > StreamerInfoMap_t
Common user-tunable settings for storing ntuples.
The class is used as a return type for operations that can fail; wraps a value of type T or an RError...
Definition RError.hxx:194
The RRawFile provides read-only access to local and remote files.
Definition RRawFile.hxx:43
Representation of an RNTuple data set in a ROOT file.
Definition RNTuple.hxx:69
Describe directory structure in memory.
Definition TDirectory.h:45
Abstract Interface class describing Streamer information for one class.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
void Write(const void *buffer, size_t nbytes, std::int64_t offset)
Low-level writing using a TFile.
TDirectory * fDirectory
A sub directory in fFile or nullptr if the data is stored in the root directory of the file.
std::uint64_t ReserveBlobKey(size_t nbytes, size_t len, unsigned char keyBuffer[kBlobKeyLen]=nullptr)
Reserves an RBlob opaque key as data record and returns the offset of the record.
bool fDirectIO
Whether the C file stream has been opened with Direct I/O, introducing alignment requirements.
std::unique_ptr< ROOT::Experimental::Internal::RTFileControlBlock > fControlBlock
Keeps track of TFile control structures, which need to be updated on committing the data set.
FILE * fFile
For the simplest cases, a C file stream can be used for writing.
RFileSimple & operator=(const RFileSimple &other)=delete
std::uint64_t fKeyOffset
Keeps track of the next key offset.
static constexpr int kBlockAlign
Direct I/O requires that all buffers and write lengths are aligned.
void Write(const void *buffer, size_t nbytes, std::int64_t offset=-1)
Writes bytes in the open stream, either at fFilePos or at the given offset.
std::uint64_t ReserveBlobKey(std::size_t nbytes, std::size_t len, unsigned char keyBuffer[kBlobKeyLen]=nullptr)
Reserves an RBlob opaque key as data record and returns the offset of the record.
static constexpr std::size_t kHeaderBlockSize
During commit, WriteTFileKeysList() updates fNBytesKeys and fSeekKeys of the RTFFile located at fSeek...
std::uint64_t fFilePos
Keeps track of the seek offset.
std::uint64_t WriteKey(const void *buffer, std::size_t nbytes, std::size_t len, std::int64_t offset=-1, std::uint64_t directoryOffset=100, const std::string &className="", const std::string &objectName="", const std::string &title="")
Writes a TKey including the data record, given by buffer, into fFile; returns the file offset to the ...
RFileSimple & operator=(RFileSimple &&other)=delete