a8d37d9b1a0e9cd62eb80fcd5c90cd8588669ad3
[platform/upstream/libzypp.git] / zypp / ResPool.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/ResPool.h
10  *
11 */
12 #ifndef ZYPP_RESPOOL_H
13 #define ZYPP_RESPOOL_H
14
15 #include <iosfwd>
16
17 #include "zypp/APIConfig.h"
18 #include "zypp/base/Iterator.h"
19
20 #include "zypp/pool/PoolTraits.h"
21 #include "zypp/PoolItem.h"
22 #include "zypp/Filter.h"
23
24 ///////////////////////////////////////////////////////////////////
25 namespace zypp
26 { /////////////////////////////////////////////////////////////////
27
28   class SerialNumber;
29   class ResPoolProxy;
30   class Resolver;
31
32   ///////////////////////////////////////////////////////////////////
33   //
34   //    CLASS NAME : ResPool
35   //
36   /** Global ResObject pool.
37    *
38    * Explicitly shared singleton.
39    *
40    * \note Filter iterators provided by ResPool are intended to
41    * operate on internal index tables for faster access. If the
42    * the index is not yet implemented, they are realized as
43    * an ordinary filter iterator. Do not provide filter iterators
44    * here, if there is no index table for it.
45    *
46    * \include n_ResPool_nomorenameiter
47   */
48   class ResPool
49   {
50     friend std::ostream & operator<<( std::ostream & str, const ResPool & obj );
51
52     public:
53       /** \ref PoolItem */
54       typedef PoolItem                                   value_type;
55       typedef pool::PoolTraits::size_type                size_type;
56       typedef pool::PoolTraits::const_iterator           const_iterator;
57       typedef pool::PoolTraits::repository_iterator      repository_iterator;
58
59     public:
60       /** Singleton ctor. */
61       static ResPool instance();
62
63       /** preliminary */
64       ResPoolProxy proxy() const;
65
66       /** The Resolver */
67       Resolver & resolver() const;
68
69     public:
70       /** The pools serial number. Changing whenever the
71        * whenever the content changes. (Resolvables or
72        * Dependencies).
73        */
74       const SerialNumber & serial() const;
75
76     public:
77       /**  */
78       bool empty() const;
79       /**  */
80       size_type size() const;
81
82       /** \name Iterate over all PoolItems (all kinds). */
83       //@{
84       /** */
85       const_iterator begin() const
86       { return make_filter_begin( pool::ByPoolItem(), store() ); }
87       /** */
88       const_iterator end() const
89       { return make_filter_end( pool::ByPoolItem(), store() ); }
90       //@}
91
92     public:
93       /** Return the corresponding \ref PoolItem.
94        * Pool and sat pool should be in sync. Returns an empty
95        * \ref PoolItem if there is no corresponding \ref PoolItem.
96        * \see \ref PoolItem::satSolvable.
97        */
98       PoolItem find( const sat::Solvable & slv_r ) const;
99       /** \overload */
100       PoolItem find( const ResObject::constPtr & resolvable_r ) const
101       { return( resolvable_r ? find( resolvable_r->satSolvable() ) : PoolItem() ); }
102
103     public:
104       /** \name Iterate over all PoolItems matching a \c _Filter. */
105       //@{
106       template<class _Filter>
107       filter_iterator<_Filter,const_iterator> filterBegin( const _Filter & filter_r ) const
108       { return make_filter_begin( filter_r, *this ); }
109
110       template<class _Filter>
111       filter_iterator<_Filter,const_iterator> filterEnd( const _Filter & filter_r ) const
112       { return make_filter_end( filter_r, *this ); }
113       //@}
114
115       /** \name Iterate over all PoolItems by status.
116        *
117        * Simply pass the \ref ResStatus predicate you want to use as filter:
118        * \code
119        *   // iterate over all orphaned items:
120        *   for_( it, pool.byStatusBegin(&ResStatus::isOrphaned), pool.byStatusEnd(&ResStatus::isOrphaned) )
121        *   {...}
122        * \endcode
123        *
124        * Or use \ref filter::ByStatus in more complex queries:
125        * \code
126        *   // iterate over all (orphaned and recommended) items:
127        *   functor::Chain<filter::ByStatus,filter::ByStatus> myfilter( filter::ByStatus(&ResStatus::isOrphaned),
128        *                                                               filter::ByStatus(&ResStatus::isRecommended) );
129        *   for_( it, pool.filterBegin(myfilter), pool.filterEnd(myfilter) )
130        *   { ... }
131        * \endcode
132        */
133       //@{
134       filter_iterator<filter::ByStatus,const_iterator> byStatusBegin( const filter::ByStatus & filter_r ) const
135       { return make_filter_begin( filter_r, *this ); }
136
137       filter_iterator<filter::ByStatus,const_iterator> byStatusEnd( const filter::ByStatus & filter_r ) const
138       { return make_filter_end( filter_r, *this ); }
139       //@}
140
141     public:
142       /** \name Iterate over all PoolItems of a certain name and kind. */
143       //@{
144       typedef pool::ByIdent                       ByIdent;
145       typedef pool::PoolTraits::byIdent_iterator  byIdent_iterator;
146
147       byIdent_iterator byIdentBegin( const ByIdent & ident_r ) const
148       {
149         return make_transform_iterator( id2item().equal_range( ident_r.get() ).first,
150                                         pool::PoolTraits::Id2ItemValueSelector() );
151       }
152
153       byIdent_iterator byIdentBegin( ResKind kind_r, IdString name_r ) const
154       { return byIdentBegin( ByIdent(kind_r,name_r) ); }
155
156       byIdent_iterator byIdentBegin( ResKind kind_r, const C_Str & name_r ) const
157       { return byIdentBegin( ByIdent(kind_r,name_r) ); }
158
159       template<class _Res>
160       byIdent_iterator byIdentBegin( IdString name_r ) const
161       { return byIdentBegin( ByIdent(ResTraits<_Res>::kind,name_r) ); }
162
163       template<class _Res>
164       byIdent_iterator byIdentBegin( const C_Str & name_r ) const
165       { return byIdentBegin( ByIdent(ResTraits<_Res>::kind,name_r) ); }
166
167       /** Derive name and kind from \ref PoolItem. */
168       byIdent_iterator byIdentBegin( const PoolItem & pi_r ) const
169       { return byIdentBegin( ByIdent(pi_r.satSolvable()) ); }
170       /** Derive name and kind from \ref sat::Solvable. */
171       byIdent_iterator byIdentBegin( sat::Solvable slv_r ) const
172       { return byIdentBegin( ByIdent(slv_r) ); }
173       /** Takes a \ref sat::Solvable::ident string. */
174       byIdent_iterator byIdentBegin( IdString ident_r ) const
175       { return byIdentBegin( ByIdent(ident_r) ); }
176
177
178       byIdent_iterator byIdentEnd( const ByIdent & ident_r ) const
179       {
180         return make_transform_iterator( id2item().equal_range( ident_r.get() ).second,
181                                         pool::PoolTraits::Id2ItemValueSelector() );
182       }
183
184       byIdent_iterator byIdentEnd( ResKind kind_r, IdString name_r ) const
185       { return byIdentEnd( ByIdent(kind_r,name_r) ); }
186
187       byIdent_iterator byIdentEnd( ResKind kind_r, const C_Str & name_r ) const
188       { return byIdentEnd( ByIdent(kind_r,name_r) ); }
189
190       template<class _Res>
191       byIdent_iterator byIdentEnd( IdString name_r ) const
192       { return byIdentEnd( ByIdent(ResTraits<_Res>::kind,name_r) ); }
193
194       template<class _Res>
195       byIdent_iterator byIdentEnd( const C_Str & name_r ) const
196       { return byIdentEnd( ByIdent(ResTraits<_Res>::kind,name_r) ); }
197
198       /** Derive name and kind from \ref PoolItem. */
199       byIdent_iterator byIdentEnd( const PoolItem & pi_r ) const
200       { return byIdentEnd( ByIdent(pi_r.satSolvable()) ); }
201       /** Derive name and kind from \ref sat::Solvable. */
202       byIdent_iterator byIdentEnd( sat::Solvable slv_r ) const
203       { return byIdentEnd( ByIdent(slv_r) ); }
204       /** Takes a \ref sat::Solvable::ident string. */
205       byIdent_iterator byIdentEnd( IdString ident_r ) const
206       { return byIdentEnd( ByIdent(ident_r) ); }
207      //@}
208
209     public:
210       /** \name Iterate over all ResObjects of a certain kind. */
211       //@{
212       typedef filter::ByKind ByKind;
213       typedef filter_iterator<ByKind,const_iterator> byKind_iterator;
214
215       byKind_iterator byKindBegin( const ResKind & kind_r ) const
216       { return make_filter_begin( ByKind(kind_r), *this ); }
217
218       template<class _Res>
219           byKind_iterator byKindBegin() const
220       { return make_filter_begin( resfilter::byKind<_Res>(), *this ); }
221
222       byKind_iterator byKindEnd( const ResKind & kind_r ) const
223       { return make_filter_end( ByKind(kind_r), *this ); }
224
225       template<class _Res>
226           byKind_iterator byKindEnd() const
227       { return make_filter_end( resfilter::byKind<_Res>(), *this ); }
228       //@}
229
230     public:
231       /** \name Iterate over all ResObjects with a certain name (all kinds). */
232       //@{
233       typedef zypp::resfilter::ByName ByName;
234       typedef filter_iterator<ByName,const_iterator> byName_iterator;
235
236       byName_iterator byNameBegin( const std::string & name_r ) const
237       { return make_filter_begin( ByName(name_r), *this ); }
238
239       byName_iterator byNameEnd( const std::string & name_r ) const
240       { return make_filter_end( ByName(name_r), *this ); }
241       //@}
242
243     public:
244       /** \name Special iterators. */
245       //@{
246
247       //@}
248    public:
249       /** \name Iterate over all Repositories that contribute ResObjects.
250        */
251       //@{
252       size_type knownRepositoriesSize() const;
253
254       repository_iterator knownRepositoriesBegin() const;
255
256       repository_iterator knownRepositoriesEnd() const;
257
258       /** Find a \ref Repository named \c alias_r.
259        * Returns \ref Repository::norepository if there is no such \ref Repository.
260        */
261       Repository reposFind( const std::string & alias_r ) const;
262       //@}
263
264     public:
265       /** \name Handle locale support.
266        *
267        * A \ref filter::ByLocaleSupport is provided to iterate over
268        * all items supporting a specific locale.
269        *
270        * \see \ref sat::LocaleSupport for a more convenient interface.
271        *
272        * \code
273        * ResPool pool( ResPool::instance() );
274        *
275        * filter::ByLocaleSupport f( Locale("de") );
276        * for_( it, pool.filterBegin(f), pool.filterEnd(f) )
277        * {
278        *   MIL << *it << endl; // supporting "de"
279        * }
280        *
281        * f = filter::ByLocaleSupport( pool.getRequestedLocales() );
282        * for_( it, pool.filterBegin(f), pool.filterEnd(f) )
283        * {
284        *   MIL << *it << endl; // supporting any requested locale
285        * }
286        * \endcode
287        */
288       //@{
289       /** Set the requested locales.
290        * Languages to be supported by the system, e.g. language specific
291        * packages to be installed.
292        */
293       void setRequestedLocales( const LocaleSet & locales_r );
294
295       /** Add one \ref Locale to the set of requested locales.
296        * Return \c true if \c locale_r was newly added to the set.
297       */
298       bool addRequestedLocale( const Locale & locale_r );
299
300       /** Erase one \ref Locale from the set of requested locales.
301       * Return \c false if \c locale_r was not found in the set.
302        */
303       bool eraseRequestedLocale( const Locale & locale_r );
304
305       /** Return the requested locales.
306        * \see \ref setRequestedLocales
307       */
308       const LocaleSet & getRequestedLocales() const;
309
310       /** Whether this \ref Locale is in the set of requested locales. */
311       bool isRequestedLocale( const Locale & locale_r ) const;
312
313       /** Get the set of available locales.
314        * This is computed from the package data so it actually
315        * represents all locales packages claim to support.
316        */
317       const LocaleSet & getAvailableLocales() const;
318
319       /** Whether this \ref Locale is in the set of available locales. */
320       bool isAvailableLocale( const Locale & locale_r ) const;
321       //@}
322
323     public:
324       /** \name Handle hard locks (e.g set from /etc/zypp/locks).
325        *
326        * As this kind of lock is query based, it's quite expensive.
327        *
328        * These queries are re-evaluated when adding new repos to the pool.
329        */
330       //@{
331       typedef pool::PoolTraits::HardLockQueries           HardLockQueries;
332       typedef pool::PoolTraits::hardLockQueries_iterator  hardLockQueries_iterator;
333
334       bool hardLockQueriesEmpty() const;
335       size_type hardLockQueriesSize() const;
336       hardLockQueries_iterator hardLockQueriesBegin() const;
337       hardLockQueries_iterator hardLockQueriesEnd() const;
338
339       /** Set a new set of queries.
340        * The hard-locks of existing PoolItems are adjusted according
341        * to the queries. (usually called on target load)
342        */
343       void setHardLockQueries( const HardLockQueries & newLocks_r );
344
345       /** Suggest a new set of queries based on the current selection.
346        * (usually remembered on commit).
347        */
348       void getHardLockQueries( HardLockQueries & activeLocks_r );
349       //@}
350
351     private:
352       const pool::PoolTraits::ItemContainerT & store() const;
353       const pool::PoolTraits::Id2ItemT & id2item() const;
354
355     private:
356       /** Ctor */
357       ResPool( pool::PoolTraits::Impl_Ptr impl_r );
358       /** Access to implementation. */
359       RW_pointer<pool::PoolTraits::Impl> _pimpl;
360   };
361   ///////////////////////////////////////////////////////////////////
362
363   /** \relates ResPool Stream output */
364   std::ostream & operator<<( std::ostream & str, const ResPool & obj );
365
366   /////////////////////////////////////////////////////////////////
367 } // namespace zypp
368 ///////////////////////////////////////////////////////////////////
369
370 #include "zypp/ResPoolProxy.h"
371
372 #endif // ZYPP_RESPOOL_H