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::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 join field \"" + std::string(fieldName) + "\" in RNTuple \"" +
49 fPageSource->GetNTupleName() + "\""));
50
51 const auto &fieldDesc = desc->GetFieldDescriptor(fieldId);
52 auto field = fieldDesc.CreateField(desc.GetRef());
53
55
56 fIndexFields.push_back(std::move(field));
57 }
58}
59
61{
62 if (!fIsBuilt)
63 throw RException(R__FAIL("index has not been built yet"));
64}
65
66std::unique_ptr<ROOT::Experimental::Internal::RNTupleIndex>
67ROOT::Experimental::Internal::RNTupleIndex::Create(const std::vector<std::string> &fieldNames,
68 const RPageSource &pageSource, bool deferBuild)
69{
70 auto index = std::unique_ptr<RNTupleIndex>(new RNTupleIndex(fieldNames, pageSource));
71
72 if (!deferBuild)
73 index->Build();
74
75 return index;
76}
77
79{
80 if (fIsBuilt)
81 return;
82
83 static const std::unordered_set<std::string> allowedTypes = {"std::int8_t", "std::int16_t", "std::int32_t",
84 "std::int64_t", "std::uint8_t", "std::uint16_t",
85 "std::uint32_t", "std::uint64_t"};
86
87 std::vector<RFieldBase::RValue> fieldValues;
88 fieldValues.reserve(fIndexFields.size());
89
90 for (const auto &field : fIndexFields) {
91 if (allowedTypes.find(field->GetTypeName()) == allowedTypes.end()) {
92 throw RException(R__FAIL("cannot use field \"" + field->GetFieldName() + "\" with type \"" +
93 field->GetTypeName() + "\" for indexing: only integral types are allowed"));
94 }
95 fieldValues.emplace_back(field->CreateValue());
96 }
97
98 std::vector<NTupleIndexValue_t> indexValues;
99 indexValues.reserve(fIndexFields.size());
100
101 for (unsigned i = 0; i < fPageSource->GetNEntries(); ++i) {
102 indexValues.clear();
103 for (auto &fieldValue : fieldValues) {
104 // TODO(fdegeus): use bulk reading
105 fieldValue.Read(i);
106
107 auto valuePtr = fieldValue.GetPtr<void>();
108 indexValues.push_back(CastValuePtr(valuePtr.get(), fieldValue.GetField()));
109 }
110 fIndex[RIndexValue(indexValues)].push_back(i);
111 }
112
113 fIsBuilt = true;
114}
115
118{
119 const auto entryIndices = GetAllEntryNumbers(valuePtrs);
120 if (!entryIndices)
121 return kInvalidNTupleIndex;
122 return entryIndices->front();
123}
124
125const std::vector<ROOT::Experimental::NTupleSize_t> *
126ROOT::Experimental::Internal::RNTupleIndex::GetAllEntryNumbers(const std::vector<void *> &valuePtrs) const
127{
128 if (valuePtrs.size() != fIndexFields.size())
129 throw RException(R__FAIL("number of value pointers must match number of indexed fields"));
130
131 EnsureBuilt();
132
133 std::vector<NTupleIndexValue_t> indexValues;
134 indexValues.reserve(fIndexFields.size());
135
136 for (unsigned i = 0; i < valuePtrs.size(); ++i) {
137 indexValues.push_back(CastValuePtr(valuePtrs[i], *fIndexFields[i]));
138 }
139
140 auto entryNumber = fIndex.find(RIndexValue(indexValues));
141
142 if (entryNumber == fIndex.end())
143 return nullptr;
144
145 return &(entryNumber->second);
146}
#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
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.
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...
virtual size_t GetValueSize() const =0
The number of bytes taken by a value of the appropriate type.
Base class for all ROOT issued exceptions.
Definition RError.hxx:79
void CallConnectPageSourceOnField(RFieldBase &, RPageSource &)
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