Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
RNTupleFillContext.hxx
Go to the documentation of this file.
1/// \file ROOT/RNTupleFillContext.hxx
2/// \author Jakob Blomer <jblomer@cern.ch>
3/// \date 2024-02-22
4
5/*************************************************************************
6 * Copyright (C) 1995-2024, Rene Brun and Fons Rademakers. *
7 * All rights reserved. *
8 * *
9 * For the licensing terms see $ROOTSYS/LICENSE. *
10 * For the list of contributors see $ROOTSYS/README/CREDITS. *
11 *************************************************************************/
12
13#ifndef ROOT_RNTupleFillContext
14#define ROOT_RNTupleFillContext
15
16#include <ROOT/RConfig.hxx> // for R__unlikely
17#include <ROOT/REntry.hxx>
18#include <ROOT/RError.hxx>
19#include <ROOT/RPageStorage.hxx>
23#include <ROOT/RNTupleModel.hxx>
24#include <ROOT/RNTupleTypes.hxx>
25
26#include <cstddef>
27#include <cstdint>
28#include <memory>
29#include <vector>
30
31namespace ROOT {
32
33namespace Experimental {
35}
36
37// clang-format off
38/**
39\class ROOT::RNTupleFillContext
40\ingroup NTuple
41\brief A context for filling entries (data) into clusters of an RNTuple
42
43An output cluster can be filled with entries. The caller has to make sure that the data that gets filled into a cluster
44is not modified for the time of the Fill() call. The fill call serializes the C++ object into the column format and
45writes data into the corresponding column page buffers. Writing of the buffers to storage is deferred and can be
46triggered by FlushCluster() or by destructing the context. On I/O errors, an exception is thrown.
47
48Instances of this class are not meant to be used in isolation and can be created from an RNTupleParallelWriter. For
49sequential writing, please refer to RNTupleWriter.
50*/
51// clang-format on
53 friend class ROOT::RNTupleWriter;
56
57private:
58 /// The page sink's parallel page compression scheduler if IMT is on.
59 /// Needs to be destructed after the page sink is destructed and so declared before.
60 std::unique_ptr<ROOT::Internal::RPageStorage::RTaskScheduler> fZipTasks;
61 std::unique_ptr<ROOT::Internal::RPageSink> fSink;
62 /// Needs to be destructed before fSink
63 std::unique_ptr<ROOT::RNTupleModel> fModel;
64
66
69 /// Keeps track of the number of bytes written into the current cluster
70 std::size_t fUnzippedClusterSize = 0;
71 /// The total number of bytes written to storage (i.e., after compression)
72 std::uint64_t fNBytesFlushed = 0;
73 /// The total number of bytes filled into all the so far committed clusters,
74 /// i.e. the uncompressed size of the written clusters
75 std::uint64_t fNBytesFilled = 0;
76 /// Limit for committing cluster no matter the other tunables
78 /// Estimator of uncompressed cluster size, taking into account the estimated compression ratio
80
81 /// Whether to enable staged cluster committing, where only an explicit call to CommitStagedClusters() will logically
82 /// append the clusters to the RNTuple.
84 /// Vector of currently staged clusters.
85 std::vector<ROOT::Internal::RPageSink::RStagedCluster> fStagedClusters;
86
87 template <typename Entry>
88 void FillNoFlushImpl(Entry &entry, ROOT::RNTupleFillStatus &status)
89 {
90 if (R__unlikely(entry.GetModelId() != fModel->GetModelId()))
91 throw RException(R__FAIL("mismatch between entry and model"));
92
93 const std::size_t bytesWritten = entry.Append();
94 fUnzippedClusterSize += bytesWritten;
95 fNEntries++;
96
99 status.fLastEntrySize = bytesWritten;
100 status.fShouldFlushCluster =
102 }
103 template <typename Entry>
104 std::size_t FillImpl(Entry &entry)
105 {
107 FillNoFlushImpl(entry, status);
108 if (status.ShouldFlushCluster())
109 FlushCluster();
110 return status.GetLastEntrySize();
111 }
112
113 RNTupleFillContext(std::unique_ptr<ROOT::RNTupleModel> model, std::unique_ptr<ROOT::Internal::RPageSink> sink);
118
119public:
121
122 /// Fill an entry into this context, but don't commit the cluster. The calling code must pass an RNTupleFillStatus
123 /// and check RNTupleFillStatus::ShouldFlushCluster.
124 ///
125 /// This method will check the entry's model ID to ensure it comes from the context's own model or throw an exception
126 /// otherwise.
127 void FillNoFlush(ROOT::REntry &entry, ROOT::RNTupleFillStatus &status) { FillNoFlushImpl(entry, status); }
128 /// Fill an entry into this context. This method will check the entry's model ID to ensure it comes from the
129 /// context's own model or throw an exception otherwise.
130 /// \return The number of uncompressed bytes written.
131 std::size_t Fill(ROOT::REntry &entry) { return FillImpl(entry); }
132
133 /// Fill an RRawPtrWriteEntry into this context, but don't commit the cluster. The calling code must pass an
134 /// RNTupleFillStatus and check RNTupleFillStatus::ShouldFlushCluster.
135 ///
136 /// This method will check the entry's model ID to ensure it comes from the context's own model or throw an exception
137 /// otherwise.
139 {
140 FillNoFlushImpl(entry, status);
141 }
142 /// Fill an RRawPtrWriteEntry into this context. This method will check the entry's model ID to ensure it comes
143 /// from the context's own model or throw an exception otherwise.
144 /// \return The number of uncompressed bytes written.
145 std::size_t Fill(ROOT::Detail::RRawPtrWriteEntry &entry) { return FillImpl(entry); }
146
147 /// Flush column data, preparing for CommitCluster or to reduce memory usage. This will trigger compression of pages,
148 /// but not actually write to storage.
149 void FlushColumns();
150 /// Flush so far filled entries to storage
151 void FlushCluster();
152 /// Logically append staged clusters to the RNTuple.
154
155 const ROOT::RNTupleModel &GetModel() const { return *fModel; }
156 std::unique_ptr<ROOT::REntry> CreateEntry() const { return fModel->CreateEntry(); }
157 std::unique_ptr<ROOT::Detail::RRawPtrWriteEntry> CreateRawPtrWriteEntry() const
158 {
159 return fModel->CreateRawPtrWriteEntry();
160 }
161
162 /// Return the entry number that was last flushed in a cluster.
164 /// Return the number of entries filled so far.
166
167 void EnableStagedClusterCommitting(bool val = true)
168 {
169 if (!val && !fStagedClusters.empty()) {
170 throw RException(R__FAIL("cannot disable staged committing with pending clusters"));
171 }
173 }
175
176 void EnableMetrics() { fMetrics.Enable(); }
178};
179
180} // namespace ROOT
181
182#endif // ROOT_RNTupleFillContext
#define R__unlikely(expr)
Definition RConfig.hxx:586
#define R__FAIL(msg)
Short-hand to return an RResult<T> in an error state; the RError is implicitly converted into RResult...
Definition RError.hxx:299
A container of const raw pointers, corresponding to a row in the data set.
A collection of Counter objects with a name, a unit, and a description.
Class used to write an RNTupleAttrSet in the context of an RNTupleWriter.
The REntry is a collection of values in an RNTuple corresponding to a complete row in the data set.
Definition REntry.hxx:54
Base class for all ROOT issued exceptions.
Definition RError.hxx:78
void FlushCluster()
Flush so far filled entries to storage.
ROOT::NTupleSize_t fLastFlushed
void FillNoFlushImpl(Entry &entry, ROOT::RNTupleFillStatus &status)
std::size_t fUnzippedClusterSizeEst
Estimator of uncompressed cluster size, taking into account the estimated compression ratio.
std::size_t Fill(ROOT::Detail::RRawPtrWriteEntry &entry)
Fill an RRawPtrWriteEntry into this context.
std::uint64_t fNBytesFilled
The total number of bytes filled into all the so far committed clusters, i.e.
void EnableStagedClusterCommitting(bool val=true)
ROOT::NTupleSize_t GetLastFlushed() const
Return the entry number that was last flushed in a cluster.
Experimental::Detail::RNTupleMetrics fMetrics
bool IsStagedClusterCommittingEnabled() const
ROOT::NTupleSize_t GetNEntries() const
Return the number of entries filled so far.
std::uint64_t fNBytesFlushed
The total number of bytes written to storage (i.e., after compression).
std::size_t FillImpl(Entry &entry)
std::vector< ROOT::Internal::RPageSink::RStagedCluster > fStagedClusters
Vector of currently staged clusters.
void FlushColumns()
Flush column data, preparing for CommitCluster or to reduce memory usage.
std::unique_ptr< ROOT::REntry > CreateEntry() const
RNTupleFillContext(std::unique_ptr< ROOT::RNTupleModel > model, std::unique_ptr< ROOT::Internal::RPageSink > sink)
std::size_t fUnzippedClusterSize
Keeps track of the number of bytes written into the current cluster.
std::unique_ptr< ROOT::Detail::RRawPtrWriteEntry > CreateRawPtrWriteEntry() const
const ROOT::RNTupleModel & GetModel() const
RNTupleFillContext & operator=(const RNTupleFillContext &)=delete
const Experimental::Detail::RNTupleMetrics & GetMetrics() const
void FillNoFlush(ROOT::Detail::RRawPtrWriteEntry &entry, ROOT::RNTupleFillStatus &status)
Fill an RRawPtrWriteEntry into this context, but don't commit the cluster.
RNTupleFillContext(RNTupleFillContext &&)=delete
void CommitStagedClusters()
Logically append staged clusters to the RNTuple.
RNTupleFillContext(const RNTupleFillContext &)=delete
RNTupleFillContext & operator=(RNTupleFillContext &&)=delete
bool fStagedClusterCommitting
Whether to enable staged cluster committing, where only an explicit call to CommitStagedClusters() wi...
std::unique_ptr< ROOT::Internal::RPageStorage::RTaskScheduler > fZipTasks
The page sink's parallel page compression scheduler if IMT is on.
std::unique_ptr< ROOT::RNTupleModel > fModel
Needs to be destructed before fSink.
std::unique_ptr< ROOT::Internal::RPageSink > fSink
std::size_t fMaxUnzippedClusterSize
Limit for committing cluster no matter the other tunables.
void FillNoFlush(ROOT::REntry &entry, ROOT::RNTupleFillStatus &status)
Fill an entry into this context, but don't commit the cluster.
std::size_t Fill(ROOT::REntry &entry)
Fill an entry into this context.
A status object after filling an entry.
ROOT::NTupleSize_t fNEntriesSinceLastFlush
Number of entries written into the current cluster.
std::size_t fUnzippedClusterSize
Number of bytes written into the current cluster.
std::size_t fLastEntrySize
Number of bytes written for the last entry.
bool ShouldFlushCluster() const
Return true if the caller should call FlushCluster.
std::size_t GetLastEntrySize() const
Return the number of bytes for the last entry.
The RNTupleModel encapulates the schema of an RNTuple.
An RNTuple that gets filled with entries (data) and writes them to storage.
Namespace for ROOT features in testing.
Definition TROOT.h:100
std::uint64_t NTupleSize_t
Integer type long enough to hold the maximum number of entries in a column.