Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RPageSourceFriends.cxx
Go to the documentation of this file.
1/// \file RPageSourceFriends.cxx
2/// \ingroup NTuple ROOT7
3/// \author Jakob Blomer <jblomer@cern.ch>
4/// \date 2019-08-10
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-2020, 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/RCluster.hxx>
17#include <ROOT/RError.hxx>
18#include <ROOT/RLogger.hxx>
21
22#include <utility>
23
25 std::string_view ntupleName, std::span<std::unique_ptr<RPageSource>> sources)
26 : RPageSource(ntupleName, RNTupleReadOptions())
27 , fMetrics(std::string(ntupleName))
28{
29 for (auto &s : sources) {
30 fSources.emplace_back(std::move(s));
31 fMetrics.ObserveMetrics(fSources.back()->GetMetrics());
32 }
33}
34
36
37
39 std::size_t originIdx,
40 const RFieldDescriptor &originField,
41 DescriptorId_t virtualParent,
42 const std::string &virtualName)
43{
44 auto virtualFieldId = fNextId++;
45 auto virtualField = RFieldDescriptorBuilder(originField)
46 .FieldId(virtualFieldId)
47 .FieldName(virtualName)
48 .MakeDescriptor().Unwrap();
49 fBuilder.AddField(virtualField);
50 fBuilder.AddFieldLink(virtualParent, virtualFieldId);
51 fIdBiMap.Insert({originIdx, originField.GetId()}, virtualFieldId);
52
53 const auto &originDesc = fSources[originIdx]->GetDescriptor();
54 for (const auto &f : originDesc.GetFieldIterable(originField))
55 AddVirtualField(originIdx, f, virtualFieldId, f.GetFieldName());
56
57 for (const auto &c: originDesc.GetColumnIterable(originField)) {
58 fBuilder.AddColumn(fNextId, virtualFieldId, c.GetVersion(), c.GetModel(), c.GetIndex());
59 fIdBiMap.Insert({originIdx, c.GetId()}, fNextId);
60 fNextId++;
61 }
62}
63
64
66{
67 fBuilder.SetNTuple(fNTupleName, "", "", RNTupleVersion(), RNTupleUuid());
68 fBuilder.AddField(RFieldDescriptorBuilder()
69 .FieldId(0)
71 .MakeDescriptor()
72 .Unwrap());
73
74 for (std::size_t i = 0; i < fSources.size(); ++i) {
75 fSources[i]->Attach();
76 const auto &desc = fSources[i]->GetDescriptor();
77
78 if (fSources[i]->GetNEntries() != fSources[0]->GetNEntries()) {
79 fNextId = 1;
80 fIdBiMap.Clear();
81 fBuilder.Reset();
82 throw RException(R__FAIL("mismatch in the number of entries of friend RNTuples"));
83 }
84 for (unsigned j = 0; j < i; ++j) {
85 if (fSources[j]->GetDescriptor().GetName() == desc.GetName()) {
86 fNextId = 1;
87 fIdBiMap.Clear();
88 fBuilder.Reset();
89 throw RException(R__FAIL("duplicate names of friend RNTuples"));
90 }
91 }
92 AddVirtualField(i, desc.GetFieldZero(), 0, desc.GetName());
93
94 for (const auto &c : desc.GetClusterIterable()) {
95 fBuilder.AddCluster(fNextId, c.GetVersion(), c.GetFirstEntryIndex(), c.GetNEntries());
96 for (auto originColumnId : c.GetColumnIds()) {
97 DescriptorId_t virtualColumnId = fIdBiMap.GetVirtualId({i, originColumnId});
98
99 auto columnRange = c.GetColumnRange(originColumnId);
100 columnRange.fColumnId = virtualColumnId;
101 fBuilder.AddClusterColumnRange(fNextId, columnRange);
102
103 auto pageRange = c.GetPageRange(originColumnId).Clone();
104 pageRange.fColumnId = virtualColumnId;
105 fBuilder.AddClusterPageRange(fNextId, std::move(pageRange));
106 }
107 fIdBiMap.Insert({i, c.GetId()}, fNextId);
108 fNextId++;
109 }
110 }
111
112 fBuilder.EnsureValidDescriptor();
113 return fBuilder.MoveDescriptor();
114}
115
116
117std::unique_ptr<ROOT::Experimental::Detail::RPageSource>
119{
120 std::vector<std::unique_ptr<RPageSource>> cloneSources;
121 for (const auto &f : fSources)
122 cloneSources.emplace_back(f->Clone());
123 return std::make_unique<RPageSourceFriends>(fNTupleName, cloneSources);
124}
125
126
129{
130 auto originFieldId = fIdBiMap.GetOriginId(fieldId);
131 fSources[originFieldId.fSourceIdx]->AddColumn(originFieldId.fId, column);
132 return RPageSource::AddColumn(fieldId, column);
133}
134
136{
137 RPageSource::DropColumn(columnHandle);
138 auto originColumnId = fIdBiMap.GetOriginId(columnHandle.fId);
139 columnHandle.fId = originColumnId.fId;
140 fSources[originColumnId.fSourceIdx]->DropColumn(columnHandle);
141}
142
143
146 ColumnHandle_t columnHandle, NTupleSize_t globalIndex)
147{
148 auto virtualColumnId = columnHandle.fId;
149 auto originColumnId = fIdBiMap.GetOriginId(virtualColumnId);
150 columnHandle.fId = originColumnId.fId;
151
152 auto page = fSources[originColumnId.fSourceIdx]->PopulatePage(columnHandle, globalIndex);
153
154 auto virtualClusterId = fIdBiMap.GetVirtualId({originColumnId.fSourceIdx, page.GetClusterInfo().GetId()});
155 page.ChangeIds(virtualColumnId, virtualClusterId);
156
157 return page;
158}
159
160
163 ColumnHandle_t columnHandle, const RClusterIndex &clusterIndex)
164{
165 auto virtualColumnId = columnHandle.fId;
166 auto originColumnId = fIdBiMap.GetOriginId(virtualColumnId);
167 RClusterIndex originClusterIndex(
168 fIdBiMap.GetOriginId(clusterIndex.GetClusterId()).fId,
169 clusterIndex.GetIndex());
170 columnHandle.fId = originColumnId.fId;
171
172 auto page = fSources[originColumnId.fSourceIdx]->PopulatePage(columnHandle, originClusterIndex);
173
174 page.ChangeIds(virtualColumnId, clusterIndex.GetClusterId());
175 return page;
176}
177
178
180 DescriptorId_t columnId, const RClusterIndex &clusterIndex, RSealedPage &sealedPage)
181{
182 auto originColumnId = fIdBiMap.GetOriginId(columnId);
183 RClusterIndex originClusterIndex(
184 fIdBiMap.GetOriginId(clusterIndex.GetClusterId()).fId,
185 clusterIndex.GetIndex());
186
187 fSources[originColumnId.fSourceIdx]->LoadSealedPage(columnId, originClusterIndex, sealedPage);
188}
189
190
192{
193 if (page.IsNull())
194 return;
195 auto sourceIdx = fIdBiMap.GetOriginId(page.GetClusterInfo().GetId()).fSourceIdx;
196 fSources[sourceIdx]->ReleasePage(page);
197}
198
199
200std::vector<std::unique_ptr<ROOT::Experimental::Detail::RCluster>>
202{
203 // The virtual friends page source does not pre-load any clusters itself. However, the underlying page sources
204 // that are combined may well do it.
205 return std::vector<std::unique_ptr<ROOT::Experimental::Detail::RCluster>>(clusterKeys.size());
206}
#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:291
#define f(i)
Definition RSha256.hxx:104
#define c(i)
Definition RSha256.hxx:101
void ObserveMetrics(RNTupleMetrics &observee)
std::unique_ptr< RPageSource > Clone() const final
Open the same storage multiple time, e.g. for reading in multiple threads.
void ReleasePage(RPage &page) final
Every page store needs to be able to free pages it handed out.
void LoadSealedPage(DescriptorId_t columnId, const RClusterIndex &clusterIndex, RSealedPage &sealedPage) final
Read the packed and compressed bytes of a page into the memory buffer provided by selaedPage.
std::vector< std::unique_ptr< RPageSource > > fSources
std::vector< std::unique_ptr< RCluster > > LoadClusters(std::span< RCluster::RKey > clusterKeys) final
Populates all the pages of the given cluster ids and columns; it is possible that some columns do not...
RPage PopulatePage(ColumnHandle_t columnHandle, NTupleSize_t globalIndex) final
Allocates and fills a page that contains the index-th element.
RPageSourceFriends(std::string_view ntupleName, std::span< std::unique_ptr< RPageSource > > sources)
void DropColumn(ColumnHandle_t columnHandle) final
Unregisters a column.
void AddVirtualField(std::size_t originIdx, const RFieldDescriptor &originField, DescriptorId_t virtualParent, const std::string &virtualName)
ColumnHandle_t AddColumn(DescriptorId_t fieldId, const RColumn &column) final
Register a new column.
Abstract interface to read data from an ntuple.
void DropColumn(ColumnHandle_t columnHandle) override
Unregisters a column.
ColumnHandle_t AddColumn(DescriptorId_t fieldId, const RColumn &column) override
Register a new column.
A page is a slice of a column that is mapped into memory.
Definition RPage.hxx:41
const RClusterInfo & GetClusterInfo() const
Definition RPage.hxx:91
Addresses a column element or field item relative to a particular cluster, instead of a global NTuple...
DescriptorId_t GetClusterId() const
ClusterSize_t::ValueType GetIndex() const
Base class for all ROOT issued exceptions.
Definition RError.hxx:114
A helper class for piece-wise construction of an RFieldDescriptor.
RFieldDescriptorBuilder & FieldName(const std::string &fieldName)
RResult< RFieldDescriptor > MakeDescriptor() const
Attempt to make a field descriptor.
RFieldDescriptorBuilder & FieldId(DescriptorId_t fieldId)
Meta-data stored for every field of an ntuple.
The on-storage meta-data of an ntuple.
Common user-tunable settings for reading ntuples.
For forward and backward compatibility, attach version information to the consitituents of the file f...
std::uint64_t NTupleSize_t
Integer type long enough to hold the maximum number of entries in a column.
std::uint64_t DescriptorId_t
Distriniguishes elements of the same type within a descriptor, e.g. different fields.
std::string RNTupleUuid
Every NTuple is identified by a UUID. TODO(jblomer): should this be a TUUID?
A sealed page contains the bytes of a page as written to storage (packed & compressed).