- change and/or/with to more symbolic versions ('|' is already used in
[platform/upstream/libsolv.git] / src / poolid.c
1 /*
2  * Copyright (c) 2007, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 /*
9  * poolid.c
10  *
11  * Id management
12  */
13
14 #include <stdlib.h>
15 #include <string.h>
16 #include <stdio.h>
17
18 #include "pool.h"
19 #include "poolid.h"
20 #include "poolid_private.h"
21 #include "util.h"
22
23
24 /* intern string into pool, return id */
25
26 Id
27 str2id(Pool *pool, const char *str, int create)
28 {
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)
32     {
33       /* grow whatprovides array */
34       pool->whatprovides = sat_realloc(pool->whatprovides, (id + (WHATPROVIDES_BLOCK + 1)) * sizeof(Offset));
35       memset(pool->whatprovides + id, 0, (WHATPROVIDES_BLOCK + 1) * sizeof(Offset));
36     }
37   return id;
38 }
39
40 Id
41 strn2id(Pool *pool, const char *str, unsigned int len, int create)
42 {
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)
46     {
47       /* grow whatprovides array */
48       pool->whatprovides = sat_realloc(pool->whatprovides, (id + (WHATPROVIDES_BLOCK + 1)) * sizeof(Offset));
49       memset(pool->whatprovides + id, 0, (WHATPROVIDES_BLOCK + 1) * sizeof(Offset));
50     }
51   return id;
52 }
53
54 Id
55 rel2id(Pool *pool, Id name, Id evr, int flags, int create)
56 {
57   Hashval h;
58   unsigned int hh;
59   Hashmask hashmask;
60   int i;
61   Id id;
62   Hashtable hashtbl;
63   Reldep *ran;
64
65   hashmask = pool->relhashmask;
66   hashtbl = pool->relhashtbl;
67   ran = pool->rels;
68   
69   /* extend hashtable if needed */
70   if (pool->nrels * 2 > hashmask)
71     {
72       sat_free(pool->relhashtbl);
73       pool->relhashmask = hashmask = mkmask(pool->nrels + REL_BLOCK);
74       pool->relhashtbl = hashtbl = sat_calloc(hashmask + 1, sizeof(Id));
75       // rehash all rels into new hashtable
76       for (i = 1; i < pool->nrels; i++)
77         {
78           h = relhash(ran[i].name, ran[i].evr, ran[i].flags) & hashmask;
79           hh = HASHCHAIN_START;
80           while (hashtbl[h])
81             h = HASHCHAIN_NEXT(h, hh, hashmask);
82           hashtbl[h] = i;
83         }
84     }
85   
86   /* compute hash and check for match */
87   h = relhash(name, evr, flags) & hashmask;
88   hh = HASHCHAIN_START;
89   while ((id = hashtbl[h]) != 0)
90     {
91       if (ran[id].name == name && ran[id].evr == evr && ran[id].flags == flags)
92         break;
93       h = HASHCHAIN_NEXT(h, hh, hashmask);
94     }
95   if (id)
96     return MAKERELDEP(id);
97
98   if (!create)
99     return ID_NULL;
100
101   id = pool->nrels++;
102   /* extend rel space if needed */
103   pool->rels = sat_extend(pool->rels, id, 1, sizeof(Reldep), REL_BLOCK);
104   hashtbl[h] = id;
105   ran = pool->rels + id;
106   ran->name = name;
107   ran->evr = evr;
108   ran->flags = flags;
109
110   /* extend whatprovides_rel if needed */
111   if (pool->whatprovides_rel && (id & WHATPROVIDES_BLOCK) == 0)
112     {
113       pool->whatprovides_rel = sat_realloc2(pool->whatprovides_rel, id + (WHATPROVIDES_BLOCK + 1), sizeof(Offset));
114       memset(pool->whatprovides_rel + id, 0, (WHATPROVIDES_BLOCK + 1) * sizeof(Offset));
115     }
116   return MAKERELDEP(id);
117 }
118
119
120 // Id -> String
121 // for rels (returns name only) and strings
122 // 
123 const char *
124 id2str(Pool *pool, Id id)
125 {
126   if (ISRELDEP(id))
127     {
128       Reldep *rd = GETRELDEP(pool, id);
129       if (ISRELDEP(rd->name))
130         return "REL";
131       return pool->ss.stringspace + pool->ss.strings[rd->name];
132     }
133   return pool->ss.stringspace + pool->ss.strings[id];
134 }
135
136 static const char *rels[] = {
137   " ! ",
138   " > ",
139   " = ",
140   " >= ",
141   " < ",
142   " <> ",
143   " <= ",
144   " <=> "
145 };
146
147
148 // get operator for RelId
149 const char *
150 id2rel(Pool *pool, Id id)
151 {
152   Reldep *rd;
153   if (!ISRELDEP(id))
154     return "";
155   rd = GETRELDEP(pool, id);
156   switch (rd->flags)
157     {
158     case 0: case 1: case 2: case 3:
159     case 4: case 5: case 6: case 7:
160       return rels[rd->flags & 7];
161     case REL_AND:
162       return " & ";
163     case REL_OR:
164       return " | ";
165     case REL_WITH:
166       return " + ";
167     case REL_NAMESPACE:
168       return " NAMESPACE ";     /* actually not used in dep2str */
169     case REL_ARCH:
170       return ".";
171     default:
172       break;
173     }
174   return " ??? ";
175 }
176
177
178 // get e:v.r for Id
179 // 
180 const char *
181 id2evr(Pool *pool, Id id)
182 {
183   Reldep *rd;
184   if (!ISRELDEP(id))
185     return "";
186   rd = GETRELDEP(pool, id);
187   if (ISRELDEP(rd->evr))
188     return "(REL)";
189   return pool->ss.stringspace + pool->ss.strings[rd->evr];
190 }
191
192 static int
193 dep2strlen(Pool *pool, Id id)
194 {
195   int l = 0;
196
197   while (ISRELDEP(id))
198     {
199       Reldep *rd = GETRELDEP(pool, id);
200       /* add 2 for parens */
201       l += 2 + dep2strlen(pool, rd->name) + strlen(id2rel(pool, id));
202       id = rd->evr;
203     }
204   return l + strlen(pool->ss.stringspace + pool->ss.strings[id]);
205 }
206
207 static void
208 dep2strcpy(Pool *pool, char *p, Id id, int oldrel)
209 {
210   while (ISRELDEP(id))
211     {
212       Reldep *rd = GETRELDEP(pool, id);
213       if (oldrel == REL_AND || oldrel == REL_OR || oldrel == REL_WITH)
214         if (rd->flags == REL_AND || rd->flags == REL_OR || rd->flags == REL_WITH)
215           if (oldrel != rd->flags)
216             {
217               *p++ = '(';
218               dep2strcpy(pool, p, rd->name, rd->flags);
219               p += strlen(p);
220               strcpy(p, id2rel(pool, id));
221               p += strlen(p);
222               dep2strcpy(pool, p, rd->evr, rd->flags);
223               strcat(p, ")");
224               return;
225             }
226       dep2strcpy(pool, p, rd->name, rd->flags);
227       p += strlen(p);
228       if (rd->flags == REL_NAMESPACE)
229         {
230           *p++ = '(';
231           dep2strcpy(pool, p, rd->evr, rd->flags);
232           strcat(p, ")");
233           return;
234         }
235       strcpy(p, id2rel(pool, id));
236       p += strlen(p);
237       id = rd->evr;
238       oldrel = rd->flags;
239     }
240   strcpy(p, pool->ss.stringspace + pool->ss.strings[id]);
241 }
242
243 const char *
244 dep2str(Pool *pool, Id id)
245 {
246   char *p;
247   if (!ISRELDEP(id))
248     return pool->ss.stringspace + pool->ss.strings[id];
249   p = pool_alloctmpspace(pool, dep2strlen(pool, id) + 1);
250   dep2strcpy(pool, p, id, 0);
251   return p;
252 }
253
254 void
255 pool_shrink_strings(Pool *pool)
256 {
257   stringpool_shrink(&pool->ss);
258 }
259
260 void
261 pool_shrink_rels(Pool *pool)
262 {
263   pool->rels = sat_extend_resize(pool->rels, pool->nrels, sizeof(Reldep), REL_BLOCK);
264 }
265
266 // reset all hash tables
267 // 
268 void
269 pool_freeidhashes(Pool *pool)
270 {
271   pool->ss.stringhashtbl = sat_free(pool->ss.stringhashtbl);
272   pool->ss.stringhashmask = 0;
273   pool->relhashtbl = sat_free(pool->relhashtbl);
274   pool->relhashmask = 0;
275 }
276
277 // EOF