backup
[platform/upstream/libzypp.git] / zypp / pool / PoolImpl.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/pool/PoolImpl.h
10  *
11 */
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/base/Deprecated.h"
21
22 #include "zypp/pool/PoolTraits.h"
23 #include "zypp/ResPoolProxy.h"
24
25 #include "zypp/sat/Pool.h"
26
27 using std::endl;
28
29 ///////////////////////////////////////////////////////////////////
30 namespace zypp
31 { /////////////////////////////////////////////////////////////////
32   ///////////////////////////////////////////////////////////////////
33   namespace pool
34   { /////////////////////////////////////////////////////////////////
35
36     ///////////////////////////////////////////////////////////////////
37     //
38     //  CLASS NAME : PoolImpl
39     //
40     /** */
41     class PoolImpl
42     {
43       friend std::ostream & operator<<( std::ostream & str, const PoolImpl & obj );
44
45       public:
46         /** */
47         typedef PoolTraits::ItemContainerT              ContainerT;
48         typedef PoolTraits::size_type                   size_type;
49         typedef PoolTraits::const_iterator              const_iterator;
50         typedef PoolTraits::Id2ItemT                    Id2ItemT;
51
52         typedef PoolTraits::repository_iterator         repository_iterator;
53
54         typedef sat::detail::SolvableIdType             SolvableIdType;
55
56       public:
57         /** Default ctor */
58         PoolImpl();
59         /** Dtor */
60         ~PoolImpl();
61
62       public:
63         /** convenience. */
64         const sat::Pool satpool() const
65         { return sat::Pool::instance(); }
66
67         /** Housekeeping data serial number. */
68         const SerialNumber & serial() const
69         { return satpool().serial(); }
70
71         ///////////////////////////////////////////////////////////////////
72         //
73         ///////////////////////////////////////////////////////////////////
74       public:
75         /**  */
76         bool empty() const
77         { return satpool().solvablesEmpty(); }
78
79         /**  */
80         size_type size() const
81         { return satpool().solvablesSize(); }
82
83         const_iterator begin() const
84         { return make_filter_begin( pool::ByPoolItem(), store() ); }
85
86         const_iterator end() const
87         { return make_filter_end( pool::ByPoolItem(), store() ); }
88
89       public:
90         /** Return the corresponding \ref PoolItem.
91          * Pool and sat pool should be in sync. Returns an empty
92          * \ref PoolItem if there is no corresponding \ref PoolItem.
93          * \see \ref PoolItem::satSolvable.
94          */
95         PoolItem find( const sat::Solvable & slv_r ) const
96         {
97           const ContainerT & mystore( store() );
98           return( slv_r.id() < mystore.size() ? mystore[slv_r.id()] : PoolItem() );
99         }
100
101         ///////////////////////////////////////////////////////////////////
102         //
103         ///////////////////////////////////////////////////////////////////
104       public:
105         /** \name Save and restore state. */
106         //@{
107         void SaveState( const ResObject::Kind & kind_r );
108
109         void RestoreState( const ResObject::Kind & kind_r );
110         //@}
111
112         ///////////////////////////////////////////////////////////////////
113         //
114         ///////////////////////////////////////////////////////////////////
115       public:
116         ResPoolProxy proxy( ResPool self ) const
117         {
118           checkSerial();
119           if ( !_poolProxy )
120           {
121             _poolProxy.reset( new ResPoolProxy( self, *this ) );
122           }
123           return *_poolProxy;
124         }
125
126       public:
127         /** Forward list of Repositories that contribute ResObjects from \ref sat::Pool */
128         size_type knownRepositoriesSize() const
129         { checkSerial(); return satpool().reposSize(); }
130
131         repository_iterator knownRepositoriesBegin() const
132         { checkSerial(); return satpool().reposBegin(); }
133
134         repository_iterator knownRepositoriesEnd() const
135         { checkSerial(); return satpool().reposEnd(); }
136
137         ///////////////////////////////////////////////////////////////////
138         //
139         ///////////////////////////////////////////////////////////////////
140       public:
141         bool hardLockAppliesTo( sat::Solvable solv_r ) const
142         {
143           return false;
144         }
145
146         bool softLockAppliesTo( sat::Solvable solv_r ) const
147         {
148           return false;
149         }
150
151         const ContainerT & store() const
152         {
153           checkSerial();
154           if ( _storeDirty )
155           {
156             sat::Pool pool( satpool() );
157
158             if ( pool.capacity() != _store.capacity() )
159             {
160               _store.resize( pool.capacity() );
161             }
162
163             if ( pool.capacity() )
164             {
165               for ( sat::detail::SolvableIdType i = pool.capacity()-1; i != 0; --i )
166               {
167                 sat::Solvable s( i );
168                 PoolItem & pi( _store[i] );
169                 if ( ! s &&  pi )
170                 {
171                   // the PoolItem got invalidated (e.g unloaded repo)
172                   pi = PoolItem();
173                 }
174                 else if ( s && ! pi )
175                 {
176                   // new PoolItem to add
177                   pi = PoolItem::makePoolItem( s ); // the only way to create a new one!
178                   // and a few checks...
179                   if ( hardLockAppliesTo( s ) )
180                   {
181                     pi.status().setLock( true, ResStatus::USER );
182                   }
183                   else if ( softLockAppliesTo( s ) )
184                   {
185                     pi.status().setSoftLock( ResStatus::USER );
186                   }
187                 }
188               }
189             }
190             _storeDirty = false;
191           }
192           return _store;
193         }
194
195         const Id2ItemT & id2item () const
196         {
197           checkSerial();
198           if ( _id2itemDirty )
199           {
200             store();
201             _id2item = Id2ItemT( size() );
202             for_( it, begin(), end() )
203             {
204               const sat::Solvable &s = (*it)->satSolvable();
205               sat::detail::IdType id = s.ident().id();
206               if ( s.isKind( ResKind::srcpackage ) )
207                 id = -id;
208               _id2item.insert( std::make_pair( id, *it ) );
209             }
210             //INT << _id2item << endl;
211             _id2itemDirty = false;
212           }
213           return _id2item;
214         }
215
216
217         ///////////////////////////////////////////////////////////////////
218         //
219         ///////////////////////////////////////////////////////////////////
220       private:
221         void checkSerial() const
222         {
223           if ( _watcher.remember( serial() ) )
224             invalidate();
225           satpool().prepare(); // always ajust dependencies.
226         }
227
228         void invalidate() const
229         {
230           _storeDirty = true;
231           _id2itemDirty = true;
232           _id2item.clear();
233           _poolProxy.reset();
234         }
235
236       private:
237         /** Watch sat pools serial number. */
238         SerialNumberWatcher                   _watcher;
239         mutable ContainerT                    _store;
240         mutable DefaultIntegral<bool,true>    _storeDirty;
241         mutable Id2ItemT                      _id2item;
242         mutable DefaultIntegral<bool,true>    _id2itemDirty;
243
244       private:
245         mutable shared_ptr<ResPoolProxy>      _poolProxy;
246     };
247     ///////////////////////////////////////////////////////////////////
248
249     /////////////////////////////////////////////////////////////////
250   } // namespace pool
251   ///////////////////////////////////////////////////////////////////
252   /////////////////////////////////////////////////////////////////
253 } // namespace zypp
254 ///////////////////////////////////////////////////////////////////
255 #endif // ZYPP_POOL_POOLIMPL_H