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_rel2id(Pool *pool, Id name, Id evr, int flags, int create)
65 hashmask = pool->relhashmask;
66 hashtbl = pool->relhashtbl;
69 /* extend hashtable if needed */
70 if (pool->nrels * 2 > hashmask)
72 solv_free(pool->relhashtbl);
73 pool->relhashmask = hashmask = mkmask(pool->nrels + REL_BLOCK);
74 pool->relhashtbl = hashtbl = solv_calloc(hashmask + 1, sizeof(Id));
75 // rehash all rels into new hashtable
76 for (i = 1; i < pool->nrels; i++)
78 h = relhash(ran[i].name, ran[i].evr, ran[i].flags) & hashmask;
81 h = HASHCHAIN_NEXT(h, hh, hashmask);
86 /* compute hash and check for match */
87 h = relhash(name, evr, flags) & hashmask;
89 while ((id = hashtbl[h]) != 0)
91 if (ran[id].name == name && ran[id].evr == evr && ran[id].flags == flags)
93 h = HASHCHAIN_NEXT(h, hh, hashmask);
96 return MAKERELDEP(id);
102 /* extend rel space if needed */
103 pool->rels = solv_extend(pool->rels, id, 1, sizeof(Reldep), REL_BLOCK);
105 ran = pool->rels + id;
110 /* extend whatprovides_rel if needed */
111 if (pool->whatprovides_rel && (id & WHATPROVIDES_BLOCK) == 0)
113 pool->whatprovides_rel = solv_realloc2(pool->whatprovides_rel, id + (WHATPROVIDES_BLOCK + 1), sizeof(Offset));
114 memset(pool->whatprovides_rel + id, 0, (WHATPROVIDES_BLOCK + 1) * sizeof(Offset));
116 return MAKERELDEP(id);
121 // for rels (returns name only) and strings
124 pool_id2str(const Pool *pool, Id id)
128 Reldep *rd = GETRELDEP(pool, id);
129 if (ISRELDEP(rd->name))
131 return pool->ss.stringspace + pool->ss.strings[rd->name];
133 return pool->ss.stringspace + pool->ss.strings[id];
136 static const char *rels[] = {
138 #ifndef DEBIAN_SEMANTICS
145 #ifndef DEBIAN_SEMANTICS
156 // get operator for RelId
158 pool_id2rel(const Pool *pool, Id id)
163 rd = GETRELDEP(pool, id);
166 case 0: case 1: case 2: case 3:
167 case 4: case 5: case 6: case 7:
168 return rels[rd->flags & 7];
176 return " NAMESPACE "; /* actually not used in dep2str */
179 case REL_FILECONFLICT:
180 return " FILECONFLICT ";
191 pool_id2evr(const Pool *pool, Id id)
196 rd = GETRELDEP(pool, id);
197 if (ISRELDEP(rd->evr))
199 return pool->ss.stringspace + pool->ss.strings[rd->evr];
203 dep2strlen(const Pool *pool, Id id)
209 Reldep *rd = GETRELDEP(pool, id);
210 /* add 2 for parens */
211 l += 2 + dep2strlen(pool, rd->name) + strlen(pool_id2rel(pool, id));
214 return l + strlen(pool->ss.stringspace + pool->ss.strings[id]);
218 dep2strcpy(const Pool *pool, char *p, Id id, int oldrel)
222 Reldep *rd = GETRELDEP(pool, id);
223 if (oldrel == REL_AND || oldrel == REL_OR || oldrel == REL_WITH)
224 if (rd->flags == REL_AND || rd->flags == REL_OR || rd->flags == REL_WITH)
225 if (oldrel != rd->flags)
228 dep2strcpy(pool, p, rd->name, rd->flags);
230 strcpy(p, pool_id2rel(pool, id));
232 dep2strcpy(pool, p, rd->evr, rd->flags);
236 dep2strcpy(pool, p, rd->name, rd->flags);
238 if (rd->flags == REL_NAMESPACE)
241 dep2strcpy(pool, p, rd->evr, rd->flags);
245 if (rd->flags == REL_FILECONFLICT)
250 strcpy(p, pool_id2rel(pool, id));
255 strcpy(p, pool->ss.stringspace + pool->ss.strings[id]);
259 pool_dep2str(Pool *pool, Id id)
263 return pool->ss.stringspace + pool->ss.strings[id];
264 p = pool_alloctmpspace(pool, dep2strlen(pool, id) + 1);
265 dep2strcpy(pool, p, id, 0);
270 pool_shrink_strings(Pool *pool)
272 stringpool_shrink(&pool->ss);
276 pool_shrink_rels(Pool *pool)
278 pool->rels = solv_extend_resize(pool->rels, pool->nrels, sizeof(Reldep), REL_BLOCK);
281 // reset all hash tables
284 pool_freeidhashes(Pool *pool)
286 stringpool_freehash(&pool->ss);
287 pool->relhashtbl = solv_free(pool->relhashtbl);
288 pool->relhashmask = 0;