Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RNTupleMerger.hxx
Go to the documentation of this file.
1/// \file ROOT/RNTupleMerger.hxx
2/// \author Jakob Blomer <jblomer@cern.ch>, Max Orok <maxwellorok@gmail.com>, Alaettin Serhan Mete <amete@anl.gov>
3/// \date 2020-07-08
4/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
5/// is welcome!
6
7/*************************************************************************
8 * Copyright (C) 1995-2020, Rene Brun and Fons Rademakers. *
9 * All rights reserved. *
10 * *
11 * For the licensing terms see $ROOTSYS/LICENSE. *
12 * For the list of contributors see $ROOTSYS/README/CREDITS. *
13 *************************************************************************/
14
15#ifndef ROOT_RNTupleMerger
16#define ROOT_RNTupleMerger
17
18#include <ROOT/RError.hxx>
20#include <ROOT/RNTupleTypes.hxx>
21#include <ROOT/RPageStorage.hxx>
22#include <ROOT/TTaskGroup.hxx>
23#include <Compression.h>
24
25#include <memory>
26#include <optional>
27
28namespace ROOT {
29
30class REntry;
31class RNTuple;
32class RNTupleAttrRange;
33
34namespace Internal {
35
36class RPageSource;
37class RNTupleMergeStrategySlow;
38class RNTupleAttrMergeActionsDefault;
39struct RNTupleSlowMergeData;
40
42 /// The merger will discard all columns that aren't present in the prototype model (i.e. the model of the first
43 /// source); also all subsequent RNTuples must contain at least all the columns that are present in the prototype
44 /// model
45 kFilter,
46 /// The merger will refuse to merge any 2 RNTuples whose schema doesn't match exactly
47 kStrict,
48 /// The merger will update the output model to include all columns from all sources. Entries corresponding to columns
49 /// that are not present in a source will be set to the default value of the type.
50 kUnion
51};
52
53inline const char *ToString(ENTupleMergingMode mode)
54{
55 static const char *const kMergingModeStr[] = {"Filter", "Strict", "Union"};
56 return kMergingModeStr[static_cast<int>(mode)];
57}
58
60 /// The merger will abort merging as soon as an error is encountered
61 kAbort,
62 /// Upon errors, the merger will skip the current source and continue
63 kSkip
64};
65
67 /// The merger will emit a warning when merging RNTuples with higher version than the latest supported by this
68 /// ROOT version, but merging will work. Some optional features present in the source(s) may be missing from the
69 /// merged RNTuple.
71 /// The merger will refuse to merge RNTuples with higher versions than the latest supported by this ROOT version.
72 /// The merging process will abort as soon as one such source is encountered.
74};
75
77
78using RNTupleAttrMergeActionsFactory = std::function<std::unique_ptr<RNTupleAttrMergeActions>()>;
79
80/// Set of merging options to pass to RNTupleMerger.
81/// If you're using the merger through TFileMerger you need to give it string-based options instead.
82/// Here is the mapping for the TFileMerger options:
83/// - `"rntuple.MergingMode=(Filter|Union|...)"` -> sets fMergingMode
84/// - `"rntuple.ErrBehavior=(Abort|Skip|...)"` -> sets fErrBehavior
85/// - `"rntuple.VersionBehavior=(WarnOnHigherVersion|AbortOnHigherVersion|...)"` -> sets fVersionBehavior
86/// - `"rntuple.ExtraVerbose"` -> sets fExtraVerbose to true
87/// Rules about the string-based options:
88/// 1. there must be no space between the separators (i.e. `.` and `=`)
89/// 2. all string matching is case insensitive
91private:
92 friend class RNTupleMerger;
93
94 // Maps { attrSetName => userMergeActions }.
95 // We store factories rather than instances because we want to keep the Options struct copyable and for every
96 // call to Merge() to receive a fresh instance of the Actions class.
97 std::unordered_map<std::string_view, RNTupleAttrMergeActionsFactory> fUserAttrMergeActions;
98
99public:
100 /// If fCompressionSettings is empty (the default), the merger will not change the
101 /// compression of any of its sources (fast merging). Otherwise, all sources will be converted to the specified
102 /// compression algorithm and level.
103 std::optional<std::uint32_t> fCompressionSettings;
104 /// Determines how the merging treats sources with different models (\see ENTupleMergingMode).
106 /// Determines how the Merge function behaves upon merging errors
108 /// Determines how the Merge function behaves depending on the RNTuple sources' version.
110 /// If true, the merger will emit further diagnostics and information.
111 bool fExtraVerbose = false;
112
113 /// Sets the user-defined merge actions associated to attribute set `attrSetName` to the type returned by `factory`.
114 /// This will replace any existing actions associated to that attribute set.
115 /// You can call `SetAttributeMergeActions(attrSetName, nullptr)` to remove any associated actions from the
116 /// given attribute set.
117 /// \return true if the attribute set already had actions that were replaced by this call, false otherwise.
118 template <typename T>
120 std::string_view attrSetName,
121 RNTupleAttrMergeActionsFactory factory = [] { return std::unique_ptr<RNTupleAttrMergeActions>(new T()); })
122 {
123 static_assert(std::is_base_of_v<RNTupleAttrMergeActions, T>,
124 "actions' class must inherit from RNTupleAttrMergeActions!");
125 auto [_it, noPreviousActionsExisted] = fUserAttrMergeActions.insert_or_assign(attrSetName, factory);
126 return !noPreviousActionsExisted;
127 }
128};
129
130/// Information regarding the current slow merge process that gets passed to the RNTupleMergeActions
142
144 /// Don't emit this entry
145 kDrop,
146 /// Emit this entry, then continue
147 kEmit,
148 /// Emit this entry, then call the function again. The entry passed to subsequent calls will retain all
149 /// the values it had during the previous call.
151};
152
153// clang-format off
154/**
155 * \class ROOT::Internal::RNTupleMergeActions
156 * \ingroup NTuple
157 * \brief Container for callbacks that are invoked during the "slow merge" process.
158 * This allows customizing how the merging happens entry-by-entry, enabling things like dropping individual
159 * entries from the destination RNTuple, modifying their values on the fly or creating new entries artificially.
160 */
161// clang-format on
163public:
165 virtual ~RNTupleMergeActions() = default;
166
171
172 /// Invoked every time an entry is merged from a source RNTuple to the destination RNTuple.
177
178 /// Invoked at the start of each sources's merging process.
180
181 /// Invoked at the end of each source's merging process.
182 /// The first time this function is invoked per source the given entry contains the value of the latest entry read
183 /// (which may be undefined if this source had no entries).
184 /// During subsequent calls (caused by kEmitMulti), the entry will contain the previously-emitted values.
189
190 /// Invoked at the end of the merge process, right before committing the dataset.
191 /// The first time this function is invoked the given entry contains unspecified values.
192 /// During subsequent calls (caused by kEmitMulti), the entry will contain the previously-emitted values.
193 /// This allows appending more entries at the end of the merge.
198};
199
200/// Like RNTupleMergeActions, but performed specifically on attributes.
201/// The main difference is that all methods also receive an in/out Range parameter, which
202/// may be modified to point the emitted entries to that range in the main RNTuple.
204public:
206 virtual ~RNTupleAttrMergeActions() = default;
207
212
213 /// Invoked every time an entry is merged from a source RNTuple to the destination RNTuple.
214 /// The first time this function is invoked per entry, the entry and range parameters contain the values of the
215 /// currently-loaded entry.
216 /// When invoked multiple times in a row due to kEmitMulti, both the range and the entry parameters will contain
217 /// the same values as the previous invocation.
222
223 /// Invoked at the end of each source's merging process.
224 /// The first time this function is invoked per source the given entry and range contain the value of the latest
225 /// attribute entry read and its range (both of which may be undefined if this source had no attribute entries).
226 /// During subsequent calls (caused by kEmitMulti), the entry and the range will contain the previously-emitted
227 /// values.
233
234 /// Invoked at the end of the merge process, right before committing the dataset.
235 /// The first time this function is invoked the given entry contains unspecified values.
236 /// During subsequent calls (caused by kEmitMulti), the entry will contain the previously-emitted values.
237 /// This allows appending more entries at the end of the merge.
243};
244
245// clang-format off
246/**
247 * \class ROOT::Internal::RNTupleMerger
248 * \ingroup NTuple
249 * \brief Given a set of RPageSources merge them into an RPagePersistentSink, optionally changing their compression.
250 * This can also be used to change the compression of a single RNTuple by just passing a single source.
251 */
252// clang-format on
253class RNTupleMerger final {
254 friend class ROOT::RNTuple;
255
256 std::unique_ptr<ROOT::Internal::RPagePersistentSink> fDestination;
257 std::unique_ptr<ROOT::RNTupleModel> fModel;
258
259 /// Creates a RNTupleMerger with the given destination.
260 /// The model must be given if and only if `destination` has been initialized with that model
261 /// (i.e. in case of incremental merging).
262 RNTupleMerger(std::unique_ptr<ROOT::Internal::RPagePersistentSink> destination,
263 std::unique_ptr<ROOT::RNTupleModel> model);
264
265public:
266 /// Creates a RNTupleMerger with the given destination.
267 explicit RNTupleMerger(std::unique_ptr<ROOT::Internal::RPagePersistentSink> destination);
268 // NOTE: outlined to avoid including RNTupleModel.hxx
270
271 /// Merge a given set of sources into the destination.
272 /// Note that sources with an empty schema (i.e. created from a Model that had no fields added to it) are in
273 /// general valid (depending on the merging mode) but add no entries to the destination.
274 RResult<void> Merge(std::span<ROOT::Internal::RPageSource *> sources,
275 const RNTupleMergeOptions &mergeOpts = RNTupleMergeOptions());
276
277}; // end of class RNTupleMerger
278
279} // namespace Internal
280} // namespace ROOT
281
282#endif
Option_t Option_t TPoint TPoint const char mode
Merge Actions used internally to merge attributes.
Like RNTupleMergeActions, but performed specifically on attributes.
RNTupleAttrMergeActions(RNTupleAttrMergeActions &&)=default
virtual ENTupleMergeActionResult BeforeCommit(ROOT::REntry &, ROOT::RNTupleAttrRange &, const RNTupleMergeActionContext &)
Invoked at the end of the merge process, right before committing the dataset.
RNTupleAttrMergeActions & operator=(RNTupleAttrMergeActions &&)=default
RNTupleAttrMergeActions(const RNTupleAttrMergeActions &)=delete
RNTupleAttrMergeActions & operator=(const RNTupleAttrMergeActions &)=delete
virtual ENTupleMergeActionResult OnFill(ROOT::REntry &, ROOT::RNTupleAttrRange &, const RNTupleMergeActionContext &)
Invoked every time an entry is merged from a source RNTuple to the destination RNTuple.
virtual ENTupleMergeActionResult OnSourceEnd(ROOT::REntry &, ROOT::RNTupleAttrRange &, const RNTupleMergeActionContext &)
Invoked at the end of each source's merging process.
Container for callbacks that are invoked during the "slow merge" process.
RNTupleMergeActions & operator=(const RNTupleMergeActions &)=delete
virtual void OnSourceBegin(ROOT::REntry &, const RNTupleMergeActionContext &)
Invoked at the start of each sources's merging process.
virtual ENTupleMergeActionResult OnFill(ROOT::REntry &, const RNTupleMergeActionContext &)
Invoked every time an entry is merged from a source RNTuple to the destination RNTuple.
RNTupleMergeActions(RNTupleMergeActions &&)=default
RNTupleMergeActions(const RNTupleMergeActions &)=delete
RNTupleMergeActions & operator=(RNTupleMergeActions &&)=default
virtual ENTupleMergeActionResult OnSourceEnd(ROOT::REntry &, const RNTupleMergeActionContext &)
Invoked at the end of each source's merging process.
virtual ENTupleMergeActionResult BeforeCommit(ROOT::REntry &, const RNTupleMergeActionContext &)
Invoked at the end of the merge process, right before committing the dataset.
virtual ~RNTupleMergeActions()=default
Given a set of RPageSources merge them into an RPagePersistentSink, optionally changing their compres...
std::unique_ptr< ROOT::RNTupleModel > fModel
RNTupleMerger(std::unique_ptr< ROOT::Internal::RPagePersistentSink > destination, std::unique_ptr< ROOT::RNTupleModel > model)
Creates a RNTupleMerger with the given destination.
std::unique_ptr< ROOT::Internal::RPagePersistentSink > fDestination
RResult< void > Merge(std::span< ROOT::Internal::RPageSource * > sources, const RNTupleMergeOptions &mergeOpts=RNTupleMergeOptions())
Merge a given set of sources into the destination.
The REntry is a collection of values in an RNTuple corresponding to a complete row in the data set.
Definition REntry.hxx:56
A range of main entries referred to by an attribute entry.
The RNTupleModel encapulates the schema of an RNTuple.
Representation of an RNTuple data set in a ROOT file.
Definition RNTuple.hxx:67
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:222
@ kEmitMulti
Emit this entry, then call the function again.
@ kEmit
Emit this entry, then continue.
std::function< std::unique_ptr< RNTupleAttrMergeActions >()> RNTupleAttrMergeActionsFactory
@ kAbortOnHigherVersion
The merger will refuse to merge RNTuples with higher versions than the latest supported by this ROOT ...
@ kWarnOnHigherVersion
The merger will emit a warning when merging RNTuples with higher version than the latest supported by...
const char * ToString(ENTupleMergingMode mode)
@ kAbort
The merger will abort merging as soon as an error is encountered.
@ kSkip
Upon errors, the merger will skip the current source and continue.
@ kStrict
The merger will refuse to merge any 2 RNTuples whose schema doesn't match exactly.
@ kUnion
The merger will update the output model to include all columns from all sources.
@ kFilter
The merger will discard all columns that aren't present in the prototype model (i....
std::uint64_t NTupleSize_t
Integer type long enough to hold the maximum number of entries in a column.
Information regarding the current slow merge process that gets passed to the RNTupleMergeActions.
Set of merging options to pass to RNTupleMerger.
std::unordered_map< std::string_view, RNTupleAttrMergeActionsFactory > fUserAttrMergeActions
ENTupleMergeVersionBehavior fVersionBehavior
Determines how the Merge function behaves depending on the RNTuple sources' version.
bool fExtraVerbose
If true, the merger will emit further diagnostics and information.
bool SetAttributeMergeActions(std::string_view attrSetName, RNTupleAttrMergeActionsFactory factory=[] { return std::unique_ptr< RNTupleAttrMergeActions >(new T());})
Sets the user-defined merge actions associated to attribute set attrSetName to the type returned by f...
std::optional< std::uint32_t > fCompressionSettings
If fCompressionSettings is empty (the default), the merger will not change the compression of any of ...
ENTupleMergingMode fMergingMode
Determines how the merging treats sources with different models (.
ENTupleMergeErrBehavior fErrBehavior
Determines how the Merge function behaves upon merging errors.