- make installed packages pass much faster
[platform/upstream/libsolv.git] / src / solver.h
1 /*
2  * Copyright (c) 2007, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 /*
9  * solver.h
10  *
11  */
12
13 #ifndef SATSOLVER_SOLVER_H
14 #define SATSOLVER_SOLVER_H
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20 #include "pooltypes.h"
21 #include "pool.h"
22 #include "repo.h"
23 #include "queue.h"
24 #include "bitmap.h"
25 #include "transaction.h"
26 #include "rules.h"
27 #include "problems.h"
28
29 /*
30  * Callback definitions in order to "overwrite" the policies by an external application.
31  */
32  
33 typedef void  (*BestSolvableCb) (Pool *pool, Queue *canditates);
34 typedef int  (*ArchCheckCb) (Pool *pool, Solvable *solvable1, Solvable *solvable2);
35 typedef int  (*VendorCheckCb) (Pool *pool, Solvable *solvable1, Solvable *solvable2);
36 typedef void (*UpdateCandidateCb) (Pool *pool, Solvable *solvable, Queue *canditates);
37
38
39 struct _Solver;
40
41 typedef struct _Solver {
42   Pool *pool;                           /* back pointer to pool */
43   Queue job;                            /* copy of the job we're solving */
44
45   Transaction trans;                    /* calculated transaction */
46
47   Repo *installed;                      /* copy of pool->installed */
48   
49   /* list of rules, ordered
50    * rpm rules first, then features, updates, jobs, learnt
51    * see start/end offsets below
52    */
53   Rule *rules;                          /* all rules */
54   Id nrules;                            /* [Offset] index of the last rule */
55
56   Queue ruleassertions;                 /* Queue of all assertion rules */
57
58   /* start/end offset for rule 'areas' */
59     
60   Id rpmrules_end;                      /* [Offset] rpm rules end */
61     
62   Id featurerules;                      /* feature rules start/end */
63   Id featurerules_end;
64     
65   Id updaterules;                       /* policy rules, e.g. keep packages installed or update. All literals > 0 */
66   Id updaterules_end;
67     
68   Id jobrules;                          /* user rules */
69   Id jobrules_end;
70
71   Id infarchrules;                      /* inferior arch rules */
72   Id infarchrules_end;
73
74   Id duprules;                          /* dist upgrade rules */
75   Id duprules_end;
76     
77   Id choicerules;                       /* choice rules (always weak) */
78   Id choicerules_end;
79   Id *choicerules_ref;
80
81   Id learntrules;                       /* learnt rules, (end == nrules) */
82
83   Map noupdate;                         /* don't try to update these
84                                            installed solvables */
85   Map noobsoletes;                      /* ignore obsoletes for these
86                                            (multiinstall) */
87
88   Map updatemap;                        /* bring those installed packages to the newest version */
89   Map fixmap;                           /* fix those packages */
90
91   Queue weakruleq;                      /* index into 'rules' for weak ones */
92   Map weakrulemap;                      /* map rule# to '1' for weak rules, 1..learntrules */
93
94   Id *watches;                          /* Array of rule offsets
95                                          * watches has nsolvables*2 entries and is addressed from the middle
96                                          * middle-solvable : decision to conflict, offset point to linked-list of rules
97                                          * middle+solvable : decision to install: offset point to linked-list of rules
98                                          */
99
100   Queue ruletojob;                      /* index into job queue: jobs for which a rule exits */
101
102   /* our decisions: */
103   Queue decisionq;                      /* >0:install, <0:remove/conflict */
104   Queue decisionq_why;                  /* index of rule, Offset into rules */
105
106   Id *decisionmap;                      /* map for all available solvables,
107                                          * = 0: undecided
108                                          * > 0: level of decision when installed,
109                                          * < 0: level of decision when conflict */
110
111   /* learnt rule history */
112   Queue learnt_why;
113   Queue learnt_pool;
114
115   Queue branches;
116   int (*solution_callback)(struct _Solver *solv, void *data);
117   void *solution_callback_data;
118
119   int propagate_index;                  /* index into decisionq for non-propagated decisions */
120
121   Queue problems;                       /* list of lists of conflicting rules, < 0 for job rules */
122   Queue solutions;                      /* refined problem storage space */
123
124   Queue recommendations;                /* recommended packages */
125   Queue suggestions;                    /* suggested packages */
126   Queue orphaned;                       /* orphaned packages */
127
128   int stats_learned;                    /* statistic */
129   int stats_unsolvable;                 /* statistic */
130
131   Map recommendsmap;                    /* recommended packages from decisionmap */
132   Map suggestsmap;                      /* suggested packages from decisionmap */
133   int recommends_index;                 /* recommendsmap/suggestsmap is created up to this level */
134
135   Id *obsoletes;                        /* obsoletes for each installed solvable */
136   Id *obsoletes_data;                   /* data area for obsoletes */
137   Id *multiversionupdaters;             /* updaters for multiversion packages in updatesystem mode */
138
139   /*-------------------------------------------------------------------------------------------------------------
140    * Solver configuration
141    *-------------------------------------------------------------------------------------------------------------*/
142
143   int fixsystem;                        /* repair errors in rpm dependency graph */
144   int allowdowngrade;                   /* allow to downgrade installed solvable */
145   int allowarchchange;                  /* allow to change architecture of installed solvables */
146   int allowvendorchange;                /* allow to change vendor of installed solvables */
147   int allowuninstall;                   /* allow removal of installed solvables */
148   int updatesystem;                     /* update all packages to the newest version */
149   int noupdateprovide;                  /* true: update packages needs not to provide old package */
150   int dosplitprovides;                  /* true: consider legacy split provides */
151   int dontinstallrecommended;           /* true: do not install recommended packages */
152   int ignorealreadyrecommended;         /* true: ignore recommended packages that were already recommended by the installed packages */
153   int dontshowinstalledrecommended;     /* true: do not show recommended packages that are already installed */
154   
155   /* distupgrade also needs updatesystem and dosplitprovides */
156   int distupgrade;
157   int distupgrade_removeunsupported;
158
159   int noinfarchcheck;                   /* true: do not forbid inferior architectures */
160
161   /* Callbacks for defining the bahaviour of the SAT solver */
162
163   /* Finding best candidate
164    *
165    * Callback definition:
166    * void  bestSolvable (Pool *pool, Queue *canditates)
167    *     candidates       : List of canditates which has to be sorted by the function call
168    *     return candidates: Sorted list of the candidates(first is the best).
169    */
170    BestSolvableCb bestSolvableCb;
171
172   /* Checking if two solvables has compatible architectures
173    *
174    * Callback definition:
175    *     int  archCheck (Pool *pool, Solvable *solvable1, Solvable *solvable2);
176    *     
177    *     return 0 it the two solvables has compatible architectures
178    */
179    ArchCheckCb archCheckCb;
180
181   /* Checking if two solvables has compatible vendors
182    *
183    * Callback definition:
184    *     int  vendorCheck (Pool *pool, Solvable *solvable1, Solvable *solvable2);
185    *     
186    *     return 0 it the two solvables has compatible architectures
187    */
188    VendorCheckCb vendorCheckCb;
189     
190   /* Evaluate update candidate
191    *
192    * Callback definition:
193    * void pdateCandidateCb (Pool *pool, Solvable *solvable, Queue *canditates)
194    *     solvable   : for which updates should be search
195    *     candidates : List of candidates (This list depends on other
196    *                  restrictions like architecture and vendor policies too)
197    */
198    UpdateCandidateCb   updateCandidateCb;
199     
200
201   /* some strange queue that doesn't belong here */
202   Queue covenantq;                      /* Covenants honored by this solver (generic locks) */
203
204   
205   Map dupmap;                           /* packages from dup repos */
206   Map dupinvolvedmap;                   /* packages involved in dup process */
207
208   Queue *ruleinfoq;                     /* tmp space for solver_ruleinfo() */
209 } Solver;
210
211 /*
212  * queue commands
213  */
214
215 #define SOLVER_SOLVABLE                 0x01
216 #define SOLVER_SOLVABLE_NAME            0x02
217 #define SOLVER_SOLVABLE_PROVIDES        0x03
218 #define SOLVER_SOLVABLE_ONE_OF          0x04
219
220 #define SOLVER_SELECTMASK               0xff
221
222 #define SOLVER_NOOP                     0x0000
223 #define SOLVER_INSTALL                  0x0100
224 #define SOLVER_ERASE                    0x0200
225 #define SOLVER_UPDATE                   0x0300
226 #define SOLVER_WEAKENDEPS               0x0400
227 #define SOLVER_NOOBSOLETES              0x0500
228 #define SOLVER_LOCK                     0x0600
229 #define SOLVER_DISTUPGRADE              0x0700
230 #define SOLVER_VERIFY                   0x0800
231
232 #define SOLVER_JOBMASK                  0xff00
233
234 #define SOLVER_WEAK                     0x010000
235 #define SOLVER_ESSENTIAL                0x020000
236
237 /* old API compatibility, do not use in new code */
238 #if 1
239 #define SOLVER_INSTALL_SOLVABLE (SOLVER_INSTALL|SOLVER_SOLVABLE)
240 #define SOLVER_ERASE_SOLVABLE (SOLVER_ERASE|SOLVER_SOLVABLE)
241 #define SOLVER_INSTALL_SOLVABLE_NAME (SOLVER_INSTALL|SOLVER_SOLVABLE_NAME)
242 #define SOLVER_ERASE_SOLVABLE_NAME (SOLVER_ERASE|SOLVER_SOLVABLE_NAME)
243 #define SOLVER_INSTALL_SOLVABLE_PROVIDES (SOLVER_INSTALL|SOLVER_SOLVABLE_PROVIDES)
244 #define SOLVER_ERASE_SOLVABLE_PROVIDES (SOLVER_ERASE|SOLVER_SOLVABLE_PROVIDES)
245 #define SOLVER_INSTALL_SOLVABLE_UPDATE (SOLVER_UPDATE|SOLVER_SOLVABLE)
246 #define SOLVER_INSTALL_SOLVABLE_ONE_OF (SOLVER_INSTALL|SOLVER_SOLVABLE_ONE_OF)
247 #define SOLVER_WEAKEN_SOLVABLE_DEPS (SOLVER_WEAKENDEPS|SOLVER_SOLVABLE)
248 #define SOLVER_NOOBSOLETES_SOLVABLE (SOLVER_NOOBSOLETES|SOLVER_SOLVABLE)
249 #define SOLVER_NOOBSOLETES_SOLVABLE_NAME (SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME)
250 #define SOLVER_NOOBSOLETES_SOLVABLE_PROVIDES (SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_PROVIDES)
251 #endif
252
253 /* backward compatibility */
254 #define SOLVER_PROBLEM_UPDATE_RULE              SOLVER_RULE_UPDATE
255 #define SOLVER_PROBLEM_JOB_RULE                 SOLVER_RULE_JOB
256 #define SOLVER_PROBLEM_JOB_NOTHING_PROVIDES_DEP SOLVER_RULE_JOB_NOTHING_PROVIDES_DEP
257 #define SOLVER_PROBLEM_NOT_INSTALLABLE          SOLVER_RULE_RPM_NOT_INSTALLABLE
258 #define SOLVER_PROBLEM_NOTHING_PROVIDES_DEP     SOLVER_RULE_RPM_NOTHING_PROVIDES_DEP
259 #define SOLVER_PROBLEM_SAME_NAME                SOLVER_RULE_RPM_SAME_NAME
260 #define SOLVER_PROBLEM_PACKAGE_CONFLICT         SOLVER_RULE_RPM_PACKAGE_CONFLICT
261 #define SOLVER_PROBLEM_PACKAGE_OBSOLETES        SOLVER_RULE_RPM_PACKAGE_OBSOLETES
262 #define SOLVER_PROBLEM_DEP_PROVIDERS_NOT_INSTALLABLE SOLVER_RULE_RPM_PACKAGE_REQUIRES
263 #define SOLVER_PROBLEM_SELF_CONFLICT            SOLVER_RULE_RPM_SELF_CONFLICT
264 #define SOLVER_PROBLEM_RPM_RULE                 SOLVER_RULE_RPM
265 #define SOLVER_PROBLEM_DISTUPGRADE_RULE         SOLVER_RULE_DISTUPGRADE
266 #define SOLVER_PROBLEM_INFARCH_RULE             SOLVER_RULE_INFARCH
267
268
269 extern Solver *solver_create(Pool *pool);
270 extern void solver_free(Solver *solv);
271 extern void solver_solve(Solver *solv, Queue *job);
272
273 extern void solver_run_sat(Solver *solv, int disablerules, int doweak);
274 extern void solver_reset(Solver *solv);
275
276 extern int solver_dep_installed(Solver *solv, Id dep);
277 extern int solver_splitprovides(Solver *solv, Id dep);
278
279 extern void solver_calculate_noobsmap(Pool *pool, Queue *job, Map *noobsmap);
280
281 /* obsolete */
282 extern SolverRuleinfo solver_problemruleinfo(Solver *solv, Queue *job, Id rid, Id *depp, Id *sourcep, Id *targetp);
283
284 /* XXX: why is this not static? */
285 Id *solver_create_decisions_obsoletesmap(Solver *solv);
286
287 static inline int
288 solver_dep_fulfilled(Solver *solv, Id dep)
289 {
290   Pool *pool = solv->pool;
291   Id p, pp;
292
293   if (ISRELDEP(dep))
294     {
295       Reldep *rd = GETRELDEP(pool, dep);
296       if (rd->flags == REL_AND)
297         {
298           if (!solver_dep_fulfilled(solv, rd->name))
299             return 0;
300           return solver_dep_fulfilled(solv, rd->evr);
301         }
302       if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_SPLITPROVIDES)
303         return solver_splitprovides(solv, rd->evr);
304       if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
305         return solver_dep_installed(solv, rd->evr);
306     }
307   FOR_PROVIDES(p, pp, dep)
308     {
309       if (solv->decisionmap[p] > 0)
310         return 1;
311     }
312   return 0;
313 }
314
315 static inline int
316 solver_is_supplementing(Solver *solv, Solvable *s)
317 {
318   Id sup, *supp;
319   if (!s->supplements)
320     return 0;
321   supp = s->repo->idarraydata + s->supplements;
322   while ((sup = *supp++) != 0)
323     if (solver_dep_fulfilled(solv, sup))
324       return 1;
325   return 0;
326 }
327
328 static inline int
329 solver_is_enhancing(Solver *solv, Solvable *s)
330 {
331   Id enh, *enhp;
332   if (!s->enhances)
333     return 0;
334   enhp = s->repo->idarraydata + s->enhances;
335   while ((enh = *enhp++) != 0)
336     if (solver_dep_fulfilled(solv, enh))
337       return 1;
338   return 0;
339 }
340
341 void solver_calc_duchanges(Solver *solv, DUChanges *mps, int nmps);
342 int solver_calc_installsizechange(Solver *solv);
343 void solver_trivial_installable(Solver *solv, Queue *pkgs, Queue *res);
344
345 void solver_find_involved(Solver *solv, Queue *installedq, Solvable *s, Queue *q);
346
347 static inline void
348 solver_create_state_maps(Solver *solv, Map *installedmap, Map *conflictsmap)
349 {
350   pool_create_state_maps(solv->pool, &solv->decisionq, installedmap, conflictsmap);
351 }
352
353 /* iterate over all literals of a rule */
354 /* WARNING: loop body must not relocate whatprovidesdata, e.g. by
355  * looking up the providers of a dependency */
356 #define FOR_RULELITERALS(l, dp, r)                              \
357     for (l = r->d < 0 ? -r->d - 1 : r->d,                       \
358          dp = !l ? &r->w2 : pool->whatprovidesdata + l,         \
359          l = r->p; l; l = (dp != &r->w2 + 1 ? *dp++ : 0))
360
361 /* iterate over all packages selected by a job */
362 #define FOR_JOB_SELECT(p, pp, select, what) \
363     for (pp = (select == SOLVER_SOLVABLE ? 0 :  \
364                select == SOLVER_SOLVABLE_ONE_OF ? what : \
365                pool_whatprovides(pool, what)),                          \
366          p = (select == SOLVER_SOLVABLE ? what : pool->whatprovidesdata[pp++]) ; p ; p = pool->whatprovidesdata[pp++]) \
367       if (select != SOLVER_SOLVABLE_NAME || pool_match_nevr(pool, pool->solvables + p, what))
368
369 #ifdef __cplusplus
370 }
371 #endif
372
373 #endif /* SATSOLVER_SOLVER_H */