Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
REntry.hxx
Go to the documentation of this file.
1/// \file ROOT/REntry.hxx
2/// \ingroup NTuple ROOT7
3/// \author Jakob Blomer <jblomer@cern.ch>
4/// \date 2018-07-19
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-2019, 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 ROOT7_REntry
17#define ROOT7_REntry
18
19#include <ROOT/RError.hxx>
20#include <ROOT/RField.hxx>
21#include <string_view>
22
23#include <TError.h>
24
25#include <algorithm>
26#include <iterator>
27#include <memory>
28#include <type_traits>
29#include <utility>
30#include <vector>
31#include <unordered_map>
32
33namespace ROOT {
34namespace Experimental {
35
36namespace Internal {
37class RNTupleProcessor;
38class RNTupleChainProcessor;
39}
40
41// clang-format off
42/**
43\class ROOT::Experimental::REntry
44\ingroup NTuple
45\brief The REntry is a collection of values in an ntuple corresponding to a complete row in the data set
46
47The entry provides a memory-managed binder for a set of values. Through shared pointers, the memory locations
48that are associated to values are managed.
49*/
50// clang-format on
51class REntry {
52 friend class RNTupleModel;
53 friend class RNTupleReader;
54 friend class RNTupleFillContext;
55 friend class RNTupleProcessor;
57
58public:
59 /// The field token identifies a (sub)field in this entry. It can be used for fast indexing in REntry's methods, e.g.
60 /// BindValue. The field token can also be created by the model.
62 friend class REntry;
63 friend class RNTupleModel;
64
65 std::size_t fIndex = 0; ///< The index in fValues that belongs to the field
66 std::uint64_t fSchemaId = std::uint64_t(-1); ///< Safety check to prevent tokens from other models being used
67 RFieldToken(std::size_t index, std::uint64_t schemaId) : fIndex(index), fSchemaId(schemaId) {}
68
69 public:
70 RFieldToken() = default; // The default constructed token cannot be used by any entry
71 };
72
73private:
74 /// The entry must be linked to a specific model, identified by a model ID
75 std::uint64_t fModelId = 0;
76 /// The entry and its tokens are also linked to a specific schema, identified by a schema ID
77 std::uint64_t fSchemaId = 0;
78 /// Corresponds to the fields of the linked model
79 std::vector<RFieldBase::RValue> fValues;
80 /// For fast lookup of token IDs given a (sub)field name present in the entry
81 std::unordered_map<std::string, std::size_t> fFieldName2Token;
82
83 // Creation of entries is done by the RNTupleModel class
84
85 REntry() = default;
86 explicit REntry(std::uint64_t modelId, std::uint64_t schemaId) : fModelId(modelId), fSchemaId(schemaId) {}
87
89 {
90 fFieldName2Token[value.GetField().GetQualifiedFieldName()] = fValues.size();
91 fValues.emplace_back(std::move(value));
92 }
93
94 /// While building the entry, adds a new value to the list and return the value's shared pointer
95 template <typename T, typename... ArgsT>
96 std::shared_ptr<T> AddValue(RField<T> &field, ArgsT &&...args)
97 {
99 auto ptr = std::make_shared<T>(std::forward<ArgsT>(args)...);
100 fValues.emplace_back(field.BindValue(ptr));
101 return ptr;
102 }
103
104 /// Update the RValue for a field in the entry. To be used when its underlying RFieldBase changes, which typically
105 /// happens when page source the field values are read from changes.
106 void UpdateValue(RFieldToken token, RFieldBase::RValue &&value) { std::swap(fValues.at(token.fIndex), value); }
107 void UpdateValue(RFieldToken token, RFieldBase::RValue &value) { std::swap(fValues.at(token.fIndex), value); }
108
110 {
111 for (auto &v : fValues) {
112 v.Read(index);
113 }
114 }
115
116 std::size_t Append()
117 {
118 std::size_t bytesWritten = 0;
119 for (auto &v : fValues) {
120 bytesWritten += v.Append();
121 }
122 return bytesWritten;
123 }
124
126 {
127 if (fSchemaId != token.fSchemaId) {
128 throw RException(R__FAIL("invalid token for this entry, "
129 "make sure to use a token from a model with the same schema as this entry."));
130 }
131 }
132
133 template <typename T>
134 void EnsureMatchingType(RFieldToken token [[maybe_unused]]) const
135 {
136 if constexpr (!std::is_void_v<T>) {
137 const auto &v = fValues[token.fIndex];
138 if (v.GetField().GetTypeName() != RField<T>::TypeName()) {
139 throw RException(R__FAIL("type mismatch for field " + v.GetField().GetQualifiedFieldName() + ": " +
140 v.GetField().GetTypeName() + " vs. " + RField<T>::TypeName()));
141 }
142 }
143 }
144
145public:
146 using ConstIterator_t = decltype(fValues)::const_iterator;
147
148 REntry(const REntry &other) = delete;
149 REntry &operator=(const REntry &other) = delete;
150 REntry(REntry &&other) = default;
151 REntry &operator=(REntry &&other) = default;
152 ~REntry() = default;
153
154 /// The ordinal of the (sub)field fieldName; can be used in other methods to address the corresponding value
155 RFieldToken GetToken(std::string_view fieldName) const
156 {
157 auto it = fFieldName2Token.find(std::string(fieldName));
158 if (it == fFieldName2Token.end()) {
159 throw RException(R__FAIL("invalid field name: " + std::string(fieldName)));
160 }
161 return RFieldToken(it->second, fSchemaId);
162 }
163
165 {
166 EnsureMatchingModel(token);
167 fValues[token.fIndex].EmplaceNew();
168 }
169
170 void EmplaceNewValue(std::string_view fieldName) { EmplaceNewValue(GetToken(fieldName)); }
171
172 template <typename T>
173 void BindValue(RFieldToken token, std::shared_ptr<T> objPtr)
174 {
175 EnsureMatchingModel(token);
176 EnsureMatchingType<T>(token);
177 fValues[token.fIndex].Bind(objPtr);
178 }
179
180 template <typename T>
181 void BindValue(std::string_view fieldName, std::shared_ptr<T> objPtr)
182 {
183 BindValue<T>(GetToken(fieldName), objPtr);
184 }
185
186 template <typename T>
187 void BindRawPtr(RFieldToken token, T *rawPtr)
188 {
189 EnsureMatchingModel(token);
190 EnsureMatchingType<T>(token);
191 fValues[token.fIndex].BindRawPtr(rawPtr);
192 }
193
194 template <typename T>
195 void BindRawPtr(std::string_view fieldName, T *rawPtr)
196 {
197 BindRawPtr<void>(GetToken(fieldName), rawPtr);
198 }
199
200 template <typename T>
201 std::shared_ptr<T> GetPtr(RFieldToken token) const
202 {
203 EnsureMatchingModel(token);
204 EnsureMatchingType<T>(token);
205 return std::static_pointer_cast<T>(fValues[token.fIndex].GetPtr<void>());
206 }
207
208 template <typename T>
209 std::shared_ptr<T> GetPtr(std::string_view fieldName) const
210 {
211 return GetPtr<T>(GetToken(fieldName));
212 }
213
214 std::uint64_t GetModelId() const { return fModelId; }
215 std::uint64_t GetSchemaId() const { return fSchemaId; }
216
217 ConstIterator_t begin() const { return fValues.cbegin(); }
218 ConstIterator_t end() const { return fValues.cend(); }
219};
220
221} // namespace Experimental
222} // namespace ROOT
223
224#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:290
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
The field token identifies a (sub)field in this entry.
Definition REntry.hxx:61
std::size_t fIndex
The index in fValues that belongs to the field.
Definition REntry.hxx:65
RFieldToken(std::size_t index, std::uint64_t schemaId)
Definition REntry.hxx:67
std::uint64_t fSchemaId
Safety check to prevent tokens from other models being used.
Definition REntry.hxx:66
The REntry is a collection of values in an ntuple corresponding to a complete row in the data set.
Definition REntry.hxx:51
void EnsureMatchingType(RFieldToken token) const
Definition REntry.hxx:134
std::uint64_t fSchemaId
The entry and its tokens are also linked to a specific schema, identified by a schema ID.
Definition REntry.hxx:77
std::uint64_t fModelId
The entry must be linked to a specific model, identified by a model ID.
Definition REntry.hxx:75
void BindValue(RFieldToken token, std::shared_ptr< T > objPtr)
Definition REntry.hxx:173
REntry & operator=(REntry &&other)=default
std::uint64_t GetSchemaId() const
Definition REntry.hxx:215
decltype(fValues)::const_iterator ConstIterator_t
Definition REntry.hxx:146
void Read(NTupleSize_t index)
Definition REntry.hxx:109
ConstIterator_t begin() const
Definition REntry.hxx:217
REntry & operator=(const REntry &other)=delete
void EmplaceNewValue(std::string_view fieldName)
Definition REntry.hxx:170
REntry(REntry &&other)=default
std::uint64_t GetModelId() const
Definition REntry.hxx:214
void EmplaceNewValue(RFieldToken token)
Definition REntry.hxx:164
std::shared_ptr< T > AddValue(RField< T > &field, ArgsT &&...args)
While building the entry, adds a new value to the list and return the value's shared pointer.
Definition REntry.hxx:96
void BindRawPtr(RFieldToken token, T *rawPtr)
Definition REntry.hxx:187
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:81
void BindValue(std::string_view fieldName, std::shared_ptr< T > objPtr)
Definition REntry.hxx:181
void UpdateValue(RFieldToken token, RFieldBase::RValue &&value)
Update the RValue for a field in the entry.
Definition REntry.hxx:106
REntry(std::uint64_t modelId, std::uint64_t schemaId)
Definition REntry.hxx:86
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:155
void AddValue(RFieldBase::RValue &&value)
Definition REntry.hxx:88
void UpdateValue(RFieldToken token, RFieldBase::RValue &value)
Definition REntry.hxx:107
std::vector< RFieldBase::RValue > fValues
Corresponds to the fields of the linked model.
Definition REntry.hxx:79
void BindRawPtr(std::string_view fieldName, T *rawPtr)
Definition REntry.hxx:195
ConstIterator_t end() const
Definition REntry.hxx:218
void EnsureMatchingModel(RFieldToken token) const
Definition REntry.hxx:125
REntry(const REntry &other)=delete
std::shared_ptr< T > GetPtr(std::string_view fieldName) const
Definition REntry.hxx:209
std::shared_ptr< T > GetPtr(RFieldToken token) const
Definition REntry.hxx:201
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.
std::string GetQualifiedFieldName() const
Returns the field name and parent field names separated by dots ("grandparent.parent....
Definition RField.cxx:571
RValue BindValue(std::shared_ptr< void > objPtr)
Creates a value from a memory location with an already constructed object.
Definition RField.cxx:1072
Classes with dictionaries that can be inspected by TClass.
Definition RField.hxx:241
static std::string TypeName()
Definition RField.hxx:243
Processor specializiation for vertically concatenated RNTuples (chains).
A context for filling entries (data) into clusters of an RNTuple.
The RNTupleModel encapulates the schema of an ntuple.
Interface for iterating over entries of RNTuples and vertically concatenated RNTuples (chains).
An RNTuple that is used to read data from storage.
std::uint64_t NTupleSize_t
Integer type long enough to hold the maximum number of entries in a column.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...