4 * The pool contains information about solvables
5 * stored optimized for memory consumption and fast retrieval.
15 #include "poolid_private.h"
21 // reset all whatprovides
24 pool_freewhatprovides(Pool *pool)
26 pool->whatprovides = xfree(pool->whatprovides);
27 pool->whatprovidesdata = xfree(pool->whatprovidesdata);
28 pool->whatprovidesdataoff = 0;
29 pool->whatprovidesdataleft = 0;
33 // list of string constants, so we can do pointer/Id instead of string comparison
34 // index into array matches ID_xxx constants in pool.h
36 static char *initpool_data[] = {
47 "solvable:recommends",
49 "solvable:supplements",
52 "rpm:dbid", /* direct key into rpmdb */
53 "solvable:prereqmarker",
54 "solvable:filemarker",
55 "namespace:installed",
68 int count, totalsize = 0;
72 pool = (Pool *)xcalloc(1, sizeof(*pool));
74 // count number and total size of predefined strings
75 for (count = 0; count < sizeof(initpool_data)/sizeof(*initpool_data); count++)
76 totalsize += strlen(initpool_data[count]) + 1;
78 // alloc appropriate space
79 pool->stringspace = (char *)xmalloc((totalsize + STRINGSPACE_BLOCK) & ~STRINGSPACE_BLOCK);
80 pool->strings = (Offset *)xmalloc(((count + STRING_BLOCK) & ~STRING_BLOCK) * sizeof(Offset));
82 // now copy predefined strings into allocated space
84 for (count = 0; count < sizeof(initpool_data)/sizeof(*initpool_data); count++)
86 strcpy(pool->stringspace + pool->sstrings, initpool_data[count]);
87 pool->strings[count] = pool->sstrings;
88 pool->sstrings += strlen(initpool_data[count]) + 1;
90 pool->nstrings = count;
92 // pre-alloc space for a RelDep
93 pool->rels = (Reldep *)xcalloc(1 + REL_BLOCK, sizeof(Reldep));
96 // pre-alloc space for a Solvable
97 pool->solvables = (Solvable *)xcalloc(2, sizeof(Solvable));
99 queue_init(&pool->vendormap);
100 s = pool->solvables + SYSTEMSOLVABLE;
101 s->name = SYSTEM_SYSTEM;
102 s->arch = ARCH_NOARCH;
111 pool_free(Pool *pool)
116 pool_freewhatprovides(pool);
117 pool_freeidhashes(pool);
118 for (i = 0; i < pool->nrepos; i++)
120 repo = pool->repos[i];
121 xfree(repo->idarraydata);
122 xfree(repo->rpmdbid);
125 xfree(pool->id2arch);
126 xfree(pool->solvables);
128 xfree(pool->stringspace);
129 xfree(pool->strings);
131 queue_free(&pool->vendormap);
132 for (i = 0; i < DEP2STRBUF; i++)
133 xfree(pool->dep2strbuf[i]);
138 static Pool *pool_shrink_whatprovides_sortcmp_data;
141 pool_shrink_whatprovides_sortcmp(const void *ap, const void *bp)
144 Pool *pool = pool_shrink_whatprovides_sortcmp_data;
146 oa = pool->whatprovides[*(Id *)ap];
147 ob = pool->whatprovides[*(Id *)bp];
149 return *(Id *)ap - *(Id *)bp;
154 da = pool->whatprovidesdata + oa;
155 db = pool->whatprovidesdata + ob;
157 if ((r = (*da++ - *db++)) != 0)
161 return *(Id *)ap - *(Id *)bp;
165 pool_shrink_whatprovides(Pool *pool)
169 Id lastid, *last, *dp, *lp;
173 if (pool->nstrings < 3)
175 sorted = xmalloc2(pool->nstrings, sizeof(Id));
176 for (id = 0; id < pool->nstrings; id++)
178 pool_shrink_whatprovides_sortcmp_data = pool;
179 qsort(sorted + 1, pool->nstrings - 1, sizeof(Id), pool_shrink_whatprovides_sortcmp);
182 for (i = 1; i < pool->nstrings; i++)
185 o = pool->whatprovides[id];
186 if (o == 0 || o == 1)
188 dp = pool->whatprovidesdata + o;
202 pool->whatprovides[id] = -lastid;
206 last = pool->whatprovidesdata + o;
210 dp = pool->whatprovidesdata + 2;
211 for (id = 1; id < pool->nstrings; id++)
213 o = pool->whatprovides[id];
214 if (o == 0 || o == 1)
221 pool->whatprovides[id] = pool->whatprovides[i];
224 lp = pool->whatprovidesdata + o;
227 pool->whatprovides[id] = dp - pool->whatprovidesdata;
228 while ((*dp++ = *lp++) != 0)
231 o = dp - pool->whatprovidesdata;
233 printf("shrunk whatprovidesdata from %d to %d\n", pool->whatprovidesdataoff, o);
234 if (pool->whatprovidesdataoff == o)
236 r = pool->whatprovidesdataoff - o;
237 pool->whatprovidesdataoff = o;
238 pool->whatprovidesdata = xrealloc(pool->whatprovidesdata, (o + pool->whatprovidesdataleft) * sizeof(Id));
239 if (r > pool->whatprovidesdataleft)
240 r = pool->whatprovidesdataleft;
241 memset(pool->whatprovidesdata + o, 0, r * sizeof(Id));
248 * create hashes over complete pool to ease lookups
253 pool_prepare(Pool *pool)
255 int i, num, np, extra;
260 Offset *whatprovides;
261 Id *whatprovidesdata, *d;
264 printf("number of solvables: %d\n", pool->nsolvables);
266 printf("number of ids: %d + %d\n", pool->nstrings, pool->nrels);
268 pool_freeidhashes(pool);
269 pool_freewhatprovides(pool);
270 num = pool->nstrings + pool->nrels;
271 whatprovides = (Offset *)xcalloc(num, sizeof(Offset));
273 /* count providers for each name */
274 for (i = 1; i < pool->nsolvables; i++)
277 s = pool->solvables + i;
280 if (!pool_installable(pool, s))
282 pp = s->repo->idarraydata + s->provides;
283 while ((id = *pp++) != ID_NULL)
287 Reldep *rd = GETRELDEP(pool, id);
290 whatprovides[id]++; /* inc count of providers */
294 off = 2; /* first entry is undef, second is empty list */
296 np = 0; /* number of names provided */
297 for (i = 0; i < num; i++, idp++)
300 if (!n) /* no providers */
302 *idp = off; /* move from counts to offsets into whatprovidesdata */
303 off += n + 1; /* make space for all providers + terminating ID_NULL */
304 np++; /* inc # of provider 'slots' */
308 printf("provide ids: %d\n", np);
309 extra = 2 * pool->nrels;
315 printf("provide space needed: %d + %d\n", off, extra);
317 /* alloc space for all providers + extra */
318 whatprovidesdata = (Id *)xcalloc(off + extra, sizeof(Id));
320 /* now fill data for all provides */
321 for (i = 1; i < pool->nsolvables; i++)
324 s = pool->solvables + i;
327 if (!pool_installable(pool, s))
330 /* for all provides of this solvable */
331 pp = s->repo->idarraydata + s->provides;
332 while ((id = *pp++) != 0)
336 Reldep *rd = GETRELDEP(pool, id);
339 d = whatprovidesdata + whatprovides[id]; /* offset into whatprovidesdata */
343 while (*d) /* find free slot */
348 if (pool->verbose) printf("duplicate entry for %s in package %s.%s\n", id2str(pool, id), id2str(pool, s->name), id2str(pool, s->arch));
353 *d = i; /* put solvable Id into data */
356 pool->whatprovides = whatprovides;
357 pool->whatprovidesdata = whatprovidesdata;
358 pool->whatprovidesdataoff = off;
359 pool->whatprovidesdataleft = extra;
360 pool_shrink_whatprovides(pool);
364 /******************************************************************************/
367 * pool_queuetowhatprovides
369 * on-demand filling of provider information
370 * move queue data into whatprovidesdata
372 * returns: Offset into whatprovides
376 pool_queuetowhatprovides(Pool *pool, Queue *q)
379 int count = q->count;
381 if (count == 0) /* queue empty -> ID_EMPTY */
384 /* extend whatprovidesdata if needed, +1 for ID_NULL-termination */
385 if (pool->whatprovidesdataleft < count + 1)
388 printf("growing provides hash data...\n");
389 pool->whatprovidesdata = (Id *)xrealloc(pool->whatprovidesdata, (pool->whatprovidesdataoff + count + 4096) * sizeof(Id));
390 pool->whatprovidesdataleft = count + 4096;
393 /* copy queue to next free slot */
394 off = pool->whatprovidesdataoff;
395 memcpy(pool->whatprovidesdata + pool->whatprovidesdataoff, q->elements, count * sizeof(Id));
397 /* adapt count and ID_NULL-terminate */
398 pool->whatprovidesdataoff += count;
399 pool->whatprovidesdata[pool->whatprovidesdataoff++] = ID_NULL;
400 pool->whatprovidesdataleft -= count + 1;
406 /******************************************************************************/
411 * add packages fulfilling the relation to whatprovides array
412 * no exact providers, do range match
417 pool_addrelproviders(Pool *pool, Id d)
419 Reldep *rd = GETRELDEP(pool, d);
425 int flags = rd->flags;
427 Id p, *pp, *pp2, *pp3;
429 d = GETRELID(pool, d);
430 queue_init_buffer(&plist, buf, sizeof(buf)/sizeof(*buf));
435 pp = GET_PROVIDESP(name, p);
436 pp2 = GET_PROVIDESP(evr, p);
437 while ((p = *pp++) != 0)
439 for (pp3 = pp2; *pp3;)
442 queue_push(&plist, p);
448 pp = GET_PROVIDESP(name, p);
449 while ((p = *pp++) != 0)
450 queue_push(&plist, p);
451 pp = GET_PROVIDESP(evr, p);
452 while ((p = *pp++) != 0)
453 queue_pushunique(&plist, p);
456 if (pool->nscallback)
458 p = pool->nscallback(pool, pool->nscallbackdata, name, evr);
462 pool->whatprovides[d] = p;
463 return pool->whatprovidesdata + p;
466 queue_push(&plist, SYSTEMSOLVABLE);
473 /* convert to whatprovides id */
476 printf("addrelproviders: what provides %s?\n", id2str(pool, name));
478 if (flags && flags < 8)
480 FOR_PROVIDES(p, pp, name)
484 printf("addrelproviders: checking package %s\n", id2str(pool, pool->p[p].name));
486 /* solvable p provides name in some rels */
487 pidp = pool->solvables[p].repo->idarraydata + pool->solvables[p].provides;
488 while ((pid = *pidp++) != 0)
494 break; /* yes, provides all versions */
496 continue; /* wrong provides name */
497 prd = GETRELDEP(pool, pid);
498 if (prd->name != name)
499 continue; /* wrong provides name */
500 /* right package, both deps are rels */
504 if (flags == 7 || pflags == 7)
505 break; /* included */
506 if ((pflags & flags & 5) != 0)
507 break; /* same direction, match */
511 if ((pflags & flags & 2) != 0)
512 break; /* both have =, match */
516 int f = flags == 5 ? 5 : flags == 2 ? pflags : (flags ^ 5) & (pflags | 5);
517 if ((f & (1 << (1 + evrcmp(pool, pevr, evr)))) != 0)
522 continue; /* no rel match */
523 queue_push(&plist, p);
525 /* make our system solvable provide all unknown rpmlib() stuff */
526 if (plist.count == 0 && !strncmp(id2str(pool, name), "rpmlib(", 7))
527 queue_push(&plist, SYSTEMSOLVABLE);
529 /* add providers to whatprovides */
531 if (pool->verbose) printf("addrelproviders: adding %d packages to %d\n", plist.count, d);
533 pool->whatprovides[d] = pool_queuetowhatprovides(pool, &plist);
536 return pool->whatprovidesdata + pool->whatprovides[d];