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, return id */
27 pool_str2id(Pool *pool, const char *str, int create)
29 int oldnstrings = pool->ss.nstrings;
30 Id id = stringpool_str2id(&pool->ss, str, create);
31 if (create && pool->whatprovides && oldnstrings != pool->ss.nstrings && (id & WHATPROVIDES_BLOCK) == 0)
33 /* grow whatprovides array */
34 pool->whatprovides = solv_realloc(pool->whatprovides, (id + (WHATPROVIDES_BLOCK + 1)) * sizeof(Offset));
35 memset(pool->whatprovides + id, 0, (WHATPROVIDES_BLOCK + 1) * sizeof(Offset));
41 pool_strn2id(Pool *pool, const char *str, unsigned int len, int create)
43 int oldnstrings = pool->ss.nstrings;
44 Id id = stringpool_strn2id(&pool->ss, str, len, create);
45 if (create && pool->whatprovides && oldnstrings != pool->ss.nstrings && (id & WHATPROVIDES_BLOCK) == 0)
47 /* grow whatprovides array */
48 pool->whatprovides = solv_realloc(pool->whatprovides, (id + (WHATPROVIDES_BLOCK + 1)) * sizeof(Offset));
49 memset(pool->whatprovides + id, 0, (WHATPROVIDES_BLOCK + 1) * sizeof(Offset));
55 pool_resize_rels_hash(Pool *pool, int numnew)
57 Hashval h, hh, hashmask;
64 hashmask = mkmask(pool->nrels + numnew);
65 if (hashmask <= pool->relhashmask)
66 return; /* same as before */
68 /* realloc hash table */
69 pool->relhashmask = hashmask;
70 solv_free(pool->relhashtbl);
71 pool->relhashtbl = hashtbl = solv_calloc(hashmask + 1, sizeof(Id));
73 /* rehash all rels into new hashtable */
74 for (i = 1, rd = pool->rels + i; i < pool->nrels; i++, rd++)
76 h = relhash(rd->name, rd->evr, rd->flags) & hashmask;
79 h = HASHCHAIN_NEXT(h, hh, hashmask);
85 pool_rel2id(Pool *pool, Id name, Id evr, int flags, int create)
87 Hashval h, hh, hashmask;
93 /* extend hashtable if needed */
94 hashmask = pool->relhashmask;
95 if ((Hashval)pool->nrels * 2 > hashmask)
97 pool_resize_rels_hash(pool, REL_BLOCK);
98 hashmask = pool->relhashmask;
100 hashtbl = pool->relhashtbl;
102 /* compute hash and check for match */
103 h = relhash(name, evr, flags) & hashmask;
104 hh = HASHCHAIN_START;
106 while ((id = hashtbl[h]) != 0)
108 if (ran[id].name == name && ran[id].evr == evr && ran[id].flags == flags)
110 h = HASHCHAIN_NEXT(h, hh, hashmask);
113 return MAKERELDEP(id);
119 /* extend rel space if needed */
120 pool->rels = solv_extend(pool->rels, id, 1, sizeof(Reldep), REL_BLOCK);
122 ran = pool->rels + id;
127 /* extend whatprovides_rel if needed */
128 if (pool->whatprovides_rel && (id & WHATPROVIDES_BLOCK) == 0)
130 pool->whatprovides_rel = solv_realloc2(pool->whatprovides_rel, id + (WHATPROVIDES_BLOCK + 1), sizeof(Offset));
131 memset(pool->whatprovides_rel + id, 0, (WHATPROVIDES_BLOCK + 1) * sizeof(Offset));
133 return MAKERELDEP(id);
138 * for rels (returns name only) and strings
141 pool_id2str(const Pool *pool, Id id)
145 Reldep *rd = GETRELDEP(pool, id);
148 return pool->ss.stringspace + pool->ss.strings[id];
151 static const char *rels[] = {
163 /* get operator for RelId */
165 pool_id2rel(const Pool *pool, Id id)
170 rd = GETRELDEP(pool, id);
174 /* debian special cases < and > */
175 /* haiku special cases <> (maybe we should use != for the others as well */
176 case 0: case REL_EQ: case REL_GT | REL_EQ:
177 case REL_LT | REL_EQ: case REL_LT | REL_EQ | REL_GT:
178 #if !defined(DEBIAN) && !defined(MULTI_SEMANTICS)
179 case REL_LT: case REL_GT:
181 #if !defined(HAIKU) && !defined(MULTI_SEMANTICS)
182 case REL_LT | REL_GT:
184 return rels[rd->flags];
185 #if defined(DEBIAN) || defined(MULTI_SEMANTICS)
187 return pool->disttype == DISTTYPE_DEB ? " >> " : rels[rd->flags];
189 return pool->disttype == DISTTYPE_DEB ? " << " : rels[rd->flags];
191 #if defined(HAIKU) || defined(MULTI_SEMANTICS)
192 case REL_LT | REL_GT:
193 return pool->disttype == DISTTYPE_HAIKU ? " != " : rels[rd->flags];
196 return pool->disttype == DISTTYPE_RPM ? " and " : " & ";
198 return pool->disttype == DISTTYPE_RPM ? " or " : " | ";
200 return pool->disttype == DISTTYPE_RPM ? " with " : " + ";
202 return pool->disttype == DISTTYPE_RPM ? " without " : " - ";
204 return " NAMESPACE "; /* actually not used in dep2str */
209 case REL_FILECONFLICT:
210 return " FILECONFLICT ";
212 return pool->disttype == DISTTYPE_RPM ? " if " : " IF ";
214 return pool->disttype == DISTTYPE_RPM ? " unless " : " UNLESS ";
216 return " compat >= ";
220 return pool->disttype == DISTTYPE_RPM ? " else " : " ELSE ";
232 /* get e:v.r for Id */
234 pool_id2evr(const Pool *pool, Id id)
239 rd = GETRELDEP(pool, id);
240 if (ISRELDEP(rd->evr))
242 return pool->ss.stringspace + pool->ss.strings[rd->evr];
246 dep2strlen(const Pool *pool, Id id)
252 Reldep *rd = GETRELDEP(pool, id);
253 /* add 2 for parens */
254 l += 2 + dep2strlen(pool, rd->name) + strlen(pool_id2rel(pool, id));
257 return l + strlen(pool->ss.stringspace + pool->ss.strings[id]);
261 dep2strcpy(const Pool *pool, char *p, Id id, int oldrel)
265 Reldep *rd = GETRELDEP(pool, id);
267 if (oldrel == REL_AND || oldrel == REL_OR || oldrel == REL_WITH || oldrel == REL_WITHOUT || oldrel == REL_COND || oldrel == REL_UNLESS || oldrel == REL_ELSE || oldrel == -1)
268 if (rel == REL_AND || rel == REL_OR || rel == REL_WITH || rel == REL_WITHOUT || rel == REL_COND || rel == REL_UNLESS || rel == REL_ELSE)
269 if ((oldrel != rel || rel == REL_COND || rel == REL_UNLESS || rel == REL_ELSE) && !((oldrel == REL_COND || oldrel == REL_UNLESS) && rel == REL_ELSE))
272 dep2strcpy(pool, p, rd->name, rd->flags);
274 strcpy(p, pool_id2rel(pool, id));
276 dep2strcpy(pool, p, rd->evr, rd->flags);
280 if (rd->flags == REL_KIND)
282 dep2strcpy(pool, p, rd->evr, rd->flags);
289 dep2strcpy(pool, p, rd->name, rd->flags);
291 if (rd->flags == REL_NAMESPACE)
294 dep2strcpy(pool, p, rd->evr, rd->flags);
298 if (rd->flags == REL_FILECONFLICT)
303 strcpy(p, pool_id2rel(pool, id));
308 strcpy(p, pool->ss.stringspace + pool->ss.strings[id]);
312 pool_dep2str(Pool *pool, Id id)
316 return pool->ss.stringspace + pool->ss.strings[id];
317 p = pool_alloctmpspace(pool, dep2strlen(pool, id) + 1);
318 dep2strcpy(pool, p, id, pool->disttype == DISTTYPE_RPM ? -1 : 0);
323 pool_free_rels_hash(Pool *pool)
325 pool->relhashtbl = solv_free(pool->relhashtbl);
326 pool->relhashmask = 0;
330 pool_shrink_strings(Pool *pool)
332 /* free excessive big hashes */
333 if (pool->ss.stringhashmask && pool->ss.stringhashmask > mkmask(pool->ss.nstrings + 8192))
334 stringpool_freehash(&pool->ss);
335 stringpool_shrink(&pool->ss);
339 pool_shrink_rels(Pool *pool)
341 /* free excessive big hashes */
342 if (pool->relhashmask && pool->relhashmask > mkmask(pool->nrels + 4096))
343 pool_free_rels_hash(pool);
344 pool->rels = solv_extend_resize(pool->rels, pool->nrels, sizeof(Reldep), REL_BLOCK);
347 /* free all hash tables */
349 pool_freeidhashes(Pool *pool)
351 stringpool_freehash(&pool->ss);
352 pool_free_rels_hash(pool);