optimization: check key storage before doing the forward
[platform/upstream/libsolv.git] / src / transaction.c
index 996f8a5..69c12d9 100644 (file)
@@ -21,6 +21,7 @@
 #include "solver.h"
 #include "bitmap.h"
 #include "pool.h"
+#include "poolarch.h"
 #include "evr.h"
 #include "util.h"
 
@@ -45,20 +46,21 @@ obsq_sortcmp(const void *ap, const void *bp, void *dp)
   obs = pool->solvables + ob;
   if (oas->name != obs->name)
     {
+      /* bring "same name" obsoleters (i.e. upgraders) to front */
       if (oas->name == s->name)
         return -1;
       if (obs->name == s->name)
         return 1;
-      return strcmp(id2str(pool, oas->name), id2str(pool, obs->name));
+      return strcmp(pool_id2str(pool, oas->name), pool_id2str(pool, obs->name));
     }
-  r = evrcmp(pool, oas->evr, obs->evr, EVRCMP_COMPARE);
+  r = pool_evrcmp(pool, oas->evr, obs->evr, EVRCMP_COMPARE);
   if (r)
     return -r; /* highest version first */
   return oa - ob;
 }
 
 void
-solver_transaction_all_pkgs(Transaction *trans, Id p, Queue *pkgs)
+transaction_all_obs_pkgs(Transaction *trans, Id p, Queue *pkgs)
 {
   Pool *pool = trans->pool;
   Solvable *s = pool->solvables + p;
@@ -76,22 +78,20 @@ solver_transaction_all_pkgs(Transaction *trans, Id p, Queue *pkgs)
        return;
       if (q > 0)
        {
+         /* only a single obsoleting package */
          queue_push(pkgs, q);
          return;
        }
       /* find which packages obsolete us */
       for (i = 0; i < ti->count; i += 2)
        if (ti->elements[i + 1] == p)
-         {
-           queue_push(pkgs, p);
-           queue_push(pkgs, ti->elements[i]);
-         }
+         queue_push2(pkgs, p, ti->elements[i]);
       /* sort obsoleters */
       if (pkgs->count > 2)
-       sat_sort(pkgs->elements, pkgs->count / 2, 2 * sizeof(Id), obsq_sortcmp, pool);
+       solv_sort(pkgs->elements, pkgs->count / 2, 2 * sizeof(Id), obsq_sortcmp, pool);
       for (i = 0; i < pkgs->count; i += 2)
        pkgs->elements[i / 2] = pkgs->elements[i + 1];
-      pkgs->count /= 2;
+      queue_truncate(pkgs, pkgs->count / 2);
     }
   else
     {
@@ -107,12 +107,12 @@ solver_transaction_all_pkgs(Transaction *trans, Id p, Queue *pkgs)
 }
 
 Id
-solver_transaction_pkg(Transaction *trans, Id p)
+transaction_obs_pkg(Transaction *trans, Id p)
 {
   Pool *pool = trans->pool;
   Solvable *s = pool->solvables + p;
-  Queue ti;
-  Id tibuf[5];
+  Queue *ti;
+  int i;
 
   if (p <= 0 || !s->repo)
     return 0;
@@ -121,50 +121,222 @@ solver_transaction_pkg(Transaction *trans, Id p)
       p = trans->transaction_installed[p - pool->installed->start];
       return p < 0 ? -p : p;
     }
-  queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
-  solver_transaction_all_pkgs(trans, p, &ti);
-  p = ti.count ? ti.elements[0] : 0;
-  queue_free(&ti);
-  return p;
+  ti = &trans->transaction_info;
+  for (i = 0; i < ti->count; i += 2)
+    if (ti->elements[i] == p)
+      return ti->elements[i + 1];
+  return 0;
 }
 
-/* type filtering, needed if either not all packages are shown
+
+/*
+ * calculate base type of transaction element
+ */
+
+static Id
+transaction_base_type(Transaction *trans, Id p)
+{
+  Pool *pool = trans->pool;
+  Solvable *s, *s2;
+  int r;
+  Id p2;
+
+  if (!MAPTST(&trans->transactsmap, p))
+    return SOLVER_TRANSACTION_IGNORE;
+  p2 = transaction_obs_pkg(trans, p);
+  if (pool->installed && pool->solvables[p].repo == pool->installed)
+    {
+      /* erase */
+      if (!p2)
+       return SOLVER_TRANSACTION_ERASE;
+      s = pool->solvables + p;
+      s2 = pool->solvables + p2;
+      if (s->name == s2->name)
+       {
+         if (s->evr == s2->evr && solvable_identical(s, s2))
+           return SOLVER_TRANSACTION_REINSTALLED;
+         r = pool_evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
+         if (r < 0)
+           return SOLVER_TRANSACTION_UPGRADED;
+         else if (r > 0)
+           return SOLVER_TRANSACTION_DOWNGRADED;
+         return SOLVER_TRANSACTION_CHANGED;
+       }
+      return SOLVER_TRANSACTION_OBSOLETED;
+    }
+  else
+    {
+      /* install or multiinstall */
+      int multi = trans->multiversionmap.size && MAPTST(&trans->multiversionmap, p);
+      if (multi)
+       {
+         if (p2)
+           {
+             s = pool->solvables + p;
+             s2 = pool->solvables + p2;
+             if (s->name == s2->name && s->arch == s2->arch && s->evr == s2->evr)
+               return SOLVER_TRANSACTION_MULTIREINSTALL;
+           }
+         return SOLVER_TRANSACTION_MULTIINSTALL;
+       }
+      if (!p2)
+       return SOLVER_TRANSACTION_INSTALL;
+      s = pool->solvables + p;
+      s2 = pool->solvables + p2;
+      if (s->name == s2->name)
+       {
+         if (s->evr == s2->evr && solvable_identical(s, s2))
+           return SOLVER_TRANSACTION_REINSTALL;
+         r = pool_evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
+         if (r > 0)
+           return SOLVER_TRANSACTION_UPGRADE;
+         else if (r < 0)
+           return SOLVER_TRANSACTION_DOWNGRADE;
+         else
+           return SOLVER_TRANSACTION_CHANGE;
+       }
+      return SOLVER_TRANSACTION_OBSOLETES;
+    }
+}
+
+static inline int
+is_pseudo_package(Pool *pool, Solvable *s)
+{
+  const char *n = pool_id2str(pool, s->name);
+  if (!strncmp(n, "patch:", 6))
+    return 1;
+  if (!strncmp(n, "pattern:", 8))
+    return 1;
+  return 0;
+}
+
+static int
+obsoleted_by_pseudos_only(Transaction *trans, Id p)
+{
+  Pool *pool = trans->pool;
+  Queue q;
+  Id op;
+  int i;
+
+  op = transaction_obs_pkg(trans, p);
+  if (op && !is_pseudo_package(pool, pool->solvables + op))
+    return 0;
+  queue_init(&q);
+  transaction_all_obs_pkgs(trans, p, &q);
+  for (i = 0; i < q.count; i++)
+    if (!is_pseudo_package(pool, pool->solvables + q.elements[i]))
+      break;
+  i = !q.count || i < q.count ? 0 : 1;
+  queue_free(&q);
+  return i;
+}
+
+/*
+ * return type of transaction element
+ *
+ * filtering is needed if either not all packages are shown
  * or replaces are not shown, as otherwise parts of the
  * transaction might not be shown to the user */
 
 Id
