libzypp  15.28.4
PoolImpl.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_POOL_POOLIMPL_H
13 #define ZYPP_POOL_POOLIMPL_H
14 
15 #include <iosfwd>
16 
17 #include "zypp/base/Easy.h"
18 #include "zypp/base/LogTools.h"
19 #include "zypp/base/SerialNumber.h"
20 #include "zypp/APIConfig.h"
21 
22 #include "zypp/pool/PoolTraits.h"
23 #include "zypp/ResPoolProxy.h"
24 #include "zypp/PoolQueryResult.h"
25 
26 #include "zypp/sat/Pool.h"
27 #include "zypp/Product.h"
28 
29 using std::endl;
30 
32 namespace zypp
33 {
34 
35  namespace resstatus
36  {
43  {
45  static void setLock( ResStatus & status_r, bool yesno_r )
46  {
47  status_r.setLock( yesno_r, ResStatus::USER );
48  status_r.setUserLockQueryMatch( yesno_r );
49  }
50 
52  static void reapplyLock( ResStatus & status_r, bool yesno_r )
53  {
54  if ( yesno_r && ! status_r.isUserLockQueryMatch() )
55  {
56  status_r.setLock( yesno_r, ResStatus::USER );
57  status_r.setUserLockQueryMatch( yesno_r );
58  }
59  }
60 
62  static int diffLock( const ResStatus & status_r )
63  {
64  bool userLock( status_r.isUserLocked() );
65  if ( userLock == status_r.isUserLockQueryMatch() )
66  return 0;
67  return userLock ? 1 : -1;
68  }
69 
70  };
71  }
72 
73  namespace
74  {
75  inline PoolQuery makeTrivialQuery( IdString ident_r )
76  {
77  sat::Solvable::SplitIdent ident( ident_r );
78 
79  PoolQuery q;
80  q.addAttribute( sat::SolvAttr::name, ident.name().asString() );
81  q.addKind( ident.kind() );
82  q.setMatchExact();
83  q.setCaseSensitive(true);
84  return q;
85  }
86 
87  inline bool hardLockQueriesRemove( pool::PoolTraits::HardLockQueries & activeLocks_r, IdString ident_r )
88  {
89  unsigned s( activeLocks_r.size() );
90  activeLocks_r.remove( makeTrivialQuery( ident_r ) );
91  return( activeLocks_r.size() != s );
92  }
93 
94  inline bool hardLockQueriesAdd( pool::PoolTraits::HardLockQueries & activeLocks_r, IdString ident_r )
95  {
96  PoolQuery q( makeTrivialQuery( ident_r ) );
97  for_( it, activeLocks_r.begin(), activeLocks_r.end() )
98  {
99  if ( *it == q )
100  return false;
101  }
102  activeLocks_r.push_back( q );
103  return true;
104  }
105  }
106 
108  namespace pool
109  {
110 
112  //
113  // CLASS NAME : PoolImpl
114  //
116  class PoolImpl
117  {
118  friend std::ostream & operator<<( std::ostream & str, const PoolImpl & obj );
119 
120  public:
126 
128 
130 
131  public:
133  PoolImpl();
135  ~PoolImpl();
136 
137  public:
139  const sat::Pool satpool() const
140  { return sat::Pool::instance(); }
141 
143  const SerialNumber & serial() const
144  { return satpool().serial(); }
145 
147  //
149  public:
151  bool empty() const
152  { return satpool().solvablesEmpty(); }
153 
155  size_type size() const
156  { return satpool().solvablesSize(); }
157 
159  { return make_filter_begin( pool::ByPoolItem(), store() ); }
160 
162  { return make_filter_end( pool::ByPoolItem(), store() ); }
163 
164  public:
170  PoolItem find( const sat::Solvable & slv_r ) const
171  {
172  const ContainerT & mystore( store() );
173  return( slv_r.id() < mystore.size() ? mystore[slv_r.id()] : PoolItem() );
174  }
175 
177  //
179  public:
182  void SaveState( const ResKind & kind_r );
183 
184  void RestoreState( const ResKind & kind_r );
186 
188  //
190  public:
191  ResPoolProxy proxy( ResPool self ) const
192  {
193  checkSerial();
194  if ( !_poolProxy )
195  {
196  _poolProxy.reset( new ResPoolProxy( self, *this ) );
197  }
198  return *_poolProxy;
199  }
200 
201  public:
204  { checkSerial(); return satpool().reposSize(); }
205 
207  { checkSerial(); return satpool().reposBegin(); }
208 
210  { checkSerial(); return satpool().reposEnd(); }
211 
212  Repository reposFind( const std::string & alias_r ) const
213  { checkSerial(); return satpool().reposFind( alias_r ); }
214 
216  //
218  public:
221 
223  { return _hardLockQueries; }
224 
225  void reapplyHardLocks() const
226  {
227  // It is assumed that reapplyHardLocks is called after new
228  // items were added to the pool, but the _hardLockQueries
229  // did not change since. Action is to be performed only on
230  // those items that gained the bit in the UserLockQueryField.
231  MIL << "Re-apply " << _hardLockQueries.size() << " HardLockQueries" << endl;
232  PoolQueryResult locked;
233  for_( it, _hardLockQueries.begin(), _hardLockQueries.end() )
234  {
235  locked += *it;
236  }
237  MIL << "HardLockQueries match " << locked.size() << " Solvables." << endl;
238  for_( it, begin(), end() )
239  {
240  resstatus::UserLockQueryManip::reapplyLock( it->status(), locked.contains( *it ) );
241  }
242  }
243 
244  void setHardLockQueries( const HardLockQueries & newLocks_r )
245  {
246  MIL << "Apply " << newLocks_r.size() << " HardLockQueries" << endl;
247  _hardLockQueries = newLocks_r;
248  // now adjust the pool status
249  PoolQueryResult locked;
250  for_( it, _hardLockQueries.begin(), _hardLockQueries.end() )
251  {
252  locked += *it;
253  }
254  MIL << "HardLockQueries match " << locked.size() << " Solvables." << endl;
255  for_( it, begin(), end() )
256  {
257  resstatus::UserLockQueryManip::setLock( it->status(), locked.contains( *it ) );
258  }
259  }
260 
261  bool getHardLockQueries( HardLockQueries & activeLocks_r )
262  {
263  activeLocks_r = _hardLockQueries; // current queries
264  // Now diff to the pool collecting names only.
265  // Thus added and removed locks are not necessarily
266  // disjoint. Added locks win.
267  typedef std::unordered_set<IdString> IdentSet;
268  IdentSet addedLocks;
269  IdentSet removedLocks;
270  for_( it, begin(), end() )
271  {
272  switch ( resstatus::UserLockQueryManip::diffLock( it->status() ) )
273  {
274  case 0: // unchanged
275  break;
276  case 1:
277  addedLocks.insert( it->satSolvable().ident() );
278  break;
279  case -1:
280  removedLocks.insert( it->satSolvable().ident() );
281  break;
282  }
283  }
284  // now the bad part - adjust the queries
285  bool setChanged = false;
286  for_( it, removedLocks.begin(), removedLocks.end() )
287  {
288  if ( addedLocks.find( *it ) != addedLocks.end() )
289  continue; // Added locks win
290  if ( hardLockQueriesRemove( activeLocks_r, *it ) && ! setChanged )
291  setChanged = true;
292  }
293  for_( it, addedLocks.begin(), addedLocks.end() )
294  {
295  if ( hardLockQueriesAdd( activeLocks_r, *it ) && ! setChanged )
296  setChanged = true;
297  }
298  return setChanged;
299  }
300 
301  public:
302  const ContainerT & store() const
303  {
304  checkSerial();
305  if ( _storeDirty )
306  {
307  sat::Pool pool( satpool() );
308  bool addedItems = false;
309  std::list<PoolItem> addedProducts;
310 
311  _store.resize( pool.capacity() );
312 
313  if ( pool.capacity() )
314  {
315  for ( sat::detail::SolvableIdType i = pool.capacity()-1; i != 0; --i )
316  {
317  sat::Solvable s( i );
318  PoolItem & pi( _store[i] );
319  if ( ! s && pi )
320  {
321  // the PoolItem got invalidated (e.g unloaded repo)
322  pi = PoolItem();
323  }
324  else if ( s && ! pi )
325  {
326  // new PoolItem to add
327  pi = PoolItem::makePoolItem( s ); // the only way to create a new one!
328  // remember products for buddy processing (requires clean store)
329  if ( s.isKind( ResKind::product ) )
330  addedProducts.push_back( pi );
331  if ( !addedItems )
332  addedItems = true;
333  }
334  }
335  }
336  _storeDirty = false;
337 
338  // Now, as the pool is adjusted, ....
339 
340  // .... we check for product buddies.
341  if ( ! addedProducts.empty() )
342  {
343  for_( it, addedProducts.begin(), addedProducts.end() )
344  {
345  it->setBuddy( asKind<Product>(*it)->referencePackage() );
346  }
347  }
348 
349  // .... we must reapply those query based hard locks.
350  if ( addedItems )
351  {
353  }
354  }
355  return _store;
356  }
357 
358  const Id2ItemT & id2item () const
359  {
360  checkSerial();
361  if ( _id2itemDirty )
362  {
363  store();
364  _id2item = Id2ItemT( size() );
365  for_( it, begin(), end() )
366  {
367  const sat::Solvable &s = (*it)->satSolvable();
368  sat::detail::IdType id = s.ident().id();
369  if ( s.isKind( ResKind::srcpackage ) )
370  id = -id;
371  _id2item.insert( std::make_pair( id, *it ) );
372  }
373  //INT << _id2item << endl;
374  _id2itemDirty = false;
375  }
376  return _id2item;
377  }
378 
380  //
382  private:
383  void checkSerial() const
384  {
385  if ( _watcher.remember( serial() ) )
386  invalidate();
387  satpool().prepare(); // always ajust dependencies.
388  }
389 
390  void invalidate() const
391  {
392  _storeDirty = true;
393  _id2itemDirty = true;
394  _id2item.clear();
395  _poolProxy.reset();
396  }
397 
398  private:
405 
406  private:
407  mutable shared_ptr<ResPoolProxy> _poolProxy;
408 
409  private:
412  };
414 
416  } // namespace pool
419 } // namespace zypp
421 #endif // ZYPP_POOL_POOLIMPL_H
#define MIL
Definition: Logger.h:64
Simple serial number watcher.
Definition: SerialNumber.h:122
ItemContainerT::size_type size_type
Definition: PoolTraits.h:75
int IdType
Generic Id type.
Definition: PoolMember.h:130
A Solvable object within the sat Pool.
Definition: Solvable.h:53
sat::detail::SolvableIdType SolvableIdType
Definition: PoolImpl.h:129
bool contains(sat::Solvable result_r) const
Test whether some item is in the result set.
IdString ident() const
The identifier.
Definition: Solvable.cc:257
size_type reposSize() const
Number of repos in Pool.
Definition: Pool.cc:73
PoolTraits::HardLockQueries HardLockQueries
Definition: PoolImpl.h:219
void RestoreState(const ResKind &kind_r)
static void setLock(ResStatus &status_r, bool yesno_r)
Set lock and UserLockQuery bit according to yesno_r.
Definition: PoolImpl.h:45
std::vector< PoolItem > ItemContainerT
pure items
Definition: PoolTraits.h:71
IdType id() const
Expert backdoor.
Definition: IdString.h:115
size_type size() const
Definition: PoolImpl.h:155
const_iterator end() const
Definition: PoolImpl.h:161
DefaultIntegral< bool, true > _storeDirty
Definition: PoolImpl.h:402
void SaveState(const ResKind &kind_r)
size_type knownRepositoriesSize() const
Forward list of Repositories that contribute ResObjects from sat::Pool.
Definition: PoolImpl.h:203
unsigned SolvableIdType
Id type to connect Solvable and sat-solvable.
Definition: PoolMember.h:151
static const ResKind srcpackage
Definition: ResKind.h:44
PoolTraits::size_type size_type
Definition: PoolImpl.h:123
PoolTraits::Id2ItemT Id2ItemT
Definition: PoolImpl.h:125
SerialNumberWatcher _watcher
Watch sat pools serial number.
Definition: PoolImpl.h:400
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:27
Manipulator for ResStatus::UserLockQueryField.
Definition: PoolImpl.h:42
const ContainerT & store() const
Definition: PoolImpl.h:302
friend std::ostream & operator<<(std::ostream &str, const PoolImpl &obj)
std::list< PoolQuery > HardLockQueries
hard locks from etc/zypp/locks
Definition: PoolTraits.h:88
static int diffLock(const ResStatus &status_r)
Test whether the lock status differs from the remembered UserLockQuery bit.
Definition: PoolImpl.h:62
PoolTraits::const_iterator const_iterator
Definition: PoolImpl.h:124
filter_iterator< TFilter, typename TContainer::const_iterator > make_filter_end(TFilter f, const TContainer &c)
Convenience to create filter_iterator from container::end().
Definition: Iterator.h:117
ResPool::instance().proxy();.
Definition: ResPoolProxy.h:34
const SerialNumber & serial() const
Housekeeping data serial number.
Definition: Pool.cc:55
size_type solvablesSize() const
Number of solvables in Pool.
Definition: Pool.cc:103
DefaultIntegral< bool, true > _id2itemDirty
Definition: PoolImpl.h:404
RepositoryIterator reposEnd() const
Iterator behind the last Repository.
Definition: Pool.cc:87
size_type capacity() const
Internal array size for stats only.
Definition: Pool.cc:52
const_iterator begin() const
Definition: PoolImpl.h:158
bool empty() const
Definition: PoolImpl.h:151
PoolImpl()
Default ctor.
Definition: PoolImpl.cc:38
void invalidate() const
Definition: PoolImpl.h:390
static Pool instance()
Singleton ctor.
Definition: Pool.h:53
void checkSerial() const
Definition: PoolImpl.h:383
std::string asString() const
Definition: IdStringType.h:106
Provides API related macros.
PoolTraits::ItemContainerT ContainerT
Definition: PoolImpl.h:122
Repository reposFind(const std::string &alias_r) const
Find a Repository named alias_r.
Definition: Pool.cc:142
PoolItem find(const sat::Solvable &slv_r) const
Return the corresponding PoolItem.
Definition: PoolImpl.h:170
RepositoryIterator reposBegin() const
Iterator to the first Repository.
Definition: Pool.cc:76
static PoolItem makePoolItem(const sat::Solvable &solvable_r)
PoolItem generator for pool::PoolImpl.
Definition: PoolItem.cc:192
bool isUserLocked() const
Definition: ResStatus.h:255
bool setLock(bool toLock_r, TransactByValue causer_r)
Apply a lock (prevent transaction).
Definition: ResStatus.h:376
static const SolvAttr name
Definition: SolvAttr.h:52
shared_ptr< ResPoolProxy > _poolProxy
Definition: PoolImpl.h:407
filter_iterator< ByPoolItem, ItemContainerT::const_iterator > const_iterator
Definition: PoolTraits.h:74
Simple serial number provider.
Definition: SerialNumber.h:44
const sat::Pool satpool() const
convenience.
Definition: PoolImpl.h:139
PoolTraits::repository_iterator repository_iterator
Definition: PoolImpl.h:127
void setUserLockQueryMatch(bool match_r)
Definition: ResStatus.h:327
Global ResObject pool.
Definition: ResPool.h:60
std::unordered_multimap< sat::detail::IdType, PoolItem > Id2ItemT
ident index
Definition: PoolTraits.h:79
repository_iterator knownRepositoriesBegin() const
Definition: PoolImpl.h:206
repository_iterator knownRepositoriesEnd() const
Definition: PoolImpl.h:209
size_type size() const
The number of sat::Solvables.
void prepare() const
Update housekeeping data if necessary (e.g.
Definition: Pool.cc:58
Global sat-pool.
Definition: Pool.h:44
HardLockQueries::const_iterator hardLockQueries_iterator
Definition: PoolTraits.h:89
Status bitfield.
Definition: ResStatus.h:53
PoolTraits::hardLockQueries_iterator hardLockQueries_iterator
Definition: PoolImpl.h:220
HardLockQueries _hardLockQueries
Set of queries that define hardlocks.
Definition: PoolImpl.h:411
const HardLockQueries & hardLockQueries() const
Definition: PoolImpl.h:222
Combining sat::Solvable and ResStatus.
Definition: PoolItem.h:50
IdType id() const
Expert backdoor.
Definition: Solvable.h:374
filter_iterator< TFilter, typename TContainer::const_iterator > make_filter_begin(TFilter f, const TContainer &c)
Convenience to create filter_iterator from container::begin().
Definition: Iterator.h:101
Repository reposFind(const std::string &alias_r) const
Definition: PoolImpl.h:212
ContainerT _store
Definition: PoolImpl.h:401
const Id2ItemT & id2item() const
Definition: PoolImpl.h:358
Helper class to collect (not only) PoolQuery results.
void setHardLockQueries(const HardLockQueries &newLocks_r)
Definition: PoolImpl.h:244
ResPoolProxy proxy(ResPool self) const
Definition: PoolImpl.h:191
bool isKind(const ResKind &kind_r) const
Test whether a Solvable is of a certain ResKind.
Definition: Solvable.cc:290
bool solvablesEmpty() const
Whether Pool contains solvables.
Definition: Pool.cc:90
static const ResKind product
Definition: ResKind.h:43
Pool internal filter skiping invalid/unwanted PoolItems.
Definition: PoolTraits.h:40
Resolvable kinds.
Definition: ResKind.h:32
const SerialNumber & serial() const
Housekeeping data serial number.
Definition: PoolImpl.h:143
bool isUserLockQueryMatch() const
Definition: ResStatus.h:324
static void reapplyLock(ResStatus &status_r, bool yesno_r)
Update lock and UserLockQuery bit IFF the item gained the bit.
Definition: PoolImpl.h:52
bool remember(unsigned serial_r) const
Return isDirty, storing serial_r as new value.
Definition: SerialNumber.h:160
void reapplyHardLocks() const
Definition: PoolImpl.h:225
bool getHardLockQueries(HardLockQueries &activeLocks_r)
Definition: PoolImpl.h:261