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 ResKind & kind_r );
184 void RestoreState( const ResKind & 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::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 )
302 const ContainerT & store() const
307 sat::Pool pool( satpool() );
308 bool addedItems = false;
309 bool reusedIDs = _watcherIDs.remember( pool.serialIDs() );
310 std::list<PoolItem> addedProducts;
312 _store.resize( pool.capacity() );
314 if ( pool.capacity() )
316 for ( sat::detail::SolvableIdType i = pool.capacity()-1; i != 0; --i )
318 sat::Solvable s( i );
319 PoolItem & pi( _store[i] );
322 // the PoolItem got invalidated (e.g unloaded repo)
325 else if ( reusedIDs || (s && ! pi) )
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 );
339 // Now, as the pool is adjusted, ....
341 // .... we check for product buddies.
342 if ( ! addedProducts.empty() )
344 for_( it, addedProducts.begin(), addedProducts.end() )
346 it->setBuddy( asKind<Product>(*it)->referencePackage() );
350 // .... we must reapply those query based hard locks.
359 const Id2ItemT & id2item () const
365 _id2item = Id2ItemT( size() );
366 for_( it, begin(), end() )
368 const sat::Solvable &s = (*it)->satSolvable();
369 sat::detail::IdType id = s.ident().id();
370 if ( s.isKind( ResKind::srcpackage ) )
372 _id2item.insert( std::make_pair( id, *it ) );
374 //INT << _id2item << endl;
375 _id2itemDirty = false;
380 ///////////////////////////////////////////////////////////////////
382 ///////////////////////////////////////////////////////////////////
384 void checkSerial() const
386 if ( _watcher.remember( serial() ) )
388 satpool().prepare(); // always ajust dependencies.
391 void invalidate() const
394 _id2itemDirty = true;
400 /** Watch sat pools serial number. */
401 SerialNumberWatcher _watcher;
402 /** Watch sat pools Serial number of IDs - changes whenever resusePoolIDs==true - ResPool must also invalidate it's PoolItems! */
403 SerialNumberWatcher _watcherIDs;
404 mutable ContainerT _store;
405 mutable DefaultIntegral<bool,true> _storeDirty;
406 mutable Id2ItemT _id2item;
407 mutable DefaultIntegral<bool,true> _id2itemDirty;
410 mutable shared_ptr<ResPoolProxy> _poolProxy;
413 /** Set of queries that define hardlocks. */
414 HardLockQueries _hardLockQueries;
416 ///////////////////////////////////////////////////////////////////
418 /////////////////////////////////////////////////////////////////
420 ///////////////////////////////////////////////////////////////////
421 /////////////////////////////////////////////////////////////////
423 ///////////////////////////////////////////////////////////////////
424 #endif // ZYPP_POOL_POOLIMPL_H