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
6/*************************************************************************
7 * Copyright (C) 1995-2024, Rene Brun and Fons Rademakers. *
8 * All rights reserved. *
9 * *
10 * For the licensing terms see $ROOTSYS/LICENSE. *
11 * For the list of contributors see $ROOTSYS/README/CREDITS. *
12 *************************************************************************/
13
14#ifndef ROOT_RNTupleFillContext
15#define ROOT_RNTupleFillContext
16
17#include <ROOT/RConfig.hxx> // for R__unlikely
18#include <ROOT/REntry.hxx>
19#include <ROOT/RError.hxx>
20#include <ROOT/RPageStorage.hxx>
24#include <ROOT/RNTupleModel.hxx>
25#include <ROOT/RNTupleTypes.hxx>
26
27#include <cstddef>
28#include <cstdint>
29#include <memory>
30#include <vector>
31
32namespace ROOT {
33
34// clang-format off
35/**
36\class ROOT::RNTupleFillContext
37\ingroup NTuple
38\brief A context for filling entries (data) into clusters of an RNTuple
39
40An output cluster can be filled with entries. The caller has to make sure that the data that gets filled into a cluster
41is not modified for the time of the Fill() call. The fill call serializes the C++ object into the column format and
42writes data into the corresponding column page buffers. Writing of the buffers to storage is deferred and can be
43triggered by FlushCluster() or by destructing the context. On I/O errors, an exception is thrown.
44
45Instances of this class are not meant to be used in isolation and can be created from an RNTupleParallelWriter. For
46sequential writing, please refer to RNTupleWriter.
47*/
48// clang-format on
50 friend class ROOT::RNTupleWriter;
52
53private:
54 /// The page sink's parallel page compression scheduler if IMT is on.
55 /// Needs to be destructed after the page sink is destructed and so declared before.
56 std::unique_ptr<ROOT::Internal::RPageStorage::RTaskScheduler> fZipTasks;
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<Experimental::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
172};
173
174} // namespace ROOT
175
176#endif // ROOT_RNTupleFillContext
#define R__unlikely(expr)
Definition RConfig.hxx:594
#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:300
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.
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:79
A context for filling entries (data) into clusters of an RNTuple.
void FlushCluster()
Flush so far filled entries to storage.
ROOT::NTupleSize_t fLastFlushed
std::size_t Fill(Experimental::Detail::RRawPtrWriteEntry &entry)
Fill an RRawPtrWriteEntry into this context.
void FillNoFlushImpl(Entry &entry, ROOT::RNTupleFillStatus &status)
std::size_t fUnzippedClusterSizeEst
Estimator of uncompressed cluster size, taking into account the estimated compression ratio.
void FillNoFlush(Experimental::Detail::RRawPtrWriteEntry &entry, ROOT::RNTupleFillStatus &status)
Fill an RRawPtrWriteEntry into this context, but don't commit the cluster.
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< Experimental::Detail::RRawPtrWriteEntry > CreateRawPtrWriteEntry() const
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.
const ROOT::RNTupleModel & GetModel() const
RNTupleFillContext & operator=(const RNTupleFillContext &)=delete
const Experimental::Detail::RNTupleMetrics & GetMetrics() const
void CommitStagedClusters()
Logically append staged clusters to the RNTuple.
RNTupleFillContext(const 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.
A writer to fill an RNTuple from multiple contexts.
An RNTuple that gets filled with entries (data) and writes them to storage.
std::uint64_t NTupleSize_t
Integer type long enough to hold the maximum number of entries in a column.