1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/sat/Transaction.h
15 #ifndef ZYPP_SAT_TRANSACTION_H
16 #define ZYPP_SAT_TRANSACTION_H
20 #include "zypp/base/PtrTypes.h"
21 #include "zypp/base/Flags.h"
22 #include "zypp/base/SafeBool.h"
23 #include "zypp/base/Iterator.h"
24 #include "zypp/base/DefaultIntegral.h"
26 #include "zypp/sat/SolvIterMixin.h"
27 #include "zypp/sat/Solvable.h"
29 #include "zypp/PoolItem.h"
31 ///////////////////////////////////////////////////////////////////
33 { /////////////////////////////////////////////////////////////////
34 ///////////////////////////////////////////////////////////////////
36 { /////////////////////////////////////////////////////////////////
40 /** Needs to be outside \ref Transaction in order to be usable in SolvIterMixin. */
41 class Transaction_iterator;
42 /** Needs to be outside \ref Transaction in order to be usable in SolvIterMixin. */
43 class Transaction_const_iterator;
46 /** Satsolver transaction wrapper.
47 * \note Note that Transaction is derived from \ref sat::SolvIterMixin which
48 * makes PoolItem and Selectable iterators automatically available.
49 * \note Changing the \ref ResPool content (loading/unloading repositories)
50 * invalidates all outstanding transaction data. \see \ref valid.
51 * \note.The transaction may inlude steps of type \ref TRANSACTION_IGNORE which
52 * do not cause/require any specific action. To skip those informal steps
53 * when iterating, use the \ref actionBegin /\ref actionEnd methods.
55 class Transaction : public SolvIterMixin<Transaction, detail::Transaction_const_iterator>
56 , protected base::SafeBool<Transaction>
58 friend std::ostream & operator<<( std::ostream & str, const Transaction & obj );
59 friend std::ostream & dumpOn( std::ostream & str, const Transaction & obj );
60 friend bool operator==( const Transaction & lhs, const Transaction & rhs );
63 /** Represents a single step within a \ref Transaction. */
66 /** Type of (rpm) action to perform in a \ref Step. */
69 TRANSACTION_IGNORE = 0x00, /**< [ ] Nothing (includes implicit deletes due to obsoletes and non-package actions) */
70 TRANSACTION_ERASE = 0x10, /**< [-] Delete item */
71 TRANSACTION_INSTALL = 0x20, /**< [+] Install(update) item */
72 TRANSACTION_MULTIINSTALL = 0x30 /**< [M] Install(multiversion) item (\see \ref ZConfig::multiversion) */
75 /** \ref Step action result. */
78 STEP_TODO = (1 << 0), /**< [__] unprocessed */
79 STEP_DONE = (1 << 1), /**< [OK] success */
80 STEP_ERROR = (1 << 2), /**< [**] error */
83 ZYPP_DECLARE_FLAGS(StepStages,StepStage);
86 /** Default ctor: empty transaction. */
89 /** Ctor cloning a sat transaction. */
90 Transaction( ::_Transaction & trans_r );
96 /** Whether transaction actually contains data and also fits the current pools content. */
99 /** Validate object in a boolean context: valid */
100 using base::SafeBool<Transaction>::operator bool_type;
102 /** Order transaction steps for commit.
103 * It's cheap to call it for an aleready ordered \ref Transaction.
104 * This invalidates outstanding iterators. Returns whether
105 * \ref Transaction is \ref valid.
109 /** Whether the transaction contains any steps. */
112 /** Number of steps in transaction steps. */
115 typedef detail::Transaction_iterator iterator;
116 typedef detail::Transaction_const_iterator const_iterator;
118 /** Iterator to the first \ref TransactionStep */
119 const_iterator begin() const;
123 /** Iterator behind the last \ref TransactionStep */
124 const_iterator end() const;
128 /** Return iterator pointing to \a solv_r or \ref end. */
129 const_iterator find( const sat::Solvable & solv_r ) const;
130 iterator find( const sat::Solvable & solv_r );
132 const_iterator find( const ResObject::constPtr & resolvable_r ) const;
133 iterator find( const ResObject::constPtr & resolvable_r );
135 const_iterator find( const PoolItem & pi_r ) const;
136 iterator find( const PoolItem & pi_r );
139 /** \name Iterate action steps (omit TRANSACTION_IGNORE steps).
141 * All these methods allow to pass an optional OR'd combination of
142 * \ref StepStages as filter. Per default all steps are processed/counted.
146 * for_( it, trans.actionBegin(~sat::Transaction::STEP_DONE), trans.actionEnd() )
148 * ... // process all steps not DONE (ERROR and TODO)
154 typedef filter_iterator<FilterAction,const_iterator> action_iterator;
156 /** Whether the [filtered] transaction contains any steps . */
157 bool actionEmpty( StepStages filter_r = StepStages() ) const;
159 /** Number of steps in [filtered] transaction steps. */
160 size_t actionSize( StepStages filter_r = StepStages() ) const;
162 /** Pointer to the 1st action step in [filtered] transaction. */
163 action_iterator actionBegin( StepStages filter_r = StepStages() ) const;
165 /** Pointer behind the last action step in transaction. */
166 action_iterator actionEnd() const;
171 friend base::SafeBool<Transaction>::operator bool_type() const;
172 /** Validate object in a boolean context. */
173 bool boolTest() const
176 /** Implementation */
179 /** Pointer to implementation */
180 RW_pointer<Impl> _pimpl;
183 ZYPP_DECLARE_OPERATORS_FOR_FLAGS(Transaction::StepStages);
185 /** \relates Transaction Stream output */
186 std::ostream & operator<<( std::ostream & str, const Transaction & obj );
188 /** \relates Transaction Verbose stream output */
189 std::ostream & dumpOn( std::ostream & str, const Transaction & obj );
191 /** \relates Transaction */
192 bool operator==( const Transaction & lhs, const Transaction & rhs );
194 /** \relates Transaction */
195 inline bool operator!=( const Transaction & lhs, const Transaction & rhs )
196 { return !( lhs == rhs ); }
199 /** A single step within a \ref Transaction.
201 * \note After commit, when the @System repo (rpm database) is reread, all
202 * @System solvables within the transaction are invalidated (they got deleted).
203 * Thats why we internally store the NVRA, so you can access \ref ident
204 * (\see \ref sat::Solvable::ident), \ref edition, \ref arch of a deleted package,
205 * even if the \ref satSolvable itself is meanwhile invalid.
207 * \see \ref Transaction.
209 class Transaction::Step
211 friend std::ostream & operator<<( std::ostream & str, const Step & obj );
215 Step( const RW_pointer<Impl> & pimpl_r, detail::IdType id_r )
221 /** Type of action to perform in this step. */
222 StepType stepType() const;
224 /** Step action result. */
225 StepStage stepStage() const;
227 /** Set step action result. */
228 void stepStage( StepStage val_r );
230 /** Return the corresponding \ref Solvable.
231 * Returns \ref Solvable::noSolvable if the item is meanwhile deleted and
232 * was removed from the pool. \see Post mortem acccess to @System solvables.
234 Solvable satSolvable() const
237 /** \name Post mortem acccess to @System solvables
239 * Transaction::Step step;
240 * if ( step.satSolvable() )
241 * std::cout << step.satSolvable() << endl;
243 * std::cout << step.ident() << endl; // deleted @System solvable
247 /** \see \ref sat::Solvable::ident. */
248 IdString ident() const;
250 /** \see \ref sat::Solvable::edition. */
251 Edition edition() const;
253 /** \see \ref sat::Solvable::arch. */
257 /** Implicit conversion to \ref Solvable */
258 operator const Solvable &() const { return _solv; }
259 /** \overload nonconst */
260 operator Solvable &() { return _solv; }
264 /** Pointer to implementation */
265 RW_pointer<Impl> _pimpl;
268 /** \relates Transaction::Step Stream output */
269 std::ostream & operator<<( std::ostream & str, const Transaction::Step & obj );
271 /** \relates Transaction::StepType Stream output */
272 std::ostream & operator<<( std::ostream & str, Transaction::StepType obj );
274 /** \relates Transaction::StepStage Stream output */
275 std::ostream & operator<<( std::ostream & str, Transaction::StepStage obj );
277 ///////////////////////////////////////////////////////////////////
279 { /////////////////////////////////////////////////////////////////
281 /** \ref Transaction iterator.
283 class Transaction_iterator : public boost::iterator_adaptor<
284 Transaction_iterator // Derived
285 , const detail::IdType * // Base
286 , Transaction::Step // Value
287 , boost::forward_traversal_tag // CategoryOrTraversal
288 , Transaction::Step // Reference
292 Transaction_iterator();
293 Transaction_iterator( const RW_pointer<Transaction::Impl> & pimpl_r, base_type id_r )
294 : Transaction_iterator::iterator_adaptor_( id_r )
299 friend class boost::iterator_core_access;
301 reference dereference() const
302 { return Transaction::Step( _pimpl, *base() ); }
305 friend class Transaction_const_iterator;
306 /** Pointer to implementation */
307 RW_pointer<Transaction::Impl> _pimpl;
310 /** \ref Transaction const_iterator.
312 class Transaction_const_iterator : public boost::iterator_adaptor<
313 Transaction_const_iterator // Derived
314 , const detail::IdType * // Base
315 , const Transaction::Step // Value
316 , boost::forward_traversal_tag // CategoryOrTraversal
317 , const Transaction::Step // Reference
321 Transaction_const_iterator();
322 Transaction_const_iterator( const Transaction_iterator & iter_r );
323 Transaction_const_iterator( const RW_pointer<Transaction::Impl> & pimpl_r, base_type id_r )
324 : Transaction_const_iterator::iterator_adaptor_( id_r )
329 friend class boost::iterator_core_access;
331 reference dereference() const
332 { return Transaction::Step( _pimpl, *base() ); }
335 /** Pointer to implementation */
336 RW_pointer<Transaction::Impl> _pimpl;
339 /////////////////////////////////////////////////////////////////
340 } // namespace detail
341 ///////////////////////////////////////////////////////////////////
343 inline Transaction::const_iterator Transaction::find( const ResObject::constPtr & resolvable_r ) const
344 { return( resolvable_r ? find( resolvable_r->satSolvable() ) : end() ); }
346 inline Transaction::iterator Transaction::find( const ResObject::constPtr & resolvable_r )
347 { return( resolvable_r ? find( resolvable_r->satSolvable() ) : end() ); }
349 inline Transaction::const_iterator Transaction::find( const PoolItem & pi_r ) const
350 { return find( pi_r.satSolvable() ); }
352 inline Transaction::iterator Transaction::find( const PoolItem & pi_r )
353 { return find( pi_r.satSolvable() ); }
356 struct Transaction::FilterAction
359 FilterAction( StepStages filter_r ) : _filter( filter_r ) {}
361 bool operator()( const Transaction::Step & step_r ) const
363 if ( step_r.stepType() == Transaction::TRANSACTION_IGNORE )
364 return false; // no action
365 return !_filter || _filter.testFlag( step_r.stepStage() );
371 inline Transaction::action_iterator Transaction::actionBegin( StepStages filter_r ) const
372 { return make_filter_begin( FilterAction( filter_r ), *this ); }
374 inline Transaction::action_iterator Transaction::actionEnd() const
375 { return make_filter_end( FilterAction(), *this ); }
377 inline bool Transaction::actionEmpty( StepStages filter_r ) const
378 { return( actionBegin( filter_r ) == actionEnd() ); }
380 inline size_t Transaction::actionSize( StepStages filter_r ) const
383 for_( it, actionBegin( filter_r ), actionEnd() )
388 /////////////////////////////////////////////////////////////////
390 ///////////////////////////////////////////////////////////////////
391 /////////////////////////////////////////////////////////////////
393 ///////////////////////////////////////////////////////////////////
394 #endif // ZYPP_SAT_TRANSACTION_H