1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/pool/PoolImpl.h
12 #ifndef ZYPP_POOL_POOLIMPL_H
13 #define ZYPP_POOL_POOLIMPL_H
17 #include "zypp/base/Easy.h"
18 #include "zypp/base/LogTools.h"
19 #include "zypp/base/SerialNumber.h"
20 #include "zypp/APIConfig.h"
22 #include "zypp/pool/PoolTraits.h"
23 #include "zypp/ResPoolProxy.h"
24 #include "zypp/PoolQueryResult.h"
26 #include "zypp/sat/Pool.h"
27 #include "zypp/Product.h"
31 ///////////////////////////////////////////////////////////////////
33 { /////////////////////////////////////////////////////////////////
37 /** Manipulator for \ref ResStatus::UserLockQueryField.
38 * Field is not public available. It is intended to remember the
39 * initial lock status usually derived from /etc/zypp/locks. So
40 * we are able to detect changes we have to write back on commit.
42 struct UserLockQueryManip
44 /** Set lock and UserLockQuery bit according to \c yesno_r. */
45 static void setLock( ResStatus & status_r, bool yesno_r )
47 status_r.setLock( yesno_r, ResStatus::USER );
48 status_r.setUserLockQueryMatch( yesno_r );
51 /** Update lock and UserLockQuery bit IFF the item gained the bit. */
52 static void reapplyLock( ResStatus & status_r, bool yesno_r )
54 if ( yesno_r && ! status_r.isUserLockQueryMatch() )
56 status_r.setLock( yesno_r, ResStatus::USER );
57 status_r.setUserLockQueryMatch( yesno_r );
61 /** Test whether the lock status differs from the remembered UserLockQuery bit. */
62 static int diffLock( const ResStatus & status_r )
64 bool userLock( status_r.isUserLocked() );
65 if ( userLock == status_r.isUserLockQueryMatch() )
67 return userLock ? 1 : -1;
75 inline PoolQuery makeTrivialQuery( IdString ident_r )
77 sat::Solvable::SplitIdent ident( ident_r );
80 q.addAttribute( sat::SolvAttr::name, ident.name().asString() );
81 q.addKind( ident.kind() );
83 q.setCaseSensitive(true);
87 inline bool hardLockQueriesRemove( pool::PoolTraits::HardLockQueries & activeLocks_r, IdString ident_r )
89 unsigned s( activeLocks_r.size() );
90 activeLocks_r.remove( makeTrivialQuery( ident_r ) );
91 return( activeLocks_r.size() != s );
94 inline bool hardLockQueriesAdd( pool::PoolTraits::HardLockQueries & activeLocks_r, IdString ident_r )
96 PoolQuery q( makeTrivialQuery( ident_r ) );
97 for_( it, activeLocks_r.begin(), activeLocks_r.end() )
102 activeLocks_r.push_back( q );
107 ///////////////////////////////////////////////////////////////////
109 { /////////////////////////////////////////////////////////////////
111 ///////////////////////////////////////////////////////////////////
113 // CLASS NAME : PoolImpl
118 friend std::ostream & operator<<( std::ostream & str, const PoolImpl & obj );
122 typedef PoolTraits::ItemContainerT ContainerT;
123 typedef PoolTraits::size_type size_type;
124 typedef PoolTraits::const_iterator const_iterator;
125 typedef PoolTraits::Id2ItemT Id2ItemT;
127 typedef PoolTraits::repository_iterator repository_iterator;
129 typedef sat::detail::SolvableIdType SolvableIdType;
139 const sat::Pool satpool() const
140 { return sat::Pool::instance(); }
142 /** Housekeeping data serial number. */
143 const SerialNumber & serial() const
144 { return satpool().serial(); }
146 ///////////////////////////////////////////////////////////////////
148 ///////////////////////////////////////////////////////////////////
152 { return satpool().solvablesEmpty(); }
155 size_type size() const
156 { return satpool().solvablesSize(); }
158 const_iterator begin() const
159 { return make_filter_begin( pool::ByPoolItem(), store() ); }
161 const_iterator end() const
162 { return make_filter_end( pool::ByPoolItem(), store() ); }
165 /** Return the corresponding \ref PoolItem.
166 * Pool and sat pool should be in sync. Returns an empty
167 * \ref PoolItem if there is no corresponding \ref PoolItem.
168 * \see \ref PoolItem::satSolvable.
170 PoolItem find( const sat::Solvable & slv_r ) const
172 const ContainerT & mystore( store() );
173 return( slv_r.id() < mystore.size() ? mystore[slv_r.id()] : PoolItem() );
176 ///////////////////////////////////////////////////////////////////
178 ///////////////////////////////////////////////////////////////////
180 /** \name Save and restore state. */
182 void SaveState( const ResObject::Kind & kind_r );
184 void RestoreState( const ResObject::Kind & kind_r );
187 ///////////////////////////////////////////////////////////////////
189 ///////////////////////////////////////////////////////////////////
191 ResPoolProxy proxy( ResPool self ) const
196 _poolProxy.reset( new ResPoolProxy( self, *this ) );
202 /** Forward list of Repositories that contribute ResObjects from \ref sat::Pool */
203 size_type knownRepositoriesSize() const
204 { checkSerial(); return satpool().reposSize(); }
206 repository_iterator knownRepositoriesBegin() const
207 { checkSerial(); return satpool().reposBegin(); }
209 repository_iterator knownRepositoriesEnd() const
210 { checkSerial(); return satpool().reposEnd(); }
212 Repository reposFind( const std::string & alias_r ) const
213 { checkSerial(); return satpool().reposFind( alias_r ); }
215 ///////////////////////////////////////////////////////////////////
217 ///////////////////////////////////////////////////////////////////
219 typedef PoolTraits::HardLockQueries HardLockQueries;
220 typedef PoolTraits::hardLockQueries_iterator hardLockQueries_iterator;
222 const HardLockQueries & hardLockQueries() const
223 { return _hardLockQueries; }
225 void reapplyHardLocks() const
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() )
237 MIL << "HardLockQueries match " << locked.size() << " Solvables." << endl;
238 for_( it, begin(), end() )
240 resstatus::UserLockQueryManip::reapplyLock( it->status(), locked.contains( *it ) );
244 void setHardLockQueries( const HardLockQueries & newLocks_r )
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() )
254 MIL << "HardLockQueries match " << locked.size() << " Solvables." << endl;
255 for_( it, begin(), end() )
257 resstatus::UserLockQueryManip::setLock( it->status(), locked.contains( *it ) );
261 bool getHardLockQueries( HardLockQueries & activeLocks_r )
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::tr1::unordered_set<IdString> IdentSet;
269 IdentSet removedLocks;
270 for_( it, begin(), end() )
272 switch ( resstatus::UserLockQueryManip::diffLock( it->status() ) )
277 addedLocks.insert( it->satSolvable().ident() );
280 removedLocks.insert( it->satSolvable().ident() );
284 // now the bad part - adjust the queries
285 bool setChanged = false;
286 for_( it, removedLocks.begin(), removedLocks.end() )
288 if ( addedLocks.find( *it ) != addedLocks.end() )
289 continue; // Added locks win
290 if ( hardLockQueriesRemove( activeLocks_r, *it ) && ! setChanged )
293 for_( it, addedLocks.begin(), addedLocks.end() )
295 if ( hardLockQueriesAdd( activeLocks_r, *it ) && ! setChanged )
303 typedef PoolTraits::AutoSoftLocks AutoSoftLocks;
304 typedef PoolTraits::autoSoftLocks_iterator autoSoftLocks_iterator;
306 const AutoSoftLocks & autoSoftLocks() const
307 { return _autoSoftLocks; }
309 bool autoSoftLockAppliesTo( sat::Solvable solv_r ) const
310 { return( _autoSoftLocks.find( solv_r.ident() ) != _autoSoftLocks.end() ); }
312 void setAutoSoftLocks( const AutoSoftLocks & newLocks_r )
314 MIL << "Apply " << newLocks_r.size() << " AutoSoftLocks: " << newLocks_r << endl;
315 _autoSoftLocks = newLocks_r;
316 // now adjust the pool status
317 for_( it, begin(), end() )
319 if ( ! it->status().isKept() )
322 if ( autoSoftLockAppliesTo( it->satSolvable() ) )
323 it->status().setSoftLock( ResStatus::USER );
325 it->status().resetTransact( ResStatus::USER );
329 void getActiveSoftLocks( AutoSoftLocks & activeLocks_r )
331 activeLocks_r = _autoSoftLocks; // current soft-locks
332 AutoSoftLocks todel; // + names to be deleted
333 AutoSoftLocks toins; // - names to be installed
335 for_( it, begin(), end() )
337 ResStatus & status( it->status() );
338 if ( ! ( status.isByUser() || status.isByApplLow() ) )
339 continue; // ignore non-user requests; ApplLow means selected
340 // by solver, but on behalf of a user request.
342 switch ( status.getTransactValue() )
344 case ResStatus::KEEP_STATE:
345 // Filter only items included in the last recommended set.
346 if ( status.isRecommended() )
347 activeLocks_r.insert( it->satSolvable().ident() );
349 case ResStatus::LOCKED:
352 case ResStatus::TRANSACT:
353 (status.isInstalled() ? todel : toins).insert( it->satSolvable().ident() );
357 for_( it, todel.begin(), todel.end() )
359 activeLocks_r.insert( *it );
361 for_( it, toins.begin(), toins.end() )
363 activeLocks_r.erase( *it );
368 const ContainerT & store() const
373 sat::Pool pool( satpool() );
374 bool addedItems = false;
375 std::list<PoolItem> addedProducts;
377 _store.resize( pool.capacity() );
379 if ( pool.capacity() )
381 for ( sat::detail::SolvableIdType i = pool.capacity()-1; i != 0; --i )
383 sat::Solvable s( i );
384 PoolItem & pi( _store[i] );
387 // the PoolItem got invalidated (e.g unloaded repo)
390 else if ( s && ! pi )
392 // new PoolItem to add
393 pi = PoolItem::makePoolItem( s ); // the only way to create a new one!
394 // remember products for buddy processing (requires clean store)
395 if ( s.isKind( ResKind::product ) )
396 addedProducts.push_back( pi );
398 // and on the fly check for weak locks...
399 if ( autoSoftLockAppliesTo( s ) )
401 pi.status().setSoftLock( ResStatus::USER );
411 // Now, as the pool is adjusted, ....
413 // .... we check for product buddies.
414 if ( ! addedProducts.empty() )
416 for_( it, addedProducts.begin(), addedProducts.end() )
418 it->setBuddy( asKind<Product>(*it)->referencePackage() );
422 // .... we must reapply those query based hard locks.
431 const Id2ItemT & id2item () const
437 _id2item = Id2ItemT( size() );
438 for_( it, begin(), end() )
440 const sat::Solvable &s = (*it)->satSolvable();
441 sat::detail::IdType id = s.ident().id();
442 if ( s.isKind( ResKind::srcpackage ) )
444 _id2item.insert( std::make_pair( id, *it ) );
446 //INT << _id2item << endl;
447 _id2itemDirty = false;
452 ///////////////////////////////////////////////////////////////////
454 ///////////////////////////////////////////////////////////////////
456 void checkSerial() const
458 if ( _watcher.remember( serial() ) )
460 satpool().prepare(); // always ajust dependencies.
463 void invalidate() const
466 _id2itemDirty = true;
472 /** Watch sat pools serial number. */
473 SerialNumberWatcher _watcher;
474 mutable ContainerT _store;
475 mutable DefaultIntegral<bool,true> _storeDirty;
476 mutable Id2ItemT _id2item;
477 mutable DefaultIntegral<bool,true> _id2itemDirty;
480 mutable shared_ptr<ResPoolProxy> _poolProxy;
484 /** Set of solvable idents that should be soft locked per default. */
485 AutoSoftLocks _autoSoftLocks;
487 /** Set of queries that define hardlocks. */
488 HardLockQueries _hardLockQueries;
490 ///////////////////////////////////////////////////////////////////
492 /////////////////////////////////////////////////////////////////
494 ///////////////////////////////////////////////////////////////////
495 /////////////////////////////////////////////////////////////////
497 ///////////////////////////////////////////////////////////////////
498 #endif // ZYPP_POOL_POOLIMPL_H