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