- add pool_search() function to search the pool
[platform/upstream/libsolv.git] / src / pool.h
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  * pool.h
10  * 
11  */
12
13 #ifndef SATSOLVER_POOL_H
14 #define SATSOLVER_POOL_H
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20 #include <stdio.h>
21
22 #include "pooltypes.h"
23 #include "poolid.h"
24 #include "solvable.h"
25 #include "bitmap.h"
26 #include "queue.h"
27 #include "strpool.h"
28
29 // see initpool_data[] in pool.c
30
31 /* well known ids */
32 #define ID_NULL                 STRID_NULL
33 #define ID_EMPTY                STRID_EMPTY
34 #define SOLVABLE_NAME           2
35 #define SOLVABLE_ARCH           3
36 #define SOLVABLE_EVR            4
37 #define SOLVABLE_VENDOR         5
38 #define SOLVABLE_PROVIDES       6
39 #define SOLVABLE_OBSOLETES      7
40 #define SOLVABLE_CONFLICTS      8
41 #define SOLVABLE_REQUIRES       9
42 #define SOLVABLE_RECOMMENDS     10
43 #define SOLVABLE_SUGGESTS       11
44 #define SOLVABLE_SUPPLEMENTS    12
45 #define SOLVABLE_ENHANCES       13
46 #define SOLVABLE_FRESHENS       14
47 #define RPM_RPMDBID             15
48 #define SOLVABLE_PREREQMARKER   16              // normal requires before this, prereqs after this
49 #define SOLVABLE_FILEMARKER     17              // normal provides before this, generated file provides after this
50 #define NAMESPACE_INSTALLED     18
51 #define NAMESPACE_MODALIAS      19
52 #define NAMESPACE_SPLITPROVIDES 20
53 #define NAMESPACE_LANGUAGE      21
54 #define SYSTEM_SYSTEM           22
55 #define ARCH_SRC                23
56 #define ARCH_NOSRC              24
57 #define ARCH_NOARCH             25
58 #define REPODATA_EXTERNAL       26
59 #define REPODATA_KEYS           27
60 #define REPODATA_LOCATION       28
61
62 #define ID_NUM_INTERNAL         29
63
64
65 /* well known solvable */
66 #define SYSTEMSOLVABLE          1
67
68
69 /* how many strings to maintain (round robin) */
70 #define DEP2STRBUF 16
71
72 //-----------------------------------------------
73
74 struct _Repo;
75 struct _Repodata;
76 struct _Repokey;
77 struct _KeyValue;
78
79 struct _Pool {
80   struct _Stringpool ss;
81
82   Reldep *rels;               // table of rels: Id -> Reldep
83   int nrels;                  // number of unique rels
84   Hashtable relhashtbl;       // hash table: (name,evr,op ->) Hash -> Id
85   Hashmask relhashmask;
86
87   struct _Repo **repos;
88   int nrepos;
89
90   Solvable *solvables;
91   int nsolvables;
92
93   int promoteepoch;             /* 0/1  */
94
95   Id *id2arch;                  /* map arch ids to scores */
96   Id lastarch;                  /* last valid entry in id2arch */
97   Queue vendormap;              /* map vendor to vendorclasses mask */
98
99   /* providers data, as two-step indirect list
100    * whatprovides[Id] -> Offset into whatprovidesdata for name
101    * whatprovidesdata[Offset] -> ID_NULL-terminated list of solvables providing Id
102    */
103   Offset *whatprovides;         /* Offset to providers of a specific name, Id -> Offset  */
104   Offset *whatprovides_rel;     /* Offset to providers of a specific relation, Id -> Offset  */
105
106   Id *whatprovidesdata;         /* Ids of solvable providing Id */
107   Offset whatprovidesdataoff;   /* next free slot within whatprovidesdata */
108   int whatprovidesdataleft;     /* number of 'free slots' within whatprovidesdata */
109
110   /* If nonzero, then consider only the solvables with Ids set in this
111      bitmap for solving.  If zero, consider all solvables.  */
112   Map *considered;
113
114   Id (*nscallback)(struct _Pool *, void *data, Id name, Id evr);
115   void *nscallbackdata;
116
117   /* our dep2str string space */
118   char *dep2strbuf[DEP2STRBUF];
119   int   dep2strlen[DEP2STRBUF];
120   int   dep2strn;
121
122   /* debug mask and callback */
123   int  debugmask;
124   void (*debugcallback)(struct _Pool *, void *data, int type, const char *str);
125   void *debugcallbackdata;
126
127   /* load callback */
128   FILE * (*loadcallback)(struct _Pool *, struct _Repodata *, void *);
129   void *loadcallbackdata;
130 };
131
132 #define SAT_FATAL                       (1<<0)
133 #define SAT_ERROR                       (1<<1)
134 #define SAT_WARN                        (1<<2)
135 #define SAT_DEBUG_STATS                 (1<<3)
136 #define SAT_DEBUG_RULE_CREATION         (1<<4)
137 #define SAT_DEBUG_PROPAGATE             (1<<5)
138 #define SAT_DEBUG_ANALYZE               (1<<6)
139 #define SAT_DEBUG_UNSOLVABLE            (1<<7)
140 #define SAT_DEBUG_SOLUTIONS             (1<<8)
141 #define SAT_DEBUG_POLICY                (1<<9)
142 #define SAT_DEBUG_RESULT                (1<<10)
143 #define SAT_DEBUG_JOB                   (1<<11)
144 #define SAT_DEBUG_SCHUBI                (1<<12)
145
146 /* The void type is usable to encode one-valued attributes, they have
147    no associated data.  This is useful to encode values which many solvables
148    have in common, and whose overall set is relatively limited.  A prime
149    example would be the media number.  The actual value is encoded in the
150    SIZE member of the key structure.  Be warned: careless use of this
151    leads to combinatoric explosion of number of schemas.  */
152
153 /* FIXME: Should be 'enum' */
154 #define TYPE_VOID               0
155 #define TYPE_ID                 1
156 #define TYPE_IDARRAY            2
157 #define TYPE_STR                3
158 #define TYPE_U32                4
159 #define TYPE_REL_IDARRAY        5
160
161 #define TYPE_ATTR_INT           6
162 #define TYPE_ATTR_CHUNK         7
163 #define TYPE_ATTR_STRING        8
164 #define TYPE_ATTR_INTLIST       9
165 #define TYPE_ATTR_LOCALIDS      10
166
167 #define TYPE_COUNT_NAMED        11
168 #define TYPE_COUNTED            12
169
170 #define TYPE_IDVALUEARRAY       13
171
172 #define TYPE_DIR                14
173 #define TYPE_DIRNUMNUMARRAY     15
174 #define TYPE_DIRSTRARRAY        16
175
176 #define TYPE_CONSTANT           17
177 #define TYPE_NUM                18
178
179 #define TYPE_ATTR_TYPE_MAX      18
180
181 //-----------------------------------------------
182
183
184 /* mark dependencies with relation by setting bit31 */
185
186 #define MAKERELDEP(id) ((id) | 0x80000000)
187 #define ISRELDEP(id) (((id) & 0x80000000) != 0)
188 #define GETRELID(id) ((id) ^ 0x80000000)                                /* returns Id */
189 #define GETRELDEP(pool, id) ((pool)->rels + ((id) ^ 0x80000000))        /* returns Reldep* */
190
191 #define REL_GT          1
192 #define REL_EQ          2
193 #define REL_LT          4
194
195 #define REL_AND         16
196 #define REL_OR          17
197 #define REL_WITH        18
198 #define REL_NAMESPACE   19
199
200 #if !defined(__GNUC__) && !defined(__attribute__)
201 # define __attribute__(x)
202 #endif
203
204 /**
205  * Creates a new pool
206  */
207 extern Pool *pool_create(void);
208 /**
209  * Delete a pool
210  */
211 extern void pool_free(Pool *pool);
212
213 extern void pool_debug(Pool *pool, int type, const char *format, ...) __attribute__((format(printf, 3, 4)));
214
215 /**
216  * Solvable management
217  */
218 extern Id pool_add_solvable(Pool *pool);
219 extern Id pool_add_solvable_block(Pool *pool, int count);
220
221 extern void pool_free_solvable_block(Pool *pool, Id start, int count, int reuseids);
222 static inline Solvable *pool_id2solvable(Pool *pool, Id p)
223 {
224   return pool->solvables + p;
225 }
226 extern const char *solvable2str(Pool *pool, Solvable *s);
227
228
229 /**
230  * Prepares a pool for solving
231  */
232 extern void pool_createwhatprovides(Pool *pool);
233 extern void pool_addfileprovides(Pool *pool, struct _Repo *installed);
234 extern void pool_freewhatprovides(Pool *pool);
235 extern Id pool_queuetowhatprovides(Pool *pool, Queue *q);
236
237 static inline int pool_installable(Pool *pool, Solvable *s)
238 {
239   if (!s->arch || s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
240     return 0;
241   if (pool->id2arch && (s->arch > pool->lastarch || !pool->id2arch[s->arch]))
242     return 0;
243   if (pool->considered)
244     { 
245       Id id = s - pool->solvables;
246       if (!MAPTST(pool->considered, id))
247         return 0;
248     }
249   return 1;
250 }
251
252 extern Id *pool_addrelproviders(Pool *pool, Id d);
253
254 static inline Id *pool_whatprovides(Pool *pool, Id d)
255 {
256   Id v;
257   if (!ISRELDEP(d))
258     return pool->whatprovidesdata + pool->whatprovides[d];
259   v = GETRELID(d);
260   if (pool->whatprovides_rel[v])
261     return pool->whatprovidesdata + pool->whatprovides_rel[v];
262   return pool_addrelproviders(pool, d);
263 }
264
265 extern void pool_setdebuglevel(Pool *pool, int level);
266
267 static inline void pool_setdebugcallback(Pool *pool, void (*debugcallback)(struct _Pool *, void *data, int type, const char *str), void *debugcallbackdata)
268 {
269   pool->debugcallback = debugcallback;
270   pool->debugcallbackdata = debugcallbackdata;
271 }
272
273 static inline void pool_setdebugmask(Pool *pool, int mask)
274 {
275   pool->debugmask = mask;
276 }
277
278 static inline void pool_setloadcallback(Pool *pool, FILE *(*cb)(struct _Pool *, struct _Repodata *, void *), void *loadcbdata)
279 {
280   pool->loadcallback = cb;
281   pool->loadcallbackdata = loadcbdata;
282 }
283
284 /* search the pool. the following filters are available:
285  *   p     - search just this solvable
286  *   key   - search only this key
287  *   match - key must match this string
288  */
289 void pool_search(Pool *pool, Id p, Id key, const char *match, int flags, int (*callback)(void *cbdata, Solvable *s, struct _Repodata *data, struct _Repokey *key, struct _KeyValue *kv), void *cbdata);
290
291 /* loop over all providers of d */
292 #define FOR_PROVIDES(v, vp, d)                                          \
293   for (vp = pool_whatprovides(pool, d) ; (v = *vp++) != 0; )
294
295 #define POOL_DEBUG(type, ...) do {if ((pool->debugmask & (type)) != 0) pool_debug(pool, (type), __VA_ARGS__);} while (0)
296 #define IF_POOLDEBUG(type) if ((pool->debugmask & (type)) != 0)
297
298 #ifdef __cplusplus
299 }
300 #endif
301
302
303 #endif /* SATSOLVER_POOL_H */