libzypp: zypp/ng/sat/components/poolcomponents.h Source File

libzypp 17.38.14

poolcomponents.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8----------------------------------------------------------------------/
9*
10* This file contains private API, this might break at any time between releases.
11* You have been warned!
12*
13*/
14#ifndef ZYPP_NG_SAT_COMPONENTS_POOLCOMPONENTS_H_INCLUDED
15#define ZYPP_NG_SAT_COMPONENTS_POOLCOMPONENTS_H_INCLUDED
16
17#include <algorithm>
18#include <memory>
19#include <typeindex>
20#include <unordered_map>
21#include <array>
22#include <vector>
23#include <zypp/ng/sat/poolconstants.h>
24
25namespace zyppng::sat {
26
27 class Pool;
28 class PreparedPool;
29
35 enum class InitStage : size_t {
36 Environment,
37 COUNT
38 };
39
45 enum class PreparedStage : size_t {
46 Policy,
47 Metadata,
48 Logging,
49 COUNT
50 };
51
52 namespace detail {
59 class IBasicPoolComponent {
60 public:
65 virtual void checkDirty ( Pool & ) {}
67 virtual void onInvalidate ( Pool &, PoolInvalidation ) {}
68 virtual void onRepoAdded ( Pool &, RepoIdType ) {}
69 virtual void onRepoRemoved( Pool &, RepoIdType ) {}
70 virtual void onReset ( Pool & ) {}
71
72 virtual ~IBasicPoolComponent() = default;
73 IBasicPoolComponent() = default;
74 IBasicPoolComponent(const IBasicPoolComponent &) = delete;
75 IBasicPoolComponent(IBasicPoolComponent &&) = delete;
76 IBasicPoolComponent &operator=(const IBasicPoolComponent &) = delete;
77 IBasicPoolComponent &operator=(IBasicPoolComponent &&) = delete;
78 };
79 } // namespace detail
80
91 class IPoolComponent : public detail::IBasicPoolComponent {
92 public:
93 virtual void attach ( Pool & ) {}
94 virtual void prepare( Pool & ) {}
95 virtual InitStage stage() const { return InitStage::Environment; }
101 virtual int priority() const { return 0; }
102 };
103
113 class IPreparedPoolComponent : public detail::IBasicPoolComponent {
114 public:
115 virtual void attach ( Pool & ) {}
116 virtual void prepare( PreparedPool & ) {}
117 virtual PreparedStage stage() const { return PreparedStage::Policy; }
123 virtual int priority() const { return 0; }
124 };
125
126
138 class PoolComponentSet {
139 public:
140 PoolComponentSet() = default;
141 PoolComponentSet(const PoolComponentSet &) = delete;
142 PoolComponentSet(PoolComponentSet &&) = default;
143 PoolComponentSet &operator=(const PoolComponentSet &) = delete;
144 PoolComponentSet &operator=(PoolComponentSet &&) = default;
145
146 template <typename T>
147 T &assertComponent( std::unique_ptr<T> &&compPtr = {} ) {
148
149 std::type_index idx( typeid(T) );
150 if ( _components.count( idx ) )
151 return *static_cast<T*>( _components[idx]->get() );
152
153 auto ptr = std::move(compPtr);
154 if ( !ptr )
155 ptr = std::make_unique<T>();
156
157 std::unique_ptr<TypeErasure> component = std::make_unique<CompContainer<T>>( std::move(ptr) );
158 auto iter = _components.insert( { std::move(idx), std::move(component) } );
159 auto genericComp = static_cast<T *>( iter.first->second->get() );
160
161 // Unified basic-callbacks list — always registered when the type derives from IBasicPoolComponent.
162 if constexpr ( std::is_base_of_v<detail::IBasicPoolComponent, T> )
163 _basicComponents.push_back( static_cast<detail::IBasicPoolComponent*>( genericComp ) );
164
165 // Pre-index component list (stage-bucketed, priority-sorted within bucket).
166 if constexpr ( std::is_base_of_v<IPoolComponent, T> ) {
167 auto iComp = static_cast<IPoolComponent*>( genericComp );
168 _initComponents[static_cast<size_t>( iComp->stage() )].push_back( iComp );
169 _initDirtyBuckets[static_cast<size_t>( iComp->stage() )] = true;
170 }
171
172 // Post-index component list (stage-bucketed, priority-sorted within bucket).
173 if constexpr ( std::is_base_of_v<IPreparedPoolComponent, T> ) {
174 auto iComp = static_cast<IPreparedPoolComponent*>( genericComp );
175 _preparedComponents[static_cast<size_t>( iComp->stage() )].push_back( iComp );
176 _preparedDirtyBuckets[static_cast<size_t>( iComp->stage() )] = true;
177 }
178
179 return *genericComp;
180 }
181
182 template <typename T>
183 const T *findComponent() const {
184 const std::type_index idx( typeid(T) );
185 if ( _components.count( idx ) )
186 return static_cast<T*>( _components.at(idx)->get() );
187 return nullptr;
188 }
189
191 void notifyCheckDirty( Pool & pool ) {
192 for ( auto *comp : _basicComponents )
193 comp->checkDirty( pool );
194 }
195
197 void notifyPrepare( Pool & pool ) {
198 sortInitBuckets();
199 for ( auto & bucket : _initComponents )
200 for ( auto *comp : bucket )
201 comp->prepare( pool );
202 }
203
205 void notifyPrepareWithIndex( PreparedPool & pp ) {
206 sortPreparedBuckets();
207 for ( auto & bucket : _preparedComponents )
208 for ( auto *comp : bucket )
209 comp->prepare( pp );
210 }
211
212 void notifyInvalidate( Pool & pool, PoolInvalidation invalidation ) {
213 for ( auto *comp : _basicComponents )
214 comp->onInvalidate( pool, invalidation );
215 }
216
217 void notifyRepoAdded( Pool & pool, detail::RepoIdType id ) {
218 for ( auto *comp : _basicComponents )
219 comp->onRepoAdded( pool, id );
220 }
221
222 void notifyRepoRemoved( Pool & pool, detail::RepoIdType id ) {
223 for ( auto *comp : _basicComponents )
224 comp->onRepoRemoved( pool, id );
225 }
226
227 void notifyReset( Pool & pool ) {
228 for ( auto *comp : _basicComponents )
229 comp->onReset( pool );
230 }
231
232 private:
233 struct TypeErasure {
234 TypeErasure() = default;
235 virtual ~TypeErasure() = default;
236 virtual void * get() = 0;
237 TypeErasure(const TypeErasure &) = delete;
238 TypeErasure(TypeErasure &&) = delete;
239 TypeErasure &operator=(const TypeErasure &) = delete;
240 TypeErasure &operator=(TypeErasure &&) = delete;
241 };
242
243 template <typename T>
244 struct CompContainer : public TypeErasure {
245 CompContainer( std::unique_ptr<T> component ) : _ptr( std::move(component) ) {}
246 ~CompContainer() override = default;
247 CompContainer(const CompContainer &) = delete;
248 CompContainer(CompContainer &&) = delete;
249 CompContainer &operator=(const CompContainer &) = delete;
250 CompContainer &operator=(CompContainer &&) = delete;
251
252 std::unique_ptr<T> _ptr;
253
254 std::type_index typeIndex() const {
255 return typeid(T);
256 }
257
258 void *get() override {
259 return _ptr.get();
260 }
261 };
262
263 void sortInitBuckets() {
264 for ( size_t i = 0; i < static_cast<size_t>(InitStage::COUNT); i++ ) {
265 if ( _initDirtyBuckets[i] ) {
266 auto & bucket = _initComponents[i];
267 _initDirtyBuckets[i] = false;
268 std::stable_sort( bucket.begin(), bucket.end(), []( const auto &a, const auto &b ) {
269 return a->priority() < b->priority();
270 });
271 }
272 }
273 }
274
275 void sortPreparedBuckets() {
276 for ( size_t i = 0; i < static_cast<size_t>(PreparedStage::COUNT); i++ ) {
277 if ( _preparedDirtyBuckets[i] ) {
278 auto & bucket = _preparedComponents[i];
279 _preparedDirtyBuckets[i] = false;
280 std::stable_sort( bucket.begin(), bucket.end(), []( const auto &a, const auto &b ) {
281 return a->priority() < b->priority();
282 });
283 }
284 }
285 }
286
287 private:
288 std::unordered_map<std::type_index, std::unique_ptr<TypeErasure>> _components;
289
291 std::vector<detail::IBasicPoolComponent*> _basicComponents;
292
294 std::array<std::vector<IPoolComponent*>, static_cast<size_t>(InitStage::COUNT)> _initComponents;
295 std::array<bool, static_cast<size_t>(InitStage::COUNT)> _initDirtyBuckets = {false,};
296
298 std::array<std::vector<IPreparedPoolComponent*>, static_cast<size_t>(PreparedStage::COUNT)> _preparedComponents;
299 std::array<bool, static_cast<size_t>(PreparedStage::COUNT)> _preparedDirtyBuckets = {false,};
300 };
301
302} // namespace zyppng::sat
303
304#endif
zyppng::sat::IPoolComponent
Interface for components that run BEFORE the whatprovides index is built.
Definition poolcomponents.h:91
zyppng::sat::IPoolComponent::stage
virtual InitStage stage() const
Definition poolcomponents.h:95
zyppng::sat::IPoolComponent::prepare
virtual void prepare(Pool &)
Definition poolcomponents.h:94
zyppng::sat::IPoolComponent::attach
virtual void attach(Pool &)
Definition poolcomponents.h:93
zyppng::sat::IPoolComponent::priority
virtual int priority() const
Fine-grained ordering within a stage.
Definition poolcomponents.h:101
zyppng::sat::IPreparedPoolComponent
Interface for components that run AFTER the whatprovides index is built.
Definition poolcomponents.h:113
zyppng::sat::IPreparedPoolComponent::stage
virtual PreparedStage stage() const
Definition poolcomponents.h:117
zyppng::sat::IPreparedPoolComponent::attach
virtual void attach(Pool &)
Definition poolcomponents.h:115
zyppng::sat::IPreparedPoolComponent::priority
virtual int priority() const
Fine-grained ordering within a stage.
Definition poolcomponents.h:123
zyppng::sat::IPreparedPoolComponent::prepare
virtual void prepare(PreparedPool &)
Definition poolcomponents.h:116
zyppng::sat::PoolComponentSet::_components
std::unordered_map< std::type_index, std::unique_ptr< TypeErasure > > _components
Definition poolcomponents.h:288
zyppng::sat::PoolComponentSet::notifyRepoAdded
void notifyRepoAdded(Pool &pool, detail::RepoIdType id)
Definition poolcomponents.h:217
zyppng::sat::PoolComponentSet::notifyPrepareWithIndex
void notifyPrepareWithIndex(PreparedPool &pp)
Pass 3 of prepare(): post-index component work (stage/priority order).
Definition poolcomponents.h:205
zyppng::sat::PoolComponentSet::notifyPrepare
void notifyPrepare(Pool &pool)
Pass 2 of prepare(): pre-index component work (stage/priority order).
Definition poolcomponents.h:197
zyppng::sat::PoolComponentSet::sortPreparedBuckets
void sortPreparedBuckets()
Definition poolcomponents.h:275
zyppng::sat::PoolComponentSet::assertComponent
T & assertComponent(std::unique_ptr< T > &&compPtr={})
Definition poolcomponents.h:147
zyppng::sat::PoolComponentSet::findComponent
const T * findComponent() const
Definition poolcomponents.h:183
zyppng::sat::PoolComponentSet::_preparedDirtyBuckets
std::array< bool, static_cast< size_t >(PreparedStage::COUNT)> _preparedDirtyBuckets
Definition poolcomponents.h:299
zyppng::sat::PoolComponentSet::PoolComponentSet
PoolComponentSet(PoolComponentSet &&)=default
zyppng::sat::PoolComponentSet::_initComponents
std::array< std::vector< IPoolComponent * >, static_cast< size_t >(InitStage::COUNT)> _initComponents
Pre-index components — stage-bucketed, priority-sorted within bucket.
Definition poolcomponents.h:294
zyppng::sat::PoolComponentSet::notifyReset
void notifyReset(Pool &pool)
Definition poolcomponents.h:227
zyppng::sat::PoolComponentSet::operator=
PoolComponentSet & operator=(PoolComponentSet &&)=default
zyppng::sat::PoolComponentSet::notifyInvalidate
void notifyInvalidate(Pool &pool, PoolInvalidation invalidation)
Definition poolcomponents.h:212
zyppng::sat::PoolComponentSet::notifyCheckDirty
void notifyCheckDirty(Pool &pool)
Pass 1 of prepare(): probe external state.
Definition poolcomponents.h:191
zyppng::sat::PoolComponentSet::_initDirtyBuckets
std::array< bool, static_cast< size_t >(InitStage::COUNT)> _initDirtyBuckets
Definition poolcomponents.h:295
zyppng::sat::PoolComponentSet::notifyRepoRemoved
void notifyRepoRemoved(Pool &pool, detail::RepoIdType id)
Definition poolcomponents.h:222
zyppng::sat::PoolComponentSet::_preparedComponents
std::array< std::vector< IPreparedPoolComponent * >, static_cast< size_t >(PreparedStage::COUNT)> _preparedComponents
Post-index components — stage-bucketed, priority-sorted within bucket.
Definition poolcomponents.h:298
zyppng::sat::PoolComponentSet::_basicComponents
std::vector< detail::IBasicPoolComponent * > _basicComponents
Unified list for checkDirty / onInvalidate / onRepoAdded / onRepoRemoved.
Definition poolcomponents.h:291
zyppng::sat::PoolComponentSet::operator=
PoolComponentSet & operator=(const PoolComponentSet &)=delete
zyppng::sat::PoolComponentSet::PoolComponentSet
PoolComponentSet()=default
zyppng::sat::PoolComponentSet::sortInitBuckets
void sortInitBuckets()
Definition poolcomponents.h:263
zyppng::sat::PoolComponentSet::PoolComponentSet
PoolComponentSet(const PoolComponentSet &)=delete
zyppng::sat::Pool
Orchestrator for a libsolv pool instance.
Definition pool.h:37
zyppng::sat::PreparedPool
A move-only, non-owning view of a Pool that guarantees the whatprovides index is valid.
Definition preparedpool.h:51
zyppng::sat::detail::IBasicPoolComponent
Shared base for IPoolComponent and IPreparedPoolComponent.
Definition poolcomponents.h:59
zyppng::sat::detail::IBasicPoolComponent::IBasicPoolComponent
IBasicPoolComponent()=default
zyppng::sat::detail::IBasicPoolComponent::IBasicPoolComponent
IBasicPoolComponent(const IBasicPoolComponent &)=delete
zyppng::sat::detail::IBasicPoolComponent::onRepoRemoved
virtual void onRepoRemoved(Pool &, RepoIdType)
Definition poolcomponents.h:69
zyppng::sat::detail::IBasicPoolComponent::IBasicPoolComponent
IBasicPoolComponent(IBasicPoolComponent &&)=delete
zyppng::sat::detail::IBasicPoolComponent::operator=
IBasicPoolComponent & operator=(IBasicPoolComponent &&)=delete
zyppng::sat::detail::IBasicPoolComponent::operator=
IBasicPoolComponent & operator=(const IBasicPoolComponent &)=delete
zyppng::sat::detail::IBasicPoolComponent::checkDirty
virtual void checkDirty(Pool &)
Probe external state.
Definition poolcomponents.h:65
zyppng::sat::detail::IBasicPoolComponent::~IBasicPoolComponent
virtual ~IBasicPoolComponent()=default
zyppng::sat::detail::IBasicPoolComponent::onInvalidate
virtual void onInvalidate(Pool &, PoolInvalidation)
React to pool invalidation — clear internal caches.
Definition poolcomponents.h:67
zyppng::sat::detail::IBasicPoolComponent::onReset
virtual void onReset(Pool &)
Definition poolcomponents.h:70
zyppng::sat::detail::IBasicPoolComponent::onRepoAdded
virtual void onRepoAdded(Pool &, RepoIdType)
Definition poolcomponents.h:68
a
unsigned short a
Definition mediablocklist.cc:0
b
unsigned short b
Definition mediablocklist.cc:1
std
Definition ansi.h:855
std::is_base_of_v
constexpr bool is_base_of_v
Definition TypeTraits.h:27
zypp::pool
Definition ByIdent.h:24
zyppng::detail
Definition reporthelper.h:31
zyppng::sat::detail::RepoIdType
zypp::sat::detail::RepoIdType RepoIdType
Definition poolconstants.h:45
zyppng::sat
This file contains private API, this might break at any time between releases.
Definition capabilities.h:22
zyppng::sat::PreparedStage
PreparedStage
Execution stages for IPreparedPoolComponent (post-index phase).
Definition poolcomponents.h:45
zyppng::sat::PreparedStage::COUNT
@ COUNT
Sentinel.
Definition poolcomponents.h:49
zyppng::sat::PreparedStage::Policy
@ Policy
Blacklists, Reboot Specs, Storage Policy.
Definition poolcomponents.h:46
zyppng::sat::PreparedStage::Logging
@ Logging
Diagnostic components.
Definition poolcomponents.h:48
zyppng::sat::PreparedStage::Metadata
@ Metadata
ID-indexed metadata stores.
Definition poolcomponents.h:47
zyppng::sat::InitStage
InitStage
Execution stages for IPoolComponent (pre-index phase).
Definition poolcomponents.h:35
zyppng::sat::InitStage::Environment
@ Environment
Arches, Locales, Namespace Callbacks (The Foundation).
Definition poolcomponents.h:36
zyppng::sat::InitStage::COUNT
@ COUNT
Sentinel.
Definition poolcomponents.h:37
zyppng::sat::PoolInvalidation
PoolInvalidation
Defines the scope of an invalidation request for the Pool.
Definition poolconstants.h:66
poolconstants.h
zyppng::sat::PoolComponentSet::CompContainer::CompContainer
CompContainer(const CompContainer &)=delete
zyppng::sat::PoolComponentSet::CompContainer::typeIndex
std::type_index typeIndex() const
Definition poolcomponents.h:254
zyppng::sat::PoolComponentSet::CompContainer::get
void * get() override
Definition poolcomponents.h:258
zyppng::sat::PoolComponentSet::CompContainer::~CompContainer
~CompContainer() override=default
zyppng::sat::PoolComponentSet::CompContainer::CompContainer
CompContainer(std::unique_ptr< T > component)
Definition poolcomponents.h:245
zyppng::sat::PoolComponentSet::CompContainer::CompContainer
CompContainer(CompContainer &&)=delete
zyppng::sat::PoolComponentSet::CompContainer::_ptr
std::unique_ptr< T > _ptr
Definition poolcomponents.h:252
zyppng::sat::PoolComponentSet::CompContainer::operator=
CompContainer & operator=(CompContainer &&)=delete
zyppng::sat::PoolComponentSet::CompContainer::operator=
CompContainer & operator=(const CompContainer &)=delete
zyppng::sat::PoolComponentSet::TypeErasure::TypeErasure
TypeErasure(TypeErasure &&)=delete
zyppng::sat::PoolComponentSet::TypeErasure::operator=
TypeErasure & operator=(const TypeErasure &)=delete
zyppng::sat::PoolComponentSet::TypeErasure::TypeErasure
TypeErasure()=default
zyppng::sat::PoolComponentSet::TypeErasure::get
virtual void * get()=0
zyppng::sat::PoolComponentSet::TypeErasure::operator=
TypeErasure & operator=(TypeErasure &&)=delete
zyppng::sat::PoolComponentSet::TypeErasure::~TypeErasure
virtual ~TypeErasure()=default
zyppng::sat::PoolComponentSet::TypeErasure::TypeErasure
TypeErasure(const TypeErasure &)=delete