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>
21#include <string_view>
22
23#include <cstdint>
24#include <cstdio>
25#include <memory>
26#include <string>
27
28class TCollection;
29class TFile;
30class TFileMergeInfo;
31
32namespace ROOT {
33
34namespace Internal {
35class RRawFile;
36}
37
38namespace Experimental {
39
40namespace Internal {
41/// Holds status information of an open ROOT file during writing
42struct RTFileControlBlock;
43
44// clang-format off
45/**
46\class ROOT::Experimental::Internal::RMiniFileReader
47\ingroup NTuple
48\brief Read RNTuple data blocks from a TFile container, provided by a RRawFile
49
50A RRawFile is used for the byte access. The class implements a minimal subset of TFile, enough to extract
51RNTuple data keys.
52*/
53// clang-format on
55private:
56 /// The raw file used to read byte ranges
58 /// Indicates whether the file is a TFile container or an RNTuple bare file
59 bool fIsBare = false;
60 /// Used when the file container turns out to be a bare file
61 RResult<RNTuple> GetNTupleBare(std::string_view ntupleName);
62 /// Used when the file turns out to be a TFile container
63 RResult<RNTuple> GetNTupleProper(std::string_view ntupleName);
64
65 RNTuple CreateAnchor(std::uint16_t versionEpoch, std::uint16_t versionMajor, std::uint16_t versionMinor,
66 std::uint16_t versionPatch, std::uint64_t seekHeader, std::uint64_t nbytesHeader,
67 std::uint64_t lenHeader, std::uint64_t seekFooter, std::uint64_t nbytesFooter,
68 std::uint64_t lenFooter, std::uint64_t maxKeySize);
69
70public:
71 RMiniFileReader() = default;
72 /// Uses the given raw file to read byte ranges
74 /// Extracts header and footer location for the RNTuple identified by ntupleName
75 RResult<RNTuple> GetNTuple(std::string_view ntupleName);
76 /// Reads a given byte range from the file into the provided memory buffer
77 void ReadBuffer(void *buffer, size_t nbytes, std::uint64_t offset);
78};
79
80// clang-format off
81/**
82\class ROOT::Experimental::Internal::RNTupleFileWriter
83\ingroup NTuple
84\brief Write RNTuple data blocks in a TFile or a bare file container
85
86The writer can create a new TFile container for an RNTuple or add an RNTuple to an existing TFile.
87Creating a single RNTuple in a new TFile container can be done with a C file stream without a TFile class.
88Updating an existing TFile requires a proper TFile object. Also, writing a remote file requires a proper TFile object.
89A stand-alone version of RNTuple can remove the TFile based writer.
90*/
91// clang-format on
93private:
94 struct RFileProper {
95 TFile *fFile = nullptr;
96 /// Low-level writing using a TFile
97 void Write(const void *buffer, size_t nbytes, std::int64_t offset);
98 /// Writes an RBlob opaque key with the provided buffer as data record and returns the offset of the record
99 std::uint64_t WriteKey(const void *buffer, size_t nbytes, size_t len);
100 operator bool() const { return fFile; }
101 };
102
103 struct RFileSimple {
104 /// For the simplest cases, a C file stream can be used for writing
105 FILE *fFile = nullptr;
106 /// Keeps track of the seek offset
107 std::uint64_t fFilePos = 0;
108 /// Keeps track of the next key offset
109 std::uint64_t fKeyOffset = 0;
110 /// Keeps track of TFile control structures, which need to be updated on committing the data set
111 std::unique_ptr<ROOT::Experimental::Internal::RTFileControlBlock> fControlBlock;
112
113 RFileSimple() = default;
114 RFileSimple(const RFileSimple &other) = delete;
115 RFileSimple(RFileSimple &&other) = delete;
116 RFileSimple &operator=(const RFileSimple &other) = delete;
118 ~RFileSimple();
119
120 /// Writes bytes in the open stream, either at fFilePos or at the given offset
121 void Write(const void *buffer, size_t nbytes, std::int64_t offset = -1);
122 /// Writes a TKey including the data record, given by buffer, into fFile; returns the file offset to the payload.
123 /// The payload is already compressed
124 std::uint64_t WriteKey(const void *buffer, std::size_t nbytes, std::size_t len, std::int64_t offset = -1,
125 std::uint64_t directoryOffset = 100, const std::string &className = "",
126 const std::string &objectName = "", const std::string &title = "");
127 operator bool() const { return fFile; }
128 };
129
130 // TODO(jblomer): wrap in an std::variant with C++17
131 /// For updating existing files and for storing more than just an RNTuple in the file
133 /// For simple use cases, survives without libRIO dependency
135 /// A simple file can either be written as TFile container or as NTuple bare file
136 bool fIsBare = false;
137 /// The identifier of the RNTuple; A single writer object can only write a single RNTuple but multiple
138 /// writers can operate on the same file if (and only if) they use a proper TFile object for writing.
139 std::string fNTupleName;
140 /// The file name without parent directory; only required when writing with a C file stream
141 std::string fFileName;
142 /// Header and footer location of the ntuple, written on Commit()
144
145 explicit RNTupleFileWriter(std::string_view name);
146
147 /// For a TFile container written by a C file stream, write the header and TFile object
148 void WriteTFileSkeleton(int defaultCompression);
149 /// The only key that will be visible in file->ls()
150 void WriteTFileNTupleKey();
151 /// Write the TList with the RNTuple key
152 void WriteTFileKeysList();
153 /// Write the compressed streamer info record with the description of the RNTuple class
155 /// Last record in the file
156 void WriteTFileFreeList();
157 /// For a bare file, which is necessarily written by a C file stream, write file header
158 void WriteBareFileSkeleton(int defaultCompression);
159
160public:
161 /// For testing purposes, RNTuple data can be written into a bare file container instead of a ROOT file
162 enum class EContainerFormat {
163 kTFile, // ROOT TFile
164 kBare, // A thin envelope supporting a single RNTuple only
165 };
166
167 /// Create or truncate the local file given by path with the new empty RNTuple identified by ntupleName.
168 /// Uses a C stream for writing
169 static std::unique_ptr<RNTupleFileWriter> Recreate(std::string_view ntupleName, std::string_view path,
170 int defaultCompression, EContainerFormat containerFormat);
171 /// Add a new RNTuple identified by ntupleName to the existing TFile.
172 static std::unique_ptr<RNTupleFileWriter> Append(std::string_view ntupleName, TFile &file);
173
174 RNTupleFileWriter(const RNTupleFileWriter &other) = delete;
179
180 /// Writes the compressed header and registeres its location; lenHeader is the size of the uncompressed header.
181 std::uint64_t WriteNTupleHeader(const void *data, size_t nbytes, size_t lenHeader);
182 /// Writes the compressed footer and registeres its location; lenFooter is the size of the uncompressed footer.
183 std::uint64_t WriteNTupleFooter(const void *data, size_t nbytes, size_t lenFooter);
184 /// Writes a new record as an RBlob key into the file
185 std::uint64_t WriteBlob(const void *data, size_t nbytes, size_t len);
186 /// Reserves a new record as an RBlob key in the file.
187 std::uint64_t ReserveBlob(size_t nbytes, size_t len);
188 /// Write into a reserved record; the caller is responsible for making sure that the written byte range is in the
189 /// previously reserved key.
190 void WriteIntoReservedBlob(const void *buffer, size_t nbytes, std::int64_t offset);
191 /// Writes the RNTuple key to the file so that the header and footer keys can be found
192 void Commit();
193};
194
195} // namespace Internal
196} // namespace Experimental
197} // namespace ROOT
198
199#endif
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 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 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:54
bool fIsBare
Indicates whether the file is a TFile container or an RNTuple bare file.
Definition RMiniFile.hxx:59
RResult< RNTuple > GetNTuple(std::string_view ntupleName)
Extracts header and footer location for the RNTuple identified by ntupleName.
RResult< RNTuple > GetNTupleProper(std::string_view ntupleName)
Used when the file turns out to be a TFile container.
RNTuple CreateAnchor(std::uint16_t versionEpoch, std::uint16_t versionMajor, std::uint16_t versionMinor, std::uint16_t versionPatch, std::uint64_t seekHeader, std::uint64_t nbytesHeader, std::uint64_t lenHeader, std::uint64_t seekFooter, std::uint64_t nbytesFooter, std::uint64_t lenFooter, std::uint64_t maxKeySize)
ROOT::Internal::RRawFile * fRawFile
The raw file used to read byte ranges.
Definition RMiniFile.hxx:57
RResult< RNTuple > GetNTupleBare(std::string_view ntupleName)
Used when the file container turns out to be a bare file.
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.
Definition RMiniFile.hxx:92
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 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.
std::uint64_t ReserveBlob(size_t nbytes, size_t len)
Reserves a new record as an RBlob key in the file.
RFileSimple fFileSimple
For simple use cases, survives without libRIO dependency.
RNTupleFileWriter(const RNTupleFileWriter &other)=delete
static std::unique_ptr< RNTupleFileWriter > Append(std::string_view ntupleName, TFile &file)
Add a new RNTuple identified by ntupleName to the existing TFile.
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.
static std::unique_ptr< RNTupleFileWriter > Recreate(std::string_view ntupleName, std::string_view path, int defaultCompression, EContainerFormat containerFormat)
Create or truncate the local file given by path with the new empty RNTuple identified by ntupleName.
RNTupleFileWriter & operator=(const RNTupleFileWriter &other)=delete
bool fIsBare
A simple file can either be written as TFile container or as NTuple bare file.
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.
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...
Representation of an RNTuple data set in a ROOT file.
Definition RNTuple.hxx:61
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
Collection abstract base class.
Definition TCollection.h:65
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
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.
std::uint64_t WriteKey(const void *buffer, size_t nbytes, size_t len)
Writes an RBlob opaque key with the provided buffer as data record and returns the offset of the reco...
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.
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 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