-solver_transaction_show(Transaction *trans, Id type, Id p, int flags)
+transaction_type(Transaction *trans, Id p, int mode)
 {
   Pool *pool = trans->pool;
   Solvable *s = pool->solvables + p;
   Queue oq, rq;
-  Id q;
+  Id type, q;
   int i, j, ref = 0;
 
+  if (!s->repo)
+    return SOLVER_TRANSACTION_IGNORE;
+
+  /* XXX: SUSE only? */
+  if (!(mode & SOLVER_TRANSACTION_KEEP_PSEUDO) && is_pseudo_package(pool, s))
+    return SOLVER_TRANSACTION_IGNORE;
+
+  type = transaction_base_type(trans, p);
+
+  if (type == SOLVER_TRANSACTION_IGNORE)
+    return SOLVER_TRANSACTION_IGNORE;  /* not part of the transaction */
+
+  if ((mode & SOLVER_TRANSACTION_RPM_ONLY) != 0)
+    {
+      /* application wants to know what to feed to rpm */
+      if (type == SOLVER_TRANSACTION_ERASE || type == SOLVER_TRANSACTION_INSTALL || type == SOLVER_TRANSACTION_MULTIINSTALL)
+       return type;
+      if (s->repo == pool->installed)
+       {
+         /* check if we're obsoleted by pseudos only */
+         if (obsoleted_by_pseudos_only(trans, s - pool->solvables))
+           return SOLVER_TRANSACTION_ERASE;
+         return SOLVER_TRANSACTION_IGNORE;     /* ignore as we're being obsoleted */
+       }
+      if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
+       return SOLVER_TRANSACTION_MULTIINSTALL;
+      return SOLVER_TRANSACTION_INSTALL;
+    }
+
+  if ((mode & SOLVER_TRANSACTION_SHOW_MULTIINSTALL) == 0)
+    {
+      /* application wants to make no difference between install
+       * and multiinstall */
+      if (type == SOLVER_TRANSACTION_MULTIINSTALL)
+        type = SOLVER_TRANSACTION_INSTALL;
+      if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
+        type = SOLVER_TRANSACTION_REINSTALL;
+    }
+
+  if ((mode & SOLVER_TRANSACTION_CHANGE_IS_REINSTALL) != 0)
+    {
+      /* application wants to make no difference between change
+       * and reinstall */
+      if (type == SOLVER_TRANSACTION_CHANGED)
+       type = SOLVER_TRANSACTION_REINSTALLED;
+      else if (type == SOLVER_TRANSACTION_CHANGE)
+       type = SOLVER_TRANSACTION_REINSTALL;
+    }
+
   if (type == SOLVER_TRANSACTION_ERASE || type == SOLVER_TRANSACTION_INSTALL || type == SOLVER_TRANSACTION_MULTIINSTALL)
     return type;
 
-  if (s->repo == pool->installed && (flags & SOLVER_TRANSACTION_SHOW_ACTIVE) == 0)
+  if (s->repo == pool->installed && (mode & SOLVER_TRANSACTION_SHOW_ACTIVE) == 0)
     {
-      /* erase element */
-      if ((flags & SOLVER_TRANSACTION_SHOW_REPLACES) == 0 && type == SOLVER_TRANSACTION_REPLACED)
+      /* erase element and we're showing the passive side */
+      if (type == SOLVER_TRANSACTION_OBSOLETED && (mode & SOLVER_TRANSACTION_SHOW_OBSOLETES) == 0)
        type = SOLVER_TRANSACTION_ERASE;
+      if (type == SOLVER_TRANSACTION_OBSOLETED && (mode & SOLVER_TRANSACTION_OBSOLETE_IS_UPGRADE) != 0)
+       type = SOLVER_TRANSACTION_UPGRADED;
       return type;
     }
-  if (s->repo != pool->installed && (flags & SOLVER_TRANSACTION_SHOW_ACTIVE) != 0)
+  if (s->repo != pool->installed && (mode & SOLVER_TRANSACTION_SHOW_ACTIVE) != 0)
     {
-      if ((flags & SOLVER_TRANSACTION_SHOW_REPLACES) == 0 && type == SOLVER_TRANSACTION_REPLACE)
+      /* install element and we're showing the active side */
+      if (type == SOLVER_TRANSACTION_OBSOLETES && (mode & SOLVER_TRANSACTION_SHOW_OBSOLETES) == 0)
        type = SOLVER_TRANSACTION_INSTALL;
+      if (type == SOLVER_TRANSACTION_OBSOLETES && (mode & SOLVER_TRANSACTION_OBSOLETE_IS_UPGRADE) != 0)
+       type = SOLVER_TRANSACTION_UPGRADE;
       return type;
     }
 
+  /* the element doesn't match the show mode */
+
+  /* if we're showing all references, we can ignore this package */
+  if ((mode & (SOLVER_TRANSACTION_SHOW_ALL|SOLVER_TRANSACTION_SHOW_OBSOLETES)) == (SOLVER_TRANSACTION_SHOW_ALL|SOLVER_TRANSACTION_SHOW_OBSOLETES))
+    return SOLVER_TRANSACTION_IGNORE;
+
+  /* we're not showing all refs. check if some other package
+   * references us. If yes, it's safe to ignore this package,
+   * otherwise we need to map the type */
+
   /* most of the time there's only one reference, so check it first */
-  q = solver_transaction_pkg(trans, p);
-  if ((flags & SOLVER_TRANSACTION_SHOW_REPLACES) == 0)
+  q = transaction_obs_pkg(trans, p);
+
+  if ((mode & SOLVER_TRANSACTION_SHOW_OBSOLETES) == 0)
     {
       Solvable *sq = pool->solvables + q;
       if (sq->name != s->name)
        {
+         /* it's a replace but we're not showing replaces. map type. */
          if (s->repo == pool->installed)
            return SOLVER_TRANSACTION_ERASE;
          else if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
@@ -173,26 +345,29 @@ solver_transaction_show(Transaction *trans, Id type, Id p, int flags)
            return SOLVER_TRANSACTION_INSTALL;
        }
     }
-  if (solver_transaction_pkg(trans, q) == p)
-    return type;
+  
+  /* if there's a match, p will be shown when q
+   * is processed */
+  if (transaction_obs_pkg(trans, q) == p)
+    return SOLVER_TRANSACTION_IGNORE;
 
   /* too bad, a miss. check em all */
   queue_init(&oq);
   queue_init(&rq);
-  solver_transaction_all_pkgs(trans, p, &oq);
+  transaction_all_obs_pkgs(trans, p, &oq);
   for (i = 0; i < oq.count; i++)
     {
       q = oq.elements[i];
-      if ((flags & SOLVER_TRANSACTION_SHOW_REPLACES) == 0)
+      if ((mode & SOLVER_TRANSACTION_SHOW_OBSOLETES) == 0)
        {
          Solvable *sq = pool->solvables + q;
          if (sq->name != s->name)
            continue;
        }
       /* check if we are referenced? */
-      if ((flags & SOLVER_TRANSACTION_SHOW_ALL) != 0)
+      if ((mode & SOLVER_TRANSACTION_SHOW_ALL) != 0)
        {
-         solver_transaction_all_pkgs(trans, q, &rq);
+         transaction_all_obs_pkgs(trans, q, &rq);
          for (j = 0; j < rq.count; j++)
            if (rq.elements[j] == p)
              {
@@ -202,7 +377,7 @@ solver_transaction_show(Transaction *trans, Id type, Id p, int flags)
          if (ref)
            break;
        }
-      else if (solver_transaction_pkg(trans, q) == p)
+      else if (transaction_obs_pkg(trans, q) == p)
         {
          ref = 1;
          break;
@@ -213,27 +388,214 @@ solver_transaction_show(Transaction *trans, Id type, Id p, int flags)
 
   if (!ref)
     {
+      /* we're not referenced. map type */
       if (s->repo == pool->installed)
-       type = SOLVER_TRANSACTION_ERASE;
+       return SOLVER_TRANSACTION_ERASE;
       else if (type == SOLVER_TRANSACTION_MULTIREINSTALL)
-       type = SOLVER_TRANSACTION_MULTIINSTALL;
+       return SOLVER_TRANSACTION_MULTIINSTALL;
       else
-       type = SOLVER_TRANSACTION_INSTALL;
+       return SOLVER_TRANSACTION_INSTALL;
+    }
+  /* there was a ref, so p is shown with some other package */
+  return SOLVER_TRANSACTION_IGNORE;
+}
+
+
+
+static int
+classify_cmp(const void *ap, const void *bp, void *dp)
+{
+  Transaction *trans = dp;
+  Pool *pool = trans->pool;
+  const Id *a = ap;
+  const Id *b = bp;
+  int r;
+
+  r = a[0] - b[0];
+  if (r)
+    return r;
+  r = a[2] - b[2];
+  if (r)
+    return a[2] && b[2] ? strcmp(pool_id2str(pool, a[2]), pool_id2str(pool, b[2])) : r;
+  r = a[3] - b[3];
+  if (r)
+    return a[3] && b[3] ? strcmp(pool_id2str(pool, a[3]), pool_id2str(pool, b[3])) : r;
+  return 0;
+}
+
+static int
+classify_cmp_pkgs(const void *ap, const void *bp, void *dp)
+{
+  Transaction *trans = dp;
+  Pool *pool = trans->pool;
+  Id a = *(Id *)ap;
+  Id b = *(Id *)bp;
+  Solvable *sa, *sb;
+
+  sa = pool->solvables + a;
+  sb = pool->solvables + b;
+  if (sa->name != sb->name)
+    return strcmp(pool_id2str(pool, sa->name), pool_id2str(pool, sb->name));
+  if (sa->evr != sb->evr)
+    {
+      int r = pool_evrcmp(pool, sa->evr, sb->evr, EVRCMP_COMPARE);
+      if (r)
+       return r;
+    }
+  return a - b;
+}
+
+void
+transaction_classify(Transaction *trans, int mode, Queue *classes)
+{
+  Pool *pool = trans->pool;
+  int ntypes[SOLVER_TRANSACTION_MAXTYPE + 1];
+  Solvable *s, *sq;
+  Id v, vq, type, p, q;
+  int i, j;
+
+  queue_empty(classes);
+  memset(ntypes, 0, sizeof(ntypes));
+  /* go through transaction and classify each step */
+  for (i = 0; i < trans->steps.count; i++)
+    {
+      p = trans->steps.elements[i];
+      s = pool->solvables + p;
+      type = transaction_type(trans, p, mode);
+      ntypes[type]++;
+      if (!pool->installed || s->repo != pool->installed)
+       continue;
+      /* don't report vendor/arch changes if we were mapped to erase. */
+      if (type == SOLVER_TRANSACTION_ERASE)
+       continue;
+      /* look at arch/vendor changes */
+      q = transaction_obs_pkg(trans, p);
+      if (!q)
+       continue;
+      sq = pool->solvables + q;
+
+      v = s->arch;
+      vq = sq->arch;
+      if (v != vq)
+       {
+         if ((mode & SOLVER_TRANSACTION_MERGE_ARCHCHANGES) != 0)
+           v = vq = 0;
+         for (j = 0; j < classes->count; j += 4)
+           if (classes->elements[j] == SOLVER_TRANSACTION_ARCHCHANGE && classes->elements[j + 2] == v && classes->elements[j + 3] == vq)
+             break;
+         if (j == classes->count)
+           {
+             queue_push(classes, SOLVER_TRANSACTION_ARCHCHANGE);
+             queue_push(classes, 1);
+             queue_push(classes, v);
+             queue_push(classes, vq);
+           }
+         else
+           classes->elements[j + 1]++;
+       }
+
+      v = s->vendor ? s->vendor : 1;
+      vq = sq->vendor ? sq->vendor : 1;
+      if (v != vq)
+       {
+         if ((mode & SOLVER_TRANSACTION_MERGE_VENDORCHANGES) != 0)
+           v = vq = 0;
+         for (j = 0; j < classes->count; j += 4)
+           if (classes->elements[j] == SOLVER_TRANSACTION_VENDORCHANGE && classes->elements[j + 2] == v && classes->elements[j + 3] == vq)
+             break;
+         if (j == classes->count)
+           {
+             queue_push(classes, SOLVER_TRANSACTION_VENDORCHANGE);
+             queue_push(classes, 1);
+             queue_push(classes, v);
+             queue_push(classes, vq);
+           }
+         else
+           classes->elements[j + 1]++;
+       }
+    }
+  /* now sort all vendor/arch changes */
+  if (classes->count > 4)
+    solv_sort(classes->elements, classes->count / 4, 4 * sizeof(Id), classify_cmp, trans);
+  /* finally add all classes. put erases last */
+  i = SOLVER_TRANSACTION_ERASE;
+  if (ntypes[i])
+    {
+      queue_unshift(classes, 0);
+      queue_unshift(classes, 0);
+      queue_unshift(classes, ntypes[i]);
+      queue_unshift(classes, i);
+    }
+  for (i = SOLVER_TRANSACTION_MAXTYPE; i > 0; i--)
+    {
+      if (!ntypes[i])
+       continue;
+      if (i == SOLVER_TRANSACTION_ERASE)
+       continue;
+      queue_unshift(classes, 0);
+      queue_unshift(classes, 0);
+      queue_unshift(classes, ntypes[i]);
+      queue_unshift(classes, i);
     }
-  return type;
+}
+
+void
+transaction_classify_pkgs(Transaction *trans, int mode, Id class, Id from, Id to, Queue *pkgs)
+{
+  Pool *pool = trans->pool;
+  int i;
+  Id type, p, q;
+  Solvable *s, *sq;
+
+  queue_empty(pkgs);
+  for (i = 0; i < trans->steps.count; i++)
+    {
+      p = trans->steps.elements[i];
+      s = pool->solvables + p;
+      if (class <= SOLVER_TRANSACTION_MAXTYPE)
+       {
+         type = transaction_type(trans, p, mode);
+         if (type == class)
+           queue_push(pkgs, p);
+         continue;
+       }
+      if (!pool->installed || s->repo != pool->installed)
+       continue;
+      q = transaction_obs_pkg(trans, p);
+      if (!q)
+       continue;
+      sq = pool->solvables + q;
+      if (class == SOLVER_TRANSACTION_ARCHCHANGE)
+       {
+         if ((!from && !to) || (s->arch == from && sq->arch == to))
+           queue_push(pkgs, p);
+         continue;
+       }
+      if (class == SOLVER_TRANSACTION_VENDORCHANGE)
+       {
+         Id v = s->vendor ? s->vendor : 1;
+         Id vq = sq->vendor ? sq->vendor : 1;
+         if ((!from && !to) || (v == from && vq == to))
+           queue_push(pkgs, p);
+         continue;
+       }
+    }
+  if (pkgs->count > 1)
+    solv_sort(pkgs->elements, pkgs->count, sizeof(Id), classify_cmp_pkgs, trans);
 }
 
 static void
-create_transaction_info(Transaction *trans, Queue *decisionq, Map *noobsmap)
+create_transaction_info(Transaction *trans, Queue *decisionq)
 {
   Pool *pool = trans->pool;
   Queue *ti = &trans->transaction_info;
   Repo *installed = pool->installed;
-  int i, j, noobs;
+  int i, j, multi;
   Id p, p2, pp2;
   Solvable *s, *s2;
 
   queue_empty(ti);
+  trans->transaction_installed = solv_free(trans->transaction_installed);
   if (!installed)
     return;    /* no info needed */
   for (i = 0; i < decisionq->count; i++)
@@ -242,9 +604,9 @@ create_transaction_info(Transaction *trans, Queue *decisionq, Map *noobsmap)
       if (p <= 0 || p == SYSTEMSOLVABLE)
        continue;
       s = pool->solvables + p;
-      if (s->repo == installed)
+      if (!s->repo || s->repo == installed)
        continue;
-      noobs = noobsmap && MAPTST(noobsmap, p);
+      multi = trans->multiversionmap.size && MAPTST(&trans->multiversionmap, p);
       FOR_PROVIDES(p2, pp2, s->name)
        {
          if (!MAPTST(&trans->transactsmap, p2))
@@ -252,14 +614,15 @@ create_transaction_info(Transaction *trans, Queue *decisionq, Map *noobsmap)
          s2 = pool->solvables + p2;
          if (s2->repo != installed)
            continue;
-         if (noobs && (s->name != s2->name || s->evr != s2->evr || s->arch != s2->arch))
+         if (multi && (s->name != s2->name || s->evr != s2->evr || s->arch != s2->arch))
            continue;
          if (!pool->implicitobsoleteusesprovides && s->name != s2->name)
            continue;
-         queue_push(ti, p);
-         queue_push(ti, p2);
+         if (pool->implicitobsoleteusescolors && !pool_colormatch(pool, s, s2))
+           continue;
+         queue_push2(ti, p, p2);
        }
-      if (s->obsoletes && !noobs)
+      if (s->obsoletes && (!multi || !pool->noobsoletesmultiversion))
        {
          Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
          while ((obs = *obsp++) != 0)
@@ -271,25 +634,30 @@ create_transaction_info(Transaction *trans, Queue *decisionq, Map *noobsmap)
                    continue;
                  if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p2, obs))
                    continue;
-                 queue_push(ti, p);
-                 queue_push(ti, p2);
+                 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
+                   continue;
+                 queue_push2(ti, p, p2);
                }
            }
        }
     }
