libzypp  17.38.7
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>
24 
25 namespace 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 {
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;
78  };
79  } // namespace detail
80 
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 
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 
139  public:
140  PoolComponentSet() = default;
141  PoolComponentSet(const PoolComponentSet &) = delete;
142  PoolComponentSet(PoolComponentSet &&) = default;
143  PoolComponentSet &operator=(const PoolComponentSet &) = delete;
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 
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 
218  for ( auto *comp : _basicComponents )
219  comp->onRepoAdded( pool, id );
220  }
221 
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 
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 
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
ID-indexed metadata stores.
virtual void onInvalidate(Pool &, PoolInvalidation)
React to pool invalidation — clear internal caches.
virtual int priority() const
Fine-grained ordering within a stage.
void notifyCheckDirty(Pool &pool)
Pass 1 of prepare(): probe external state.
std::vector< detail::IBasicPoolComponent * > _basicComponents
Unified list for checkDirty / onInvalidate / onRepoAdded / onRepoRemoved.
unsigned short b
PreparedStage
Execution stages for IPreparedPoolComponent (post-index phase).
virtual PreparedStage stage() const
std::array< std::vector< IPoolComponent * >, static_cast< size_t >InitStage::COUNT)> _initComponents
Pre-index components — stage-bucketed, priority-sorted within bucket.
Definition: ansi.h:854
const T * findComponent() const
std::array< bool, static_cast< size_t >PreparedStage::COUNT)> _preparedDirtyBuckets
Orchestrator for a libsolv pool instance.
Definition: pool.h:36
Interface for components that run AFTER the whatprovides index is built.
TypeErasure & operator=(const TypeErasure &)=delete
void notifyRepoAdded(Pool &pool, detail::RepoIdType id)
A move-only, non-owning view of a Pool that guarantees the whatprovides index is valid.
Definition: preparedpool.h:50
virtual void onRepoRemoved(Pool &, RepoIdType)
Shared base for IPoolComponent and IPreparedPoolComponent.
Arches, Locales, Namespace Callbacks (The Foundation)
PoolComponentSet & operator=(const PoolComponentSet &)=delete
Interface for components that run BEFORE the whatprovides index is built.
Registry and dispatcher for all pool components.
This file contains private API, this might break at any time between releases.
Definition: capabilities.h:22
virtual void checkDirty(Pool &)
Probe external state.
void notifyRepoRemoved(Pool &pool, detail::RepoIdType id)
Blacklists, Reboot Specs, Storage Policy.
virtual int priority() const
Fine-grained ordering within a stage.
virtual void prepare(PreparedPool &)
virtual void onRepoAdded(Pool &, RepoIdType)
CompContainer & operator=(const CompContainer &)=delete
virtual void prepare(Pool &)
PoolInvalidation
Defines the scope of an invalidation request for the Pool.
Definition: poolconstants.h:66
unsigned short a
virtual InitStage stage() const
void notifyInvalidate(Pool &pool, PoolInvalidation invalidation)
std::array< bool, static_cast< size_t >InitStage::COUNT)> _initDirtyBuckets
T & assertComponent(std::unique_ptr< T > &&compPtr={})
CompContainer(std::unique_ptr< T > component)
void notifyPrepare(Pool &pool)
Pass 2 of prepare(): pre-index component work (stage/priority order).
IBasicPoolComponent & operator=(const IBasicPoolComponent &)=delete
void notifyPrepareWithIndex(PreparedPool &pp)
Pass 3 of prepare(): post-index component work (stage/priority order).
std::array< std::vector< IPreparedPoolComponent * >, static_cast< size_t >PreparedStage::COUNT)> _preparedComponents
Post-index components — stage-bucketed, priority-sorted within bucket.
InitStage
Execution stages for IPoolComponent (pre-index phase).
zypp::sat::detail::RepoIdType RepoIdType
Definition: poolconstants.h:45
std::unordered_map< std::type_index, std::unique_ptr< TypeErasure > > _components
virtual void attach(Pool &)