Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
RFieldBase.hxx
Go to the documentation of this file.
1/// \file ROOT/RFieldBase.hxx
2/// \author Jakob Blomer <jblomer@cern.ch>
3/// \date 2018-10-09
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_RFieldBase
14#define ROOT_RFieldBase
15
16#include <ROOT/RColumn.hxx>
18#include <ROOT/RError.hxx>
19#include <ROOT/RFieldUtils.hxx>
20#include <ROOT/RNTupleRange.hxx>
21#include <ROOT/RNTupleTypes.hxx>
22
23#include <atomic>
24#include <cstddef>
25#include <functional>
26#include <iterator>
27#include <memory>
28#include <new>
29#include <string>
30#include <string_view>
31#include <typeinfo>
32#include <type_traits>
33#include <vector>
34
35namespace ROOT {
36
37class REntry;
38class RFieldBase;
39class RClassField;
40
41namespace Detail {
42class RFieldVisitor;
44} // namespace Detail
45
46namespace Experimental {
48}
49
50namespace Internal {
51
52class RPageSink;
53class RPageSource;
54struct RFieldCallbackInjector;
56
57// TODO(jblomer): find a better way to not have these methods in the RFieldBase public API
58void CallFlushColumnsOnField(RFieldBase &);
59void CallCommitClusterOnField(RFieldBase &);
60void CallConnectPageSinkOnField(RFieldBase &, ROOT::Internal::RPageSink &, ROOT::NTupleSize_t firstEntry = 0);
61void CallConnectPageSourceOnField(RFieldBase &, ROOT::Internal::RPageSource &);
62ROOT::RResult<std::unique_ptr<ROOT::RFieldBase>>
63CallFieldBaseCreate(const std::string &fieldName, const std::string &typeName, const ROOT::RCreateFieldOptions &options,
64 const ROOT::RNTupleDescriptor *desc, ROOT::DescriptorId_t fieldId);
65
66} // namespace Internal
67
68namespace Experimental::Internal {
69struct RNTupleAttrEntry;
70}
71
72// clang-format off
73/**
74\class ROOT::RFieldBase
75\ingroup NTuple
76\brief A field translates read and write calls from/to underlying columns to/from tree values
77
78A field is a serializable C++ type or a container for a collection of subfields. The RFieldBase and its
79type-safe descendants provide the object to column mapper. They map C++ objects to primitive columns. The
80mapping is trivial for simple types such as 'double'. Complex types resolve to multiple primitive columns.
81The field knows based on its type and the field name the type(s) and name(s) of the columns.
82
83Note: the class hierarchy starting at RFieldBase is not meant to be extended by user-provided child classes.
84This is and can only be partially enforced through C++.
85*/
86// clang-format on
88 friend class RFieldZero; // to reset fParent pointer in ReleaseSubfields()
89 friend class ROOT::Detail::RRawPtrWriteEntry; // to call Append()
90 friend class ROOT::Experimental::RNTupleAttrSetReader; // for field->Read() in LoadEntry()
91 friend struct ROOT::Internal::RFieldCallbackInjector; // used for unit tests
92 friend struct ROOT::Internal::RFieldRepresentationModifier; // used for unit tests
98 Internal::CallFieldBaseCreate(const std::string &fieldName, const std::string &typeName,
99 const ROOT::RCreateFieldOptions &options, const ROOT::RNTupleDescriptor *desc,
100 ROOT::DescriptorId_t fieldId);
101
102 using ReadCallback_t = std::function<void(void *)>;
103
104protected:
105 /// A functor to release the memory acquired by CreateValue() (memory and constructor).
106 /// This implementation works for types with a trivial destructor. More complex fields implement a derived deleter.
107 /// The deleter is operational without the field object and thus can be used to destruct/release a value after
108 /// the field has been destructed.
109 class RDeleter {
110 public:
111 virtual ~RDeleter() = default;
112 virtual void operator()(void *objPtr, bool dtorOnly)
113 {
114 if (!dtorOnly)
115 operator delete(objPtr);
116 }
117 };
118
119 /// A deleter for templated RFieldBase descendents where the value type is known.
120 template <typename T>
121 class RTypedDeleter : public RDeleter {
122 public:
123 void operator()(void *objPtr, bool dtorOnly) final
124 {
125 std::destroy_at(static_cast<T *>(objPtr));
126 RDeleter::operator()(objPtr, dtorOnly);
127 }
128 };
129
130 // We cannot directly use RFieldBase::RDeleter as a shared pointer deleter due to splicing. We use this
131 // wrapper class to store a polymorphic pointer to the actual deleter.
133 std::unique_ptr<RFieldBase::RDeleter> fDeleter;
134 void operator()(void *objPtr) { fDeleter->operator()(objPtr, false /* dtorOnly*/); }
135 explicit RSharedPtrDeleter(std::unique_ptr<RFieldBase::RDeleter> deleter) : fDeleter(std::move(deleter)) {}
136 };
137
138public:
139 static constexpr std::uint32_t kInvalidTypeVersion = -1U;
140 enum {
141 /// No constructor needs to be called, i.e. any bit pattern in the allocated memory represents a valid type
142 /// A trivially constructible field has a no-op ConstructValue() implementation
144 /// The type is cleaned up just by freeing its memory. I.e. the destructor performs a no-op.
146 /// A field of a fundamental type that can be directly mapped via RField<T>::Map(), i.e. maps as-is to a single
147 /// column
149 /// The TClass checksum is set and valid
151 /// This field is an instance of RInvalidField and can be safely `static_cast` to it
153 /// This field is a user defined type that was missing dictionaries and was reconstructed from the on-disk
154 /// information
156 /// Can attach new item fields even when already connected
158 /// The field represents a collection in SoA layout
160
161 /// Shorthand for types that are both trivially constructible and destructible
163 };
164
165 using ColumnRepresentation_t = std::vector<ROOT::ENTupleColumnType>;
166
167 /// During its lifetime, a field undergoes the following possible state transitions:
168 ///
169 /// [*] --> Unconnected --> ConnectedToSink ----
170 /// | | |
171 /// | --> ConnectedToSource ---> [*]
172 /// | |
173 /// -------------------------------
179
180 // clang-format off
181 /**
182 \class ROOT::RFieldBase::RColumnRepresentations
183 \ingroup NTuple
184 \brief The list of column representations a field can have.
185
186 Some fields have multiple possible column representations, e.g. with or without split encoding.
187 All column representations supported for writing also need to be supported for reading. In addition,
188 fields can support extra column representations for reading only, e.g. a 64bit integer reading from a
189 32bit column.
190 The defined column representations must be supported by corresponding column packing/unpacking implementations,
191 i.e. for the example above, the unpacking of 32bit ints to 64bit pages must be implemented in RColumnElement.hxx
192 */
193 // clang-format on
195 public:
196 /// A list of column representations
197 using Selection_t = std::vector<ColumnRepresentation_t>;
198
200 RColumnRepresentations(const Selection_t &serializationTypes, const Selection_t &deserializationExtraTypes);
201
202 /// The first column list from `fSerializationTypes` is the default for writing.
206
207 private:
209 /// The union of the serialization types and the deserialization extra types passed during construction.
210 /// Duplicates the serialization types list but the benefit is that GetDeserializationTypes() does not need to
211 /// compile the list.
213 }; // class RColumnRepresentations
214
215 class RValue;
216 class RBulkValues;
217
218private:
219 /// The field name relative to its parent field
220 std::string fName;
221 /// The C++ type captured by this field
222 std::string fType;
223 /// The role of this field in the data model structure
225 /// For fixed sized arrays, the array length
226 std::size_t fNRepetitions;
227 /// A field qualifies as simple if it is mappable (which implies it has a single principal column),
228 /// and it is not an artificial field and has no post-read callback
230 /// A field that is not backed on disk but computed, e.g. a default-constructed missing field or
231 /// a field whose data is created by I/O customization rules. Subfields of artificial fields are
232 /// artificial, too.
233 bool fIsArtificial = false;
234 /// When the columns are connected to a page source or page sink, the field represents a field id in the
235 /// corresponding RNTuple descriptor. This on-disk ID is set in RPageSink::Create() for writing and by
236 /// RFieldDescriptor::CreateField() when recreating a field / model from the stored descriptor.
238 /// Free text set by the user
239 std::string fDescription;
240 /// Changed by ConnectTo[Sink,Source], reset by Clone()
242
243 void InvokeReadCallbacks(void *target)
244 {
245 for (const auto &func : fReadCallbacks)
246 func(target);
247 }
248
249 /// Translate an entry index to a column element index of the principal column and vice versa. These functions
250 /// take into account the role and number of repetitions on each level of the field hierarchy as follows:
251 /// - Top level fields: element index == entry index
252 /// - Record fields propagate their principal column index to the principal columns of direct descendant fields
253 /// - Collection and variant fields set the principal column index of their children to 0
254 ///
255 /// The column element index also depends on the number of repetitions of each field in the hierarchy, e.g., given a
256 /// field with type `std::array<std::array<float, 4>, 2>`, this function returns 8 for the innermost field.
258
259 /// Flushes data from active columns
260 void FlushColumns();
261 /// Flushes data from active columns to disk and calls CommitClusterImpl()
262 void CommitCluster();
263 /// Fields and their columns live in the void until connected to a physical page storage. Only once connected, data
264 /// can be read or written. In order to find the field in the page storage, the field's on-disk ID has to be set.
265 /// \param firstEntry The global index of the first entry with on-disk data for the connected field
266 void ConnectPageSink(ROOT::Internal::RPageSink &pageSink, ROOT::NTupleSize_t firstEntry = 0);
267 /// Connects the field and its subfield tree to the given page source. Once connected, data can be read.
268 /// Only unconnected fields may be connected, i.e. the method is not idempotent. The field ID has to be set prior to
269 /// calling this function. For subfields, a field ID may or may not be set. If the field ID is unset, it will be
270 /// determined using the page source descriptor, based on the parent field ID and the subfield name.
272
274 {
275 fIsSimple = false;
276 fIsArtificial = true;
277 for (auto &field : fSubfields) {
278 field->SetArtificial();
279 }
280 }
281
282protected:
283 struct RBulkSpec;
284
285 /// Bits used in CompareOnDisk()
286 enum {
287 /// The in-memory field and the on-disk field differ in the field version
289 /// The in-memory field and the on-disk field differ in the type version
291 /// The in-memory field and the on-disk field differ in their structural roles
293 /// The in-memory field and the on-disk field have different type names
295 /// The in-memory field and the on-disk field have different repetition counts
297 };
298
299 /// Collections and classes own subfields
300 std::vector<std::unique_ptr<RFieldBase>> fSubfields;
301 /// Subfields point to their mother field
303 /// All fields that have columns have a distinct main column. E.g., for simple fields (`float`, `int`, ...), the
304 /// principal column corresponds to the field type. For collection fields except fixed-sized arrays,
305 /// the main column is the offset field. Class fields have no column of their own.
306 /// When reading, points to any column of the column team of the active representation. Usually, this is just
307 /// the first column.
308 /// When writing, points to the first column index of the currently active (not suppressed) column representation.
310 /// Some fields have a second column in its column representation. In this case, `fAuxiliaryColumn` points into
311 /// `fAvailableColumns` to the column that immediately follows the column `fPrincipalColumn` points to.
313 /// The columns are connected either to a sink or to a source (not to both); they are owned by the field.
314 /// Contains all columns of all representations in order of representation and column index.
315 std::vector<std::unique_ptr<ROOT::Internal::RColumn>> fAvailableColumns;
316 /// Properties of the type that allow for optimizations of collections of that type
317 std::uint32_t fTraits = 0;
318 /// A typedef or using name that was used when creating the field
319 std::string fTypeAlias;
320 /// List of functions to be called after reading a value
321 std::vector<ReadCallback_t> fReadCallbacks;
322 /// C++ type version cached from the descriptor after a call to ConnectPageSource()
324 /// TClass checksum cached from the descriptor after a call to ConnectPageSource(). Only set
325 /// for classes with dictionaries.
326 std::uint32_t fOnDiskTypeChecksum = 0;
327 /// Pointers into the static vector returned by RColumnRepresentations::GetSerializationTypes() when
328 /// SetColumnRepresentatives() is called. Otherwise (if empty) GetColumnRepresentatives() returns a vector
329 /// with a single element, the default representation. Always empty for artificial fields.
330 std::vector<std::reference_wrapper<const ColumnRepresentation_t>> fColumnRepresentatives;
331
332 /// Factory method for the field's type. The caller owns the returned pointer
333 void *CreateObjectRawPtr() const;
334
335 /// Helpers for generating columns. We use the fact that most fields have the same C++/memory types
336 /// for all their column representations.
337 /// Where possible, we call the helpers not from the header to reduce compilation time.
338 template <std::uint32_t ColumnIndexT, typename HeadT, typename... TailTs>
339 void GenerateColumnsImpl(const ColumnRepresentation_t &representation, std::uint16_t representationIndex)
340 {
341 assert(ColumnIndexT < representation.size());
342 auto &column = fAvailableColumns.emplace_back(
343 ROOT::Internal::RColumn::Create<HeadT>(representation[ColumnIndexT], ColumnIndexT, representationIndex));
344
345 // Initially, the first two columns become the active column representation
346 if (representationIndex == 0 && !fPrincipalColumn) {
347 fPrincipalColumn = column.get();
348 } else if (representationIndex == 0 && !fAuxiliaryColumn) {
349 fAuxiliaryColumn = column.get();
350 } else {
351 // We currently have no fields with more than 2 columns in its column representation
352 R__ASSERT(representationIndex > 0);
353 }
354
355 if constexpr (sizeof...(TailTs))
356 GenerateColumnsImpl<ColumnIndexT + 1, TailTs...>(representation, representationIndex);
357 }
358
359 /// For writing, use the currently set column representative
360 template <typename... ColumnCppTs>
362 {
363 if (fColumnRepresentatives.empty()) {
364 fAvailableColumns.reserve(sizeof...(ColumnCppTs));
366 } else {
367 const auto N = fColumnRepresentatives.size();
368 fAvailableColumns.reserve(N * sizeof...(ColumnCppTs));
369 for (unsigned i = 0; i < N; ++i) {
370 GenerateColumnsImpl<0, ColumnCppTs...>(fColumnRepresentatives[i].get(), i);
371 }
372 }
373 }
374
375 /// For reading, use the on-disk column list
376 template <typename... ColumnCppTs>
378 {
379 std::uint16_t representationIndex = 0;
380 do {
381 const auto &onDiskTypes = EnsureCompatibleColumnTypes(desc, representationIndex);
382 if (onDiskTypes.empty())
383 break;
384 GenerateColumnsImpl<0, ColumnCppTs...>(onDiskTypes, representationIndex);
385 fColumnRepresentatives.emplace_back(onDiskTypes);
386 if (representationIndex > 0) {
387 for (std::size_t i = 0; i < sizeof...(ColumnCppTs); ++i) {
388 fAvailableColumns[i]->MergeTeams(
389 *fAvailableColumns[representationIndex * sizeof...(ColumnCppTs) + i].get());
390 }
391 }
392 representationIndex++;
393 } while (true);
394 }
395
396 /// Implementations in derived classes should return a static RColumnRepresentations object. The default
397 /// implementation does not attach any columns to the field.
398 virtual const RColumnRepresentations &GetColumnRepresentations() const;
399 /// Implementations in derived classes should create the backing columns corresponding to the field type for
400 /// writing. The default implementation does not attach any columns to the field.
401 virtual void GenerateColumns() {}
402 /// Implementations in derived classes should create the backing columns corresponding to the field type for reading.
403 /// The default implementation does not attach any columns to the field. The method should check, using the page
404 /// source and `fOnDiskId`, if the column types match and throw if they don't.
405 virtual void GenerateColumns(const ROOT::RNTupleDescriptor & /*desc*/) {}
406 /// Returns the on-disk column types found in the provided descriptor for `fOnDiskId` and the given
407 /// representation index. If there are no columns for the given representation index, return an empty
408 /// ColumnRepresentation_t list. Otherwise, the returned reference points into the static array returned by
409 /// GetColumnRepresentations().
410 /// Throws an exception if the types on disk don't match any of the deserialization types from
411 /// GetColumnRepresentations().
413 EnsureCompatibleColumnTypes(const ROOT::RNTupleDescriptor &desc, std::uint16_t representationIndex) const;
414 /// When connecting a field to a page sink, the field's default column representation is subject
415 /// to adjustment according to the write options. E.g., if compression is turned off, encoded columns
416 /// are changed to their unencoded counterparts.
418
419 /// Called by Clone(), which additionally copies the on-disk ID
420 virtual std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const = 0;
421
422 /// Constructs value in a given location of size at least GetValueSize(). Called by the base class' CreateValue().
423 virtual void ConstructValue(void *where) const = 0;
424 virtual std::unique_ptr<RDeleter> GetDeleter() const { return std::make_unique<RDeleter>(); }
425 /// Allow derived classes to call ConstructValue(void *) and GetDeleter() on other (sub)fields.
426 static void CallConstructValueOn(const RFieldBase &other, void *where) { other.ConstructValue(where); }
427 static std::unique_ptr<RDeleter> GetDeleterOf(const RFieldBase &other) { return other.GetDeleter(); }
428
429 /// Allow parents to mark their childs as artificial fields (used in class and record fields)
430 static void CallSetArtificialOn(RFieldBase &other) { other.SetArtificial(); }
431
432 /// Operations on values of complex types, e.g. ones that involve multiple columns or for which no direct
433 /// column type exists.
434 virtual std::size_t AppendImpl(const void *from);
435 virtual void ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to);
436 virtual void ReadInClusterImpl(RNTupleLocalIndex localIndex, void *to);
437
438 /// Write the given value into columns. The value object has to be of the same type as the field.
439 /// Returns the number of uncompressed bytes written.
440 std::size_t Append(const void *from);
441
442 /// Populate a single value with data from the field. The memory location pointed to by to needs to be of the
443 /// fitting type. The fast path is conditioned by the field qualifying as simple, i.e. maps as-is
444 /// to a single column and has no read callback.
445 void Read(ROOT::NTupleSize_t globalIndex, void *to)
446 {
447 if (fIsSimple)
448 return (void)fPrincipalColumn->Read(globalIndex, to);
449
450 if (!fIsArtificial) {
452 fPrincipalColumn->Read(globalIndex, to);
453 else
454 ReadGlobalImpl(globalIndex, to);
455 }
456 if (R__unlikely(!fReadCallbacks.empty()))
458 }
459
460 /// Populate a single value with data from the field. The memory location pointed to by to needs to be of the
461 /// fitting type. The fast path is conditioned by the field qualifying as simple, i.e. maps as-is
462 /// to a single column and has no read callback.
463 void Read(RNTupleLocalIndex localIndex, void *to)
464 {
465 if (fIsSimple)
466 return (void)fPrincipalColumn->Read(localIndex, to);
467
468 if (!fIsArtificial) {
470 fPrincipalColumn->Read(localIndex, to);
471 else
472 ReadInClusterImpl(localIndex, to);
473 }
474 if (R__unlikely(!fReadCallbacks.empty()))
476 }
477
478 /// General implementation of bulk read. Loop over the required range and read values that are required
479 /// and not already present. Derived classes may implement more optimized versions of this method.
480 /// See ReadBulk() for the return value.
481 virtual std::size_t ReadBulkImpl(const RBulkSpec &bulkSpec);
482
483 /// Returns the number of newly available values, that is the number of bools in `bulkSpec.fMaskAvail` that
484 /// flipped from false to true. As a special return value, `kAllSet` can be used if all values are read
485 /// independent from the masks.
486 std::size_t ReadBulk(const RBulkSpec &bulkSpec);
487
488 /// Allow derived classes to call Append() and Read() on other (sub)fields.
489 static std::size_t CallAppendOn(RFieldBase &other, const void *from) { return other.Append(from); }
490 static void CallReadOn(RFieldBase &other, RNTupleLocalIndex localIndex, void *to) { other.Read(localIndex, to); }
491 static void CallReadOn(RFieldBase &other, ROOT::NTupleSize_t globalIndex, void *to) { other.Read(globalIndex, to); }
492 static void *CallCreateObjectRawPtrOn(RFieldBase &other) { return other.CreateObjectRawPtr(); }
493
494 /// Fields may need direct access to the principal column of their subfields, e.g. in RRVecField::ReadBulk()
496
497 /// Set a user-defined function to be called after reading a value, giving a chance to inspect and/or modify the
498 /// value object.
499 /// Returns an index that can be used to remove the callback.
500 size_t AddReadCallback(ReadCallback_t func);
501 void RemoveReadCallback(size_t idx);
502
503 // Perform housekeeping tasks for global to cluster-local index translation
504 virtual void CommitClusterImpl() {}
505 // The field can indicate that it needs to register extra type information in the on-disk schema.
506 // In this case, a callback from the page sink to the field will be registered on connect, so that the
507 // extra type information can be collected when the dataset gets committed.
508 virtual bool HasExtraTypeInfo() const { return false; }
509 // The page sink's callback when the data set gets committed will call this method to get the field's extra
510 // type information. This has to happen at the end of writing because the type information may change depending
511 // on the data that's written, e.g. for polymorphic types in the streamer field.
513
514 /// Add a new subfield to the list of nested fields. Throws an exception if childName is non-empty and the passed
515 /// field has a different name.
516 void Attach(std::unique_ptr<RFieldBase> child, std::string_view expectedChildName = "");
517
518 /// Called by ConnectPageSource() before connecting; derived classes may override this as appropriate, e.g.
519 /// for the application of I/O rules. In the process, the field at hand or its subfields may be marked as
520 /// "artifical", i.e. introduced by schema evolution and not backed by on-disk information.
521 /// May return a field substitute that fits the on-disk schema as a replacement for the field at hand.
522 /// A field substitute must read into the same in-memory layout than the original field and field substitutions
523 /// must not be cyclic.
524 virtual std::unique_ptr<RFieldBase> BeforeConnectPageSource(ROOT::Internal::RPageSource & /* source */)
525 {
526 return nullptr;
527 }
528
529 /// For non-artificial fields, check compatibility of the in-memory field and the on-disk field. In the process,
530 /// the field at hand may change its on-disk ID or perform other tasks related to automatic schema evolution.
531 /// If the on-disk field is incompatible with the in-memory field at hand, an exception is thrown.
532 virtual void ReconcileOnDiskField(const RNTupleDescriptor &desc);
533
534 /// Returns a combination of kDiff... flags, indicating peroperties that are different between the field at hand
535 /// and the given on-disk field
536 std::uint32_t CompareOnDiskField(const RFieldDescriptor &fieldDesc, std::uint32_t ignoreBits) const;
537 /// Compares the field to the corresponding on-disk field information in the provided descriptor.
538 /// Throws an exception if the fields don't match.
539 /// Optionally, a set of bits can be provided that should be ignored in the comparison.
540 RResult<void> EnsureMatchingOnDiskField(const RNTupleDescriptor &desc, std::uint32_t ignoreBits = 0) const;
541 /// Convenience wrapper for the common case of calling EnsureMatchinOnDiskField() for collections. Collections
542 /// may differ in type name (most collections schema evolve into each other). An on-disk SoA collection may also
543 /// have any type version whereas all other collections need to have type version 0.
545 /// Many fields accept a range of type prefixes for schema evolution,
546 /// e.g. std::unique_ptr< and std::optional< for nullable fields
548 EnsureMatchingTypePrefix(const RNTupleDescriptor &desc, const std::vector<std::string> &prefixes) const;
549
550 /// Factory method to resurrect a field from the stored on-disk type information. This overload takes an already
551 /// normalized type name and type alias.
552 /// `desc` and `fieldId` must be passed if `options.fEmulateUnknownTypes` is true, otherwise they can be left blank.
554 Create(const std::string &fieldName, const std::string &typeName, const ROOT::RCreateFieldOptions &options,
555 const ROOT::RNTupleDescriptor *desc, ROOT::DescriptorId_t fieldId);
556
557public:
558 template <bool IsConstT>
559 class RSchemaIteratorTemplate;
562
563 // This is used in CreateObject() and is specialized for void
564 template <typename T>
566 using deleter = std::default_delete<T>;
567 };
568
569 /// Used in the return value of the Check() method
571 std::string fFieldName; ///< Qualified field name causing the error
572 std::string fTypeName; ///< Type name corresponding to the (sub)field
573 std::string fErrMsg; ///< Cause of the failure, e.g. unsupported type
574 };
575
576 /// The constructor creates the underlying column objects and connects them to either a sink or a source.
577 /// If `isSimple` is `true`, the trait `kTraitMappable` is automatically set on construction. However, the
578 /// field might be demoted to non-simple if a post-read callback is set.
579 RFieldBase(std::string_view name, std::string_view type, ROOT::ENTupleStructure structure, bool isSimple,
580 std::size_t nRepetitions = 0);
581 RFieldBase(const RFieldBase &) = delete;
582 RFieldBase(RFieldBase &&) = default;
583 RFieldBase &operator=(const RFieldBase &) = delete;
585 virtual ~RFieldBase() = default;
586
587 /// Copies the field and its subfields using a possibly new name and a new, unconnected set of columns
588 std::unique_ptr<RFieldBase> Clone(std::string_view newName) const;
589
590 /// Factory method to create a field from a certain type given as string.
591 /// Note that the provided type name must be a valid C++ type name. Template arguments of templated types
592 /// must be type names or integers (e.g., no expressions).
594 Create(const std::string &fieldName, const std::string &typeName);
595
596 /// Checks if the given type is supported by RNTuple. In case of success, the result vector is empty.
597 /// Otherwise there is an error record for each failing subfield (subtype).
598 static std::vector<RCheckResult> Check(const std::string &fieldName, const std::string &typeName);
599
600 /// Generates an object of the field type and allocates new initialized memory according to the type.
601 /// Implemented at the end of this header because the implementation is using RField<T>::TypeName()
602 /// The returned object can be released with `delete`, i.e. it is valid to call:
603 /// ~~~{.cpp}
604 /// auto ptr = field->CreateObject();
605 /// delete ptr.release();
606 /// ~~~
607 ///
608 /// Note that CreateObject<void>() is supported. The returned `unique_ptr` has a custom deleter that reports an error
609 /// if it is called. The intended use of the returned `unique_ptr<void>` is to call `release()`. In this way, the
610 /// transfer of pointer ownership is explicit.
611 template <typename T>
612 std::unique_ptr<T, typename RCreateObjectDeleter<T>::deleter> CreateObject() const;
613 /// Generates an object of the field's type, wraps it in a shared pointer and returns it as an RValue connected to
614 /// the field.
616 /// Creates a new, initially empty bulk.
617 /// RBulkValues::ReadBulk() will construct the array of values. The memory of the value array is managed by the
618 /// RBulkValues class.
620 /// Creates a value from a memory location with an already constructed object
621 RValue BindValue(std::shared_ptr<void> objPtr);
622 /// Creates the list of direct child values given an existing value for this field. E.g. a single value for the
623 /// correct `std::variant` or all the elements of a collection. The default implementation assumes no subvalues
624 /// and returns an empty vector.
625 virtual std::vector<RValue> SplitValue(const RValue &value) const;
626 /// The number of bytes taken by a value of the appropriate type
627 virtual size_t GetValueSize() const = 0;
628 /// As a rule of thumb, the alignment is equal to the size of the type. There are, however, various exceptions
629 /// to this rule depending on OS and CPU architecture. So enforce the alignment to be explicitly spelled out.
630 virtual size_t GetAlignment() const = 0;
631 std::uint32_t GetTraits() const { return fTraits; }
632 bool HasReadCallbacks() const { return !fReadCallbacks.empty(); }
633
634 const std::string &GetFieldName() const { return fName; }
635 /// Returns the field name and parent field names separated by dots (`grandparent.parent.child`)
636 std::string GetQualifiedFieldName() const;
637 const std::string &GetTypeName() const { return fType; }
638 const std::string &GetTypeAlias() const { return fTypeAlias; }
640 std::size_t GetNRepetitions() const { return fNRepetitions; }
641 const RFieldBase *GetParent() const { return fParent; }
642 std::vector<RFieldBase *> GetMutableSubfields();
643 std::vector<const RFieldBase *> GetConstSubfields() const;
644 bool IsSimple() const { return fIsSimple; }
645 bool IsArtificial() const { return fIsArtificial; }
646 /// Get the field's description
647 const std::string &GetDescription() const { return fDescription; }
648 void SetDescription(std::string_view description);
649 EState GetState() const { return fState; }
650
653
654 /// Returns the `fColumnRepresentative` pointee or, if unset (always the case for artificial fields), the field's
655 /// default representative
657 /// Fixes a column representative. This can only be done _before_ connecting the field to a page sink.
658 /// Otherwise, or if the provided representation is not in the list of GetColumnRepresentations(),
659 /// an exception is thrown
661 /// Whether or not an explicit column representative was set
663
664 /// Indicates an evolution of the mapping scheme from C++ type to columns
665 virtual std::uint32_t GetFieldVersion() const { return 0; }
666 /// Indicates an evolution of the C++ type itself
667 virtual std::uint32_t GetTypeVersion() const { return 0; }
668 /// Return the current TClass reported checksum of this class. Only valid if `kTraitTypeChecksum` is set.
669 virtual std::uint32_t GetTypeChecksum() const { return 0; }
670 /// Return the C++ type version stored in the field descriptor; only valid after a call to ConnectPageSource()
671 std::uint32_t GetOnDiskTypeVersion() const { return fOnDiskTypeVersion; }
672 /// Return checksum stored in the field descriptor; only valid after a call to ConnectPageSource(),
673 /// if the field stored a type checksum
674 std::uint32_t GetOnDiskTypeChecksum() const { return fOnDiskTypeChecksum; }
675
682
683 virtual void AcceptVisitor(ROOT::Detail::RFieldVisitor &visitor) const;
684}; // class RFieldBase
685
686/// Iterates over the subtree of fields in depth-first search order
687template <bool IsConstT>
689private:
690 struct Position {
691 using FieldPtr_t = std::conditional_t<IsConstT, const RFieldBase *, RFieldBase *>;
692 Position() : fFieldPtr(nullptr), fIdxInParent(-1) {}
693 Position(FieldPtr_t fieldPtr, int idxInParent) : fFieldPtr(fieldPtr), fIdxInParent(idxInParent) {}
696 };
697 /// The stack of nodes visited when walking down the tree of fields
698 std::vector<Position> fStack;
699
700public:
702 using iterator_category = std::forward_iterator_tag;
703 using difference_type = std::ptrdiff_t;
704 using value_type = std::conditional_t<IsConstT, const RFieldBase, RFieldBase>;
705 using pointer = std::conditional_t<IsConstT, const RFieldBase *, RFieldBase *>;
706 using reference = std::conditional_t<IsConstT, const RFieldBase &, RFieldBase &>;
707
708 RSchemaIteratorTemplate() { fStack.emplace_back(Position()); }
709 RSchemaIteratorTemplate(pointer val, int idxInParent) { fStack.emplace_back(Position(val, idxInParent)); }
711 /// Given that the iterator points to a valid field which is not the end iterator, go to the next field
712 /// in depth-first search order
713 void Advance()
714 {
715 auto itr = fStack.rbegin();
716 if (!itr->fFieldPtr->fSubfields.empty()) {
717 fStack.emplace_back(Position(itr->fFieldPtr->fSubfields[0].get(), 0));
718 return;
719 }
720
721 unsigned int nextIdxInParent = ++(itr->fIdxInParent);
722 while (nextIdxInParent >= itr->fFieldPtr->fParent->fSubfields.size()) {
723 if (fStack.size() == 1) {
724 itr->fFieldPtr = itr->fFieldPtr->fParent;
725 itr->fIdxInParent = -1;
726 return;
727 }
728 fStack.pop_back();
729 itr = fStack.rbegin();
730 nextIdxInParent = ++(itr->fIdxInParent);
731 }
732 itr->fFieldPtr = itr->fFieldPtr->fParent->fSubfields[nextIdxInParent].get();
733 }
734
735 iterator operator++(int) /* postfix */
736 {
737 auto r = *this;
738 Advance();
739 return r;
740 }
741 iterator &operator++() /* prefix */
742 {
743 Advance();
744 return *this;
745 }
746 reference operator*() const { return *fStack.back().fFieldPtr; }
747 pointer operator->() const { return fStack.back().fFieldPtr; }
748 bool operator==(const iterator &rh) const { return fStack.back().fFieldPtr == rh.fStack.back().fFieldPtr; }
749 bool operator!=(const iterator &rh) const { return fStack.back().fFieldPtr != rh.fStack.back().fFieldPtr; }
750};
751
752/// Points to an object with RNTuple I/O support and keeps a pointer to the corresponding field.
753/// Fields can create RValue objects through RFieldBase::CreateValue(), RFieldBase::BindValue()) or
754/// RFieldBase::SplitValue().
756 friend class RFieldBase;
757 friend class ROOT::REntry;
759
760private:
761 RFieldBase *fField = nullptr; ///< The field that created the RValue
762 /// Set by Bind() or by RFieldBase::CreateValue(), RFieldBase::SplitValue() or RFieldBase::BindValue()
763 std::shared_ptr<void> fObjPtr;
764 mutable std::atomic<const std::type_info *> fTypeInfo = nullptr;
765
766 RValue(RFieldBase *field, std::shared_ptr<void> objPtr) : fField(field), fObjPtr(objPtr) {}
767
768public:
769 RValue(const RValue &other) : fField(other.fField), fObjPtr(other.fObjPtr) {}
770 RValue &operator=(const RValue &other)
771 {
772 fField = other.fField;
773 fObjPtr = other.fObjPtr;
774 // We could copy over the cached type info, or just start with a fresh state...
775 fTypeInfo = nullptr;
776 return *this;
777 }
778 RValue(RValue &&other) : fField(other.fField), fObjPtr(other.fObjPtr) {}
780 {
781 fField = other.fField;
782 fObjPtr = other.fObjPtr;
783 // We could copy over the cached type info, or just start with a fresh state...
784 fTypeInfo = nullptr;
785 return *this;
786 }
787 ~RValue() = default;
788
789private:
790 template <typename T>
792 {
793 if constexpr (!std::is_void_v<T>) {
794 const std::type_info &ti = typeid(T);
795 // Fast path: if we had a matching type before, try comparing the type_info's. This may still fail in case the
796 // type has a suppressed template argument that may change the typeid.
797 auto *cachedTypeInfo = fTypeInfo.load();
798 if (cachedTypeInfo != nullptr && *cachedTypeInfo == ti) {
799 return;
800 }
801 std::string renormalizedTypeName = Internal::GetRenormalizedTypeName(ti);
802 if (Internal::IsMatchingFieldType(fField->GetTypeName(), renormalizedTypeName, ti)) {
803 fTypeInfo.store(&ti);
804 return;
805 }
806 throw RException(R__FAIL("type mismatch for field \"" + fField->GetFieldName() + "\": expected " +
807 fField->GetTypeName() + ", got " + renormalizedTypeName));
808 }
809 }
810
811 std::size_t Append() { return fField->Append(fObjPtr.get()); }
812
813public:
814 void Read(ROOT::NTupleSize_t globalIndex) { fField->Read(globalIndex, fObjPtr.get()); }
815 void Read(RNTupleLocalIndex localIndex) { fField->Read(localIndex, fObjPtr.get()); }
816
817 void Bind(std::shared_ptr<void> objPtr) { fObjPtr = objPtr; }
818 void BindRawPtr(void *rawPtr);
819 /// Replace the current object pointer by a pointer to a new object constructed by the field
820 void EmplaceNew() { fObjPtr = fField->CreateValue().GetPtr<void>(); }
821
822 template <typename T>
823 std::shared_ptr<T> GetPtr() const
824 {
826 return std::static_pointer_cast<T>(fObjPtr);
827 }
828
829 template <typename T>
830 const T &GetRef() const
831 {
833 return *static_cast<T *>(fObjPtr.get());
834 }
835
836 const RFieldBase &GetField() const { return *fField; }
837};
838
839/// Input parameter to RFieldBase::ReadBulk() and RFieldBase::ReadBulkImpl().
840// See the RBulkValues class documentation for more information.
842 /// Possible return value of ReadBulk() and ReadBulkImpl(), which indicates that the full bulk range was read
843 /// independently of the provided masks.
844 static const std::size_t kAllSet = std::size_t(-1);
845
846 RNTupleLocalIndex fFirstIndex; ///< Start of the bulk range
847 std::size_t fCount = 0; ///< Size of the bulk range
848 /// A bool array of size fCount, indicating the required values in the requested range
849 const bool *fMaskReq = nullptr;
850 bool *fMaskAvail = nullptr; ///< A bool array of size `fCount`, indicating the valid values in fValues
851 /// The destination area, which has to be an array of valid objects of the correct type large enough to hold the bulk
852 /// range.
853 void *fValues = nullptr;
854 /// Reference to memory owned by the RBulkValues class. The field implementing BulkReadImpl() may use `fAuxData` as
855 /// memory that stays persistent between calls.
856 std::vector<unsigned char> *fAuxData = nullptr;
857};
858
859// clang-format off
860/**
861\class ROOT::RFieldBase::RBulkValues
862\ingroup NTuple
863\brief Points to an array of objects with RNTuple I/O support, used for bulk reading.
864
865Similar to RValue, but manages an array of consecutive values. Bulks have to come from the same cluster.
866Bulk I/O works with two bit masks: the mask of all the available entries in the current bulk and the mask
867of the required entries in a bulk read. The idea is that a single bulk may serve multiple read operations
868on the same range, where in each read operation a different subset of values is required.
869The memory of the value array is managed by the RBulkValues class.
870*/
871// clang-format on
873private:
874 friend class RFieldBase;
875
876 RFieldBase *fField = nullptr; ///< The field that created the array of values
877 std::unique_ptr<RFieldBase::RDeleter> fDeleter; /// Cached deleter of fField
878 void *fValues = nullptr; ///< Pointer to the start of the array
879 std::size_t fValueSize = 0; ///< Cached copy of RFieldBase::GetValueSize()
880 std::size_t fCapacity = 0; ///< The size of the array memory block in number of values
881 std::size_t fSize = 0; ///< The number of available values in the array (provided their mask is set)
882 bool fIsAdopted = false; ///< True if the user provides the memory buffer for fValues
883 std::unique_ptr<bool[]> fMaskAvail; ///< Masks invalid values in the array
884 std::size_t fNValidValues = 0; ///< The sum of non-zero elements in the fMask
885 RNTupleLocalIndex fFirstIndex; ///< Index of the first value of the array
886 /// Reading arrays of complex values may require additional memory, for instance for the elements of
887 /// arrays of vectors. A pointer to the `fAuxData` array is passed to the field's BulkRead method.
888 /// The RBulkValues class does not modify the array in-between calls to the field's BulkRead method.
889 std::vector<unsigned char> fAuxData;
890
891 void ReleaseValues();
892 /// Sets a new range for the bulk. If there is enough capacity, the `fValues` array will be reused.
893 /// Otherwise a new array is allocated. After reset, fMaskAvail is false for all values.
894 void Reset(RNTupleLocalIndex firstIndex, std::size_t size);
895
896 bool ContainsRange(RNTupleLocalIndex firstIndex, std::size_t size) const
897 {
898 if (firstIndex.GetClusterId() != fFirstIndex.GetClusterId())
899 return false;
900 return (firstIndex.GetIndexInCluster() >= fFirstIndex.GetIndexInCluster()) &&
901 ((firstIndex.GetIndexInCluster() + size) <= (fFirstIndex.GetIndexInCluster() + fSize));
902 }
903
904 void *GetValuePtrAt(std::size_t idx) const { return reinterpret_cast<unsigned char *>(fValues) + idx * fValueSize; }
905
906 explicit RBulkValues(RFieldBase *field)
907 : fField(field), fDeleter(field->GetDeleter()), fValueSize(field->GetValueSize())
908 {
909 }
910
911public:
912 ~RBulkValues();
913 RBulkValues(const RBulkValues &) = delete;
915 RBulkValues(RBulkValues &&other);
917
918 // Sets `fValues` and `fSize`/`fCapacity` to the given values. The capacity is specified in number of values.
919 // Once a buffer is adopted, an attempt to read more values then available throws an exception.
920 void AdoptBuffer(void *buf, std::size_t capacity);
921
922 /// Reads `size` values from the associated field, starting from `firstIndex`. Note that the index is given
923 /// relative to a certain cluster. The return value points to the array of read objects.
924 /// The `maskReq` parameter is a bool array of at least `size` elements. Only objects for which the mask is
925 /// true are guaranteed to be read in the returned value array. A `nullptr` means to read all elements.
926 void *ReadBulk(RNTupleLocalIndex firstIndex, const bool *maskReq, std::size_t size)
927 {
928 if (!ContainsRange(firstIndex, size))
929 Reset(firstIndex, size);
930
931 // We may read a subrange of the currently available range
932 auto offset = firstIndex.GetIndexInCluster() - fFirstIndex.GetIndexInCluster();
933
934 if (fNValidValues == fSize)
935 return GetValuePtrAt(offset);
936
937 RBulkSpec bulkSpec;
938 bulkSpec.fFirstIndex = firstIndex;
939 bulkSpec.fCount = size;
940 bulkSpec.fMaskReq = maskReq;
941 bulkSpec.fMaskAvail = &fMaskAvail[offset];
942 bulkSpec.fValues = GetValuePtrAt(offset);
943 bulkSpec.fAuxData = &fAuxData;
944 auto nRead = fField->ReadBulk(bulkSpec);
945 if (nRead == RBulkSpec::kAllSet) {
946 // We expect that field implementations consistently return kAllSet either in all cases or never. This avoids
947 // the following case where we would have to manually count how many valid values we actually have:
948 // 1. A partial ReadBulk, according to maskReq, with values potentially missing in the middle.
949 // 2. A second ReadBulk that reads a complete subrange. If this returned kAllSet, we don't know how to update
950 // fNValidValues, other than counting. The field should return a concrete number of how many new values it read
951 // in addition to those already present.
952 R__ASSERT((offset == 0) && (size == fSize));
954 } else {
955 fNValidValues += nRead;
956 }
957 return GetValuePtrAt(offset);
958 }
959
960 /// Overload to read all elements in the given cluster range.
961 void *ReadBulk(ROOT::RNTupleLocalRange range) { return ReadBulk(*range.begin(), nullptr, range.size()); }
962};
963
964namespace Internal {
965// At some point, RFieldBase::OnClusterCommit() may allow for a user-defined callback to change the
966// column representation. For now, we inject this for testing and internal use only.
968 static void SetPrimaryColumnRepresentation(RFieldBase &field, std::uint16_t newRepresentationIdx)
969 {
970 R__ASSERT(newRepresentationIdx < field.fColumnRepresentatives.size());
971 const auto N = field.fColumnRepresentatives[0].get().size();
972 R__ASSERT(N >= 1 && N <= 2);
974 field.fPrincipalColumn = field.fAvailableColumns[newRepresentationIdx * N].get();
975 if (field.fAuxiliaryColumn) {
976 R__ASSERT(N == 2);
977 field.fAuxiliaryColumn = field.fAvailableColumns[newRepresentationIdx * N + 1].get();
978 }
979 }
980};
981} // namespace Internal
982} // namespace ROOT
983
984#endif
ROOT::R::TRInterface & r
Definition Object.C:4
#define R__unlikely(expr)
Definition RConfig.hxx:586
#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:299
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
Definition TError.h:125
#define N
char name[80]
Definition TGX11.cxx:148
Abstract base class for classes implementing the visitor design pattern.
A container of const raw pointers, corresponding to a row in the data set.
Class used to read a RNTupleAttrSet in the context of a RNTupleReader.
A column is a storage-backed array of a simple, fixed-size type, from which pages can be mapped into ...
Definition RColumn.hxx:37
static std::unique_ptr< RColumn > Create(ROOT::ENTupleColumnType type, std::uint32_t columnIdx, std::uint16_t representationIdx)
Definition RColumn.hxx:106
Abstract interface to write data into an ntuple.
Abstract interface to read data from an ntuple.
The field for a class with dictionary.
Definition RField.hxx:135
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:78
Field specific extra type information from the header / extenstion header.
Points to an array of objects with RNTuple I/O support, used for bulk reading.
std::unique_ptr< bool[]> fMaskAvail
Masks invalid values in the array.
std::unique_ptr< RFieldBase::RDeleter > fDeleter
void * ReadBulk(RNTupleLocalIndex firstIndex, const bool *maskReq, std::size_t size)
Reads size values from the associated field, starting from firstIndex.
void * GetValuePtrAt(std::size_t idx) const
bool ContainsRange(RNTupleLocalIndex firstIndex, std::size_t size) const
std::size_t fNValidValues
The sum of non-zero elements in the fMask.
bool fIsAdopted
True if the user provides the memory buffer for fValues.
void Reset(RNTupleLocalIndex firstIndex, std::size_t size)
Sets a new range for the bulk.
void * fValues
Cached deleter of fField.
std::size_t fCapacity
The size of the array memory block in number of values.
void * ReadBulk(ROOT::RNTupleLocalRange range)
Overload to read all elements in the given cluster range.
std::size_t fValueSize
Cached copy of RFieldBase::GetValueSize().
RFieldBase * fField
The field that created the array of values.
RBulkValues & operator=(const RBulkValues &)=delete
RBulkValues(RFieldBase *field)
std::size_t fSize
The number of available values in the array (provided their mask is set).
void AdoptBuffer(void *buf, std::size_t capacity)
std::vector< unsigned char > fAuxData
Reading arrays of complex values may require additional memory, for instance for the elements of arra...
RNTupleLocalIndex fFirstIndex
Index of the first value of the array.
RBulkValues(const RBulkValues &)=delete
const Selection_t & GetSerializationTypes() const
const Selection_t & GetDeserializationTypes() const
const ColumnRepresentation_t & GetSerializationDefault() const
The first column list from fSerializationTypes is the default for writing.
std::vector< ColumnRepresentation_t > Selection_t
A list of column representations.
Selection_t fDeserializationTypes
The union of the serialization types and the deserialization extra types passed during construction.
A functor to release the memory acquired by CreateValue() (memory and constructor).
virtual void operator()(void *objPtr, bool dtorOnly)
virtual ~RDeleter()=default
Iterates over the subtree of fields in depth-first search order.
bool operator==(const iterator &rh) const
std::conditional_t< IsConstT, const RFieldBase &, RFieldBase & > reference
std::conditional_t< IsConstT, const RFieldBase *, RFieldBase * > pointer
std::conditional_t< IsConstT, const RFieldBase, RFieldBase > value_type
void Advance()
Given that the iterator points to a valid field which is not the end iterator, go to the next field i...
bool operator!=(const iterator &rh) const
RSchemaIteratorTemplate< IsConstT > iterator
RSchemaIteratorTemplate(pointer val, int idxInParent)
A deleter for templated RFieldBase descendents where the value type is known.
void operator()(void *objPtr, bool dtorOnly) final
Points to an object with RNTuple I/O support and keeps a pointer to the corresponding field.
std::shared_ptr< void > fObjPtr
Set by Bind() or by RFieldBase::CreateValue(), RFieldBase::SplitValue() or RFieldBase::BindValue().
RValue & operator=(const RValue &other)
void Read(ROOT::NTupleSize_t globalIndex)
void EmplaceNew()
Replace the current object pointer by a pointer to a new object constructed by the field.
void EnsureMatchingType() const
void Bind(std::shared_ptr< void > objPtr)
void Read(RNTupleLocalIndex localIndex)
RValue(const RValue &other)
RValue & operator=(RValue &&other)
const RFieldBase & GetField() const
std::atomic< const std::type_info * > fTypeInfo
std::shared_ptr< T > GetPtr() const
RFieldBase * fField
The field that created the RValue.
RValue(RFieldBase *field, std::shared_ptr< void > objPtr)
void BindRawPtr(void *rawPtr)
const T & GetRef() const
A field translates read and write calls from/to underlying columns to/from tree values.
ROOT::DescriptorId_t fOnDiskId
When the columns are connected to a page source or page sink, the field represents a field id in the ...
ROOT::ENTupleStructure GetStructure() const
virtual size_t GetValueSize() const =0
The number of bytes taken by a value of the appropriate type.
static constexpr std::uint32_t kInvalidTypeVersion
RSchemaIterator end()
void Attach(std::unique_ptr< RFieldBase > child, std::string_view expectedChildName="")
Add a new subfield to the list of nested fields.
void SetColumnRepresentatives(const RColumnRepresentations::Selection_t &representatives)
Fixes a column representative.
void InvokeReadCallbacks(void *target)
ROOT::Internal::RColumn * fPrincipalColumn
All fields that have columns have a distinct main column.
virtual size_t GetAlignment() const =0
As a rule of thumb, the alignment is equal to the size of the type.
virtual std::unique_ptr< RDeleter > GetDeleter() const
virtual void ReconcileOnDiskField(const RNTupleDescriptor &desc)
For non-artificial fields, check compatibility of the in-memory field and the on-disk field.
ROOT::NTupleSize_t EntryToColumnElementIndex(ROOT::NTupleSize_t globalIndex) const
Translate an entry index to a column element index of the principal column and vice versa.
virtual void AcceptVisitor(ROOT::Detail::RFieldVisitor &visitor) const
void FlushColumns()
Flushes data from active columns.
virtual void ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to)
std::vector< std::unique_ptr< RFieldBase > > fSubfields
Collections and classes own subfields.
std::uint32_t GetOnDiskTypeVersion() const
Return the C++ type version stored in the field descriptor; only valid after a call to ConnectPageSou...
virtual const RColumnRepresentations & GetColumnRepresentations() const
Implementations in derived classes should return a static RColumnRepresentations object.
EState
During its lifetime, a field undergoes the following possible state transitions:
bool fIsSimple
A field qualifies as simple if it is mappable (which implies it has a single principal column),...
RConstSchemaIterator cbegin() const
std::unique_ptr< T, typename RCreateObjectDeleter< T >::deleter > CreateObject() const
Generates an object of the field type and allocates new initialized memory according to the type.
Definition RField.hxx:567
RFieldBase & operator=(const RFieldBase &)=delete
virtual void GenerateColumns(const ROOT::RNTupleDescriptor &)
Implementations in derived classes should create the backing columns corresponding to the field type ...
void AutoAdjustColumnTypes(const ROOT::RNTupleWriteOptions &options)
When connecting a field to a page sink, the field's default column representation is subject to adjus...
virtual void ConstructValue(void *where) const =0
Constructs value in a given location of size at least GetValueSize(). Called by the base class' Creat...
std::vector< const RFieldBase * > GetConstSubfields() const
void SetOnDiskId(ROOT::DescriptorId_t id)
void RemoveReadCallback(size_t idx)
void GenerateColumnsImpl(const ROOT::RNTupleDescriptor &desc)
For reading, use the on-disk column list.
@ kTraitEmulatedField
This field is a user defined type that was missing dictionaries and was reconstructed from the on-dis...
@ kTraitTrivialType
Shorthand for types that are both trivially constructible and destructible.
@ kTraitTriviallyDestructible
The type is cleaned up just by freeing its memory. I.e. the destructor performs a no-op.
@ kTraitExtensible
Can attach new item fields even when already connected.
@ kTraitTriviallyConstructible
No constructor needs to be called, i.e.
@ kTraitSoACollection
The field represents a collection in SoA layout.
@ kTraitMappable
A field of a fundamental type that can be directly mapped via RField<T>::Map(), i....
@ kTraitInvalidField
This field is an instance of RInvalidField and can be safely static_cast to it.
@ kTraitTypeChecksum
The TClass checksum is set and valid.
virtual void GenerateColumns()
Implementations in derived classes should create the backing columns corresponding to the field type ...
void Read(RNTupleLocalIndex localIndex, void *to)
Populate a single value with data from the field.
const RFieldBase * GetParent() const
std::vector< RFieldBase * > GetMutableSubfields()
std::string fDescription
Free text set by the user.
static std::unique_ptr< RDeleter > GetDeleterOf(const RFieldBase &other)
static ROOT::Internal::RColumn * GetPrincipalColumnOf(const RFieldBase &other)
Fields may need direct access to the principal column of their subfields, e.g. in RRVecField::ReadBul...
static std::vector< RCheckResult > Check(const std::string &fieldName, const std::string &typeName)
Checks if the given type is supported by RNTuple.
RSchemaIterator begin()
ROOT::Internal::RColumn * fAuxiliaryColumn
Some fields have a second column in its column representation.
size_t AddReadCallback(ReadCallback_t func)
Set a user-defined function to be called after reading a value, giving a chance to inspect and/or mod...
RResult< void > EnsureMatchingOnDiskCollection(const RNTupleDescriptor &desc) const
Convenience wrapper for the common case of calling EnsureMatchinOnDiskField() for collections.
RConstSchemaIterator cend() const
std::size_t fNRepetitions
For fixed sized arrays, the array length.
std::function< void(void *)> ReadCallback_t
std::size_t Append(const void *from)
Write the given value into columns.
RValue CreateValue()
Generates an object of the field's type, wraps it in a shared pointer and returns it as an RValue con...
RSchemaIteratorTemplate< false > RSchemaIterator
const ColumnRepresentation_t & EnsureCompatibleColumnTypes(const ROOT::RNTupleDescriptor &desc, std::uint16_t representationIndex) const
Returns the on-disk column types found in the provided descriptor for fOnDiskId and the given represe...
RFieldBase(RFieldBase &&)=default
virtual std::vector< RValue > SplitValue(const RValue &value) const
Creates the list of direct child values given an existing value for this field.
virtual std::unique_ptr< RFieldBase > CloneImpl(std::string_view newName) const =0
Called by Clone(), which additionally copies the on-disk ID.
static void CallSetArtificialOn(RFieldBase &other)
Allow parents to mark their childs as artificial fields (used in class and record fields).
std::string GetQualifiedFieldName() const
Returns the field name and parent field names separated by dots (grandparent.parent....
RBulkValues CreateBulk()
Creates a new, initially empty bulk.
const std::string & GetFieldName() const
void ConnectPageSink(ROOT::Internal::RPageSink &pageSink, ROOT::NTupleSize_t firstEntry=0)
Fields and their columns live in the void until connected to a physical page storage.
std::size_t ReadBulk(const RBulkSpec &bulkSpec)
Returns the number of newly available values, that is the number of bools in bulkSpec....
std::vector< ROOT::ENTupleColumnType > ColumnRepresentation_t
std::vector< ReadCallback_t > fReadCallbacks
List of functions to be called after reading a value.
RFieldBase & operator=(RFieldBase &&)=default
RResult< void > EnsureMatchingOnDiskField(const RNTupleDescriptor &desc, std::uint32_t ignoreBits=0) const
Compares the field to the corresponding on-disk field information in the provided descriptor.
const std::string & GetTypeAlias() const
static void CallReadOn(RFieldBase &other, ROOT::NTupleSize_t globalIndex, void *to)
virtual ~RFieldBase()=default
static std::size_t CallAppendOn(RFieldBase &other, const void *from)
Allow derived classes to call Append() and Read() on other (sub)fields.
virtual void ReadInClusterImpl(RNTupleLocalIndex localIndex, void *to)
virtual void CommitClusterImpl()
std::vector< std::reference_wrapper< const ColumnRepresentation_t > > fColumnRepresentatives
Pointers into the static vector returned by RColumnRepresentations::GetSerializationTypes() when SetC...
std::uint32_t fTraits
Properties of the type that allow for optimizations of collections of that type.
friend struct ROOT::Internal::RFieldCallbackInjector
virtual std::size_t AppendImpl(const void *from)
Operations on values of complex types, e.g.
RFieldBase * fParent
Subfields point to their mother field.
std::vector< std::unique_ptr< ROOT::Internal::RColumn > > fAvailableColumns
The columns are connected either to a sink or to a source (not to both); they are owned by the field.
RFieldBase(std::string_view name, std::string_view type, ROOT::ENTupleStructure structure, bool isSimple, std::size_t nRepetitions=0)
The constructor creates the underlying column objects and connects them to either a sink or a source.
EState fState
Changed by ConnectTo[Sink,Source], reset by Clone().
friend class RFieldZero
static void * CallCreateObjectRawPtrOn(RFieldBase &other)
bool IsArtificial() const
static RResult< std::unique_ptr< RFieldBase > > Create(const std::string &fieldName, const std::string &typeName, const ROOT::RCreateFieldOptions &options, const ROOT::RNTupleDescriptor *desc, ROOT::DescriptorId_t fieldId)
Factory method to resurrect a field from the stored on-disk type information.
const std::string & GetDescription() const
Get the field's description.
bool HasReadCallbacks() const
std::string fTypeAlias
A typedef or using name that was used when creating the field.
virtual std::uint32_t GetFieldVersion() const
Indicates an evolution of the mapping scheme from C++ type to columns.
virtual std::unique_ptr< RFieldBase > BeforeConnectPageSource(ROOT::Internal::RPageSource &)
Called by ConnectPageSource() before connecting; derived classes may override this as appropriate,...
std::uint32_t CompareOnDiskField(const RFieldDescriptor &fieldDesc, std::uint32_t ignoreBits) const
Returns a combination of kDiff... flags, indicating peroperties that are different between the field ...
std::string fType
The C++ type captured by this field.
RColumnRepresentations::Selection_t GetColumnRepresentatives() const
Returns the fColumnRepresentative pointee or, if unset (always the case for artificial fields),...
RSchemaIteratorTemplate< true > RConstSchemaIterator
virtual std::uint32_t GetTypeChecksum() const
Return the current TClass reported checksum of this class. Only valid if kTraitTypeChecksum is set.
bool IsSimple() const
std::uint32_t GetTraits() const
std::size_t GetNRepetitions() const
std::uint32_t fOnDiskTypeChecksum
TClass checksum cached from the descriptor after a call to ConnectPageSource().
const std::string & GetTypeName() const
ROOT::ENTupleStructure fStructure
The role of this field in the data model structure.
void GenerateColumnsImpl(const ColumnRepresentation_t &representation, std::uint16_t representationIndex)
Helpers for generating columns.
RValue BindValue(std::shared_ptr< void > objPtr)
Creates a value from a memory location with an already constructed object.
void SetDescription(std::string_view description)
static void CallReadOn(RFieldBase &other, RNTupleLocalIndex localIndex, void *to)
ROOT::DescriptorId_t GetOnDiskId() const
std::uint32_t fOnDiskTypeVersion
C++ type version cached from the descriptor after a call to ConnectPageSource().
std::unique_ptr< RFieldBase > Clone(std::string_view newName) const
Copies the field and its subfields using a possibly new name and a new, unconnected set of columns.
std::string fName
The field name relative to its parent field.
void CommitCluster()
Flushes data from active columns to disk and calls CommitClusterImpl().
void ConnectPageSource(ROOT::Internal::RPageSource &pageSource)
Connects the field and its subfield tree to the given page source.
static void CallConstructValueOn(const RFieldBase &other, void *where)
Allow derived classes to call ConstructValue(void *) and GetDeleter() on other (sub)fields.
EState GetState() const
void GenerateColumnsImpl()
For writing, use the currently set column representative.
RResult< void > EnsureMatchingTypePrefix(const RNTupleDescriptor &desc, const std::vector< std::string > &prefixes) const
Many fields accept a range of type prefixes for schema evolution, e.g.
virtual ROOT::RExtraTypeInfoDescriptor GetExtraTypeInfo() const
virtual std::uint32_t GetTypeVersion() const
Indicates an evolution of the C++ type itself.
void * CreateObjectRawPtr() const
Factory method for the field's type. The caller owns the returned pointer.
void Read(ROOT::NTupleSize_t globalIndex, void *to)
Populate a single value with data from the field.
std::uint32_t GetOnDiskTypeChecksum() const
Return checksum stored in the field descriptor; only valid after a call to ConnectPageSource(),...
RFieldBase(const RFieldBase &)=delete
virtual bool HasExtraTypeInfo() const
bool fIsArtificial
A field that is not backed on disk but computed, e.g.
virtual std::size_t ReadBulkImpl(const RBulkSpec &bulkSpec)
General implementation of bulk read.
bool HasDefaultColumnRepresentative() const
Whether or not an explicit column representative was set.
@ kDiffStructure
The in-memory field and the on-disk field differ in their structural roles.
@ kDiffTypeName
The in-memory field and the on-disk field have different type names.
@ kDiffTypeVersion
The in-memory field and the on-disk field differ in the type version.
@ kDiffFieldVersion
The in-memory field and the on-disk field differ in the field version.
@ kDiffNRepetitions
The in-memory field and the on-disk field have different repetition counts.
Metadata stored for every field of an RNTuple.
The on-storage metadata of an RNTuple.
Addresses a column element or field item relative to a particular cluster, instead of a global NTuple...
ROOT::NTupleSize_t GetIndexInCluster() const
ROOT::DescriptorId_t GetClusterId() const
Used to loop over entries of collections in a single cluster.
ROOT::NTupleSize_t size() const
RIterator begin() const
Common user-tunable settings for storing RNTuples.
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:197
Special implementation of ROOT::RRangeCast for TCollection, including a check that the cast target ty...
Definition TObject.h:395
Namespace for ROOT features in testing.
Definition TROOT.h:100
void CallCommitClusterOnField(RFieldBase &)
void CallConnectPageSourceOnField(RFieldBase &, ROOT::Internal::RPageSource &)
ROOT::RResult< std::unique_ptr< ROOT::RFieldBase > > CallFieldBaseCreate(const std::string &fieldName, const std::string &typeName, const ROOT::RCreateFieldOptions &options, const ROOT::RNTupleDescriptor *desc, ROOT::DescriptorId_t fieldId)
void CallFlushColumnsOnField(RFieldBase &)
bool IsMatchingFieldType(const std::string &actualTypeName)
Helper to check if a given type name is the one expected of Field<T>.
Definition RField.hxx:559
std::string GetRenormalizedTypeName(const std::string &metaNormalizedName)
Given a type name normalized by ROOT meta, renormalize it for RNTuple. E.g., insert std::prefix.
void CallConnectPageSinkOnField(RFieldBase &, ROOT::Internal::RPageSink &, ROOT::NTupleSize_t firstEntry=0)
std::uint64_t DescriptorId_t
Distriniguishes elements of the same type within a descriptor, e.g. different fields.
std::uint64_t NTupleSize_t
Integer type long enough to hold the maximum number of entries in a column.
constexpr DescriptorId_t kInvalidDescriptorId
ENTupleStructure
The fields in the RNTuple data model tree can carry different structural information about the type s...
A pair of scoped + meta entry used by the RNTupleAttrSetWriter.
static void SetPrimaryColumnRepresentation(RFieldBase &field, std::uint16_t newRepresentationIdx)
Input parameter to RFieldBase::ReadBulk() and RFieldBase::ReadBulkImpl().
static const std::size_t kAllSet
Possible return value of ReadBulk() and ReadBulkImpl(), which indicates that the full bulk range was ...
RNTupleLocalIndex fFirstIndex
Start of the bulk range.
void * fValues
The destination area, which has to be an array of valid objects of the correct type large enough to h...
std::size_t fCount
Size of the bulk range.
bool * fMaskAvail
A bool array of size fCount, indicating the valid values in fValues.
const bool * fMaskReq
A bool array of size fCount, indicating the required values in the requested range.
std::vector< unsigned char > * fAuxData
Reference to memory owned by the RBulkValues class.
Used in the return value of the Check() method.
std::string fFieldName
Qualified field name causing the error.
std::string fTypeName
Type name corresponding to the (sub)field.
std::string fErrMsg
Cause of the failure, e.g. unsupported type.
Position(FieldPtr_t fieldPtr, int idxInParent)
std::conditional_t< IsConstT, const RFieldBase *, RFieldBase * > FieldPtr_t
RSharedPtrDeleter(std::unique_ptr< RFieldBase::RDeleter > deleter)
std::unique_ptr< RFieldBase::RDeleter > fDeleter