-  sat_sort(ti->elements, ti->count / 2, 2 * sizeof(Id), obsq_sortcmp, pool);
-  /* now unify */
-  for (i = j = 0; i < ti->count; i += 2)
+  if (ti->count > 2)
     {
-      if (j && ti->elements[i] == ti->elements[j - 2] && ti->elements[i + 1] == ti->elements[j - 1])
-       continue;
-      ti->elements[j++] = ti->elements[i];
-      ti->elements[j++] = ti->elements[i + 1];
+      /* sort and unify */
+      solv_sort(ti->elements, ti->count / 2, 2 * sizeof(Id), obsq_sortcmp, pool);
+      for (i = j = 2; i < ti->count; i += 2)
+       {
+         if (ti->elements[i] == ti->elements[j - 2] && ti->elements[i + 1] == ti->elements[j - 1])
+           continue;
+         ti->elements[j++] = ti->elements[i];
+         ti->elements[j++] = ti->elements[i + 1];
+       }
+      queue_truncate(ti, j);
     }
-  ti->count = j;
 
   /* create transaction_installed helper */
-  trans->transaction_installed = sat_calloc(installed->end - installed->start, sizeof(Id));
+  /*   entry > 0: exactly one obsoleter, entry < 0: multiple obsoleters, -entry is "best" */
+  trans->transaction_installed = solv_calloc(installed->end - installed->start, sizeof(Id));
   for (i = 0; i < ti->count; i += 2)
     {
       j = ti->elements[i + 1] - installed->start;
@@ -297,7 +665,7 @@ create_transaction_info(Transaction *trans, Queue *decisionq, Map *noobsmap)
        trans->transaction_installed[j] = ti->elements[i];
       else
        {
-         /* more than one package obsoletes us. compare */
+         /* more than one package obsoletes us. compare to find "best" */
          Id q[4];
          if (trans->transaction_installed[j] > 0)
            trans->transaction_installed[j] = -trans->transaction_installed[j];
@@ -310,19 +678,22 @@ create_transaction_info(Transaction *trans, Queue *decisionq, Map *noobsmap)
     }
 }
 
