Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
REntry.hxx
Go to the documentation of this file.
1/// \file ROOT/REntry.hxx
2/// \author Jakob Blomer <jblomer@cern.ch>
3/// \date 2018-07-19
4
5/*************************************************************************
6 * Copyright (C) 1995-2019, 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_REntry
14#define ROOT_REntry
15
16#include <ROOT/RError.hxx>
17#include <ROOT/RField.hxx>
18#include <ROOT/RFieldToken.hxx>
19#include <string_view>
20
21#include <TError.h>
22
23#include <algorithm>
24#include <iterator>
25#include <memory>
26#include <type_traits>
27#include <utility>
28#include <vector>
29#include <unordered_map>
30
31namespace ROOT {
32
33class RNTupleFillContext;
34class RNTupleReader;
35class RNTupleAttrSetReader;
36
37namespace Internal {
38struct RNTupleAttrEntry;
39}
40
41namespace Internal {
42class RNTupleAttrMergeActionsDefault;
43class RNTupleMergeStrategySlow;
44}
45
46// clang-format off
47/**
48\class ROOT::REntry
49\ingroup NTuple
50\brief The REntry is a collection of values in an RNTuple corresponding to a complete row in the data set.
51
52The entry provides a memory-managed binder for a set of values read from fields in an RNTuple. The memory locations that are associated
53with values are managed through shared pointers.
54*/
55// clang-format on
56class REntry {
57 friend class RNTupleFillContext;
58 friend class RNTupleModel;
59 friend class RNTupleReader;
64
65private:
66 /// The entry must be linked to a specific model, identified by a model ID
67 std::uint64_t fModelId = 0;
68 /// The entry and its tokens are also linked to a specific schema, identified by a schema ID
69 std::uint64_t fSchemaId = 0;
70 /// Corresponds to the fields of the linked model
71 std::vector<ROOT::RFieldBase::RValue> fValues;
72 /// For fast lookup of token IDs given a (sub)field name present in the entry
73 std::unordered_map<std::string, std::size_t> fFieldName2Token;
74 /// To ensure that the entry is standalone, a copy of all field types
75 std::vector<std::string> fFieldTypes;
76
77 /// Creation of entries can be done by the RNTupleModel, the RNTupleReader, or the RNTupleWriter.
78 REntry() = default;
79 explicit REntry(std::uint64_t modelId, std::uint64_t schemaId) : fModelId(modelId), fSchemaId(schemaId) {}
80
82 {
83 fFieldName2Token[value.GetField().GetQualifiedFieldName()] = fValues.size();
84 fFieldTypes.push_back(value.GetField().GetTypeName());
85 fValues.emplace_back(std::move(value));
86 }
87
88 /// While building the entry, adds a new value for the field and returns the value's shared pointer
89 template <typename T>
90 std::shared_ptr<T> AddValue(ROOT::RField<T> &field)
91 {
93 fFieldTypes.push_back(field.GetTypeName());
94 auto value = field.CreateValue();
95 fValues.emplace_back(value);
96 // We know that the created RValue has the right type, skip the unnecessary check.
97 return std::static_pointer_cast<T>(value.template GetPtr<void>());
98 }
99
101 {
102 for (auto &v : fValues) {
103 v.Read(index);
104 }
105 }
106
107 std::size_t Append()
108 {
109 std::size_t bytesWritten = 0;
110 for (auto &v : fValues) {
111 bytesWritten += v.Append();
112 }
113 return bytesWritten;
114 }
115
117 {
118 if (fSchemaId != token.fSchemaId) {
119 throw RException(R__FAIL("invalid token for this entry, "
120 "make sure to use a token from a model with the same schema as this entry."));
121 }
122 }
123
124 /// This function has linear complexity, only use it for more helpful error messages!
125 const std::string &FindFieldName(ROOT::RFieldToken token) const
126 {
127 for (const auto &[fieldName, index] : fFieldName2Token) {
128 if (index == token.fIndex) {
129 return fieldName;
130 }
131 }
132 // Should never happen, but avoid compiler warning about "returning reference to local temporary object".
133 static const std::string empty = "";
134 return empty;
135 }
136
137 template <typename T>
138 void EnsureMatchingType(ROOT::RFieldToken token [[maybe_unused]]) const
139 {
140 if constexpr (!std::is_void_v<T>) {
141 if (!Internal::IsMatchingFieldType<T>(fFieldTypes[token.fIndex])) {
142 throw RException(R__FAIL("type mismatch for field " + FindFieldName(token) + ": " +
143 fFieldTypes[token.fIndex] + " vs. " + ROOT::RField<T>::TypeName()));
144 }
145 }
146 }
147
148public:
149 using ConstIterator_t = decltype(fValues)::const_iterator;
150
151 REntry(const REntry &other) = delete;
152 REntry &operator=(const REntry &other) = delete;
153 REntry(REntry &&other) = default;
154 REntry &operator=(REntry &&other) = default;
155 ~REntry() = default;
156
157 /// The ordinal of the (sub)field fieldName; can be used in other methods to address the corresponding value
158 ROOT::RFieldToken GetToken(std::string_view fieldName) const
159 {
160 auto it = fFieldName2Token.find(std::string(fieldName));
161 if (it == fFieldName2Token.end()) {
162 throw RException(R__FAIL("invalid field name: " + std::string(fieldName)));
163 }
164 return ROOT::RFieldToken(it->second, fSchemaId);
165 }
166
167 /// Create a new value for the field referenced by `token`.
169 {
170 EnsureMatchingModel(token);
171 fValues[token.fIndex].EmplaceNew();
172 }
173
174 /// Create a new value for the field referenced by its name.
175 void EmplaceNewValue(std::string_view fieldName) { EmplaceNewValue(GetToken(fieldName)); }
176
177 /// Bind the value for the field, referenced by `token`, to `objPtr`.
178 ///
179 /// \sa BindValue(std::string_view, std::shared_ptr<T>)
180 template <typename T>
181 void BindValue(ROOT::RFieldToken token, std::shared_ptr<T> objPtr)
182 {
183 EnsureMatchingModel(token);
184 EnsureMatchingType<T>(token);
185 fValues[token.fIndex].Bind(std::move(objPtr));
186 }
187
188 /// Bind the value for the field, referenced by its name, to `objPtr`.
189 ///
190 /// Ownership is shared with the caller and the object will be kept alive until it is replaced (by a call to
191 /// EmplaceNewValue, BindValue, or BindRawPtr) or the entry is destructed.
192 ///
193 /// **Note**: if `T = void`, type checks are disabled. It is the caller's responsibility to match the field and
194 /// object types.
195 template <typename T>
196 void BindValue(std::string_view fieldName, std::shared_ptr<T> objPtr)
197 {
198 BindValue<T>(GetToken(fieldName), std::move(objPtr));
199 }
200
201 /// Bind the value for the field, referenced by `token`, to `rawPtr`.
202 ///
203 /// \sa BindRawPtr(std::string_view, T *)
204 template <typename T>
205 void BindRawPtr(ROOT::RFieldToken token, T *rawPtr)
206 {
207 EnsureMatchingModel(token);
208 EnsureMatchingType<T>(token);
209 fValues[token.fIndex].BindRawPtr(rawPtr);
210 }
211
212 /// Bind the value for the field, referenced by its name, to `rawPtr`.
213 ///
214 /// The caller retains ownership of the object and must ensure it is kept alive when reading or writing using the
215 /// entry.
216 ///
217 /// **Note**: if `T = void`, type checks are disabled. It is the caller's responsibility to match the field and
218 /// object types.
219 template <typename T>
220 void BindRawPtr(std::string_view fieldName, T *rawPtr)
221 {
222 BindRawPtr<T>(GetToken(fieldName), rawPtr);
223 }
224
225 /// Get the (typed) pointer to the value for the field referenced by `token`.
226 ///
227 /// \sa GetPtr(std::string_view)
228 template <typename T>
229 std::shared_ptr<T> GetPtr(ROOT::RFieldToken token) const
230 {
231 EnsureMatchingModel(token);
232 EnsureMatchingType<T>(token);
233 return std::static_pointer_cast<T>(fValues[token.fIndex].GetPtr<void>());
234 }
235
236 /// Get the (typed) pointer to the value for the field referenced by `token`.
237 ///
238 /// Ownership is shared and the caller can continue to use the object after the entry is destructed.
239 ///
240 /// **Note**: if `T = void`, type checks are disabled. It is the caller's responsibility to use the returned pointer
241 /// according to the field type.
242 template <typename T>
243 std::shared_ptr<T> GetPtr(std::string_view fieldName) const
244 {
245 return GetPtr<T>(GetToken(fieldName));
246 }
247
248 const std::string &GetTypeName(ROOT::RFieldToken token) const
249 {
250 EnsureMatchingModel(token);
251 return fFieldTypes[token.fIndex];
252 }
253
254 const std::string &GetTypeName(std::string_view fieldName) const { return GetTypeName(GetToken(fieldName)); }
255
256 bool HasField(std::string_view fieldName) const { return fFieldName2Token.count(std::string(fieldName)) > 0; }
257
258 std::uint64_t GetModelId() const { return fModelId; }
259 std::uint64_t GetSchemaId() const { return fSchemaId; }
260
261 ConstIterator_t begin() const { return fValues.cbegin(); }
262 ConstIterator_t end() const { return fValues.cend(); }
263};
264
265} // namespace ROOT
266
267#endif
#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:322
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Merge Actions used internally to merge attributes.
The REntry is a collection of values in an RNTuple corresponding to a complete row in the data set.
Definition REntry.hxx:56
std::uint64_t fModelId
The entry must be linked to a specific model, identified by a model ID.
Definition REntry.hxx:67
void EmplaceNewValue(ROOT::RFieldToken token)
Create a new value for the field referenced by token.
Definition REntry.hxx:168
std::shared_ptr< T > GetPtr(ROOT::RFieldToken token) const
Get the (typed) pointer to the value for the field referenced by token.
Definition REntry.hxx:229
std::vector< ROOT::RFieldBase::RValue > fValues
Corresponds to the fields of the linked model.
Definition REntry.hxx:71
std::uint64_t GetModelId() const
Definition REntry.hxx:258
void BindValue(std::string_view fieldName, std::shared_ptr< T > objPtr)
Bind the value for the field, referenced by its name, to objPtr.
Definition REntry.hxx:196
void BindValue(ROOT::RFieldToken token, std::shared_ptr< T > objPtr)
Bind the value for the field, referenced by token, to objPtr.
Definition REntry.hxx:181
std::uint64_t GetSchemaId() const
Definition REntry.hxx:259
REntry(const REntry &other)=delete
const std::string & FindFieldName(ROOT::RFieldToken token) const
This function has linear complexity, only use it for more helpful error messages!
Definition REntry.hxx:125
const std::string & GetTypeName(ROOT::RFieldToken token) const
Definition REntry.hxx:248
void EnsureMatchingType(ROOT::RFieldToken token) const
Definition REntry.hxx:138
ConstIterator_t begin() const
Definition REntry.hxx:261
void Read(ROOT::NTupleSize_t index)
Definition REntry.hxx:100
std::vector< std::string > fFieldTypes
To ensure that the entry is standalone, a copy of all field types.
Definition REntry.hxx:75
std::uint64_t fSchemaId
The entry and its tokens are also linked to a specific schema, identified by a schema ID.
Definition REntry.hxx:69
void BindRawPtr(std::string_view fieldName, T *rawPtr)
Bind the value for the field, referenced by its name, to rawPtr.
Definition REntry.hxx:220
void AddValue(ROOT::RFieldBase::RValue &&value)
Definition REntry.hxx:81
std::shared_ptr< T > GetPtr(std::string_view fieldName) const
Get the (typed) pointer to the value for the field referenced by token.
Definition REntry.hxx:243
REntry()=default
Creation of entries can be done by the RNTupleModel, the RNTupleReader, or the RNTupleWriter.
REntry(std::uint64_t modelId, std::uint64_t schemaId)
Definition REntry.hxx:79
REntry & operator=(REntry &&other)=default
REntry & operator=(const REntry &other)=delete
ConstIterator_t end() const
Definition REntry.hxx:262
std::shared_ptr< T > AddValue(ROOT::RField< T > &field)
While building the entry, adds a new value for the field and returns the value's shared pointer.
Definition REntry.hxx:90
bool HasField(std::string_view fieldName) const
Definition REntry.hxx:256
void EnsureMatchingModel(ROOT::RFieldToken token) const
Definition REntry.hxx:116
void BindRawPtr(ROOT::RFieldToken token, T *rawPtr)
Bind the value for the field, referenced by token, to rawPtr.
Definition REntry.hxx:205
void EmplaceNewValue(std::string_view fieldName)
Create a new value for the field referenced by its name.
Definition REntry.hxx:175
std::size_t Append()
Definition REntry.hxx:107
decltype(fValues)::const_iterator ConstIterator_t
Definition REntry.hxx:149
~REntry()=default
REntry(REntry &&other)=default
ROOT::RFieldToken GetToken(std::string_view fieldName) const
The ordinal of the (sub)field fieldName; can be used in other methods to address the corresponding va...
Definition REntry.hxx:158
std::unordered_map< std::string, std::size_t > fFieldName2Token
For fast lookup of token IDs given a (sub)field name present in the entry.
Definition REntry.hxx:73
const std::string & GetTypeName(std::string_view fieldName) const
Definition REntry.hxx:254
Base class for all ROOT issued exceptions.
Definition RError.hxx:78
Points to an object with RNTuple I/O support and keeps a pointer to the corresponding field.
RValue CreateValue()
Generates an object of the field's type, wraps it in a shared pointer and returns it as an RValue con...
std::string GetQualifiedFieldName() const
Returns the field name and parent field names separated by dots (grandparent.parent....
const std::string & GetTypeName() const
A field token identifies a (sub)field in an entry.
std::size_t fIndex
The index of the field (top-level or registered subfield)
std::uint64_t fSchemaId
Safety check to prevent tokens from other models being used.
Classes with dictionaries that can be inspected by TClass.
Definition RField.hxx:352
Class used to read a RNTupleAttrSet in the context of a RNTupleReader.
A context for filling entries (data) into clusters of an RNTuple.
The RNTupleModel encapulates the schema of an RNTuple.
Reads RNTuple data from storage.
std::uint64_t NTupleSize_t
Integer type long enough to hold the maximum number of entries in a column.
A pair of scoped + meta entry used by the RNTupleAttrSetWriter.