Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RNTupleIndex.cxx
Go to the documentation of this file.
1/// \file RNTupleIndex.cxx
2/// \ingroup NTuple ROOT7
3/// \author Florine de Geus <florine.de.geus@cern.ch>
4/// \date 2024-04-02
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-2024, 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#include <ROOT/RNTupleIndex.hxx>
17
18namespace {
20CastValuePtr(void *valuePtr, const ROOT::Experimental::RFieldBase &field)
21{
23
24 switch (field.GetValueSize()) {
25 case 1: value = *reinterpret_cast<std::uint8_t *>(valuePtr); break;
26 case 2: value = *reinterpret_cast<std::uint16_t *>(valuePtr); break;
27 case 4: value = *reinterpret_cast<std::uint32_t *>(valuePtr); break;
28 case 8: value = *reinterpret_cast<std::uint64_t *>(valuePtr); break;
29 default: throw ROOT::Experimental::RException(R__FAIL("value size not supported"));
30 }
31
32 return value;
33}
34} // anonymous namespace
35
36ROOT::Experimental::Internal::RNTupleIndex::RNTupleIndex(const std::vector<std::string> &fieldNames,
37 const RPageSource &pageSource)
38 : fPageSource(pageSource.Clone())
39{
40 fPageSource->Attach();
41 auto desc = fPageSource->GetSharedDescriptorGuard();
42
43 fIndexFields.reserve(fieldNames.size());
44
45 for (const auto &fieldName : fieldNames) {
46 auto fieldId = desc->FindFieldId(fieldName);
47 if (fieldId == kInvalidDescriptorId)
48 throw RException(R__FAIL("Could not find field \"" + std::string(fieldName) + "."));
49
50 const auto &fieldDesc = desc->GetFieldDescriptor(fieldId);
51 auto field = fieldDesc.CreateField(desc.GetRef());
52
54
55 fIndexFields.push_back(std::move(field));
56 }
57}
58
60{
61 if (!fIsBuilt)
62 throw RException(R__FAIL("Index has not been built yet"));
63}
64
65std::unique_ptr<ROOT::Experimental::Internal::RNTupleIndex>
66ROOT::Experimental::Internal::RNTupleIndex::Create(const std::vector<std::string> &fieldNames,
67 const RPageSource &pageSource, bool deferBuild)
68{
69 auto index = std::unique_ptr<RNTupleIndex>(new RNTupleIndex(fieldNames, pageSource));
70
71 if (!deferBuild)
72 index->Build();
73
74 return index;
75}
76
78{
79 if (fIsBuilt)
80 return;
81
82 static const std::unordered_set<std::string> allowedTypes = {"std::int8_t", "std::int16_t", "std::int32_t",
83 "std::int64_t", "std::uint8_t", "std::uint16_t",
84 "std::uint32_t", "std::uint64_t"};
85
86 std::vector<RFieldBase::RValue> fieldValues;
87 fieldValues.reserve(fIndexFields.size());
88
89 for (const auto &field : fIndexFields) {
90 if (allowedTypes.find(field->GetTypeName()) == allowedTypes.end()) {
91 throw RException(R__FAIL("Cannot use field \"" + field->GetFieldName() + "\" with type \"" +
92 field->GetTypeName() + "\" for indexing. Only integral types are allowed."));
93 }
94 fieldValues.emplace_back(field->CreateValue());
95 }
96
97 std::vector<NTupleIndexValue_t> indexValues;
98 indexValues.reserve(fIndexFields.size());
99
100 for (unsigned i = 0; i < fPageSource->GetNEntries(); ++i) {
101 indexValues.clear();
102 for (auto &fieldValue : fieldValues) {
103 // TODO(fdegeus): use bulk reading
104 fieldValue.Read(i);
105
106 auto valuePtr = fieldValue.GetPtr<void>();
107 indexValues.push_back(CastValuePtr(valuePtr.get(), fieldValue.GetField()));
108 }
109 fIndex[RIndexValue(indexValues)].push_back(i);
110 }
111
112 fIsBuilt = true;
113}
114
117{
118 const auto entryIndices = GetAllEntryNumbers(valuePtrs);
119 if (!entryIndices)
120 return kInvalidNTupleIndex;
121 return entryIndices->front();
122}
123
124const std::vector<ROOT::Experimental::NTupleSize_t> *
125ROOT::Experimental::Internal::RNTupleIndex::GetAllEntryNumbers(const std::vector<void *> &valuePtrs) const
126{
127 if (valuePtrs.size() != fIndexFields.size())
128 throw RException(R__FAIL("Number of value pointers must match number of indexed fields."));
129
130 EnsureBuilt();
131
132 std::vector<NTupleIndexValue_t> indexValues;
133 indexValues.reserve(fIndexFields.size());
134
135 for (unsigned i = 0; i < valuePtrs.size(); ++i) {
136 indexValues.push_back(CastValuePtr(valuePtrs[i], *fIndexFields[i]));
137 }
138
139 auto entryNumber = fIndex.find(RIndexValue(indexValues));
140
141 if (entryNumber == fIndex.end())
142 return nullptr;
143
144 return &(entryNumber->second);
145}
#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
Container for the hashes of the indexed fields.
Builds an index on one or several fields of an RNTuple so it can be joined onto other RNTuples.
std::unique_ptr< RPageSource > fPageSource
The page source belonging to the RNTuple for which to build the index.
static std::unique_ptr< RNTupleIndex > Create(const std::vector< std::string > &fieldNames, const RPageSource &pageSource, bool deferBuild=false)
Create an RNTupleIndex from an existing RNTuple.
const std::vector< NTupleSize_t > * GetAllEntryNumbers(const std::vector< void * > &valuePtrs) const
Get all entry numbers for the given index.
NTupleSize_t GetFirstEntryNumber(const std::vector< void * > &valuePtrs) const
Get the first entry number containing the given index value.
RNTupleIndex(const std::vector< std::string > &fieldNames, const RPageSource &pageSource)
Create an a new RNTupleIndex for the RNTuple represented by the provided page source.
void EnsureBuilt() const
Ensure the RNTupleIndex has been built.
std::vector< std::unique_ptr< RFieldBase > > fIndexFields
The fields for which the index is built. Used to compute the hashes for each entry value.
Abstract interface to read data from an ntuple.
Base class for all ROOT issued exceptions.
Definition RError.hxx:78
A field translates read and write calls from/to underlying columns to/from tree values.
const std::string & GetFieldName() const
const std::string & GetTypeName() const
RValue CreateValue()
Generates an object of the field type and wraps the created object in a shared pointer and returns it...
Definition RField.cxx:969
virtual size_t GetValueSize() const =0
The number of bytes taken by a value of the appropriate type.
void CallConnectPageSourceOnField(RFieldBase &, RPageSource &)
Definition RField.cxx:411
std::uint64_t NTupleSize_t
Integer type long enough to hold the maximum number of entries in a column.
constexpr NTupleSize_t kInvalidNTupleIndex
constexpr DescriptorId_t kInvalidDescriptorId