2 * Copyright (c) 2007, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
11 * The pool contains information about solvables
12 * stored optimized for memory consumption and fast retrieval.
24 #include "poolid_private.h"
29 #define SOLVABLE_BLOCK 255
31 // reset all whatprovides
34 pool_freewhatprovides(Pool *pool)
36 pool->whatprovides = xfree(pool->whatprovides);
37 pool->whatprovidesdata = xfree(pool->whatprovidesdata);
38 pool->whatprovidesdataoff = 0;
39 pool->whatprovidesdataleft = 0;
43 // list of string constants, so we can do pointer/Id instead of string comparison
44 // index into array matches ID_xxx constants in pool.h
46 static const char *initpool_data[] = {
57 "solvable:recommends",
59 "solvable:supplements",
62 "rpm:dbid", /* direct key into rpmdb */
63 "solvable:prereqmarker",
64 "solvable:filemarker",
65 "namespace:installed",
82 pool = (Pool *)xcalloc(1, sizeof(*pool));
84 stringpool_init (&pool->ss, initpool_data);
86 // pre-alloc space for a RelDep
87 pool->rels = (Reldep *)xcalloc(1 + REL_BLOCK, sizeof(Reldep));
90 // pre-alloc space for a Solvable
91 pool->solvables = (Solvable *)xcalloc(SOLVABLE_BLOCK + 1, sizeof(Solvable));
93 queue_init(&pool->vendormap);
94 s = pool->solvables + SYSTEMSOLVABLE;
95 s->name = SYSTEM_SYSTEM;
96 s->arch = ARCH_NOARCH;
99 pool->debugmask = SAT_DEBUG_RESULT; /* FIXME */
107 pool_free(Pool *pool)
111 pool_freewhatprovides(pool);
112 pool_freeidhashes(pool);
113 repo_freeallrepos(pool, 1);
114 xfree(pool->id2arch);
115 xfree(pool->solvables);
116 xfree(pool->ss.stringspace);
117 xfree(pool->ss.strings);
119 queue_free(&pool->vendormap);
120 for (i = 0; i < DEP2STRBUF; i++)
121 xfree(pool->dep2strbuf[i]);
126 pool_add_solvable(Pool *pool)
128 if ((pool->nsolvables & SOLVABLE_BLOCK) == 0)
129 pool->solvables = xrealloc2(pool->solvables, pool->nsolvables + (SOLVABLE_BLOCK + 1), sizeof(Solvable));
130 memset(pool->solvables + pool->nsolvables, 0, sizeof(Solvable));
131 return pool->nsolvables++;
135 pool_add_solvable_block(Pool *pool, int count)
137 Id nsolvables = pool->nsolvables;
140 if (((nsolvables - 1) | SOLVABLE_BLOCK) != ((nsolvables + count - 1) | SOLVABLE_BLOCK))
141 pool->solvables = xrealloc2(pool->solvables, (nsolvables + count + SOLVABLE_BLOCK) & ~SOLVABLE_BLOCK, sizeof(Solvable));
142 memset(pool->solvables + nsolvables, 0, sizeof(Solvable) * count);
143 pool->nsolvables += count;
148 pool_free_solvable_block(Pool *pool, Id start, int count, int reuseids)
152 if (reuseids && start + count == pool->nsolvables)
154 /* might want to shrink solvable array */
155 pool->nsolvables = start;
158 memset(pool->solvables + start, 0, sizeof(Solvable) * count);
163 solvable2str(Pool *pool, Solvable *s)
165 int l, nn = pool->dep2strn;
166 const char *n, *e, *a;
167 n = id2str(pool, s->name);
168 e = id2str(pool, s->evr);
169 a = id2str(pool, s->arch);
170 l = strlen(n) + strlen(e) + strlen(a) + 3;
171 if (l > pool->dep2strlen[nn])
173 pool->dep2strbuf[nn] = xrealloc(pool->dep2strbuf[nn], l + 32);
174 pool->dep2strlen[nn] = l + 32;
176 sprintf(pool->dep2strbuf[nn], "%s-%s.%s", n, e, a);
177 pool->dep2strn = (nn + 1) % DEP2STRBUF;
178 return pool->dep2strbuf[nn];
181 static Pool *pool_shrink_whatprovides_sortcmp_data;
184 pool_shrink_whatprovides_sortcmp(const void *ap, const void *bp)
187 Pool *pool = pool_shrink_whatprovides_sortcmp_data;
189 oa = pool->whatprovides[*(Id *)ap];
190 ob = pool->whatprovides[*(Id *)bp];
192 return *(Id *)ap - *(Id *)bp;
197 da = pool->whatprovidesdata + oa;
198 db = pool->whatprovidesdata + ob;
200 if ((r = (*da++ - *db++)) != 0)
204 return *(Id *)ap - *(Id *)bp;
208 pool_shrink_whatprovides(Pool *pool)
212 Id lastid, *last, *dp, *lp;
216 if (pool->ss.nstrings < 3)
218 sorted = xmalloc2(pool->ss.nstrings, sizeof(Id));
219 for (id = 0; id < pool->ss.nstrings; id++)
221 pool_shrink_whatprovides_sortcmp_data = pool;
222 qsort(sorted + 1, pool->ss.nstrings - 1, sizeof(Id), pool_shrink_whatprovides_sortcmp);
225 for (i = 1; i < pool->ss.nstrings; i++)
228 o = pool->whatprovides[id];
229 if (o == 0 || o == 1)
231 dp = pool->whatprovidesdata + o;
245 pool->whatprovides[id] = -lastid;
249 last = pool->whatprovidesdata + o;
253 dp = pool->whatprovidesdata + 2;
254 for (id = 1; id < pool->ss.nstrings; id++)
256 o = pool->whatprovides[id];
257 if (o == 0 || o == 1)
264 pool->whatprovides[id] = pool->whatprovides[i];
267 lp = pool->whatprovidesdata + o;
270 pool->whatprovides[id] = dp - pool->whatprovidesdata;
271 while ((*dp++ = *lp++) != 0)
274 o = dp - pool->whatprovidesdata;
275 POOL_DEBUG(SAT_DEBUG_STATS, "shrunk whatprovidesdata from %d to %d\n", pool->whatprovidesdataoff, o);
276 if (pool->whatprovidesdataoff == o)
278 r = pool->whatprovidesdataoff - o;
279 pool->whatprovidesdataoff = o;
280 pool->whatprovidesdata = xrealloc(pool->whatprovidesdata, (o + pool->whatprovidesdataleft) * sizeof(Id));
281 if (r > pool->whatprovidesdataleft)
282 r = pool->whatprovidesdataleft;
283 memset(pool->whatprovidesdata + o, 0, r * sizeof(Id));
288 * pool_createwhatprovides()
290 * create hashes over complete pool to ease lookups
295 pool_createwhatprovides(Pool *pool)
297 int i, num, np, extra;
302 Offset *whatprovides;
303 Id *whatprovidesdata, *d;
305 POOL_DEBUG(SAT_DEBUG_STATS, "number of solvables: %d\n", pool->nsolvables);
306 POOL_DEBUG(SAT_DEBUG_STATS, "number of ids: %d + %d\n", pool->ss.nstrings, pool->nrels);
308 pool_freeidhashes(pool);
309 pool_freewhatprovides(pool);
310 num = pool->ss.nstrings + pool->nrels;
311 whatprovides = (Offset *)xcalloc(num, sizeof(Offset));
313 /* count providers for each name */
314 for (i = 1; i < pool->nsolvables; i++)
317 s = pool->solvables + i;
320 if (!pool_installable(pool, s))
322 pp = s->repo->idarraydata + s->provides;
323 while ((id = *pp++) != ID_NULL)
327 Reldep *rd = GETRELDEP(pool, id);
330 whatprovides[id]++; /* inc count of providers */
334 off = 2; /* first entry is undef, second is empty list */
336 np = 0; /* number of names provided */
337 for (i = 0; i < num; i++, idp++)
340 if (!n) /* no providers */
342 *idp = off; /* move from counts to offsets into whatprovidesdata */
343 off += n + 1; /* make space for all providers + terminating ID_NULL */
344 np++; /* inc # of provider 'slots' */
347 POOL_DEBUG(SAT_DEBUG_STATS, "provide ids: %d\n", np);
348 extra = 2 * pool->nrels;
353 POOL_DEBUG(SAT_DEBUG_STATS, "provide space needed: %d + %d\n", off, extra);
355 /* alloc space for all providers + extra */
356 whatprovidesdata = (Id *)xcalloc(off + extra, sizeof(Id));
358 /* now fill data for all provides */
359 for (i = 1; i < pool->nsolvables; i++)
362 s = pool->solvables + i;
365 if (!pool_installable(pool, s))
368 /* for all provides of this solvable */
369 pp = s->repo->idarraydata + s->provides;
370 while ((id = *pp++) != 0)
374 Reldep *rd = GETRELDEP(pool, id);
377 d = whatprovidesdata + whatprovides[id]; /* offset into whatprovidesdata */
381 while (*d) /* find free slot */
386 *d = i; /* put solvable Id into data */
389 pool->whatprovides = whatprovides;
390 pool->whatprovidesdata = whatprovidesdata;
391 pool->whatprovidesdataoff = off;
392 pool->whatprovidesdataleft = extra;
393 pool_shrink_whatprovides(pool);
397 /******************************************************************************/
400 * pool_queuetowhatprovides
402 * on-demand filling of provider information
403 * move queue data into whatprovidesdata
405 * returns: Offset into whatprovides
409 pool_queuetowhatprovides(Pool *pool, Queue *q)
412 int count = q->count;
414 if (count == 0) /* queue empty -> ID_EMPTY */
417 /* extend whatprovidesdata if needed, +1 for ID_NULL-termination */
418 if (pool->whatprovidesdataleft < count + 1)
420 POOL_DEBUG(SAT_DEBUG_STATS, "growing provides hash data...\n");
421 pool->whatprovidesdata = (Id *)xrealloc(pool->whatprovidesdata, (pool->whatprovidesdataoff + count + 4096) * sizeof(Id));
422 pool->whatprovidesdataleft = count + 4096;
425 /* copy queue to next free slot */
426 off = pool->whatprovidesdataoff;
427 memcpy(pool->whatprovidesdata + pool->whatprovidesdataoff, q->elements, count * sizeof(Id));
429 /* adapt count and ID_NULL-terminate */
430 pool->whatprovidesdataoff += count;
431 pool->whatprovidesdata[pool->whatprovidesdataoff++] = ID_NULL;
432 pool->whatprovidesdataleft -= count + 1;
438 /*************************************************************************/
443 * add packages fulfilling the relation to whatprovides array
444 * no exact providers, do range match
449 pool_addrelproviders(Pool *pool, Id d)
451 Reldep *rd = GETRELDEP(pool, d);
457 int flags = rd->flags;
459 Id p, *pp, *pp2, *pp3;
461 d = GETRELID(pool, d);
462 queue_init_buffer(&plist, buf, sizeof(buf)/sizeof(*buf));
467 pp = pool_whatprovides(pool, name);
468 pp2 = pool_whatprovides(pool, evr);
469 while ((p = *pp++) != 0)
471 for (pp3 = pp2; *pp3;)
474 queue_push(&plist, p);
480 pp = pool_whatprovides(pool, name);
481 while ((p = *pp++) != 0)
482 queue_push(&plist, p);
483 pp = pool_whatprovides(pool, evr);
484 while ((p = *pp++) != 0)
485 queue_pushunique(&plist, p);
488 if (pool->nscallback)
490 p = pool->nscallback(pool, pool->nscallbackdata, name, evr);
494 pool->whatprovides[d] = p;
495 return pool->whatprovidesdata + p;
498 queue_push(&plist, SYSTEMSOLVABLE);
505 /* convert to whatprovides id */
507 POOL_DEBUG(DEBUG_1, "addrelproviders: what provides %s?\n", id2str(pool, name));
509 if (flags && flags < 8)
511 FOR_PROVIDES(p, pp, name)
514 POOL_DEBUG(DEBUG_1, "addrelproviders: checking package %s\n", id2str(pool, pool->p[p].name));
516 /* solvable p provides name in some rels */
517 pidp = pool->solvables[p].repo->idarraydata + pool->solvables[p].provides;
518 while ((pid = *pidp++) != 0)
524 break; /* yes, provides all versions */
526 continue; /* wrong provides name */
527 prd = GETRELDEP(pool, pid);
528 if (prd->name != name)
529 continue; /* wrong provides name */
530 /* right package, both deps are rels */
534 if (flags == 7 || pflags == 7)
535 break; /* included */
536 if ((pflags & flags & 5) != 0)
537 break; /* same direction, match */
541 if ((pflags & flags & 2) != 0)
542 break; /* both have =, match */
546 int f = flags == 5 ? 5 : flags == 2 ? pflags : (flags ^ 5) & (pflags | 5);
547 if ((f & (1 << (1 + evrcmp(pool, pevr, evr)))) != 0)
552 continue; /* no rel match */
553 queue_push(&plist, p);
555 /* make our system solvable provide all unknown rpmlib() stuff */
556 if (plist.count == 0 && !strncmp(id2str(pool, name), "rpmlib(", 7))
557 queue_push(&plist, SYSTEMSOLVABLE);
559 /* add providers to whatprovides */
561 POOL_DEBUG(DEBUG_1, "addrelproviders: adding %d packages to %d\n", plist.count, d);
563 pool->whatprovides[d] = pool_queuetowhatprovides(pool, &plist);
566 return pool->whatprovidesdata + pool->whatprovides[d];
569 /*************************************************************************/
572 pool_debug(Pool *pool, int type, const char *format, ...)
577 if ((type & SAT_FATAL) == 0)
579 if ((pool->debugmask & type) == 0)
582 va_start(args, format);
583 if (!pool->debugcallback)
585 if ((type & (SAT_FATAL|SAT_ERROR)) == 0)
586 vprintf(format, args);
588 vfprintf(stderr, format, args);
591 vsnprintf(buf, sizeof(buf), format, args);
592 pool->debugcallback(pool, pool->debugcallbackdata, type, buf);
596 pool_setdebuglevel(Pool *pool, int level)
598 int mask = SAT_DEBUG_RESULT;
600 mask |= SAT_DEBUG_STATS|SAT_DEBUG_ANALYZE|SAT_DEBUG_UNSOLVABLE;
602 mask |= SAT_DEBUG_JOB|SAT_DEBUG_SOLUTIONS|SAT_DEBUG_POLICY;
604 mask |= SAT_DEBUG_PROPAGATE;
606 mask |= SAT_DEBUG_RULE_CREATION;
608 mask |= SAT_DEBUG_SCHUBI;
609 pool->debugmask = mask;