libzypp  17.14.0
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  bool reusedIDs = _watcherIDs.remember( pool.serialIDs() );
310  std::list<PoolItem> addedProducts;
311 
312  _store.resize( pool.capacity() );
313 
314  if ( pool.capacity() )
315  {
316  for ( sat::detail::SolvableIdType i = pool.capacity()-1; i != 0; --i )
317  {
318  sat::Solvable s( i );
319  PoolItem & pi( _store[i] );
320  if ( ! s && pi )
321  {
322  // the PoolItem got invalidated (e.g unloaded repo)
323  pi = PoolItem();
324  }
325  else if ( reusedIDs || (s && ! pi) )
326  {
327  // new PoolItem to add
328  pi = PoolItem::makePoolItem( s ); // the only way to create a new one!
329  // remember products for buddy processing (requires clean store)
330  if ( s.isKind( ResKind::product ) )
331  addedProducts.push_back( pi );
332  if ( !addedItems )
333  addedItems = true;
334  }
335  }
336  }
337  _storeDirty = false;
338 
339  // Now, as the pool is adjusted, ....
340 
341  // .... we check for product buddies.
342  if ( ! addedProducts.empty() )
343  {
344  for_( it, addedProducts.begin(), addedProducts.end() )
345  {
346  it->setBuddy( asKind<Product>(*it)->referencePackage() );
347  }
348  }
349 
350  // .... we must reapply those query based hard locks.
351  if ( addedItems )
352  {
354  }
355  }
356  return _store;
357  }
358 
359  const Id2ItemT & id2item () const
360  {
361  checkSerial();
362  if ( _id2itemDirty )
363  {
364  store();
365  _id2item = Id2ItemT( size() );
366  for_( it, begin(), end() )
367  {
368  const sat::Solvable &s = (*it)->satSolvable();
369  sat::detail::IdType id = s.ident().id();
370  if ( s.isKind( ResKind::srcpackage ) )
371  id = -id;
372  _id2item.insert( std::make_pair( id, *it ) );
373  }
374  //INT << _id2item << endl;
375  _id2itemDirty = false;
376  }
377  return _id2item;
378  }
379 
381  //
383  private:
384  void checkSerial() const
385  {
386  if ( _watcher.remember( serial() ) )
387  invalidate();
388  satpool().prepare(); // always ajust dependencies.
389  }
390 
391  void invalidate() const
392  {
393  _storeDirty = true;
394  _id2itemDirty = true;
395  _id2item.clear();
396  _poolProxy.reset();
397  }
398 
399  private:
408 
409  private:
410  mutable shared_ptr<ResPoolProxy> _poolProxy;
411 
412  private:
415  };
417 
419  } // namespace pool
422 } // namespace zypp
424 #endif // ZYPP_POOL_POOLIMPL_H