1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/PoolQuery.h
12 #ifndef ZYPP_POOLQUERY_H
13 #define ZYPP_POOLQUERY_H
19 #include "zypp/base/Regex.h"
20 #include "zypp/base/PtrTypes.h"
21 #include "zypp/base/Function.h"
23 #include "zypp/sat/SolvIterMixin.h"
24 #include "zypp/sat/LookupAttr.h"
25 #include "zypp/sat/AttrMatcher.h"
26 #include "zypp/sat/Pool.h"
28 ///////////////////////////////////////////////////////////////////
30 { /////////////////////////////////////////////////////////////////
34 class PoolQueryIterator;
37 ///////////////////////////////////////////////////////////////////
39 // CLASS NAME : PoolQuery
42 * Meta-data query API. Returns solvables of specified kinds from specified
43 * repositories with attributes matching the specified search strings.
45 * The search strings can be specified via \ref addString() and
46 * \ref addAttribute() methods. String matching type can be set using the
47 * setMatch*() methods. Multiple search strings for a particular attribute
48 * will be combined into a regex (see \ref addString() and
49 * \ref addAttribute() for more details).
51 * The begin() and end() methods return a PoolQueryIterator returning
52 * \ref sat::Solvable objects which can easily be turned into \ref Resolvable
53 * objects. Additionally, thanx to the \ref sat::SolvIterMixin, a Selectable
54 * and PoolItem iterators are automatically available.
58 * q.addAttribute(sat::SolvAttr::name, "zypp*");
59 * q.addKind(ResKind::package);
62 * for (PoolQuery::Selectable_iterator it = q.selectableBegin();
63 * it != q.selectableEnd(); ++it)
65 * ui::Selectable::constPtr s = *it;
70 * Performance considerations
72 * Results of simple queries like those using one string and/or one attribute
73 * and/or one repository are filtered by sat-solver's Dataiterator directly,
74 * and thus it is fast.
76 * Queries with multiple strings are implemented using regexes. Queries based
77 * on kinds, multiple repos, and multiple attributes are filtered inside
78 * the PoolQuery, so these tend to be slower.
80 * \see tests/zypp/PoolQuery_test.cc for more examples
81 * \see sat::SolvIterMixin
83 class PoolQuery : public sat::SolvIterMixin<PoolQuery, detail::PoolQueryIterator>
86 typedef std::set<std::string> StrContainer;
87 typedef std::set<ResKind> Kinds;
88 typedef std::map<sat::SolvAttr, StrContainer> AttrRawStrMap;
89 typedef std::map<sat::SolvAttr, std::string> AttrCompiledStrMap;
90 typedef std::map<sat::SolvAttr, str::regex> AttrRegexMap;
92 typedef detail::PoolQueryIterator const_iterator;
93 typedef unsigned int size_type;
96 typedef function<bool( const sat::Solvable & )> ProcessResolvable;
101 /** Query result accessers. */
104 * Compile the query and return an iterator to the result.
106 * \return An iterator (\ref detail::PoolQueryIterator) returning
107 * sat::Solvable objects pointing at the beginning of the query result.
108 * \throws sat::MatchInvalidRegexException if the query was about to use a regex which
111 * \note Note that PoolQuery is derived from \ref sat::SolvIterMixin which
112 * makes PoolItem and Selectable iterators automatically available.
113 * \see \ref sat::SolvIterMixin
115 const_iterator begin() const;
117 /** An iterator pointing to the end of the query result. */
118 const_iterator end() const;
120 /** Whether the result is empty. */
123 /** Number of solvables in the query result. */
124 size_type size() const;
128 * Executes the query with the current settings.
129 * Results are yielded via the \a fnc callback.
131 void execute(ProcessResolvable fnc);
134 * Filter by selectable kind.
136 * By default, all kinds will be returned. If addKind() is used,
137 * only the specified kinds will be returned (multiple kinds will be ORed).
139 * Pass ResKind constants to this method, (e.g. ResKind::package).
141 void addKind(const ResKind & kind);
146 * By default, all repos will be returned. If addRepo() is used,
147 * only the specified repo will be returned (multiple repos will be ORed).
149 void addRepo(const std::string &repoalias);
151 /** Installed status filter setters. */
155 * Filter by status (installed uninstalled)
158 ALL = 0, // both install filter and uninstall filter bits are 0
163 /** Return only @System repo packages */
164 void setInstalledOnly();
165 /** Return only packages from repos other than @System. */
166 void setUninstalledOnly();
167 /** Set status filter directly \see StatusFilter */
168 void setStatusFilterFlags( StatusFilter flags );
173 * Add a global query string. The string added via this method is applied
174 * to all query attributes as if addAttribute(..., \value) was called
177 * This method can be used multiple times in which case the query strings
178 * will be combined (together with strings added via addAttribute()) into
179 * a regex. Searched attribute value will match this regex if <b>any</b>
180 * of these strings will match the value. This can be changed by
181 * (not yet implemented) \ref setRequireAll() method.
183 void addString(const std::string & value);
186 * Filter by the \a value of the specified \a attr attribute. This can
187 * be any of the available solvable attributes.
189 * This method can be used multiple times with the same \a attr in which
190 * case the query strings will be combined (together with strings added
191 * via addString()) into a regex. Searched attribute value will match
192 * this regex if <b>any</b> of these strings will match the value.
193 * This can be changed by (not yet implemented) \ref setRequireAll()
196 * \note Solvables of a kind not supporting the specified attribute will
197 * <b>not</b> be returned.
198 * \todo check the above
200 * \param attr Attribute identfier. Use sat::Solvattr::* constants
201 * \param value What to search for.
205 void addAttribute(const sat::SolvAttr & attr, const std::string & value = "");
208 * Set version condition. This will filter out solvables not matching
209 * <tt>solvableEdition \a op \a edition</tt>.
211 * \param edition Edition to look for.
212 * \param op Found-wanted relation operator.
214 void setEdition(const Edition & edition, const Rel & op = Rel::EQ);
216 /** \name Text Matching Options
217 * \note The implementation treats an empty search string as
218 * <it>"match always"</it>. So if you want to actually match
219 * an empty value, try <tt>( "^$", setMatchRegex )</tt>.
223 * Turn case sentitivity on or off (unsets or sets \ref SEARCH_NOCASE flag).
224 * PoolQuery defaults to case insensitive search unless this method
227 * \param value Whether to turn the case sensitivity on (default) or off.
229 void setCaseSensitive( bool value = true );
232 * If set (default), look at the full path when searching in filelists.
233 * Otherwise just match the the basenames.
234 * \see \ref Match::FILES
236 void setFilesMatchFullPath( bool value = true );
238 void setFilesMatchBasename( bool value = true )
239 { setFilesMatchFullPath( !value ); }
241 /** Set to match exact string instead of substring.*/
242 void setMatchExact();
243 /** Set to substring (the default). */
244 void setMatchSubstring();
245 /** Set to match globs. */
247 /** Set to use the query strings as regexes */
248 void setMatchRegex();
249 /** Set to match words (uses regex) */
251 //void setLocale(const Locale & locale);
255 * Require that all of the values set by addString or addAttribute
256 * match the values of respective attributes.
258 * \todo doesn't work yet, don't use this function
260 void setRequireAll( bool require_all = true );
266 /** Search strings added via addString() */
267 const StrContainer & strings() const;
269 * Map (map<SolvAttr, StrContainer>) of attribute values added via
270 * addAttribute(), addDep in string form */
271 const AttrRawStrMap & attributes() const;
273 const StrContainer & attribute(const sat::SolvAttr & attr) const;
275 const Kinds & kinds() const;
277 const StrContainer & repos() const;
279 const Edition edition() const;
280 const Rel editionRel() const;
283 * returns true if search is case sensitive
285 bool caseSensitive() const;
287 /** Whether searching in filelists looks at the full path or just at the basenames. */
288 bool filesMatchFullPath() const;
290 bool filesMatchBasename() const
291 { return !filesMatchFullPath(); }
293 bool matchExact() const;
294 bool matchSubstring() const;
295 bool matchGlob() const;
296 bool matchRegex() const;
297 bool matchWord() const;
299 /** Returns string matching mode as enum.
300 * \see \ref Match::Mode
302 Match::Mode matchMode() const
303 { return flags().mode(); }
306 * Whether all values added via addString() or addAttribute() are required
307 * to match the values of the respective attributes.
309 bool requireAll() const;
311 StatusFilter statusFilterFlags() const;
315 * Reads from stream query. Attributes is sepated by delim. Query is
316 * separated by two delim.
318 * \param str input stream which contains query
319 * \param delim delimeter for attributes
320 * \return true if non-empty query is recovered
322 * \see readPoolQueriesFromFile
324 bool recover( std::istream &str, char delim = '\n' );
327 * Writes a machine-readable string representation of the query to stream.
328 * Use \a delim as attribute delimiter.
330 * \param str output stream to write to
331 * \param delim delimiter for attributes
333 * \see writePoolQueriesToFile
335 void serialize( std::ostream &str, char delim = '\n' ) const;
337 /** Return a human-readable description of the query */
338 std::string asString() const;
340 bool operator==(const PoolQuery& b) const;
341 bool operator!=(const PoolQuery& b) const { return !(*this == b ); }
346 * Free function to get the satsolver repo search
354 * Free function to set the satsolver repo search
359 void setFlags( const Match & flags );
364 /** \deprecated unused, buggy and useless. */
365 ZYPP_DEPRECATED void setMatchFiles() {}
366 /** \deprecated unused, buggy and useless. */
367 ZYPP_DEPRECATED bool matchFiles() const { return false; }
368 /** \deprecated There should be no need for this internal value. To
369 * switch across all match mode types, use the enum values returned
370 * by \ref matchMode(). \see \ref Match::Mode.
373 ZYPP_DEPRECATED int matchType() const { return flags().modeval(); }
377 /** Pointer to implementation */
378 RW_pointer<Impl> _pimpl;
380 ///////////////////////////////////////////////////////////////////
382 /** \relates PoolQuery Stream output. */
383 std::ostream & operator<<( std::ostream & str, const PoolQuery & obj );
386 ///////////////////////////////////////////////////////////////////
388 { /////////////////////////////////////////////////////////////////
390 class PoolQueryMatcher;
392 ///////////////////////////////////////////////////////////////////
394 // CLASS NAME : PoolQuery::PoolQueryIterator
396 /** \ref PoolQuery iterator as returned by \ref PoolQuery::begin.
398 class PoolQueryIterator : public boost::iterator_adaptor<
399 PoolQueryIterator // Derived
400 , sat::LookupAttr::iterator // Base
401 , const sat::Solvable // Value
402 , boost::forward_traversal_tag // CategoryOrTraversal
403 , const sat::Solvable // Reference
407 /** Default ctor is also \c end.*/
411 /** \Ref PoolQuery ctor. */
412 PoolQueryIterator( const shared_ptr<PoolQueryMatcher> & matcher_r )
413 : _matcher( matcher_r )
417 friend class boost::iterator_core_access;
419 sat::Solvable dereference() const
420 { return base_reference().inSolvable(); }
425 shared_ptr<PoolQueryMatcher> _matcher;
427 ///////////////////////////////////////////////////////////////////
429 /** \relates PoolQueryIterator Stream output. */
430 inline std::ostream & operator<<( std::ostream & str, const PoolQueryIterator & obj )
431 { return str << obj.base(); }
433 ///////////////////////////////////////////////////////////////////
435 ///////////////////////////////////////////////////////////////////
437 inline detail::PoolQueryIterator PoolQuery::end() const
438 { return detail::PoolQueryIterator(); }
440 /////////////////////////////////////////////////////////////////
442 ///////////////////////////////////////////////////////////////////
444 #endif // ZYPP_POOLQUERY_H