-void
-transaction_calculate(Transaction *trans, Queue *decisionq, Map *noobsmap)
+
+Transaction *
+transaction_create_decisionq(Pool *pool, Queue *decisionq, Map *multiversionmap)
 {
-  Pool *pool = trans->pool;
   Repo *installed = pool->installed;
-  int i, r, noobs;
-  Id p, p2;
-  Solvable *s, *s2;
+  int i, needmulti;
+  Id p;
+  Solvable *s;
+  Transaction *trans;
 
-  if (noobsmap && !noobsmap->size)
-    noobsmap = 0;      /* ignore empty map */
+  trans = transaction_create(pool);
+  if (multiversionmap && !multiversionmap->size)
+    multiversionmap = 0;       /* ignore empty map */
   queue_empty(&trans->steps);
   map_init(&trans->transactsmap, pool->nsolvables);
+  needmulti = 0;
   for (i = 0; i < decisionq->count; i++)
     {
       p = decisionq->elements[i];
@@ -332,86 +703,120 @@ transaction_calculate(Transaction *trans, Queue *decisionq, Map *noobsmap)
       if (installed && s->repo == installed && p < 0)
        MAPSET(&trans->transactsmap, -p);
       if ((!installed || s->repo != installed) && p > 0)
-       MAPSET(&trans->transactsmap, p);
+       {
+#if 0
+         const char *n = pool_id2str(pool, s->name);
+         if (!strncmp(n, "patch:", 6))
+           continue;
+         if (!strncmp(n, "pattern:", 8))
+           continue;
+#endif
+         MAPSET(&trans->transactsmap, p);
+         if (multiversionmap && MAPTST(multiversionmap, p))
+           needmulti = 1;
+       }
     }
-  create_transaction_info(trans, decisionq, noobsmap);
+  MAPCLR(&trans->transactsmap, SYSTEMSOLVABLE);
+  if (needmulti)
+    map_init_clone(&trans->multiversionmap, multiversionmap);
+
+  create_transaction_info(trans, decisionq);
 
   if (installed)
     {
       FOR_REPO_SOLVABLES(installed, p, s)
        {
-         if (!MAPTST(&trans->transactsmap, p))
-           continue;
-         p2 = solver_transaction_pkg(trans, p);
-         if (!p2)
-           queue_push(&trans->steps, SOLVER_TRANSACTION_ERASE);
-         else
-           {
-             s2 = pool->solvables + p2;
-             if (s->name == s2->name)
-               {
-                 if (s->evr == s2->evr && solvable_identical(s, s2))
-                   queue_push(&trans->steps, SOLVER_TRANSACTION_REINSTALLED);
-                 else
-                   {
-                     r = evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
-                     if (r < 0)
-                       queue_push(&trans->steps, SOLVER_TRANSACTION_UPGRADED);
-                     else if (r > 0)
-                       queue_push(&trans->steps, SOLVER_TRANSACTION_DOWNGRADED);
-                     else
-                       queue_push(&trans->steps, SOLVER_TRANSACTION_CHANGED);
-                   }
-               }
-             else
-               queue_push(&trans->steps, SOLVER_TRANSACTION_REPLACED);
-           }
-         queue_push(&trans->steps, p);
+         if (MAPTST(&trans->transactsmap, p))
+           queue_push(&trans->steps, p);
        }
     }
   for (i = 0; i < decisionq->count; i++)
     {
       p = decisionq->elements[i];
-      if (p < 0 || p == SYSTEMSOLVABLE)
-       continue;
+      if (p > 0 && MAPTST(&trans->transactsmap, p))
+        queue_push(&trans->steps, p);
+    }
+  return trans;
+}
+
+int
+transaction_installedresult(Transaction *trans, Queue *installedq)
+{
+  Pool *pool = trans->pool;
+  Repo *installed = pool->installed;
+  Solvable *s;
+  int i, cutoff;
+  Id p;
+
+  queue_empty(installedq);
+  /* first the new installs, than the kept packages */
+  for (i = 0; i < trans->steps.count; i++)
+    {
+      p = trans->steps.elements[i];
       s = pool->solvables + p;
       if (installed && s->repo == installed)
        continue;
-      noobs = noobsmap && MAPTST(noobsmap, p);
-      p2 = solver_transaction_pkg(trans, p);
-      if (noobs)
-       queue_push(&trans->steps, p2 ? SOLVER_TRANSACTION_MULTIREINSTALL : SOLVER_TRANSACTION_MULTIINSTALL);
-      else if (!p2)
-       queue_push(&trans->steps, SOLVER_TRANSACTION_INSTALL);
-      else
-       {
-         s2 = pool->solvables + p2;
-         if (s->name == s2->name)
-           {
-             if (s->evr == s2->evr && solvable_identical(s, s2))
-               queue_push(&trans->steps, SOLVER_TRANSACTION_REINSTALL);
-             else
-               {
-                 r = evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
-                 if (r > 0)
-                   queue_push(&trans->steps, SOLVER_TRANSACTION_UPGRADE);
-                 else if (r < 0)
-                   queue_push(&trans->steps, SOLVER_TRANSACTION_DOWNGRADE);
-                 else
-                   queue_push(&trans->steps, SOLVER_TRANSACTION_CHANGE);
-               }
-           }
-         else
-           queue_push(&trans->steps, SOLVER_TRANSACTION_REPLACE);
-       }
-      queue_push(&trans->steps, p);
+      queue_push(installedq, p);
     }
+  cutoff = installedq->count;
+  if (installed)
+    {
+      FOR_REPO_SOLVABLES(installed, p, s)
+       if (!MAPTST(&trans->transactsmap, p))
+          queue_push(installedq, p);
+    }
+  return cutoff;
 }
 
+static void
+transaction_create_installedmap(Transaction *trans, Map *installedmap)
+{
+  Pool *pool = trans->pool;
+  Repo *installed = pool->installed;
+  Solvable *s;
+  Id p;
+  int i;
+
+  map_init(installedmap, pool->nsolvables);
+  for (i = 0; i < trans->steps.count; i++)
+    {
+      p = trans->steps.elements[i];
+      s = pool->solvables + p;
+      if (!installed || s->repo != installed)
+        MAPSET(installedmap, p);
+    }
+  if (installed)
+    {
+      FOR_REPO_SOLVABLES(installed, p, s)
+       if (!MAPTST(&trans->transactsmap, p))
+          MAPSET(installedmap, p);
+    }
+}
+
+int
+transaction_calc_installsizechange(Transaction *trans)
+{
+  Map installedmap;
+  int change;
+
+  transaction_create_installedmap(trans, &installedmap);
+  change = pool_calc_installsizechange(trans->pool, &installedmap);
+  map_free(&installedmap);
+  return change;
+}
+
+void
+transaction_calc_duchanges(Transaction *trans, DUChanges *mps, int nmps)
+{
+  Map installedmap;
+
+  transaction_create_installedmap(trans, &installedmap);
+  pool_calc_duchanges(trans->pool, &installedmap, mps, nmps);
+  map_free(&installedmap);
+}
 
 struct _TransactionElement {
   Id p;                /* solvable id */
-  Id type;     /* installation type */
   Id edges;    /* pointer into edges data */
   Id mark;
 };
@@ -420,6 +825,7 @@ struct _TransactionOrderdata {
   struct _TransactionElement *tes;
   int ntes;
   Id *invedgedata;
+  int ninvedgedata;
 };
 
 #define TYPE_BROKEN    (1<<0)
