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 {
38}
39
40// clang-format off
41/**
42\class ROOT::Experimental::REntry
43\ingroup NTuple
44\brief The REntry is a collection of values in an ntuple corresponding to a complete row in the data set
45
46The entry provides a memory-managed binder for a set of values. Through shared pointers, the memory locations
47that are associated to values are managed.
48*/
49// clang-format on
50class REntry {
52 friend class RNTupleModel;
53 friend class RNTupleReader;
54 friend class RNTupleFillContext;
56
57public:
58 /// The field token identifies a top-level field in this entry. It can be used for fast indexing in REntry's
59 /// methods, e.g. BindValue. The field token can also be created by the model.
61 friend class REntry;
62 friend class RNTupleModel;
63
64 std::size_t fIndex; ///< the index in fValues that belongs to the top-level field
65 std::uint64_t fModelId; ///< Safety check to prevent tokens from other models being used
66 RFieldToken(std::size_t index, std::uint64_t modelId) : fIndex(index), fModelId(modelId) {}
67 };
68
69private:
70 /// The entry must be linked to a specific model (or one if its clones), identified by a model ID
71 std::uint64_t fModelId = 0;
72 /// Corresponds to the top-level fields of the linked model
73 std::vector<RFieldBase::RValue> fValues;
74 /// For fast lookup of token IDs given a top-level field name
75 std::unordered_map<std::string, std::size_t> fFieldName2Token;
76
77 // Creation of entries is done by the RNTupleModel class
78
79 REntry() = default;
80 explicit REntry(std::uint64_t modelId) : fModelId(modelId) {}
81
83 {
84 fFieldName2Token[value.GetField().GetFieldName()] = fValues.size();
85 fValues.emplace_back(std::move(value));
86 }
87
88 /// While building the entry, adds a new value to the list and return the value's shared pointer
89 template <typename T, typename... ArgsT>
90 std::shared_ptr<T> AddValue(RField<T> &field, ArgsT &&...args)
91 {
92 fFieldName2Token[field.GetFieldName()] = fValues.size();
93 auto ptr = std::make_shared<T>(std::forward<ArgsT>(args)...);
94 fValues.emplace_back(field.BindValue(ptr));
95 return ptr;
96 }
97
98 /// Update the RValue for a field in the entry. To be used when its underlying RFieldBase changes, which typically
99 /// happens when page source the field values are read from changes.
100 void UpdateValue(RFieldToken token, RFieldBase::RValue &&value) { std::swap(fValues.at(token.fIndex), value); }
101 void UpdateValue(RFieldToken token, RFieldBase::RValue &value) { std::swap(fValues.at(token.fIndex), value); }
102
104 {
105 for (auto &v : fValues) {
106 v.Read(index);
107 }
108 }
109
110 std::size_t Append()
111 {
112 std::size_t bytesWritten = 0;
113 for (auto &v : fValues) {
114 bytesWritten += v.Append();
115 }
116 return bytesWritten;
117 }
118
120 {
121 if (fModelId != token.fModelId) {
122 throw RException(R__FAIL("invalid token for this entry, "
123 "make sure to use a token from the same model as this entry."));
124 }
125 }
126
127 template <typename T>
128 void EnsureMatchingType(RFieldToken token [[maybe_unused]]) const
129 {
130 if constexpr (!std::is_void_v<T>) {
131 const auto &v = fValues[token.fIndex];
132 if (v.GetField().GetTypeName() != RField<T>::TypeName()) {
133 throw RException(R__FAIL("type mismatch for field " + v.GetField().GetFieldName() + ": " +
134 v.GetField().GetTypeName() + " vs. " + RField<T>::TypeName()));
135 }
136 }
137 }
138
139public:
140 using ConstIterator_t = decltype(fValues)::const_iterator;
141
142 REntry(const REntry &other) = delete;
143 REntry &operator=(const REntry &other) = delete;
144 REntry(REntry &&other) = default;
145 REntry &operator=(REntry &&other) = default;
146 ~REntry() = default;
147
148 /// The ordinal of the top-level field fieldName; can be used in other methods to address the corresponding value
149 RFieldToken GetToken(std::string_view fieldName) const
150 {
151 auto it = fFieldName2Token.find(std::string(fieldName));
152 if (it == fFieldName2Token.end()) {
153 throw RException(R__FAIL("invalid field name: " + std::string(fieldName)));
154 }
155 return RFieldToken(it->second, fModelId);
156 }
157
159 {
160 EnsureMatchingModel(token);
161 fValues[token.fIndex].EmplaceNew();
162 }
163
164 void EmplaceNewValue(std::string_view fieldName) { EmplaceNewValue(GetToken(fieldName)); }
165
166 template <typename T>
167 void BindValue(RFieldToken token, std::shared_ptr<T> objPtr)
168 {
169 EnsureMatchingModel(token);
170 EnsureMatchingType<T>(token);
171 fValues[token.fIndex].Bind(objPtr);
172 }
173
174 template <typename T>
175 void BindValue(std::string_view fieldName, std::shared_ptr<T> objPtr)
176 {
177 BindValue<T>(GetToken(fieldName), objPtr);
178 }
179
180 template <typename T>
181 void BindRawPtr(RFieldToken token, T *rawPtr)
182 {
183 EnsureMatchingModel(token);
184 EnsureMatchingType<T>(token);
185 fValues[token.fIndex].BindRawPtr(rawPtr);
186 }
187
188 template <typename T>
189 void BindRawPtr(std::string_view fieldName, T *rawPtr)
190 {
191 BindRawPtr<void>(GetToken(fieldName), rawPtr);
192 }
193
194 template <typename T>
195 std::shared_ptr<T> GetPtr(RFieldToken token) const
196 {
197 EnsureMatchingModel(token);
198 EnsureMatchingType<T>(token);
199 return std::static_pointer_cast<T>(fValues[token.fIndex].GetPtr<void>());
200 }
201
202 template <typename T>
203 std::shared_ptr<T> GetPtr(std::string_view fieldName) const
204 {
205 return GetPtr<T>(GetToken(fieldName));
206 }
207
208 std::uint64_t GetModelId() const { return fModelId; }
209
210 ConstIterator_t begin() const { return fValues.cbegin(); }
211 ConstIterator_t end() const { return fValues.cend(); }
212};
213
214} // namespace Experimental
215} // namespace ROOT
216
217#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 top-level field in this entry.
Definition REntry.hxx:60
std::uint64_t fModelId
Safety check to prevent tokens from other models being used.
Definition REntry.hxx:65
std::size_t fIndex
the index in fValues that belongs to the top-level field
Definition REntry.hxx:64
RFieldToken(std::size_t index, std::uint64_t modelId)
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:50
void EnsureMatchingType(RFieldToken token) const
Definition REntry.hxx:128
std::uint64_t fModelId
The entry must be linked to a specific model (or one if its clones), identified by a model ID.
Definition REntry.hxx:71
void BindValue(RFieldToken token, std::shared_ptr< T > objPtr)
Definition REntry.hxx:167
REntry & operator=(REntry &&other)=default
decltype(fValues)::const_iterator ConstIterator_t
Definition REntry.hxx:140
void Read(NTupleSize_t index)
Definition REntry.hxx:103
ConstIterator_t begin() const
Definition REntry.hxx:210
REntry & operator=(const REntry &other)=delete
REntry(std::uint64_t modelId)
Definition REntry.hxx:80
void EmplaceNewValue(std::string_view fieldName)
Definition REntry.hxx:164
REntry(REntry &&other)=default
std::uint64_t GetModelId() const
Definition REntry.hxx:208
void EmplaceNewValue(RFieldToken token)
Definition REntry.hxx:158
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:90
void BindRawPtr(RFieldToken token, T *rawPtr)
Definition REntry.hxx:181
std::unordered_map< std::string, std::size_t > fFieldName2Token
For fast lookup of token IDs given a top-level field name.
Definition REntry.hxx:75
void BindValue(std::string_view fieldName, std::shared_ptr< T > objPtr)
Definition REntry.hxx:175
void UpdateValue(RFieldToken token, RFieldBase::RValue &&value)
Update the RValue for a field in the entry.
Definition REntry.hxx:100
RFieldToken GetToken(std::string_view fieldName) const
The ordinal of the top-level field fieldName; can be used in other methods to address the correspondi...
Definition REntry.hxx:149
void AddValue(RFieldBase::RValue &&value)
Definition REntry.hxx:82
void UpdateValue(RFieldToken token, RFieldBase::RValue &value)
Definition REntry.hxx:101
std::vector< RFieldBase::RValue > fValues
Corresponds to the top-level fields of the linked model.
Definition REntry.hxx:73
void BindRawPtr(std::string_view fieldName, T *rawPtr)
Definition REntry.hxx:189
ConstIterator_t end() const
Definition REntry.hxx:211
void EnsureMatchingModel(RFieldToken token) const
Definition REntry.hxx:119
REntry(const REntry &other)=delete
std::shared_ptr< T > GetPtr(std::string_view fieldName) const
Definition REntry.hxx:203
std::shared_ptr< T > GetPtr(RFieldToken token) const
Definition REntry.hxx:195
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.
const std::string & GetFieldName() const
RValue BindValue(std::shared_ptr< void > objPtr)
Creates a value from a memory location with an already constructed object.
Classes with dictionaries that can be inspected by TClass.
Definition RField.hxx:239
static std::string TypeName()
Definition RField.hxx:252
A virtual ntuple used for writing untyped collections that can be used to some extent like an RNTuple...
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...