Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RNTupleFillContext.hxx
Go to the documentation of this file.
1/// \file ROOT/RNTupleFillContext.hxx
2/// \ingroup NTuple
3/// \author Jakob Blomer <jblomer@cern.ch>
4/// \date 2024-02-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-2024, 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 ROOT_RNTupleFillContext
17#define ROOT_RNTupleFillContext
18
19#include <ROOT/RConfig.hxx> // for R__unlikely
20#include <ROOT/REntry.hxx>
21#include <ROOT/RError.hxx>
22#include <ROOT/RPageStorage.hxx>
26#include <ROOT/RNTupleModel.hxx>
27#include <ROOT/RNTupleUtil.hxx>
28
29#include <cstddef>
30#include <cstdint>
31#include <memory>
32#include <vector>
33
34namespace ROOT {
35namespace Experimental {
36
37// clang-format off
38/**
39\class ROOT::Experimental::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;
55
56private:
57 std::unique_ptr<ROOT::Internal::RPageSink> fSink;
58 /// Needs to be destructed before fSink
59 std::unique_ptr<ROOT::RNTupleModel> fModel;
60
62
65 /// Keeps track of the number of bytes written into the current cluster
66 std::size_t fUnzippedClusterSize = 0;
67 /// The total number of bytes written to storage (i.e., after compression)
68 std::uint64_t fNBytesFlushed = 0;
69 /// The total number of bytes filled into all the so far committed clusters,
70 /// i.e. the uncompressed size of the written clusters
71 std::uint64_t fNBytesFilled = 0;
72 /// Limit for committing cluster no matter the other tunables
74 /// Estimator of uncompressed cluster size, taking into account the estimated compression ratio
76
77 /// Whether to enable staged cluster committing, where only an explicit call to CommitStagedClusters() will logically
78 /// append the clusters to the RNTuple.
80 /// Vector of currently staged clusters.
81 std::vector<ROOT::Internal::RPageSink::RStagedCluster> fStagedClusters;
82
83 template <typename Entry>
85 {
86 if (R__unlikely(entry.GetModelId() != fModel->GetModelId()))
87 throw RException(R__FAIL("mismatch between entry and model"));
88
89 const std::size_t bytesWritten = entry.Append();
91 fNEntries++;
92
96 status.fShouldFlushCluster =
98 }
99 template <typename Entry>
100 std::size_t FillImpl(Entry &entry)
101 {
103 FillNoFlush(entry, status);
104 if (status.ShouldFlushCluster())
105 FlushCluster();
106 return status.GetLastEntrySize();
107 }
108
109 RNTupleFillContext(std::unique_ptr<ROOT::RNTupleModel> model, std::unique_ptr<ROOT::Internal::RPageSink> sink);
112
113public:
115
116 /// Fill an entry into this context, but don't commit the cluster. The calling code must pass an RNTupleFillStatus
117 /// and check RNTupleFillStatus::ShouldFlushCluster.
118 ///
119 /// This method will check the entry's model ID to ensure it comes from the context's own model or throw an exception
120 /// otherwise.
122 /// Fill an entry into this context. This method will check the entry's model ID to ensure it comes from the
123 /// context's own model or throw an exception otherwise.
124 /// \return The number of uncompressed bytes written.
125 std::size_t Fill(ROOT::REntry &entry) { return FillImpl(entry); }
126
127 /// Fill an RRawPtrWriteEntry into this context, but don't commit the cluster. The calling code must pass an
128 /// RNTupleFillStatus and check RNTupleFillStatus::ShouldFlushCluster.
129 ///
130 /// This method will check the entry's model ID to ensure it comes from the context's own model or throw an exception
131 /// otherwise.
136 /// Fill an RRawPtrWriteEntry into this context. This method will check the entry's model ID to ensure it comes
137 /// from the context's own model or throw an exception otherwise.
138 /// \return The number of uncompressed bytes written.
140
141 /// Flush column data, preparing for CommitCluster or to reduce memory usage. This will trigger compression of pages,
142 /// but not actually write to storage.
143 void FlushColumns();
144 /// Flush so far filled entries to storage
145 void FlushCluster();
146 /// Logically append staged clusters to the RNTuple.
148
149 const ROOT::RNTupleModel &GetModel() const { return *fModel; }
150 std::unique_ptr<ROOT::REntry> CreateEntry() const { return fModel->CreateEntry(); }
151 std::unique_ptr<Detail::RRawPtrWriteEntry> CreateRawPtrWriteEntry() const
152 {
153 return fModel->CreateRawPtrWriteEntry();
154 }
155
156 /// Return the entry number that was last flushed in a cluster.
158 /// Return the number of entries filled so far.
160
161 void EnableStagedClusterCommitting(bool val = true)
162 {
163 if (!val && !fStagedClusters.empty()) {
164 throw RException(R__FAIL("cannot disable staged committing with pending clusters"));
165 }
167 }
169
171 const Detail::RNTupleMetrics &GetMetrics() const { return fMetrics; }
172}; // class RNTupleFillContext
173
174} // namespace Experimental
175} // namespace ROOT
176
177#endif // ROOT_RNTupleFillContext
#define R__unlikely(expr)
Definition RConfig.hxx:602
#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
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
A collection of Counter objects with a name, a unit, and a description.
A container of const raw pointers, corresponding to a row in the data set.
A context for filling entries (data) into clusters of an RNTuple.
ROOT::NTupleSize_t GetNEntries() const
Return the number of entries filled so far.
std::size_t fUnzippedClusterSize
Keeps track of the number of bytes written into the current cluster.
const ROOT::RNTupleModel & GetModel() const
std::unique_ptr< ROOT::Internal::RPageSink > fSink
void FillNoFlush(ROOT::REntry &entry, ROOT::RNTupleFillStatus &status)
Fill an entry into this context, but don't commit the cluster.
bool fStagedClusterCommitting
Whether to enable staged cluster committing, where only an explicit call to CommitStagedClusters() wi...
std::uint64_t fNBytesFilled
The total number of bytes filled into all the so far committed clusters, i.e.
RNTupleFillContext(const RNTupleFillContext &)=delete
std::unique_ptr< ROOT::RNTupleModel > fModel
Needs to be destructed before fSink.
void FlushCluster()
Flush so far filled entries to storage.
std::size_t Fill(Detail::RRawPtrWriteEntry &entry)
Fill an RRawPtrWriteEntry into this context.
std::size_t fUnzippedClusterSizeEst
Estimator of uncompressed cluster size, taking into account the estimated compression ratio.
RNTupleFillContext & operator=(const RNTupleFillContext &)=delete
void FillNoFlushImpl(Entry &entry, ROOT::RNTupleFillStatus &status)
std::vector< ROOT::Internal::RPageSink::RStagedCluster > fStagedClusters
Vector of currently staged clusters.
std::size_t fMaxUnzippedClusterSize
Limit for committing cluster no matter the other tunables.
const Detail::RNTupleMetrics & GetMetrics() const
std::uint64_t fNBytesFlushed
The total number of bytes written to storage (i.e., after compression)
std::size_t Fill(ROOT::REntry &entry)
Fill an entry into this context.
ROOT::NTupleSize_t GetLastFlushed() const
Return the entry number that was last flushed in a cluster.
void CommitStagedClusters()
Logically append staged clusters to the RNTuple.
void FillNoFlush(Detail::RRawPtrWriteEntry &entry, ROOT::RNTupleFillStatus &status)
Fill an RRawPtrWriteEntry into this context, but don't commit the cluster.
void FlushColumns()
Flush column data, preparing for CommitCluster or to reduce memory usage.
RNTupleFillContext(std::unique_ptr< ROOT::RNTupleModel > model, std::unique_ptr< ROOT::Internal::RPageSink > sink)
std::unique_ptr< ROOT::REntry > CreateEntry() const
std::unique_ptr< Detail::RRawPtrWriteEntry > CreateRawPtrWriteEntry() const
A writer to fill an RNTuple from multiple contexts.
The REntry is a collection of values in an ntuple corresponding to a complete row in the data set.
Definition REntry.hxx:54
Base class for all ROOT issued exceptions.
Definition RError.hxx:79
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.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
std::uint64_t NTupleSize_t
Integer type long enough to hold the maximum number of entries in a column.