@@ -436,11 +842,40 @@ struct _TransactionOrderdata {
 
 #define EDGEDATA_BLOCK 127
 
-void
-transaction_init(Transaction *trans, Pool *pool)
+Transaction *
+transaction_create(Pool *pool)
 {
-  memset(trans, 0, sizeof(*trans));
+  Transaction *trans = solv_calloc(1, sizeof(*trans));
   trans->pool = pool;
+  return trans;
+}
+
+Transaction *
+transaction_create_clone(Transaction *srctrans)
+{
+  Transaction *trans = transaction_create(srctrans->pool);
+  queue_init_clone(&trans->steps, &srctrans->steps);
+  queue_init_clone(&trans->transaction_info, &srctrans->transaction_info);
+  if (srctrans->transaction_installed)
+    {
+      Repo *installed = srctrans->pool->installed;
+      trans->transaction_installed = solv_calloc(installed->end - installed->start, sizeof(Id));
+      memcpy(trans->transaction_installed, srctrans->transaction_installed, (installed->end - installed->start) * sizeof(Id));
+    }
+  map_init_clone(&trans->transactsmap, &srctrans->transactsmap);
+  map_init_clone(&trans->multiversionmap, &srctrans->multiversionmap);
+  if (srctrans->orderdata)
+    {
+      struct _TransactionOrderdata *od = srctrans->orderdata;
+      trans->orderdata = solv_calloc(1, sizeof(*trans->orderdata));
+      trans->orderdata->tes = solv_malloc2(od->ntes, sizeof(*od->tes));
+      memcpy(trans->orderdata->tes, od->tes, od->ntes * sizeof(*od->tes));
+      trans->orderdata->ntes = od->ntes;
+      trans->orderdata->invedgedata = solv_malloc2(od->ninvedgedata, sizeof(Id));
+      memcpy(trans->orderdata->invedgedata, od->invedgedata, od->ninvedgedata * sizeof(Id));
+      trans->orderdata->ninvedgedata = od->ninvedgedata;
+    }
+  return trans;
 }
 
 void
@@ -448,14 +883,22 @@ transaction_free(Transaction *trans)
 {
   queue_free(&trans->steps);
   queue_free(&trans->transaction_info);
-  trans->transaction_installed = sat_free(trans->transaction_installed);
+  trans->transaction_installed = solv_free(trans->transaction_installed);
   map_free(&trans->transactsmap);
+  map_free(&trans->multiversionmap);
+  transaction_free_orderdata(trans);
+  free(trans);
+}
+
+void
+transaction_free_orderdata(Transaction *trans)
+{
   if (trans->orderdata)
     {
       struct _TransactionOrderdata *od = trans->orderdata;
-      od->tes = sat_free(od->tes);
-      od->invedgedata = sat_free(od->invedgedata);
-      trans->orderdata = sat_free(trans->orderdata);
+      od->tes = solv_free(od->tes);
+      od->invedgedata = solv_free(od->invedgedata);
+      trans->orderdata = solv_free(trans->orderdata);
     }
 }
 
@@ -481,7 +924,7 @@ addteedge(struct orderdata *od, int from, int to, int type)
   if (from == to)
     return 0;
 
-  /* printf("edge %d(%s) -> %d(%s) type %x\n", from, solvid2str(pool, od->tes[from].p), to, solvid2str(pool, od->tes[to].p), type); */
+  /* printf("edge %d(%s) -> %d(%s) type %x\n", from, pool_solvid2str(pool, od->tes[from].p), to, pool_solvid2str(pool, od->tes[to].p), type); */
 
   te = od->tes + from;
   for (i = te->edges; od->edgedata[i]; i += 2)
@@ -500,12 +943,12 @@ addteedge(struct orderdata *od, int from, int to, int type)
       /* printf("tail add %d\n", i - te->edges); */
       if (!i)
        te->edges = ++i;
-      od->edgedata = sat_extend(od->edgedata, od->nedgedata, 3, sizeof(Id), EDGEDATA_BLOCK);
+      od->edgedata = solv_extend(od->edgedata, od->nedgedata, 3, sizeof(Id), EDGEDATA_BLOCK);
     }
   else
     {
       /* printf("extend %d\n", i - te->edges); */
-      od->edgedata = sat_extend(od->edgedata, od->nedgedata, 3 + (i - te->edges), sizeof(Id), EDGEDATA_BLOCK);
+      od->edgedata = solv_extend(od->edgedata, od->nedgedata, 3 + (i - te->edges), sizeof(Id), EDGEDATA_BLOCK);
       if (i > te->edges)
        memcpy(od->edgedata + od->nedgedata, od->edgedata + te->edges, sizeof(Id) * (i - te->edges));
       i = od->nedgedata + (i - te->edges);
@@ -527,7 +970,7 @@ addedge(struct orderdata *od, Id from, Id to, int type)
   struct _TransactionElement *te;
   int i;
 
-  // printf("addedge %d %d type %d\n", from, to, type);
+  /* printf("addedge %d %d type %d\n", from, to, type); */
   s = pool->solvables + from;
   if (s->repo == pool->installed && trans->transaction_installed[from - pool->installed->start])
     {
@@ -541,7 +984,7 @@ addedge(struct orderdata *od, Id from, Id to, int type)
          Id tibuf[5];
 
          queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
-         solver_transaction_all_pkgs(trans, from, &ti);
+         transaction_all_obs_pkgs(trans, from, &ti);
          for (i = 0; i < ti.count; i++)
            ret |= addedge(od, ti.elements[i], to, type);
          queue_free(&ti);
@@ -561,7 +1004,7 @@ addedge(struct orderdata *od, Id from, Id to, int type)
          Id tibuf[5];
 
          queue_init_buffer(&ti, tibuf, sizeof(tibuf)/sizeof(*tibuf));
-         solver_transaction_all_pkgs(trans, to, &ti);
+         transaction_all_obs_pkgs(trans, to, &ti);
          for (i = 0; i < ti.count; i++)
            ret |= addedge(od, from, ti.elements[i], type);
          queue_free(&ti);
@@ -610,9 +1053,9 @@ havechoice(struct orderdata *od, Id p, Id q1, Id q2)
   if (trans->transaction_installed[q1 - pool->installed->start] > 0 && trans->transaction_installed[q2 - pool->installed->start] > 0)
     return 1;
   queue_init_buffer(&ti1, ti1buf, sizeof(ti1buf)/sizeof(*ti1buf));
-  solver_transaction_all_pkgs(trans, q1, &ti1);
+  transaction_all_obs_pkgs(trans, q1, &ti1);
   queue_init_buffer(&ti2, ti2buf, sizeof(ti2buf)/sizeof(*ti2buf));
-  solver_transaction_all_pkgs(trans, q2, &ti2);
+  transaction_all_obs_pkgs(trans, q2, &ti2);
   for (i = 0; i < ti1.count; i++)
     for (j = 0; j < ti2.count; j++)
       if (ti1.elements[i] == ti2.elements[j])
@@ -647,7 +1090,7 @@ havescripts(Pool *pool, Id solvid)
            }
          if (!inpre)
            continue;
-         dep = id2str(pool, req);
+         dep = pool_id2str(pool, req);
          if (*dep == '/' && strcmp(dep, "/sbin/ldconfig") != 0)
            return 1;
        }
@@ -669,7 +1112,7 @@ addsolvableedges(struct orderdata *od, Solvable *s)
   int provbyinst;
 
 #if 0
-  printf("addsolvableedges %s\n", solvable2str(pool, s));
+  printf("addsolvableedges %s\n", pool_solvable2str(pool, s));
 #endif
   p = s - pool->solvables;
   queue_init(&reqq);
@@ -707,7 +1150,7 @@ addsolvableedges(struct orderdata *od, Solvable *s)
                {
                  provbyinst = 1;
 #if 0
-                 printf("IGNORE inst provides %s by %s\n", dep2str(pool, req), solvable2str(pool, s2));
+                 printf("IGNORE inst provides %s by %s\n", pool_dep2str(pool, req), pool_solvable2str(pool, s2));
                  reqq.count = 0;       /* provided by package that stays installed */
                  break;
 #else
@@ -755,7 +1198,7 @@ addsolvableedges(struct orderdata *od, Solvable *s)
                                  if (trans->transaction_installed[reqq.elements[i] - pool->installed->start] == reqq.elements[j])
                                    continue;   /* no self edge */
 #if 0
-                                 printf("add interrreq uninst->inst edge (%s -> %s -> %s)\n", solvid2str(pool, reqq.elements[i]), dep2str(pool, req), solvid2str(pool, reqq.elements[j]));
+                                 printf("add interrreq uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, reqq.elements[i]), pool_dep2str(pool, req), pool_solvid2str(pool, reqq.elements[j]));
 #endif
                                  addedge(od, reqq.elements[i], reqq.elements[j], pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
                                }
@@ -782,14 +1225,14 @@ addsolvableedges(struct orderdata *od, Solvable *s)
                  if (pool->solvables[p].repo != installed)
                    {
 #if 0
-                     printf("add inst->inst edge choice %d (%s -> %s -> %s)\n", choice, solvid2str(pool, p), dep2str(pool, req), solvid2str(pool, p2));
+                     printf("add inst->inst edge choice %d (%s -> %s -> %s)\n", choice, pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
 #endif
                      addedge(od, p, p2, pre);
                    }
                  else
                    {
 #if 0
-                     printf("add uninst->inst edge choice %d (%s -> %s -> %s)\n", choice, solvid2str(pool, p), dep2str(pool, req), solvid2str(pool, p2));
+                     printf("add uninst->inst edge choice %d (%s -> %s -> %s)\n", choice, pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
 #endif
                      addedge(od, p, p2, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
                    }
@@ -817,7 +1260,7 @@ addsolvableedges(struct orderdata *od, Solvable *s)
                    }
 #endif
 #if 0
-                 printf("add uninst->uninst edge choice %d (%s -> %s -> %s)\n", choice, solvid2str(pool, p), dep2str(pool, req), solvid2str(pool, p2));
+                 printf("add uninst->uninst edge choice %d (%s -> %s -> %s)\n", choice, pool_solvid2str(pool, p), pool_dep2str(pool, req), pool_solvid2str(pool, p2));
 #endif
                  addedge(od, p2, p, pre == TYPE_PREREQ ? TYPE_PREREQ_P : TYPE_REQ_P);
                }
@@ -841,6 +1284,9 @@ addsolvableedges(struct orderdata *od, Solvable *s)
                  if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
                    {
                      /* deinstall p before installing p2 */
+#if 0
+                     printf("add conflict uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p2), pool_dep2str(pool, con), pool_solvid2str(pool, p));
+#endif
                      addedge(od, p2, p, TYPE_CON);
                    }
                }
@@ -849,6 +1295,9 @@ addsolvableedges(struct orderdata *od, Solvable *s)
                  if (s2->repo == installed && MAPTST(&trans->transactsmap, p2))
                    {
                      /* deinstall p2 before installing p */
+#if 0
+                     printf("add conflict uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p), pool_dep2str(pool, con), pool_solvid2str(pool, p2));
+#endif
                      addedge(od, p, p2, TYPE_CON);
                    }
                }
@@ -856,6 +1305,33 @@ addsolvableedges(struct orderdata *od, Solvable *s)
            }
        }
     }
