- Prepare ResPool being a singleton ontop of sat::Pool.
[platform/upstream/libzypp.git] / zypp / solver / detail / InstallOrder.cc
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* InstallOrder.cc
3  *
4  * Copyright (C) 2005 SUSE Linux Products GmbH
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18  * 02111-1307, USA.
19  */
20
21 // stolen from yast2-packagemanager
22 /*
23    File:       InstallOrder.h
24    Purpose:    Determine order for installing packages
25    Author:     Ludwig Nussel <lnussel@suse.de>
26    Maintainer: Ludwig Nussel <lnussel@suse.de>
27
28 /-*/
29
30 #include "zypp/solver/detail/InstallOrder.h"
31 #include "zypp/base/LogTools.h"
32 #include "zypp/base/Iterator.h"
33 #include "zypp/base/Algorithm.h"
34
35 #include "zypp/ResFilters.h"
36 #include "zypp/ResStatus.h"
37 #include "zypp/CapAndItem.h"
38 #include "zypp/NameKindProxy.h"
39
40 /////////////////////////////////////////////////////////////////////////
41 namespace zypp
42 { ///////////////////////////////////////////////////////////////////////
43   ///////////////////////////////////////////////////////////////////////
44   namespace solver
45   { /////////////////////////////////////////////////////////////////////
46     /////////////////////////////////////////////////////////////////////
47     namespace detail
48     { ///////////////////////////////////////////////////////////////////
49
50 using namespace std;
51 using namespace zypp;
52
53 #define ITEMNAME(item) (item)->name()
54 //-----------------------------------------------------------------------------
55
56 InstallOrder::InstallOrder( const ResPool & pool, const PoolItemSet & toinstall, const PoolItemSet & installed )
57     : _pool( pool )
58     , _toinstall( toinstall )
59     , _installed( installed )
60     , _dirty (true)
61     , _numrun (0)
62 {
63     _DEBUG("InstallOrder::InstallOrder(_toinstall " << _toinstall.size() << " items, _installed " << _installed.size() << " items)");
64 }
65
66
67 //-----------------------------------------------------------------------------
68
69 void
70 InstallOrder::printAdj (std::ostream& os, bool reversed) const
71 {
72     const Graph& g = (reversed ? _rgraph : _graph);
73     os << "digraph pkgdeps {" << endl;
74     for (Graph::const_iterator gcit = g.begin(); gcit != g.end(); ++gcit)
75     {
76         Nodes::const_iterator niit = _nodes.find(gcit->first);
77         int order = niit->second.order;
78         string name = gcit->first->name();
79         os << "\"" << name << "\"" << "[label=\"" << name << "\\n" << order << "\"";
80         os << "] " << endl;
81         for (PoolItemList::const_iterator scit = gcit->second.begin(); scit != gcit->second.end(); ++scit)
82         {
83             os << "\"" << name << "\" -> \"" << (*scit)->name() << "\"" << endl;
84         }
85     }
86     os << "}" << endl;
87 }
88
89
90 //-----------------------------------------------------------------------------
91
92 // yea, that stuff is suboptimal. there should be a heap sorted by order
93 PoolItemList
94 InstallOrder::computeNextSet()
95 {
96     PoolItemList newlist;
97
98     if (_dirty) startrdfs();
99
100     for (Nodes::iterator it = _nodes.begin(); it != _nodes.end(); ++it)
101     {
102         if (it->second.order == 0
103             && it->second.item)                 // the default Nodes constructor leaves this empty
104         {
105             XXX << "InstallOrder::computeNextSet found " << ITEMNAME(it->second.item) << endl;
106
107             newlist.push_back(it->second.item);
108         }
109     }
110
111     return newlist;
112 }
113
114
115 // decrease order of every adjacent node
116 void
117 InstallOrder::setInstalled(PoolItem item )
118 {
119     _dirty = true;
120
121     PoolItemList adj = _rgraph[item];
122
123     XXX << "InstallOrder::setInstalled " << ITEMNAME(item) << endl;
124
125     // order will be < 0
126     _nodes[item].order--;
127     _installed.insert( item );
128     _toinstall.erase( item );
129
130     for (PoolItemList::iterator it = adj.begin(); it != adj.end(); ++it)
131     {
132         NodeInfo& info = _nodes[*it];
133         info.order--;
134         if (info.order < 0)
135         {
136             WAR << "order of node " << (*it) << " is < 0" << endl;
137         }
138     }
139 }
140
141
142 void
143 InstallOrder::setInstalled( const PoolItemList & rl )
144 {
145     for (PoolItemList::const_iterator it = rl.begin(); it != rl.end(); ++it)
146     {
147         setInstalled(*it);
148     }
149 }
150
151 //-----------------------------------------------------------------------------
152
153 bool
154 InstallOrder::doesProvide( const Capability requirement, PoolItem item ) const
155 {
156     Capabilities::const_iterator pend = item->dep( Dep::PROVIDES ).end();
157     for( Capabilities::const_iterator pit = item->dep( Dep::PROVIDES ).begin(); pit != pend; ++pit) {
158         if( pit->matches( requirement ) == CapMatch::yes ) {
159             return item;
160         }
161     }
162     return PoolItem();
163 }
164
165
166 PoolItem
167 InstallOrder::findProviderInSet( const Capability requirement, const PoolItemSet & candidates ) const
168 {
169     for( PoolItemSet::const_iterator citer = candidates.begin(); citer != candidates.end(); citer++) {
170         if( doesProvide( requirement, *citer ) ) {
171             return *citer;
172         }
173     }
174
175     return PoolItem();
176 }
177
178 struct CollectProviders
179 {
180     const PoolItem requestor;
181     PoolItemList result;
182     const PoolItemSet & limitto;                // limit search to members of this set
183
184     CollectProviders (const PoolItem pi, const PoolItemSet & limit)
185         : requestor (pi)
186         , limitto (limit)
187     { }
188
189
190     bool operator()( const CapAndItem & c_and_i )
191     {
192         // item provides cap which matches a requirement from info->requestor
193         //   this function gets _all_ providers and filter out those which are
194         //   either installed or in our toinstall input list
195         //
196 XXX << "info(" << c_and_i.item <<")"<< endl;
197         if ((c_and_i.item.resolvable() != requestor.resolvable())       // resolvable could provide its own requirement
198             && (limitto.find( c_and_i.item ) != limitto.end()))         // limit to members of 'limitto' set
199         {
200             XXX << "tovisit " << ITEMNAME(c_and_i.item) << endl;
201             result.push_back (c_and_i.item);
202         }
203
204         return true;
205     }
206
207 };
208
209 //-----------------------------------------------------------------------------
210
211
212 void
213 InstallOrder::rdfsvisit (const PoolItem item)
214 {
215     typedef list<Capability> CapList;
216     CapList requires;
217
218     XXX << "InstallOrder::rdfsvisit, visiting " << ITEMNAME(item) << endl;
219
220     NodeInfo& nodeinfo = _nodes[item];
221
222     nodeinfo.visited = true;
223     nodeinfo.begintime = _rdfstime;
224     _rdfstime++;
225
226     // items prereq
227     CapabilitySet prq( item->dep(Dep::PREREQUIRES).begin(), item->dep(Dep::PREREQUIRES).end() );
228     // an installed items prereq (in case they are reqired for uninstall scripts)
229     NameKindProxy nkp( _pool, item->name(), item->kind() );
230     if ( ! nkp.installedEmpty() )
231     {
232       prq.insert( (*nkp.installedBegin())->dep(Dep::PREREQUIRES).begin(),
233                   (*nkp.installedBegin())->dep(Dep::PREREQUIRES).end() );
234     }
235     // put prerequires first and requires last on list to ensure
236     // that prerequires are processed first
237     for (CapabilitySet::const_iterator it = prq.begin(); it != prq.end(); ++it)
238     {
239         requires.push_back(*it);
240     }
241
242     // Product requirements are ignored to assert Product gets installed
243     // as early as possible. Some stuff depends on it (e.g. registration).
244     if ( ! isKind<Product>( item.resolvable() ) )
245       {
246         for (Capabilities::const_iterator it = item->dep (Dep::REQUIRES).begin(); it != item->dep (Dep::REQUIRES).end(); ++it)
247           {
248             requires.push_back(*it);
249           }
250       }
251
252     for (CapList::const_iterator iter = requires.begin(); iter != requires.end(); ++iter)
253     {
254         const Capability requirement = *iter;
255         XXX << "check requirement " << requirement << " of " << ITEMNAME(item) << endl;
256         PoolItemList tovisit;
257
258         // _world->foreachProvidingResItem (requirement, collect_providers, &info);
259         Dep dep (Dep::PROVIDES);
260
261         // first, look in _installed
262         CollectProviders info ( item, _installed );
263
264         invokeOnEach( _pool.byCapabilityIndexBegin( requirement.index(), dep ),
265                       _pool.byCapabilityIndexEnd( requirement.index(), dep ),
266                       resfilter::ByCapMatch( requirement ),
267                       functor::functorRef<bool,CapAndItem>(info) );
268
269         // if not found in _iustalled, look in _toinstall
270
271         if (info.result.empty()) {
272             CollectProviders info1 ( item, _toinstall );
273
274             invokeOnEach( _pool.byCapabilityIndexBegin( requirement.index(), dep ),
275                           _pool.byCapabilityIndexEnd( requirement.index(), dep ),
276                           resfilter::ByCapMatch( requirement ),
277                           functor::functorRef<bool,CapAndItem>(info1) );
278
279             tovisit = info1.result;
280         }
281
282         for (PoolItemList::iterator it = tovisit.begin(); it != tovisit.end(); ++it)
283         {
284             const PoolItem must_visit = *it;
285             if (_nodes[must_visit].visited == false)
286             {
287                 nodeinfo.order++;
288                 _rgraph[must_visit].push_back( item );
289                 _graph[item].push_back( must_visit );
290                 rdfsvisit( must_visit );
291             }
292             else if (_nodes[must_visit].endtime == 0)
293             {
294                 if (must_visit != item)
295                 {
296                   // log only the 1st occurrence.
297                   std::string lstr( ITEMNAME(item) );
298                   lstr += " -> ";
299                   lstr += ITEMNAME(must_visit);
300                   if ( _logset.insert( lstr ).second )
301                   {
302                     WAR << "** dependency loop: " << lstr << endl;
303                   }
304                 }
305             }
306             else
307             {
308                 // filter multiple depends on same item (cosmetic)
309                 PoolItemList & lrg = _rgraph[must_visit];
310                 if( find( lrg.begin(), lrg.end(), item) == lrg.end() )
311                 {
312                     nodeinfo.order++;
313                     lrg.push_back( item );
314
315                     PoolItemList & lg = _graph[item];
316                     if( find( lg.begin(), lg.end(), must_visit ) == lg.end() )
317                         lg.push_back( must_visit );
318                 }
319             }
320         }
321     }
322     _topsorted.push_back(item);
323     _nodes[item].endtime = _rdfstime;
324     _rdfstime++;
325
326     XXX << ITEMNAME(item) << " done" << endl;
327 }
328
329
330 void
331 InstallOrder::startrdfs()
332 {
333     _nodes.clear();
334     _rgraph.clear();
335     _graph.clear();
336
337     _rdfstime = 1;
338
339     _topsorted.clear();
340
341     _numrun++;
342     XXX << "run #" << _numrun << endl;
343
344     // initialize all nodes
345     for (PoolItemSet::iterator it = _toinstall.begin(); it != _toinstall.end(); ++it)
346     {
347         PoolItem item = *it;
348         _nodes[item] = NodeInfo (item);
349         _rgraph[item] = PoolItemList();
350         _graph[item] = PoolItemList();
351     }
352
353     // visit all nodes
354     for (PoolItemSet::iterator it = _toinstall.begin(); it != _toinstall.end(); ++it)
355     {
356         const PoolItem item = *it;
357         if (_nodes[item].visited == false)
358         {
359             XXX << "start recursion on " << ITEMNAME(item) << endl;
360             rdfsvisit (item);
361         }
362     }
363
364     _dirty = false;
365 }
366
367
368 //-----------------------------------------------------------------------------
369
370 const PoolItemList
371 InstallOrder::getTopSorted() const
372 {
373     return _topsorted;
374 }
375
376
377 ///////////////////////////////////////////////////////////////////
378     };// namespace detail
379     /////////////////////////////////////////////////////////////////////
380     /////////////////////////////////////////////////////////////////////
381   };// namespace solver
382   ///////////////////////////////////////////////////////////////////////
383   ///////////////////////////////////////////////////////////////////////
384 };// namespace zypp
385 /////////////////////////////////////////////////////////////////////////