libzypp 17.38.15
ResPoolProxy.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#include <iostream>
13#include <utility>
15
17#include <zypp/base/Algorithm.h>
19
20#include <zypp/ResPoolProxy.h>
21#include <zypp/pool/PoolImpl.h>
23
24using std::endl;
25
27namespace zypp
28{
29
32 {
33 void saveState( const ResPool& pool_r )
34 {
35 std::for_each( pool_r.begin(), pool_r.end(),
36 std::mem_fn(&PoolItem::saveState) );
37 }
38
39 void saveState( const ResPool& pool_r, const ResKind & kind_r )
40 {
41 std::for_each( pool_r.byKindBegin(kind_r), pool_r.byKindEnd(kind_r),
42 std::mem_fn(&PoolItem::saveState) );
43 }
44
45 void restoreState( const ResPool& pool_r )
46 {
47 std::for_each( pool_r.begin(), pool_r.end(),
48 std::mem_fn(&PoolItem::restoreState) );
49 }
50
51 void restoreState( const ResPool& pool_r, const ResKind & kind_r )
52 {
53 std::for_each( pool_r.byKindBegin(kind_r), pool_r.byKindEnd(kind_r),
54 std::mem_fn(&PoolItem::restoreState) );
55 }
56
57 bool diffState( const ResPool& pool_r ) const
58 {
59 // return whether some PoolItem::sameState reported \c false.
60 return( invokeOnEach( pool_r.begin(), pool_r.end(),
61 std::mem_fn(&PoolItem::sameState) ) < 0 );
62 }
63
64 bool diffState( const ResPool& pool_r, const ResKind & kind_r ) const
65 {
66 // return whether some PoolItem::sameState reported \c false.
67 return( invokeOnEach( pool_r.byKindBegin(kind_r), pool_r.byKindEnd(kind_r),
68 std::mem_fn(&PoolItem::sameState) ) < 0 );
69 }
70 };
71
72 namespace
73 {
74 ui::Selectable::Ptr makeSelectablePtr( pool::PoolImpl::Id2ItemT::const_iterator begin_r,
75 pool::PoolImpl::Id2ItemT::const_iterator end_r )
76 {
79 sat::Solvable solv( begin->satSolvable() );
80
81 return new ui::Selectable( ui::Selectable::Impl_Ptr( new ui::Selectable::Impl( solv.kind(), solv.name(), begin, end ) ) );
82 }
83 } // namespace
84
86 //
87 // CLASS NAME : ResPoolProxy::Impl
88 //
93 {
94 friend std::ostream & operator<<( std::ostream & str, const Impl & obj );
95 friend std::ostream & dumpOn( std::ostream & str, const Impl & obj );
96
97 using SelectableIndex = std::unordered_map<sat::detail::IdType, ui::Selectable::Ptr>;
99
100 public:
102 :_pool( ResPool::instance() )
103 {}
104
105 Impl( ResPool &&pool_r, const pool::PoolImpl & poolImpl_r )
106 : _pool( std::move(pool_r) )
107 , _id2item( &poolImpl_r.id2item() )
108 , _serial( poolImpl_r.serial().serial() )
109 {}
110
111 public:
112 ui::Selectable::Ptr lookup( const pool::ByIdent & ident_r ) const
113 {
114 SelectableIndex::const_iterator it( _selIndex.find( ident_r.get() ) );
115 if ( it != _selIndex.end() )
116 return it->second;
117 if ( _id2item && _pool.serial().serial() == _serial )
118 {
119 // create just this Selectable; a single lookup does not
120 // have to pay for materializing the whole pool
121 auto range = _id2item->equal_range( ident_r.get() );
122 if ( range.first != range.second )
123 {
124 ui::Selectable::Ptr p( makeSelectablePtr( range.first, range.second ) );
125 _selPool.insert( SelectablePool::value_type( p->kind(), p ) );
126 _selIndex[ident_r.get()] = p;
127 return p;
128 }
129 }
130 return ui::Selectable::Ptr();
131 }
132
133 private:
134 void materialize() const
135 {
136 if ( ! _id2item )
137 return;
138 if ( _pool.serial().serial() != _serial )
139 {
140 // The pool changed under a proxy that was handed out before.
141 // Keep whatever was already created rather than reporting an
142 // empty proxy, and keep _id2item for a later lookup(), which
143 // checks the serial itself.
144 return;
145 }
146 const pool::PoolImpl::Id2ItemT & id2item( *_id2item );
147 _id2item = nullptr;
148 if ( ! id2item.empty() )
149 {
150 // set startpoint
151 pool::PoolImpl::Id2ItemT::const_iterator cbegin = id2item.begin();
152
153 for_( it, id2item.begin(), id2item.end() )
154 {
155 if ( it->first != cbegin->first )
156 {
157 // starting a new Selectable, create the previous one
158 if ( ! _selIndex.count( cbegin->first ) )
159 {
160 ui::Selectable::Ptr p( makeSelectablePtr( cbegin, it ) );
161 _selPool.insert( SelectablePool::value_type( p->kind(), p ) );
162 _selIndex[cbegin->first] = p;
163 }
164 // remember new startpoint
165 cbegin = it;
166 }
167 }
168 // create the final one
169 if ( ! _selIndex.count( cbegin->first ) )
170 {
171 ui::Selectable::Ptr p( makeSelectablePtr( cbegin, id2item.end() ) );
172 _selPool.insert( SelectablePool::value_type( p->kind(), p ) );
173 _selIndex[cbegin->first] = p;
174 }
175 }
176 }
177
178 public:
179 bool empty() const
180 { materialize(); return _selPool.empty(); }
181
183 { materialize(); return _selPool.size(); }
184
187
190
191 public:
192 bool empty( const ResKind & kind_r ) const
193 { materialize(); return( _selPool.count( kind_r ) == 0 ); }
194
195 size_type size( const ResKind & kind_r ) const
196 { materialize(); return _selPool.count( kind_r ); }
197
198 const_iterator byKindBegin( const ResKind & kind_r ) const
199 { materialize(); return make_map_value_lower_bound( _selPool, kind_r ); }
200
201 const_iterator byKindEnd( const ResKind & kind_r ) const
202 { materialize(); return make_map_value_upper_bound( _selPool, kind_r ); }
203
204 public:
207
210
213
214 public:
215
216 void saveState() const
218
219 void saveState( const ResKind & kind_r ) const
220 { PoolItemSaver().saveState( _pool, kind_r ); }
221
222 void restoreState() const
224
225 void restoreState( const ResKind & kind_r ) const
226 { PoolItemSaver().restoreState( _pool, kind_r ); }
227
228 bool diffState() const
229 { return PoolItemSaver().diffState( _pool ); }
230
231 bool diffState( const ResKind & kind_r ) const
232 { return PoolItemSaver().diffState( _pool, kind_r ); }
233
234 private:
236 mutable const pool::PoolImpl::Id2ItemT * _id2item = nullptr; //< unmaterialized idents, guarded by _serial
237 unsigned _serial = 0;
240
241 public:
244 {
245 static shared_ptr<Impl> _nullimpl( new Impl );
246 return _nullimpl;
247 }
248 };
249
250
252 inline std::ostream & operator<<( std::ostream & str, const ResPoolProxy::Impl & obj )
253 {
254 return str << "ResPoolProxy (" << obj._pool.serial() << ") [" << obj._pool.size()
255 << "solv/" << obj.size()<< "sel]";
256 }
257
258 namespace detail
259 {
261 {
262 bool operator()( const ui::Selectable::Ptr & selp ) const
263 { return selp->toModify(); }
264 };
265 }
266
268 inline std::ostream & dumpOn( std::ostream & str, const ResPoolProxy::Impl & obj )
269 {
271 return dumpRange( str << obj << " toModify: ",
272 make_filter_begin( f, obj ),
273 make_filter_end( f, obj ) );
274 }
275
277 //
278 // CLASS NAME : ResPoolProxy
279 //
281
283 //
284 // METHOD NAME : ResPoolProxy::ResPoolProxy
285 // METHOD TYPE : Ctor
286 //
288 : _pimpl( Impl::nullimpl() )
289 {}
290
292 //
293 // METHOD NAME : ResPoolProxy::ResPoolProxy
294 // METHOD TYPE : Ctor
295 //
297 : _pimpl( new Impl( std::move(pool_r), poolImpl_r ) )
298 {}
299
301 //
302 // METHOD NAME : ResPoolProxy::~ResPoolProxy
303 // METHOD TYPE : Dtor
304 //
307
309 //
310 // forward to implementation
311 //
313
315 { return _pimpl->lookup( ident_r ); }
316
318 { return _pimpl->empty(); }
319
322
325
328
329 bool ResPoolProxy::empty( const ResKind & kind_r ) const
330 { return _pimpl->empty( kind_r ); }
331
333 { return _pimpl->size( kind_r ); }
334
336 { return _pimpl->byKindBegin( kind_r ); }
337
339 { return _pimpl->byKindEnd( kind_r ); }
340
343
346
349
351 { _pimpl->saveState(); }
352
353 void ResPoolProxy::saveState( const ResKind & kind_r ) const
354 { _pimpl->saveState( kind_r ); }
355
358
359 void ResPoolProxy::restoreState( const ResKind & kind_r ) const
360 { _pimpl->restoreState( kind_r ); }
361
363 { return _pimpl->diffState(); }
364
365 bool ResPoolProxy::diffState( const ResKind & kind_r ) const
366 { return _pimpl->diffState( kind_r ); }
367
368 std::ostream & operator<<( std::ostream & str, const ResPoolProxy & obj )
369 { return str << *obj._pimpl; }
370
371 std::ostream & dumpOn( std::ostream & str, const ResPoolProxy & obj )
372 { return dumpOn( str, *obj._pimpl ); }
373
375} // namespace zypp
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition Easy.h:27
void restoreState() const
Definition PoolItem.cc:225
void saveState() const
Definition PoolItem.cc:224
bool sameState() const
Definition PoolItem.cc:226
Resolvable kinds.
Definition ResKind.h:33
ResPool::repository_iterator repository_iterator
void saveState() const
size_type size() const
const_iterator byKindEnd() const
size_type knownRepositoriesSize() const
friend std::ostream & operator<<(std::ostream &str, const ResPoolProxy &obj)
relates: ResPoolProxy Stream output
const_iterator begin() const
std::multimap< ResKind, ui::Selectable::Ptr > SelectablePool
const_iterator end() const
ResPoolProxy()
Default ctor: no pool Nonempty proxies are provided by ResPool.
RW_pointer< Impl > _pimpl
Pointer to implementation.
bool diffState() const
MapKVIteratorTraits< SelectablePool >::Value_const_iterator const_iterator
void restoreState() const
SelectablePool::size_type size_type
repository_iterator knownRepositoriesBegin() const
ui::Selectable::Ptr lookup(const pool::ByIdent &ident_r) const
friend std::ostream & dumpOn(std::ostream &str, const ResPoolProxy &obj)
relates: ResPoolProxy Verbose stream output
repository_iterator knownRepositoriesEnd() const
const_iterator byKindBegin() const
Global ResObject pool.
Definition ResPool.h:62
size_type knownRepositoriesSize() const
Definition ResPool.cc:80
repository_iterator knownRepositoriesBegin() const
Definition ResPool.cc:83
const_iterator end() const
Definition ResPool.h:101
byKind_iterator byKindEnd(const ResKind &kind_r) const
Definition ResPool.h:269
repository_iterator knownRepositoriesEnd() const
Definition ResPool.cc:86
const SerialNumber & serial() const
The pools serial number.
Definition ResPool.cc:65
byKind_iterator byKindBegin(const ResKind &kind_r) const
Definition ResPool.h:262
const_iterator begin() const
Definition ResPool.h:98
size_type size() const
Definition ResPool.cc:71
unsigned serial() const
Main filter selecting PoolItems by name and kind.
Definition ByIdent.h:29
sat::detail::IdType get() const
Definition ByIdent.h:90
PoolTraits::Id2ItemT Id2ItemT
Definition PoolImpl.h:177
shared_ptr< Impl > Impl_Ptr
Definition Selectable.h:562
intrusive_ptr< Selectable > Ptr
Definition Selectable.h:58
CURL handle void * p
Definition curl_dl.cc:142
Definition ansi.h:855
String related utilities and Regular expression matching.
Easy-to use interface to the ZYPP dependency resolver.
std::ostream & dumpRange(std::ostream &str, TIterator begin, TIterator end, const std::string &intro="{", const std::string &pfx="\n ", const std::string &sep="\n ", const std::string &sfx="\n", const std::string &extro="}")
Print range defined by iterators (multiline style).
Definition LogTools.h:419
MapKVIteratorTraits< TMap >::Value_const_iterator make_map_value_lower_bound(const TMap &map_r, const typename TMap::key_type &key_r)
Convenience to create the value iterator from container::lower_bound().
Definition Iterator.h:256
MapKVIteratorTraits< TMap >::Value_const_iterator make_map_value_begin(const TMap &map_r)
Convenience to create the value iterator from container::begin().
Definition Iterator.h:236
filter_iterator< TFilter, typename TContainer::const_iterator > make_filter_begin(TFilter f, const TContainer &c)
Convenience to create filter_iterator from container::begin().
Definition Iterator.h:101
MapKVIteratorTraits< TMap >::Value_const_iterator make_map_value_end(const TMap &map_r)
Convenience to create the value iterator from container::end().
Definition Iterator.h:241
MapKVIteratorTraits< TMap >::Value_const_iterator make_map_value_upper_bound(const TMap &map_r, const typename TMap::key_type &key_r)
Convenience to create the value iterator from container::upper_bound().
Definition Iterator.h:261
filter_iterator< TFilter, typename TContainer::const_iterator > make_filter_end(TFilter f, const TContainer &c)
Convenience to create filter_iterator from container::end().
Definition Iterator.h:117
int invokeOnEach(TIterator begin_r, TIterator end_r, TFilter filter_r, TFunction fnc_r)
Iterate through [begin_r,end_r) and invoke fnc_r on each item that passes filter_r.
Definition Algorithm.h:30
void saveState(const ResPool &pool_r, const ResKind &kind_r)
bool diffState(const ResPool &pool_r, const ResKind &kind_r) const
void saveState(const ResPool &pool_r)
void restoreState(const ResPool &pool_r, const ResKind &kind_r)
void restoreState(const ResPool &pool_r)
bool diffState(const ResPool &pool_r) const
ResPoolProxy implementation.
size_type size() const
void restoreState(const ResKind &kind_r) const
bool diffState(const ResKind &kind_r) const
repository_iterator knownRepositoriesEnd() const
repository_iterator knownRepositoriesBegin() const
Impl(ResPool &&pool_r, const pool::PoolImpl &poolImpl_r)
const_iterator begin() const
static shared_ptr< Impl > nullimpl()
Offer default Impl.
const pool::PoolImpl::Id2ItemT * _id2item
SelectableIndex _selIndex
bool empty(const ResKind &kind_r) const
friend std::ostream & operator<<(std::ostream &str, const Impl &obj)
relates: ResPoolProxy::Impl Stream output
void saveState(const ResKind &kind_r) const
size_type size(const ResKind &kind_r) const
std::unordered_map< sat::detail::IdType, ui::Selectable::Ptr > SelectableIndex
ui::Selectable::Ptr lookup(const pool::ByIdent &ident_r) const
size_type knownRepositoriesSize() const
const_iterator end() const
ResPoolProxy::const_iterator const_iterator
const_iterator byKindEnd(const ResKind &kind_r) const
const_iterator byKindBegin(const ResKind &kind_r) const
friend std::ostream & dumpOn(std::ostream &str, const Impl &obj)
relates: ResPoolProxy::Impl Verbose stream output
bool operator()(const ui::Selectable::Ptr &selp) const
P_Select2nd< Id2ItemT::value_type > Id2ItemValueSelector
Definition PoolTraits.h:78
transform_iterator< Id2ItemValueSelector, Id2ItemT::const_iterator > byIdent_iterator
Definition PoolTraits.h:79