+  if (s->repo == installed && solvable_lookup_idarray(s, SOLVABLE_TRIGGERS, &reqq) && reqq.count)
+    {
+      /* we're getting deinstalled/updated. Try to do this before our
+       * triggers are hit */
+      for (i = 0; i < reqq.count; i++)
+       {
+         Id tri = reqq.elements[i];
+         FOR_PROVIDES(p2, pp2, tri)
+           {
+             if (p2 == p)
+               continue;
+             s2 = pool->solvables + p2;
+             if (!s2->repo)
+               continue;
+             if (s2->name == s->name)
+               continue;       /* obsoleted anyway */
+             if (s2->repo != installed && MAPTST(&trans->transactsmap, p2))
+               {
+                 /* deinstall/update p before installing p2 */
+#if 0
+                 printf("add trigger uninst->inst edge (%s -> %s -> %s)\n", pool_solvid2str(pool, p2), pool_dep2str(pool, tri), pool_solvid2str(pool, p));
+#endif
+                 addedge(od, p2, p, TYPE_CON);
+               }
+           }
+       }
+    }
   queue_free(&reqq);
 }
 
@@ -921,17 +1397,17 @@ breakcycle(struct orderdata *od, Id *cycle)
 
   /* cycle recorded, print it */
   if (ddegmin >= TYPE_REQ && (ddegmax & TYPE_PREREQ) != 0)
