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 checksum);
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
81// clang-format off
82/**
83\class ROOT::Experimental::Internal::RNTupleFileWriter
84\ingroup NTuple
85\brief Write RNTuple data blocks in a TFile or a bare file container
86
87The writer can create a new TFile container for an RNTuple or add an RNTuple to an existing TFile.
88Creating a single RNTuple in a new TFile container can be done with a C file stream without a TFile class.
89Updating an existing TFile requires a proper TFile object. Also, writing a remote file requires a proper TFile object.
90A stand-alone version of RNTuple can remove the TFile based writer.
91*/
92// clang-format on
94private:
95 struct RFileProper {
96 TFile *fFile = nullptr;
97 /// Low-level writing using a TFile
98 void Write(const void *buffer, size_t nbytes, std::int64_t offset);
99 /// Writes an RBlob opaque key with the provided buffer as data record and returns the offset of the record
100 std::uint64_t WriteKey(const void *buffer, size_t nbytes, size_t len);
101 operator bool() const { return fFile; }
102 };
103
104 struct RFileSimple {
105 /// For the simplest cases, a C file stream can be used for writing
106 FILE *fFile = nullptr;
107 /// Keeps track of the seek offset
108 std::uint64_t fFilePos = 0;
109 /// Keeps track of the next key offset
110 std::uint64_t fKeyOffset = 0;
111 /// Keeps track of TFile control structures, which need to be updated on committing the data set
112 std::unique_ptr<ROOT::Experimental::Internal::RTFileControlBlock> fControlBlock;
113
114 RFileSimple() = default;
115 RFileSimple(const RFileSimple &other) = delete;
116 RFileSimple(RFileSimple &&other) = delete;
117 RFileSimple &operator =(const RFileSimple &other) = delete;
119 ~RFileSimple();
120
121 /// Writes bytes in the open stream, either at fFilePos or at the given offset
122 void Write(const void *buffer, size_t nbytes, std::int64_t offset = -1);
123 /// Writes a TKey including the data record, given by buffer, into fFile; returns the file offset to the payload.
124 /// The payload is already compressed
125 std::uint64_t WriteKey(const void *buffer, std::size_t nbytes, std::size_t len, std::int64_t offset = -1,
126 std::uint64_t directoryOffset = 100,
127 const std::string &className = "",
128 const std::string &objectName = "",
129 const std::string &title = "");
130 operator bool() const { return fFile; }
131 };
132
133 // TODO(jblomer): wrap in an std::variant with C++17
134 /// For updating existing files and for storing more than just an RNTuple in the file
136 /// For simple use cases, survives without libRIO dependency
138 /// A simple file can either be written as TFile container or as NTuple bare file
139 bool fIsBare = false;
140 /// The identifier of the RNTuple; A single writer object can only write a single RNTuple but multiple
141 /// writers can operate on the same file if (and only if) they use a proper TFile object for writing.
142 std::string fNTupleName;
143 /// The file name without parent directory; only required when writing with a C file stream
144 std::string fFileName;
145 /// Header and footer location of the ntuple, written on Commit()
147
148 explicit RNTupleFileWriter(std::string_view name);
149
150 /// For a TFile container written by a C file stream, write the header and TFile object
151 void WriteTFileSkeleton(int defaultCompression);
152 /// The only key that will be visible in file->ls()
153 void WriteTFileNTupleKey();
154 /// Write the TList with the RNTuple key
155 void WriteTFileKeysList();
156 /// Write the compressed streamer info record with the description of the RNTuple class
158 /// Last record in the file
159 void WriteTFileFreeList();
160 /// For a bare file, which is necessarily written by a C file stream, write file header
161 void WriteBareFileSkeleton(int defaultCompression);
162
163public:
164 /// For testing purposes, RNTuple data can be written into a bare file container instead of a ROOT file
165 enum class EContainerFormat {
166 kTFile, // ROOT TFile
167 kBare, // A thin envelope supporting a single RNTuple only
168 };
169
170 /// Create or truncate the local file given by path with the new empty RNTuple identified by ntupleName.
171 /// Uses a C stream for writing
172 static RNTupleFileWriter *Recreate(std::string_view ntupleName, std::string_view path, int defaultCompression,
173 EContainerFormat containerFormat);
174 /// Add a new RNTuple identified by ntupleName to the existing TFile.
175 static RNTupleFileWriter *Append(std::string_view ntupleName, TFile &file);
176
177 RNTupleFileWriter(const RNTupleFileWriter &other) = delete;
182
183 /// Writes the compressed header and registeres its location; lenHeader is the size of the uncompressed header.
184 std::uint64_t WriteNTupleHeader(const void *data, size_t nbytes, size_t lenHeader);
185 /// Writes the compressed footer and registeres its location; lenFooter is the size of the uncompressed footer.
186 std::uint64_t WriteNTupleFooter(const void *data, size_t nbytes, size_t lenFooter);
187 /// Writes a new record as an RBlob key into the file
188 std::uint64_t WriteBlob(const void *data, size_t nbytes, size_t len);
189 /// Reserves a new record as an RBlob key in the file.
190 std::uint64_t ReserveBlob(size_t nbytes, size_t len);
191 /// Write into a reserved record; the caller is responsible for making sure that the written byte range is in the
192 /// previously reserved key.
193 void WriteIntoReservedBlob(const void *buffer, size_t nbytes, std::int64_t offset);
194 /// Writes the RNTuple key to the file so that the header and footer keys can be found
195 void Commit();
196};
197
198} // namespace Internal
199} // namespace Experimental
200} // namespace ROOT
201
202#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
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 checksum)
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.
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:93
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 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 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
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 ...