2 * Copyright (c) 2007, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
20 #include "poolid_private.h"
24 // intern string into pool
28 str2id(Pool *pool, const char *str, int create)
30 int old_nstrings = pool->ss.nstrings;
31 Id id = stringpool_str2id (&pool->ss, str, create);
32 /* If we changed the ID->string relations we need to get rid of an
33 eventually existing provides lookup cache. */
34 if (old_nstrings != pool->ss.nstrings)
35 pool_freewhatprovides(pool);
40 rel2id(Pool *pool, Id name, Id evr, int flags, int create)
50 hashmask = pool->relhashmask;
51 hashtbl = pool->relhashtbl;
54 // extend hashtable if needed
55 if (pool->nrels * 2 > hashmask)
57 xfree(pool->relhashtbl);
58 pool->relhashmask = hashmask = mkmask(pool->ss.nstrings + REL_BLOCK);
59 pool->relhashtbl = hashtbl = xcalloc(hashmask + 1, sizeof(Id));
60 // rehash all rels into new hashtable
61 for (i = 1; i < pool->nrels; i++)
63 h = relhash(ran[i].name, ran[i].evr, ran[i].flags) & hashmask;
66 h = HASHCHAIN_NEXT(h, hh, hashmask);
71 // compute hash and check for match
73 h = relhash(name, evr, flags) & hashmask;
75 while ((id = hashtbl[h]) != 0)
77 if (ran[id].name == name && ran[id].evr == evr && ran[id].flags == flags)
79 h = HASHCHAIN_NEXT(h, hh, hashmask);
82 return MAKERELDEP(id);
87 pool_freewhatprovides(pool);
90 // extend rel space if needed
91 if ((id & REL_BLOCK) == 0)
92 pool->rels = xrealloc(pool->rels, ((pool->nrels + REL_BLOCK) & ~REL_BLOCK) * sizeof(Reldep));
94 ran = pool->rels + id;
98 return MAKERELDEP(id);
103 // for rels (returns name only) and strings
106 id2str(Pool *pool, Id id)
110 Reldep *rd = GETRELDEP(pool, id);
111 if (ISRELDEP(rd->name))
113 return pool->ss.stringspace + pool->ss.strings[rd->name];
115 return pool->ss.stringspace + pool->ss.strings[id];
118 static const char *rels[] = {
130 // get operator for RelId
132 id2rel(Pool *pool, Id id)
137 rd = GETRELDEP(pool, id);
140 case 0: case 1: case 2: case 3:
141 case 4: case 5: case 6: case 7:
142 return rels[rd->flags & 7];
150 return " NAMESPACE ";
161 id2evr(Pool *pool, Id id)
166 rd = GETRELDEP(pool, id);
167 if (ISRELDEP(rd->evr))
169 return pool->ss.stringspace + pool->ss.strings[rd->evr];
173 dep2str(Pool *pool, Id id)
178 int n, l, ls1, ls2, lsr;
181 return pool->ss.stringspace + pool->ss.strings[id];
182 rd = GETRELDEP(pool, id);
185 sr = id2rel(pool, id);
188 s2 = (char *)dep2str(pool, rd->evr);
192 s1 = (char *)dep2str(pool, rd->name);
196 if (rd->flags == REL_NAMESPACE)
204 if (l + 1 > pool->dep2strlen[n])
206 if (s1 != pool->dep2strbuf[n])
207 pool->dep2strbuf[n] = xrealloc(pool->dep2strbuf[n], l + 32);
210 pool->dep2strbuf[n] = xrealloc(pool->dep2strbuf[n], l + 32);
211 s1 = pool->dep2strbuf[n];
213 pool->dep2strlen[n] = l + 32;
215 if (s1 != pool->dep2strbuf[n])
217 strcpy(pool->dep2strbuf[n], s1);
218 s1 = pool->dep2strbuf[n];
220 strcpy(s1 + ls1, sr);
221 pool->dep2strbuf[n] = s1 + ls1 + lsr;
222 s2 = (char *)dep2str(pool, rd->evr);
223 if (s2 != pool->dep2strbuf[n])
224 strcpy(pool->dep2strbuf[n], s2);
225 pool->dep2strbuf[n] = s1;
226 if (rd->flags == REL_NAMESPACE)
228 s1[ls1 + ls2 + lsr - 1] = ')';
229 s1[ls1 + ls2 + lsr] = 0;
231 pool->dep2strn = (n + 1) % DEP2STRBUF;
237 pool_shrink_strings(Pool *pool)
239 stringpool_shrink (&pool->ss);
243 pool_shrink_rels(Pool *pool)
245 pool->rels = (Reldep *)xrealloc(pool->rels, ((pool->nrels + REL_BLOCK) & ~REL_BLOCK) * sizeof(Reldep));
248 // reset all hash tables
251 pool_freeidhashes(Pool *pool)
253 pool->ss.stringhashtbl = xfree(pool->ss.stringhashtbl);
254 pool->ss.stringhashmask = 0;
255 pool->relhashtbl = xfree(pool->relhashtbl);
256 pool->relhashmask = 0;