-    printf("CRITICAL ");
-  printf("cycle: --> ");
+    POOL_DEBUG(SOLV_DEBUG_STATS, "CRITICAL ");
+  POOL_DEBUG(SOLV_DEBUG_STATS, "cycle: --> ");
   for (k = 0; cycle[k + 1]; k += 2)
     {
       te = od->tes +  cycle[k];
       if ((od->edgedata[cycle[k + 1] + 1] & TYPE_BROKEN) != 0)
-        printf("%s ##%x##> ", solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
+        POOL_DEBUG(SOLV_DEBUG_STATS, "%s ##%x##> ", pool_solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
       else
-        printf("%s --%x--> ", solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
+        POOL_DEBUG(SOLV_DEBUG_STATS, "%s --%x--> ", pool_solvid2str(pool, te->p), od->edgedata[cycle[k + 1] + 1]);
     }
-  printf("\n");
+  POOL_DEBUG(SOLV_DEBUG_STATS, "\n");
 }
 
 static inline void
@@ -946,18 +1422,21 @@ dump_tes(struct orderdata *od)
   for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
     {
       Solvable *s = pool->solvables + te->p;
-      printf("TE %4d: %c%s\n", i, s->repo == pool->installed ? '-' : '+', solvable2str(pool, s));
+      POOL_DEBUG(SOLV_DEBUG_RESULT, "TE %4d: %c%s\n", i, s->repo == pool->installed ? '-' : '+', pool_solvable2str(pool, s));
       if (s->repo != pool->installed)
         {
          queue_empty(&obsq);
-         solver_transaction_all_pkgs(od->trans, te->p, &obsq);
+         transaction_all_obs_pkgs(od->trans, te->p, &obsq);
          for (j = 0; j < obsq.count; j++)
-           printf("         -%s\n", solvid2str(pool, obsq.elements[j]));
+           POOL_DEBUG(SOLV_DEBUG_RESULT, "         -%s\n", pool_solvid2str(pool, obsq.elements[j]));
        }
       for (j = te->edges; od->edgedata[j]; j += 2)
        {
          te2 = od->tes + od->edgedata[j];
-         printf("       --%x--> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], solvid2str(pool, te2->p));
+         if ((od->edgedata[j + 1] & TYPE_BROKEN) == 0)
+           POOL_DEBUG(SOLV_DEBUG_RESULT, "       --%x--> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], pool_solvid2str(pool, te2->p));
+         else
+           POOL_DEBUG(SOLV_DEBUG_RESULT, "       ##%x##> TE %4d: %s\n", od->edgedata[j + 1], od->edgedata[j], pool_solvid2str(pool, te2->p));
        }
     }
 }
@@ -1004,7 +1483,7 @@ addcycleedges(struct orderdata *od, Id *cycle, Queue *todo)
 #if 0
   printf("addcycleedges\n");
   for (i = 0; (j = cycle[i]) != 0; i++)
-    printf("cycle %s\n", solvid2str(pool, od->tes[j].p));
+    printf("cycle %s\n", pool_solvid2str(pool, od->tes[j].p));
 #endif
 
   /* first add all the tail cycle edges */
@@ -1052,7 +1531,7 @@ addcycleedges(struct orderdata *od, Id *cycle, Queue *todo)
          /* We found an edge to the cycle. Add an extra edge to the tail */
          /* the TE was not reachable, so we're not creating a new cycle! */
 #if 0
-         printf("adding TO TAIL cycle edge %d->%d %s->%s!\n", i, tail, solvid2str(pool, od->tes[i].p), solvid2str(pool, od->tes[tail].p));
+         printf("adding TO TAIL cycle edge %d->%d %s->%s!\n", i, tail, pool_solvid2str(pool, od->tes[i].p), pool_solvid2str(pool, od->tes[tail].p));
 #endif
          j -= te->edges;       /* in case we move */
          addteedge(od, i, tail, TYPE_CYCLETAIL);
@@ -1099,7 +1578,7 @@ addcycleedges(struct orderdata *od, Id *cycle, Queue *todo)
          if (od->tes[k].mark == -1)
            {
 #if 0
-             printf("adding FROM HEAD cycle edge %d->%d %s->%s [%s]!\n", head, k, solvid2str(pool, od->tes[head].p), solvid2str(pool, od->tes[k].p), solvid2str(pool, od->tes[cycle[i]].p));
+             printf("adding FROM HEAD cycle edge %d->%d %s->%s [%s]!\n", head, k, pool_solvid2str(pool, od->tes[head].p), pool_solvid2str(pool, od->tes[k].p), pool_solvid2str(pool, od->tes[cycle[i]].p));
 #endif
              addteedge(od, head, k, TYPE_CYCLEHEAD);
              od->tes[k].mark = -2;     /* no need to add that one again */
@@ -1115,39 +1594,42 @@ transaction_order(Transaction *trans, int flags)
   Pool *pool = trans->pool;
   Queue *tr = &trans->steps;
   Repo *installed = pool->installed;
-  Id type, p;
+  Id p;
   Solvable *s;
   int i, j, k, numte, numedge;
   struct orderdata od;
   struct _TransactionElement *te;
-  Queue todo, obsq;
+  Queue todo, obsq, samerepoq, uninstq;
   int cycstart, cycel;
-  Id *cycle, *obstypes;
+  Id *cycle;
   int oldcount;
   int start, now;
+  Repo *lastrepo;
+  int lastmedia;
+  Id *temedianr;
 
-  start = now = sat_timems(0);
-  POOL_DEBUG(SAT_DEBUG_STATS, "ordering transaction\n");
+  start = now = solv_timems(0);
+  POOL_DEBUG(SOLV_DEBUG_STATS, "ordering transaction\n");
   /* free old data if present */
   if (trans->orderdata)
     {
       struct _TransactionOrderdata *od = trans->orderdata;
-      od->tes = sat_free(od->tes);
-      od->invedgedata = sat_free(od->invedgedata);
-      trans->orderdata = sat_free(trans->orderdata);
+      od->tes = solv_free(od->tes);
+      od->invedgedata = solv_free(od->invedgedata);
+      trans->orderdata = solv_free(trans->orderdata);
     }
 
   /* create a transaction element for every active component */
   numte = 0;
-  for (i = 0; i < tr->count; i += 2)
+  for (i = 0; i < tr->count; i++)
     {
-      p = tr->elements[i + 1];
+      p = tr->elements[i];
       s = pool->solvables + p;
       if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
        continue;
       numte++;
     }
-  POOL_DEBUG(SAT_DEBUG_STATS, "transaction elements: %d\n", numte);
+  POOL_DEBUG(SOLV_DEBUG_STATS, "transaction elements: %d\n", numte);
   if (!numte)
     return;    /* nothing to do... */
 
@@ -1155,44 +1637,40 @@ transaction_order(Transaction *trans, int flags)
   memset(&od, 0, sizeof(od));
   od.trans = trans;
   od.ntes = numte;
-  od.tes = sat_calloc(numte, sizeof(*od.tes));
-  od.edgedata = sat_extend(0, 0, 1, sizeof(Id), EDGEDATA_BLOCK);
+  od.tes = solv_calloc(numte, sizeof(*od.tes));
+  od.edgedata = solv_extend(0, 0, 1, sizeof(Id), EDGEDATA_BLOCK);
   od.edgedata[0] = 0;
   od.nedgedata = 1;
   queue_init(&od.cycles);
 
-  for (i = 0, te = od.tes + 1; i < tr->count; i += 2)
+  /* initialize TEs */
+  for (i = 0, te = od.tes + 1; i < tr->count; i++)
     {
-      p = tr->elements[i + 1];
+      p = tr->elements[i];
       s = pool->solvables + p;
       if (installed && s->repo == installed && trans->transaction_installed[p - installed->start])
        continue;
       te->p = p;
-      te->type = tr->elements[i];
       te++;
     }
 
   /* create dependency graph */
-  for (i = 0; i < tr->count; i += 2)
-    {
-      type = tr->elements[i];
-      p = tr->elements[i + 1];
-      addsolvableedges(&od, pool->solvables + p);
-    }
+  for (i = 0; i < tr->count; i++)
+    addsolvableedges(&od, pool->solvables + tr->elements[i]);
 
   /* count edges */
   numedge = 0;
   for (i = 1, te = od.tes + i; i < numte; i++, te++)
     for (j = te->edges; od.edgedata[j]; j += 2)
       numedge++;
-  POOL_DEBUG(SAT_DEBUG_STATS, "edges: %d, edge space: %d\n", numedge, od.nedgedata / 2);
-  POOL_DEBUG(SAT_DEBUG_STATS, "edge creation took %d ms\n", sat_timems(now));
+  POOL_DEBUG(SOLV_DEBUG_STATS, "edges: %d, edge space: %d\n", numedge, od.nedgedata / 2);
+  POOL_DEBUG(SOLV_DEBUG_STATS, "edge creation took %d ms\n", solv_timems(now));
 
 #if 0
   dump_tes(&od);
 #endif
   
-  now = sat_timems(0);
+  now = solv_timems(0);
   /* kill all cycles */
   queue_init(&todo);
   for (i = numte - 1; i > 0; i--)
@@ -1270,10 +1748,10 @@ transaction_order(Transaction *trans, int flags)
       /* restart with start of cycle */
       todo.count = cycstart + 1;
     }
-  POOL_DEBUG(SAT_DEBUG_STATS, "cycles broken: %d\n", od.ncycles);
-  POOL_DEBUG(SAT_DEBUG_STATS, "cycle breaking took %d ms\n", sat_timems(now));
+  POOL_DEBUG(SOLV_DEBUG_STATS, "cycles broken: %d\n", od.ncycles);
+  POOL_DEBUG(SOLV_DEBUG_STATS, "cycle breaking took %d ms\n", solv_timems(now));
 
-  now = sat_timems(0);
+  now = solv_timems(0);
   /* now go through all broken cycles and create cycle edges to help
      the ordering */
    for (i = od.cycles.count - 3; i >= 0; i -= 3)
@@ -1286,11 +1764,14 @@ transaction_order(Transaction *trans, int flags)
        if (od.cycles.elements[i + 2] < TYPE_REQ)
          addcycleedges(&od, od.cyclesdata.elements + od.cycles.elements[i], &todo);
      }
-  POOL_DEBUG(SAT_DEBUG_STATS, "cycle edge creation took %d ms\n", sat_timems(now));
+  POOL_DEBUG(SOLV_DEBUG_STATS, "cycle edge creation took %d ms\n", solv_timems(now));
 
+#if 0
+  dump_tes(&od);
+#endif
   /* all edges are finally set up and there are no cycles, now the easy part.
    * Create an ordered transaction */
-  now = sat_timems(0);
+  now = solv_timems(0);
   /* first invert all edges */
   for (i = 1, te = od.tes + i; i < numte; i++, te++)
     te->mark = 1;      /* term 0 */
@@ -1309,8 +1790,8 @@ transaction_order(Transaction *trans, int flags)
       te->mark += j;
       j = te->mark;
     }
-  POOL_DEBUG(SAT_DEBUG_STATS, "invedge space: %d\n", j + 1);
-  od.invedgedata = sat_calloc(j + 1, sizeof(Id));
+  POOL_DEBUG(SOLV_DEBUG_STATS, "invedge space: %d\n", j + 1);
+  od.invedgedata = solv_calloc(j + 1, sizeof(Id));
   for (i = 1, te = od.tes + i; i < numte; i++, te++)
     {
       for (j = te->edges; od.edgedata[j]; j += 2)
@@ -1322,7 +1803,8 @@ transaction_order(Transaction *trans, int flags)
     }
   for (i = 1, te = od.tes + i; i < numte; i++, te++)
     te->edges = te->mark;      /* edges now points into invedgedata */
-  od.edgedata = sat_free(od.edgedata);
+  od.edgedata = solv_free(od.edgedata);
+  od.nedgedata = j + 1;
 
   /* now the final ordering */
   for (i = 1, te = od.tes + i; i < numte; i++, te++)
@@ -1330,100 +1812,229 @@ transaction_order(Transaction *trans, int flags)
   for (i = 1, te = od.tes + i; i < numte; i++, te++)
     for (j = te->edges; od.invedgedata[j]; j++)
       od.tes[od.invedgedata[j]].mark++;
+
+  queue_init(&samerepoq);
+  queue_init(&uninstq);
+  queue_empty(&todo);
   for (i = 1, te = od.tes + i; i < numte; i++, te++)
     if (te->mark == 0)
-      queue_push(&todo, i);
-  assert(todo.count > 0);
-  if (installed)
-    {
-      obstypes = sat_calloc(installed->end - installed->start, sizeof(Id));
-      for (i = 0; i < tr->count; i += 2)
-       {
-         p = tr->elements[i + 1];
-         s = pool->solvables + p;
-         if (s->repo == installed && trans->transaction_installed[p - installed->start])
-           obstypes[p - installed->start] = tr->elements[i];
-       }
-    }
-  else
-    obstypes = 0;
+      {
+       if (installed && pool->solvables[te->p].repo == installed)
+          queue_push(&uninstq, i);
+       else
+          queue_push(&todo, i);
+      }
+  assert(todo.count > 0 || uninstq.count > 0);
   oldcount = tr->count;
   queue_empty(tr);
+
   queue_init(&obsq);
   
-  while (todo.count)
+  lastrepo = 0;
+  lastmedia = 0;
+  temedianr = solv_calloc(numte, sizeof(Id));
+  for (i = 1; i < numte; i++)
     {
-      /* select an i */
-      i = todo.count;
-      if (installed)
+      Solvable *s = pool->solvables + od.tes[i].p;
+      if (installed && s->repo == installed)
+       j = 1;
+      else
+        j = solvable_lookup_num(s, SOLVABLE_MEDIANR, 1);
+      temedianr[i] = j;
+    }
+  for (;;)
+    {
+      /* select an TE i */
+      if (uninstq.count)
+       i = queue_shift(&uninstq);
+      else if (samerepoq.count)
+       i = queue_shift(&samerepoq);
+      else if (todo.count)
        {
-         for (i = 0; i < todo.count; i++)
+         /* find next repo/media */
+         for (j = 0; j < todo.count; j++)
            {
-             j = todo.elements[i];
-             if (pool->solvables[od.tes[j].p].repo == installed)
-               break;
+             if (!j || temedianr[todo.elements[j]] < lastmedia)
+               {
+                 i = j;
+                 lastmedia = temedianr[todo.elements[j]];
+               }
            }
+         lastrepo = pool->solvables[od.tes[todo.elements[i]].p].repo;
+
+         /* move all matching TEs to samerepoq */
+         for (i = j = 0; j < todo.count; j++)
+           {
+             int k = todo.elements[j];
+             if (temedianr[k] == lastmedia && pool->solvables[od.tes[k].p].repo == lastrepo)
+               queue_push(&samerepoq, k);
+             else
+               todo.elements[i++] = k;
+           }
+         todo.count = i;
+
+         assert(samerepoq.count);
+         i = queue_shift(&samerepoq);
        }
-      if (i == todo.count)
-       i = 0;
-      te = od.tes + todo.elements[i];
-      queue_push(tr, te->type);
+      else
+       break;
+
+      te = od.tes + i;
       queue_push(tr, te->p);
 #if 0
-printf("do %s\n", solvid2str(pool, te->p));
+printf("do %s [%d]\n", pool_solvid2str(pool, te->p), temedianr[i]);
 #endif
       s = pool->solvables + te->p;
-      if (installed && s->repo != installed)
-       {
-         queue_empty(&obsq);
-         solver_transaction_all_pkgs(trans, te->p, &obsq);
-         for (j = 0; j < obsq.count; j++)
-           {
-             p = obsq.elements[j];
-             assert(p >= installed->start && p < installed->end);
-             if (obstypes[p - installed->start])
-               {
-                 queue_push(tr, obstypes[p - installed->start]);
-                 queue_push(tr, p);
-                 obstypes[p - installed->start] = 0;
-               }
-           }
-       }
-      queue_delete(&todo, i);
       for (j = te->edges; od.invedgedata[j]; j++)
        {
          struct _TransactionElement *te2 = od.tes + od.invedgedata[j];
          assert(te2->mark > 0);
          if (--te2->mark == 0)
            {
-             queue_push(&todo, od.invedgedata[j]);
+             Solvable *s = pool->solvables + te2->p;
 #if 0
-printf("free %s\n", solvid2str(pool, od.tes[od.invedgedata[j]].p));
+printf("free %s [%d]\n", pool_solvid2str(pool, te2->p), temedianr[od.invedgedata[j]]);
 #endif
+             if (installed && s->repo == installed)
+               queue_push(&uninstq, od.invedgedata[j]);
+             else if (s->repo == lastrepo && temedianr[od.invedgedata[j]] == lastmedia)
+               queue_push(&samerepoq, od.invedgedata[j]);
+             else
+               queue_push(&todo, od.invedgedata[j]);
            }
        }
     }
-  sat_free(obstypes);
+  solv_free(temedianr);
   queue_free(&todo);
+  queue_free(&samerepoq);
+  queue_free(&uninstq);
   queue_free(&obsq);
   for (i = 1, te = od.tes + i; i < numte; i++, te++)
     assert(te->mark == 0);
+
+  /* add back obsoleted packages */
+  transaction_add_obsoleted(trans);
   assert(tr->count == oldcount);
-  POOL_DEBUG(SAT_DEBUG_STATS, "creating new transaction took %d ms\n", sat_timems(now));
-  POOL_DEBUG(SAT_DEBUG_STATS, "transaction ordering took %d ms\n", sat_timems(start));
+
+  POOL_DEBUG(SOLV_DEBUG_STATS, "creating new transaction took %d ms\n", solv_timems(now));
+  POOL_DEBUG(SOLV_DEBUG_STATS, "transaction ordering took %d ms\n", solv_timems(start));
 
   if ((flags & SOLVER_TRANSACTION_KEEP_ORDERDATA) != 0)
     {
-      trans->orderdata = sat_calloc(1, sizeof(*trans->orderdata));
+      trans->orderdata = solv_calloc(1, sizeof(*trans->orderdata));
       trans->orderdata->tes = od.tes;
       trans->orderdata->ntes = numte;
       trans->orderdata->invedgedata = od.invedgedata;
+      trans->orderdata->ninvedgedata = od.nedgedata;
     }
   else
     {
-      sat_free(od.tes);
-      sat_free(od.invedgedata);
+      solv_free(od.tes);
+      solv_free(od.invedgedata);
     }
+  queue_free(&od.cycles);
+  queue_free(&od.cyclesdata);
+}
+
+
+int
+transaction_order_add_choices(Transaction *trans, Id chosen, Queue *choices)
+{
+  int i, j;
+  struct _TransactionOrderdata *od = trans->orderdata;
+  struct _TransactionElement *te;
+
+  if (!od)
+     return choices->count;
+  if (!chosen)
+    {
+      /* initialization step */
+      for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
+       te->mark = 0;
+      for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
+       {
+         for (j = te->edges; od->invedgedata[j]; j++)
+           od->tes[od->invedgedata[j]].mark++;
+       }
+      for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
+       if (!te->mark)
+         queue_push(choices, te->p);
+      return choices->count;
+    }
+  for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
+    if (te->p == chosen)
+      break;
+  if (i == od->ntes)
+    return choices->count;
+  if (te->mark > 0)
+    {
+      /* hey! out-of-order installation! */
+      te->mark = -1;
+    }
+  for (j = te->edges; od->invedgedata[j]; j++)
+    {
+      te = od->tes + od->invedgedata[j];
+      assert(te->mark > 0 || te->mark == -1);
+      if (te->mark > 0 && --te->mark == 0)
+       queue_push(choices, te->p);
+    }
+  return choices->count;
+}
+
+void
+transaction_add_obsoleted(Transaction *trans)
+{
+  Pool *pool = trans->pool;
+  Repo *installed = pool->installed;
+  Id p;
+  Solvable *s;
+  int i, j, k, max;
+  Map done;
+  Queue obsq, *steps;
+
+  if (!installed || !trans->steps.count)
+    return;
+  /* calculate upper bound */
+  max = 0;
+  FOR_REPO_SOLVABLES(installed, p, s)
+    if (MAPTST(&trans->transactsmap, p))
+      max++;
+  if (!max)
+    return;
+  /* make room */
+  steps = &trans->steps;
+  queue_insertn(steps, 0, max, 0);
+
+  /* now add em */
+  map_init(&done, installed->end - installed->start);
+  queue_init(&obsq);
+  for (j = 0, i = max; i < steps->count; i++)
+    {
+      p = trans->steps.elements[i];
+      if (pool->solvables[p].repo == installed)
+       {
+         if (!trans->transaction_installed[p - pool->installed->start])
+           trans->steps.elements[j++] = p;
+         continue;
+       }
+      trans->steps.elements[j++] = p;
+      queue_empty(&obsq);
+      transaction_all_obs_pkgs(trans, p, &obsq);
+      for (k = 0; k < obsq.count; k++)
+       {
+         p = obsq.elements[k];
+         assert(p >= installed->start && p < installed->end);
+         if (MAPTST(&done, p - installed->start))
+           continue;
+         MAPSET(&done, p - installed->start);
+         trans->steps.elements[j++] = p;
+       }
+    }
+
+  /* free unneeded space */
+  queue_truncate(steps, j);
+  map_free(&done);
+  queue_free(&obsq);
 }
 
 static void
