- add repoid to repo struct
[platform/upstream/libsolv.git] / src / repo.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  * repo.h
10  * 
11  */
12
13 #ifndef SATSOLVER_REPO_H
14 #define SATSOLVER_REPO_H
15
16 #include "pooltypes.h"
17 #include "pool.h"
18 #include "repodata.h"
19
20
21
22 typedef struct _Repo {
23   const char *name;
24   Id repoid;                    /* our id */
25   Pool *pool;                   /* pool containing repo data */
26
27   int start;                    /* start of this repo solvables within pool->solvables */
28   int end;                      /* last solvable + 1 of this repo */
29   int nsolvables;               /* number of solvables repo is contributing to pool */
30
31   int priority;                 /* priority of this repo */
32   int subpriority;              /* sub-priority of this repo, used just for sorting, not pruning */
33
34   Id *idarraydata;              /* array of metadata Ids, solvable dependencies are offsets into this array */
35   int idarraysize;
36   Offset lastoff;
37
38   Id *rpmdbid;                  /* solvable side data */
39
40   Repodata *repodata;           /* our stores for non-solvable related data */
41   unsigned nrepodata;           /* number of our stores..  */
42 } Repo;
43
44 extern Repo *repo_create(Pool *pool, const char *name);
45 extern void repo_free(Repo *repo, int reuseids);
46 extern void repo_freeallrepos(Pool *pool, int reuseids);
47 extern void *repo_sidedata_create(Repo *repo, size_t size);
48 extern void *repo_sidedata_extend(Repo *repo, void *b, size_t size, Id p, int count);
49
50 extern Offset repo_addid(Repo *repo, Offset olddeps, Id id);
51 extern Offset repo_addid_dep(Repo *repo, Offset olddeps, Id id, Id marker);
52 extern Offset repo_reserve_ids(Repo *repo, Offset olddeps, int num);
53 extern Offset repo_fix_supplements(Repo *repo, Offset provides, Offset supplements, Offset freshens);
54 extern Offset repo_fix_conflicts(Repo *repo, Offset conflicts);
55
56 static inline const char *repo_name(const Repo *repo)
57 {
58   return repo->name;
59 }
60
61 static inline Id repo_add_solvable(Repo *repo)
62 {
63   extern Id pool_add_solvable(Pool *pool);
64   Id p = pool_add_solvable(repo->pool);
65   if (!repo->start || repo->start == repo->end)
66     {
67       repo->start = p;
68       repo->end = p + 1;
69     }
70   else
71     {
72       if (repo->rpmdbid)
73         repo->rpmdbid = (Id *)repo_sidedata_extend(repo, repo->rpmdbid, sizeof(Id), p, 1);
74       if (p < repo->start)
75         repo->start = p;
76       if (p + 1 > repo->end)
77         repo->end = p + 1;
78     }
79   repo->nsolvables++;
80   repo->pool->solvables[p].repo = repo;
81   return p;
82 }
83
84 static inline Id repo_add_solvable_block(Repo *repo, int count)
85 {
86   extern Id pool_add_solvable_block(Pool *pool, int count);
87   Id p;
88   Solvable *s;
89   if (!count)
90     return 0;
91   p = pool_add_solvable_block(repo->pool, count);
92   if (!repo->start || repo->start == repo->end)
93     {
94       repo->start = p;
95       repo->end = p + count;
96     }
97   else
98     {
99       if (repo->rpmdbid)
100         repo->rpmdbid = (Id *)repo_sidedata_extend(repo, repo->rpmdbid, sizeof(Id), p, count);
101       if (p < repo->start)
102         repo->start = p;
103       if (p + count > repo->end)
104         repo->end = p + count;
105     }
106   repo->nsolvables += count;
107   for (s = repo->pool->solvables + p; count--; s++)
108     s->repo = repo;
109   return p;
110 }
111
112 static inline void repo_free_solvable_block(Repo *repo, Id start, int count, int reuseids)
113 {
114   extern void pool_free_solvable_block(Pool *pool, Id start, int count, int reuseids);
115   Solvable *s;
116   int i;
117   if (start + count == repo->end)
118     repo->end -= count;
119   repo->nsolvables -= count;
120   for (s = repo->pool->solvables + start, i = count; i--; s++)
121     s->repo = 0;
122   pool_free_solvable_block(repo->pool, start, count, reuseids);
123 }
124
125 static inline Repo *pool_id2repo(Pool *pool, Id repoid)
126 {
127   return pool->repos[repoid - 1];
128 }
129
130 #define FOR_REPO_SOLVABLES(r, p, s)                                             \
131   for (p = (r)->start, s = (r)->pool->solvables + p; p < (r)->end; p++, s = (r)->pool->solvables + p)   \
132     if (s->repo == (r))
133
134
135 /* search callback values */
136
137 #define SEARCH_NEXT_KEY         1
138 #define SEARCH_NEXT_SOLVABLE    2
139 #define SEARCH_STOP             3
140 #define SEARCH_ENTERSUB         -1
141
142 typedef struct _KeyValue {
143   Id id;
144   const char *str;
145   int num;
146   int num2;
147
148   int entry;    /* array entry, starts with 0 */
149   int eof;      /* last entry reached */
150
151   struct _KeyValue *parent;
152 } KeyValue;
153
154 /* search flags */
155 #define SEARCH_STRINGMASK       15
156 #define SEARCH_STRING           1
157 #define SEARCH_SUBSTRING        2
158 #define SEARCH_GLOB             3
159 #define SEARCH_REGEX            4
160 #define SEARCH_ERROR            5
161
162 #define SEARCH_NOCASE                   (1<<8)
163 #define SEARCH_NO_STORAGE_SOLVABLE      (1<<9)
164 #define SEARCH_SUB                      (1<<10)
165 #define SEARCH_ARRAYSENTINEL            (1<<11)
166 #define SEARCH_SKIP_KIND                (1<<12)
167
168
169 /* By default we don't match in attributes representing filelists
170    because the construction of those strings is costly.  Specify this
171    flag if you want this.  In that case kv->str will contain the full
172    filename (if matched of course).  */
173 #define SEARCH_FILES                    (1<<13)
174
175 /* Internal */
176 #define SEARCH_THISSOLVID               (1<<31)
177
178
179 /* standard flags used in the repo_add functions */
180 #define REPO_REUSE_REPODATA             (1 << 0)
181 #define REPO_NO_INTERNALIZE             (1 << 1)
182
183 Repodata *repo_add_repodata(Repo *repo, int localpool);
184 Repodata *repo_last_repodata(Repo *repo);
185
186 void repo_search(Repo *repo, Id p, Id key, const char *match, int flags, int (*callback)(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *kv), void *cbdata);
187
188 /* returns the string value of the attribute, or NULL if not found */
189 const char *repo_lookup_str(Repo *repo, Id entry, Id key);
190 /* returns the integer value of the attribute, or 0 if not found */
191 unsigned int repo_lookup_num(Repo *repo, Id entry, Id key, unsigned int notfound);
192 Id repo_lookup_id(Repo *repo, Id entry, Id keyid);
193 int repo_lookup_void(Repo *repo, Id entry, Id keyid);
194 const unsigned char *repo_lookup_bin_checksum(Repo *repo, Id entry, Id keyid, Id *typep);
195
196 typedef struct _Datamatcher {
197   int flags;
198   void *match;
199   int error;
200 } Datamatcher;
201
202 typedef struct _Dataiterator
203 {
204   int state;
205   int flags;
206
207   Pool *pool;
208   Repo *repo;
209   Repodata *data;
210
211   /* data pointers */
212   unsigned char *dp;
213   unsigned char *ddp;
214   Id *idp;
215   Id *keyp;
216
217   /* the result */
218   Repokey *key;
219   KeyValue kv;
220
221   /* our matcher */
222   Datamatcher matcher;
223
224   /* iterators/filters */
225   Id keyname;
226   Id repodataid;
227   Id solvid;
228   Id repoid;
229
230   /* recursion data */
231   struct di_parent {
232     KeyValue kv;
233     unsigned char *dp;
234     Id *keyp;
235   } parents[3];
236   int nparents;
237   Id keynames[3 + 1];
238   int nkeynames;
239
240 } Dataiterator;
241
242 int datamatcher_init(Datamatcher *ma, const char *match, int flags);
243 void datamatcher_free(Datamatcher *ma);
244 int datamatcher_match(Datamatcher *ma, const char *str);
245
246 /*
247  * Dataiterator
248  * 
249  * Iterator like interface to 'search' functionality
250  * 
251  * Dataiterator is per-pool, additional filters can be applied
252  * to limit the search domain. See dataiterator_init below.
253  * 
254  * Use these like:
255  *    Dataiterator di;
256  *    dataiterator_init(&di, repo->pool, repo, 0, 0, "bla", SEARCH_SUBSTRING);
257  *    while (dataiterator_step(&di))
258  *      dosomething(di.solvid, di.key, di.kv);
259  *    dataiterator_free(&di);
260  */
261
262 /*
263  * Initialize dataiterator
264  * 
265  * di:      Pointer to Dataiterator to be initialized
266  * pool:    Search domain for the iterator
267  * repo:    if non-null, limit search to this repo
268  * solvid:  if non-null, limit search to this solvable
269  * keyname: if non-null, limit search to this keyname
270  * match:   if non-null, limit search to this match
271  */
272 int dataiterator_init(Dataiterator *di, Pool *pool, Repo *repo, Id p, Id keyname,
273                        const char *match, int flags);
274 void dataiterator_prepend_keyname(Dataiterator *di, Id keyname);
275 void dataiterator_free(Dataiterator *di);
276 int dataiterator_step(Dataiterator *di);
277 void dataiterator_setpos(Dataiterator *di);
278 void dataiterator_setpos_parent(Dataiterator *di);
279 int dataiterator_match(Dataiterator *di, Datamatcher *ma);
280 void dataiterator_skip_attribute(Dataiterator *di);
281 void dataiterator_skip_solvable(Dataiterator *di);
282 void dataiterator_skip_repo(Dataiterator *di);
283 void dataiterator_jump_to_solvid(Dataiterator *di, Id solvid);
284 void dataiterator_jump_to_repo(Dataiterator *di, Repo *repo);
285 void dataiterator_entersub(Dataiterator *di);
286
287 void repo_set_id(Repo *repo, Id p, Id keyname, Id id);
288 void repo_set_num(Repo *repo, Id p, Id keyname, Id num);
289 void repo_set_str(Repo *repo, Id p, Id keyname, const char *str);
290 void repo_set_poolstr(Repo *repo, Id p, Id keyname, const char *str);
291 void repo_add_poolstr_array(Repo *repo, Id p, Id keyname, const char *str);
292 void repo_internalize(Repo *repo);
293 void repo_disable_paging(Repo *repo);
294
295 #endif /* SATSOLVER_REPO_H */