@@ -1439,7 +2050,7 @@ transaction_check_pkg(Transaction *trans, Id tepkg, Id pkg, Map *ins, Map *seen,
   MAPSET(seen, pkg);
   s = pool->solvables + pkg;
 #if 0
-  printf("- %*s%c%s\n", depth * 2, "", s->repo == pool->installed ? '-' : '+', solvable2str(pool, s));
+  printf("- %*s%c%s\n", depth * 2, "", s->repo == pool->installed ? '-' : '+', pool_solvable2str(pool, s));
 #endif
   if (s->requires)
     {
@@ -1455,7 +2066,7 @@ transaction_check_pkg(Transaction *trans, Id tepkg, Id pkg, Map *ins, Map *seen,
            }
          if (onlyprereq && !inpre)
            continue;
-         if (!strncmp(id2str(pool, req), "rpmlib(", 7))
+         if (!strncmp(pool_id2str(pool, req), "rpmlib(", 7))
            continue;
          good = 0;
          /* first check kept packages, then freshly installed, then not yet uninstalled */
@@ -1492,32 +2103,31 @@ transaction_check_pkg(Transaction *trans, Id tepkg, Id pkg, Map *ins, Map *seen,
            }
          if (!good)
            {
-             printf("  %c%s: nothing provides %s needed by %c%s\n", pool->solvables[tepkg].repo == pool->installed ? '-' : '+', solvid2str(pool, tepkg), dep2str(pool, req), s->repo == pool->installed ? '-' : '+', solvable2str(pool, s));
+             POOL_DEBUG(SOLV_DEBUG_RESULT, "  %c%s: nothing provides %s needed by %c%s\n", pool->solvables[tepkg].repo == pool->installed ? '-' : '+', pool_solvid2str(pool, tepkg), pool_dep2str(pool, req), s->repo == pool->installed ? '-' : '+', pool_solvable2str(pool, s));
            }
        }
     }
 }
 
 void
-transaction_check(Transaction *trans)
+transaction_check_order(Transaction *trans)
 {
   Pool *pool = trans->pool;
   Solvable *s;
-  Id p, type, lastins;
+  Id p, lastins;
   Map ins, seen;
   int i;
 
-  printf("\nchecking transaction order...\n");
+  POOL_DEBUG(SOLV_DEBUG_RESULT, "\nchecking transaction order...\n");
   map_init(&ins, pool->nsolvables);
   map_init(&seen, pool->nsolvables);
   if (pool->installed)
     FOR_REPO_SOLVABLES(pool->installed, p, s)
       MAPSET(&ins, p);
   lastins = 0;
-  for (i = 0; i < trans->steps.count; i += 2)
+  for (i = 0; i < trans->steps.count; i++)
     {
-      type = trans->steps.elements[i];
-      p = trans->steps.elements[i + 1];
+      p = trans->steps.elements[i];
       s = pool->solvables + p;
       if (s->repo != pool->installed)
        lastins = p;
@@ -1533,55 +2143,5 @@ transaction_check(Transaction *trans)
     }
   map_free(&seen);
   map_free(&ins);
-  printf("transaction order check done.\n");
-}
-
-int
-transaction_add_choices(Transaction *trans, Queue *choices, Id chosen)
-{
-  int i, j;
-  struct _TransactionOrderdata *od = trans->orderdata;
-  struct _TransactionElement *te;
-
-  if (!od)
-     return choices->count;
-  if (!chosen)
-    {
-      /* initialization step */
-      for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
-       te->mark = 0;
-      for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
-       {
-         for (j = te->edges; od->invedgedata[j]; j++)
-           od->tes[od->invedgedata[j]].mark++;
-       }
-      for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
-       if (!te->mark)
-         {
-           queue_push(choices, te->type);
-           queue_push(choices, te->p);
-         }
-      return choices->count;
-    }
-  for (i = 1, te = od->tes + i; i < od->ntes; i++, te++)
-    if (te->p == chosen)
-      break;
-  if (i == od->ntes)
-    return choices->count;
-  if (te->mark > 0)
-    {
-      /* hey! out-of-order installation! */
-      te->mark = -1;
-    }
-  for (j = te->edges; od->invedgedata[j]; j++)
-    {
-      te = od->tes + od->invedgedata[j];
-      assert(te->mark > 0 || te->mark == -1);
-      if (te->mark > 0 && --te->mark == 0)
-       {
-         queue_push(choices, te->type);
-         queue_push(choices, te->p);
-       }
-    }
-  return choices->count;
+  POOL_DEBUG(SOLV_DEBUG_RESULT, "transaction order check done.\n");
 }