1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/PoolQuery.cc
15 #include "zypp/base/Gettext.h"
16 #include "zypp/base/LogTools.h"
17 #include "zypp/base/Algorithm.h"
18 #include "zypp/base/String.h"
19 #include "zypp/repo/RepoException.h"
20 #include "zypp/RelCompare.h"
22 #include "zypp/sat/Pool.h"
23 #include "zypp/sat/Solvable.h"
24 #include "zypp/base/StrMatcher.h"
26 #include "zypp/PoolQuery.h"
28 #undef ZYPP_BASE_LOGGER_LOGGROUP
29 #define ZYPP_BASE_LOGGER_LOGGROUP "PoolQuery"
32 using namespace zypp::sat;
34 ///////////////////////////////////////////////////////////////////
36 { /////////////////////////////////////////////////////////////////
38 ///////////////////////////////////////////////////////////////////
40 { /////////////////////////////////////////////////////////////////
42 /////////////////////////////////////////////////////////////////
43 // some Helpers and Predicates
44 /////////////////////////////////////////////////////////////////
46 bool isDependencyAttribute( sat::SolvAttr attr_r )
48 static sat::SolvAttr deps[] = {
55 SolvAttr::supplements,
58 for_( it, arrayBegin(deps), arrayEnd(deps) )
64 /** Whether the current capabilities edition range ovelaps and/or its solvables arch matches.
65 * Query asserts \a iter_r points to a capability and we
66 * have to check the range only.
68 struct EditionRangePredicate
70 EditionRangePredicate( const Rel & op, const Edition & edition )
71 : _range( op, edition )
74 EditionRangePredicate( const Rel & op, const Edition & edition, const Arch & arch )
75 : _range( op, edition )
79 bool operator()( sat::LookupAttr::iterator iter_r )
81 if ( !_arch.empty() && iter_r.inSolvable().arch() != _arch )
84 CapDetail cap( iter_r.id() );
85 if ( ! cap.isSimple() )
87 if ( cap.isNamed() ) // no range to match
89 return overlaps( Edition::MatchRange( cap.op(), cap.ed() ), _range );
92 std::string serialize() const
94 std::string ret( "EditionRange" );
95 str::appendEscaped( ret, _range.op.asString() );
96 str::appendEscaped( ret, _range.value.asString() );
97 str::appendEscaped( ret, _arch.asString() );
101 Edition::MatchRange _range;
105 /** Whether the current Solvables edition is within a given range and/or its arch matches. */
106 struct SolvableRangePredicate
108 SolvableRangePredicate( const Rel & op, const Edition & edition )
109 : _range( op, edition )
110 , _arch( Arch_empty )
113 SolvableRangePredicate( const Rel & op, const Edition & edition, const Arch & arch )
114 : _range( op, edition )
118 bool operator()( sat::LookupAttr::iterator iter_r )
120 if ( !_arch.empty() && iter_r.inSolvable().arch() != _arch )
122 return overlaps( Edition::MatchRange( Rel::EQ, iter_r.inSolvable().edition() ), _range );
125 std::string serialize() const
127 std::string ret( "SolvableRange" );
128 str::appendEscaped( ret, _range.op.asString() );
129 str::appendEscaped( ret, _range.value.asString() );
130 str::appendEscaped( ret, _arch.asString() );
134 Edition::MatchRange _range;
138 /** Whether the current capability matches a given one.
139 * Query asserts \a iter_r points to a capability and we
140 * have to check the match only.
142 struct CapabilityMatchPredicate
144 CapabilityMatchPredicate( Capability cap_r )
148 bool operator()( sat::LookupAttr::iterator iter_r ) const
150 return _cap.matches( iter_r.asType<Capability>() ) == CapMatch::yes;
153 std::string serialize() const
155 std::string ret( "CapabilityMatch" );
156 str::appendEscaped( ret, _cap.asString() );
163 /////////////////////////////////////////////////////////////////
165 /////////////////////////////////////////////////////////////////
166 /** Match data per attribtue.
168 * This includes the attribute itself, an optional \ref StrMatcher
169 * to restrict the query to certain string values, and an optional
170 * boolean \ref Predicate that may apply further restrictions that can
171 * not be expressed by the \ref strMatcher.
173 * Example for such a \ref predicate would be an additional edition range
174 * check whan looking for dependencies. The \ref strMatcher would
175 * find potential matches by looking at the dependencies name, the
176 * predicate will then check the edition ranges.
178 * As the \ref predicate takes an iterator pointing to the current
179 * match, it's also suitable for sub-structure (flexarray) inspection
180 * (\see \ref sat::LookupAttr::iterator::solvAttrSubEntry).
182 * (bsc#1035729) If SolvAttr::name searches for an explicit \c kind:name,
183 * this \c kind is stored in \ref kindPredicate and will overwrite any
184 * 'global' kind restriction applied via \ref PoolQuery::addKind. This
185 * task can't be passed off to a predicate, as \ref PoolQueryMatcher::isAMatch
186 * must accept only explicit-kind-checking predicate matches, in case the
187 * 'global' kind restriction woudl otherwise discard the match.
189 * \note: \see \ref addPredicate for further constraints.
193 typedef function<bool(sat::LookupAttr::iterator)> Predicate;
195 static bool always( sat::LookupAttr::iterator ) { return true; }
196 static bool never( sat::LookupAttr::iterator ) { return false; }
201 AttrMatchData( sat::SolvAttr attr_r )
205 AttrMatchData( sat::SolvAttr attr_r, const StrMatcher & strMatcher_r )
207 , strMatcher( strMatcher_r )
210 AttrMatchData( sat::SolvAttr attr_r, const StrMatcher & strMatcher_r,
211 const Predicate & predicate_r, const std::string & predicateStr_r )
213 , strMatcher( strMatcher_r )
214 , predicate( predicate_r )
215 , predicateStr( predicateStr_r )
218 /** A usable Predicate must provide a string serialization.
219 * As there is no \c operator== for \ref Predicate, we compare it's
220 * string representation instead. If you add new predicated, check the
221 * deserialization code in \ref deserialize.
223 template<class TPredicate>
224 void addPredicate( const TPredicate & predicate_r )
226 predicate = predicate_r;
227 predicateStr = predicate_r.serialize();
230 /** Dumb serialization.
232 * AttrMatchData ATTRIBUTE SEARCHSTRING [C|SEARCHMODE|X] SERIALIZED_PREDICATE
235 std::string serialize() const
237 std::string ret( "AttrMatchData" );
238 str::appendEscaped( ret, attr.asString() );
239 str::appendEscaped( ret, strMatcher.searchstring() );
240 str::appendEscaped( ret, serializeMode( strMatcher.flags().mode() ) );
241 str::appendEscaped( ret, predicateStr );
245 /** Dumb restore from serialized string.
246 * \throw Exception on parse error.
248 static AttrMatchData deserialize( const std::string & str_r )
250 std::vector<std::string> words;
251 str::splitEscaped( str_r, std::back_inserter(words) );
252 if ( words.empty() || words[0] != "AttrMatchData" )
253 ZYPP_THROW( Exception( str::Str() << "Expecting AttrMatchData: " << str_r ) );
254 if ( words.size() != 5 )
255 ZYPP_THROW( Exception( str::Str() << "Wrong number of words: " << str_r ) );
258 ret.attr = sat::SolvAttr( words[1] );
259 ret.strMatcher = StrMatcher( words[2] );
260 if ( Match::Mode mode = deserializeMode( words[3] ) )
261 ret.strMatcher.setFlags( mode );
262 ret.predicateStr = words[4];
266 str::splitEscaped( ret.predicateStr, std::back_inserter(words) );
267 if ( ! words.empty() )
269 if ( words[0] == "EditionRange" )
271 switch( words.size() )
274 ret.predicate = EditionRangePredicate( Rel(words[1]), Edition(words[2]) );
277 ret.predicate = EditionRangePredicate( Rel(words[1]), Edition(words[2]), Arch(words[3]) );
280 ZYPP_THROW( Exception( str::Str() << "Wrong number of words: " << str_r ) );
284 else if ( words[0] == "SolvableRange" )
286 switch( words.size() )
289 ret.predicate = SolvableRangePredicate( Rel(words[1]), Edition(words[2]) );
292 ret.predicate = SolvableRangePredicate( Rel(words[1]), Edition(words[2]), Arch(words[3]) );
295 ZYPP_THROW( Exception( str::Str() << "Wrong number of words: " << str_r ) );
299 else if ( words[0] == "CapabilityMatch" )
301 if ( words.size() != 2 )
302 ZYPP_THROW( Exception( str::Str() << "Wrong number of words: " << str_r ) );
303 ret.predicate = CapabilityMatchPredicate( Capability(words[1]) );
306 ZYPP_THROW( Exception( str::Str() << "Unknown predicate: " << str_r ) );
312 StrMatcher strMatcher;
314 std::string predicateStr;
315 ResKind kindPredicate = ResKind::nokind; // holds the 'kind' part if SolvAttr:name looks for an explicit 'kind:name'
318 /** Serialize \ref strMatcher \ref Match::Mode */
319 static std::string serializeMode( Match::Mode mode_r )
321 // Legacy code used "[C|X]" to differ just between OTHER (need to (C)ompile) and
322 // using the default search mode. As we now allow to specify a SEARCHMODE we
323 // need to serialize it:
326 #define OUTS(M,S) case Match::M: return #S; break
331 OUTS( STRINGSTART, S );
332 OUTS( STRINGEND, E );
333 OUTS( SUBSTRING, B );
337 // everything else use default
344 /** Deserialize \ref strMatcher \ref Match::Mode */
345 static Match::Mode deserializeMode( const std::string & str_r )
349 #define OUTS(M,C) case *#C: return Match::M; break
354 OUTS( STRINGSTART, S );
355 OUTS( STRINGEND, E );
356 OUTS( SUBSTRING, B );
360 // everything else use default
364 return Match::NOTHING;
368 /** \relates AttrMatchData */
369 inline std::ostream & operator<<( std::ostream & str, const AttrMatchData & obj )
371 str << obj.attr << ": " << obj.strMatcher;
372 if ( obj.kindPredicate )
373 str << " +(" << obj.kindPredicate << ")";
375 str << " +(" << obj.predicateStr << ")";
379 /** \relates AttrMatchData */
380 inline bool operator==( const AttrMatchData & lhs, const AttrMatchData & rhs )
382 return ( lhs.attr == rhs.attr
383 && lhs.strMatcher == rhs.strMatcher
384 && lhs.predicateStr == rhs.predicateStr );
387 /** \relates AttrMatchData */
388 inline bool operator!=( const AttrMatchData & lhs, const AttrMatchData & rhs )
389 { return !( lhs == rhs ); }
391 /** \relates AttrMatchData Arbitrary order for std::container. */
392 inline bool operator<( const AttrMatchData & lhs, const AttrMatchData & rhs )
394 if ( lhs.attr != rhs.attr )
395 return ( lhs.attr < rhs.attr );
396 if ( lhs.strMatcher != rhs.strMatcher )
397 return ( lhs.strMatcher < rhs.strMatcher );
398 if ( lhs.predicateStr != rhs.predicateStr )
399 return ( lhs.predicateStr < rhs.predicateStr );
403 typedef std::list<AttrMatchData> AttrMatchList;
406 } /////////////////////////////////////////////////////////////////
408 ///////////////////////////////////////////////////////////////////
410 ///////////////////////////////////////////////////////////////////
412 // CLASS NAME : PoolQuery::Impl
415 class PoolQuery::Impl
419 : _flags( Match::SUBSTRING | Match::NOCASE | Match::SKIP_KIND )
428 /** String representation */
429 string asString() const;
431 /** \name Raw query options. */
433 /** Raw search strings. */
434 StrContainer _strings;
435 /** Raw attributes */
436 AttrRawStrMap _attrs;
437 /** Uncompiled attributes with predicate. */
438 std::set<AttrMatchData> _uncompiledPredicated;
440 /** Sat solver search flags */
444 /** Sat solver status flags */
445 StatusFilter _status_flags;
447 /** Edition condition operand */
449 /** Operator for edition condition */
452 /** Repos to search. */
455 /** Kinds to search */
461 bool operator<( const PoolQuery::Impl & rhs ) const
463 #define OUTS(A) if ( A != rhs.A ) return A < rhs.A;
466 OUTS( _uncompiledPredicated );
467 OUTS( _flags.get() );
469 OUTS( _status_flags );
471 OUTS( _op.inSwitch() );
478 bool operator==( const PoolQuery::Impl & rhs ) const
480 if ( _flags == rhs._flags
481 // bnc#792901: while libzypp uses exact match mode for a single
482 // package name lock, zypper always uses glob. :(
483 // We unify those two forms to enable zypper to remove zypp locks
484 // without need to actually evaluate the query (which would require
485 // repos to be loaded).
486 || ( ( ( _flags.isModeString() && rhs._flags.isModeGlob() )
487 || ( _flags.isModeGlob() && rhs._flags.isModeString() ) )
489 && _attrs.size() == 1
490 && _attrs.begin()->first == sat::SolvAttr::name ) )
492 return ( _strings == rhs._strings
493 && _attrs == rhs._attrs
494 && _uncompiledPredicated == rhs._uncompiledPredicated
495 && _match_word == rhs._match_word
496 && _status_flags == rhs._status_flags
497 && _edition == rhs._edition
499 && _repos == rhs._repos
500 && _kinds == rhs._kinds );
505 bool operator!=( const PoolQuery::Impl & rhs ) const
506 { return ! operator==( rhs ); }
509 /** Compile the regex.
510 * Basically building the \ref _attrMatchList from strings.
511 * \throws MatchException Any of the exceptions thrown by \ref StrMatcher::compile.
513 void compile() const;
515 /** StrMatcher per attribtue. */
516 mutable AttrMatchList _attrMatchList;
519 /** Join patterns in \a container_r according to \a flags_r into a single \ref StrMatcher.
520 * The \ref StrMatcher returned will be a REGEX if more than one pattern was passed.
522 StrMatcher joinedStrMatcher( const StrContainer & container_r, const Match & flags_r ) const;
525 friend Impl * rwcowClone<Impl>( const Impl * rhs );
526 /** clone for RWCOW_pointer */
528 { return new Impl( *this ); }
531 ///////////////////////////////////////////////////////////////////
535 MyInserter(PoolQuery::StrContainer & cont) : _cont(cont) {}
537 bool operator()(const string & str)
543 PoolQuery::StrContainer & _cont;
549 bool operator()(const string & str)
555 void PoolQuery::Impl::compile() const
557 _attrMatchList.clear();
559 if ( _flags.mode() == Match::OTHER ) // this will never succeed...
560 ZYPP_THROW( MatchUnknownModeException( _flags ) );
562 // 'different' - will have to iterate through all and match by ourselves (slow)
563 // 'same' - will pass the compiled string to dataiterator_init
564 // 'one-attr' - will pass it to dataiterator_init
565 // 'one-non-regex-str' - will pass to dataiterator_init, set flag to SEARCH_STRING or SEARCH_SUBSTRING
570 // create regex; store in rcstrings; if more strings flag regex;
573 ; // A default 'query-all' will be added after all sources are processed.
577 // else if _attrs is not empty but it contains just one attr
578 // for all _strings and _attr[key] strings
579 // create regex; flag 'one-attr'; if more strings flag regex;
580 else if (_attrs.size() == 1)
583 invokeOnEach(_strings.begin(), _strings.end(), EmptyFilter(), MyInserter(joined));
584 invokeOnEach(_attrs.begin()->second.begin(), _attrs.begin()->second.end(), EmptyFilter(), MyInserter(joined));
586 _attrMatchList.push_back( AttrMatchData( _attrs.begin()->first, joinedStrMatcher( joined, _flags ) ) );
589 // // MULTIPLE ATTRIBUTES
592 // check whether there are any per-attribute strings
593 bool attrvals_empty = true;
594 for_( ai, _attrs.begin(), _attrs.end() )
596 if ( ai->second.empty() )
598 for_( it, ai->second.begin(), ai->second.end() )
602 attrvals_empty = false;
606 if ( ! attrvals_empty )
610 // chceck whether the per-attribute strings are all the same
611 bool attrvals_thesame = true;
612 AttrRawStrMap::const_iterator ai = _attrs.begin();
613 const StrContainer & set1 = ai->second;
615 for (; ai != _attrs.end(); ++ai)
619 set1.begin(), set1.end(),
620 ai->second.begin(), ai->second.end(),
621 inserter(result, result.begin())/*, ltstr()*/);
624 attrvals_thesame = false;
629 // // THE SAME STRINGS FOR DIFFERENT ATTRS
630 // else if _attrs is not empty but it does not contain strings
631 // for each key in _attrs take all _strings
632 // create regex; store in rcstrings; flag 'same'; if more strings flag regex;
633 if (attrvals_empty || attrvals_thesame)
638 invokeOnEach(_strings.begin(), _strings.end(), EmptyFilter(), MyInserter(joined));
642 invokeOnEach(_strings.begin(), _strings.end(), EmptyFilter(), MyInserter(joined));
643 invokeOnEach(_attrs.begin()->second.begin(), _attrs.begin()->second.end(), EmptyFilter(), MyInserter(joined));
646 // May use the same StrMatcher for all
647 StrMatcher matcher( joinedStrMatcher( joined, _flags ) );
648 for_( ai, _attrs.begin(), _attrs.end() )
650 _attrMatchList.push_back( AttrMatchData( ai->first, matcher ) );
654 // // DIFFERENT STRINGS FOR DIFFERENT ATTRS
655 // if _attrs is not empty and it contains non-empty vectors with non-empty strings
656 // for each key in _attrs take all _strings + all _attrs[key] strings
657 // create regex; flag 'different'; if more strings flag regex;
660 for_(ai, _attrs.begin(), _attrs.end())
663 invokeOnEach(_strings.begin(), _strings.end(), EmptyFilter(), MyInserter(joined));
664 invokeOnEach(ai->second.begin(), ai->second.end(), EmptyFilter(), MyInserter(joined));
666 _attrMatchList.push_back( AttrMatchData( ai->first, joinedStrMatcher( joined, _flags ) ) );
671 // Now handle any predicated queries
672 if ( ! _uncompiledPredicated.empty() )
675 invokeOnEach( _strings.begin(), _strings.end(), EmptyFilter(), MyInserter(global) );
676 for_( it, _uncompiledPredicated.begin(), _uncompiledPredicated.end() )
678 if ( it->strMatcher.flags().mode() == Match::OTHER )
681 StrContainer joined( global );
682 const std::string & mstr( it->strMatcher.searchstring() );
683 if ( ! mstr.empty() )
684 joined.insert( mstr );
686 // copy and exchange the StrMatcher
687 AttrMatchData nattr( *it );
688 nattr.strMatcher = joinedStrMatcher( joined, _flags );
689 _attrMatchList.push_back( std::move(nattr) );
694 _attrMatchList.push_back( *it );
699 // If no attributes defined at all, then add 'query all'
700 if ( _attrMatchList.empty() )
702 _attrMatchList.push_back( AttrMatchData( sat::SolvAttr::allAttr, joinedStrMatcher( _strings, _flags ) ) );
705 // Finally check here, whether all involved regex compile.
706 for_( it, _attrMatchList.begin(), _attrMatchList.end() )
708 it->strMatcher.compile(); // throws on error
710 //DBG << asString() << endl;
713 ///////////////////////////////////////////////////////////////////
716 /** Escape \a str_r for use in a regex.
717 * \a flags_r determines whether the input string is interpreted
718 * as regex, glob or plain string.
720 std::string rxEscape( std::string str_r, const Match & flags_r )
722 if ( str_r.empty() || flags_r.isModeRegex() )
725 if ( flags_r.isModeGlob() )
726 return str::rxEscapeGlob( std::move(str_r) );
728 return str::rxEscapeStr( std::move(str_r) );
731 ///////////////////////////////////////////////////////////////////
733 StrMatcher PoolQuery::Impl::joinedStrMatcher( const StrContainer & container_r, const Match & flags_r ) const
735 if ( container_r.empty() )
736 return StrMatcher( std::string(), flags_r );
738 if ( container_r.size() == 1 && !_match_word ) // use RX to match words
739 return StrMatcher( *container_r.begin(), flags_r );
741 // Convert to a regex.
742 // Note: Modes STRING and GLOB match whole strings (anchored ^ $)
743 // SUBSTRING and REGEX match substrings (match_word anchores SUBSTRING \b)
744 Match retflags( flags_r );
745 retflags.setModeRegex();
748 if ( flags_r.isModeString() || flags_r.isModeGlob() )
750 else if ( _match_word )
755 for ( const::std::string & s : container_r )
757 ret << sep << rxEscape( s, flags_r );
763 if ( flags_r.isModeString() || flags_r.isModeGlob() )
765 else if ( _match_word )
768 return StrMatcher( ret, retflags );
771 string PoolQuery::Impl::asString() const
776 if ( _kinds.empty() )
780 for(Kinds::const_iterator it = _kinds.begin();
781 it != _kinds.end(); ++it)
787 if ( _repos.empty() )
791 for(StrContainer::const_iterator it = _repos.begin();
792 it != _repos.end(); ++it)
797 o << "version: "<< _op << " " << _edition.asString() << endl;
798 o << "status: " << ( _status_flags ? ( _status_flags == INSTALLED_ONLY ? "INSTALLED_ONLY" : "UNINSTALLED_ONLY" )
801 o << "string match flags: " << Match(_flags) << endl;
805 for(StrContainer::const_iterator it = _strings.begin();
806 it != _strings.end(); ++it)
810 o << "attributes: " << endl;
811 for(AttrRawStrMap::const_iterator ai = _attrs.begin(); ai != _attrs.end(); ++ai)
813 o << "* " << ai->first << ": ";
814 for(StrContainer::const_iterator vi = ai->second.begin();
815 vi != ai->second.end(); ++vi)
820 o << "predicated: " << endl;
821 for_( it, _uncompiledPredicated.begin(), _uncompiledPredicated.end() )
823 o << "* " << *it << endl;
827 o << "last attribute matcher compiled: " << endl;
828 if ( _attrMatchList.empty() )
830 o << "not yet compiled" << endl;
834 for_( it, _attrMatchList.begin(), _attrMatchList.end() )
836 o << "* " << *it << endl;
842 ///////////////////////////////////////////////////////////////////
844 ///////////////////////////////////////////////////////////////////
846 // CLASS NAME : PoolQuery
848 ///////////////////////////////////////////////////////////////////
850 PoolQuery::PoolQuery()
854 PoolQuery::~PoolQuery()
857 void PoolQuery::addRepo(const std::string &repoalias)
859 if (repoalias.empty())
861 WAR << "ignoring an empty repository alias" << endl;
864 _pimpl->_repos.insert(repoalias);
867 void PoolQuery::addKind(const ResKind & kind)
868 { _pimpl->_kinds.insert(kind); }
870 void PoolQuery::addString(const string & value)
871 { _pimpl->_strings.insert(value); }
873 void PoolQuery::addAttribute(const sat::SolvAttr & attr, const std::string & value)
874 { _pimpl->_attrs[attr].insert(value); }
876 void PoolQuery::addDependency( const sat::SolvAttr & attr, const std::string & name, const Rel & op, const Edition & edition )
878 // Default Match::OTHER indicates need to compile, i.e. to merge name into the global search string and mode.
879 return addDependency( attr, name, op, edition, Arch_empty, Match::OTHER );
882 void PoolQuery::addDependency( const sat::SolvAttr & attr, const std::string & name, const Rel & op, const Edition & edition, const Arch & arch )
884 // Default Match::OTHER indicates need to compile, i.e. to merge name into the global search string and mode.
885 return addDependency( attr, name, op, edition, arch, Match::OTHER );
888 void PoolQuery::addDependency( const sat::SolvAttr & attr, const std::string & name, const Rel & op, const Edition & edition, const Arch & arch, Match::Mode mode )
890 if ( op == Rel::NONE ) // will never match.
893 // SolvAttr::name with explicit 'kind:name' will overwrite the default _kinds
894 ResKind explicitKind;
895 if ( attr == sat::SolvAttr::name )
896 explicitKind = ResKind::explicitBuiltin( name );
898 // Legacy: Match::OTHER and no additional constraints on edition/arch/kind
899 // require addAttribute, otherwise de-serialisation breaks (serialized
900 // and de-serialized query could be !=).
901 // From the results POV we could also use the predicated case below.
902 if ( op == Rel::ANY && arch.empty() && !explicitKind && mode == Match::OTHER )
904 addAttribute( attr, name );
908 // Match::OTHER indicates need to compile
909 // (merge global search strings into name).
910 AttrMatchData attrMatchData( attr );
912 attrMatchData.strMatcher = StrMatcher( name, mode );
915 // ResKind::explicitBuiltin call above asserts the presence of the ':' in name
916 attrMatchData.strMatcher = StrMatcher( strchr( name.c_str(), ':')+1, mode );
917 attrMatchData.kindPredicate = explicitKind;
920 if ( isDependencyAttribute( attr ) )
921 attrMatchData.addPredicate( EditionRangePredicate( op, edition, arch ) );
923 attrMatchData.addPredicate( SolvableRangePredicate( op, edition, arch ) );
925 _pimpl->_uncompiledPredicated.insert( attrMatchData );
928 void PoolQuery::addDependency( const sat::SolvAttr & attr, Capability cap_r )
930 CapDetail cap( cap_r );
931 if ( ! cap.isSimple() ) // will never match.
934 // Matches STRING per default. (won't get compiled!)
935 AttrMatchData attrMatchData( attr, StrMatcher( cap.name().asString() ) );
937 if ( isDependencyAttribute( attr ) )
938 attrMatchData.addPredicate( CapabilityMatchPredicate( cap_r ) );
940 attrMatchData.addPredicate( SolvableRangePredicate( cap.op(), cap.ed() ) );
942 _pimpl->_uncompiledPredicated.insert( attrMatchData );
945 void PoolQuery::setEdition(const Edition & edition, const Rel & op)
947 _pimpl->_edition = edition;
951 void PoolQuery::setMatchSubstring() { _pimpl->_flags.setModeSubstring(); _pimpl->_match_word = false; }
952 void PoolQuery::setMatchExact() { _pimpl->_flags.setModeString(); _pimpl->_match_word = false; }
953 void PoolQuery::setMatchRegex() { _pimpl->_flags.setModeRegex(); _pimpl->_match_word = false; }
954 void PoolQuery::setMatchGlob() { _pimpl->_flags.setModeGlob(); _pimpl->_match_word = false; }
955 void PoolQuery::setMatchWord() { _pimpl->_flags.setModeSubstring(); _pimpl->_match_word = true; }
957 Match PoolQuery::flags() const
958 { return _pimpl->_flags; }
959 void PoolQuery::setFlags( const Match & flags )
960 { _pimpl->_flags = flags; }
963 void PoolQuery::setInstalledOnly()
964 { _pimpl->_status_flags = INSTALLED_ONLY; }
965 void PoolQuery::setUninstalledOnly()
966 { _pimpl->_status_flags = UNINSTALLED_ONLY; }
967 void PoolQuery::setStatusFilterFlags( PoolQuery::StatusFilter flags )
968 { _pimpl->_status_flags = flags; }
971 const PoolQuery::StrContainer &
972 PoolQuery::strings() const
973 { return _pimpl->_strings; }
975 const PoolQuery::AttrRawStrMap &
976 PoolQuery::attributes() const
977 { return _pimpl->_attrs; }
979 const PoolQuery::StrContainer &
980 PoolQuery::attribute(const sat::SolvAttr & attr) const
982 static const PoolQuery::StrContainer nocontainer;
983 AttrRawStrMap::const_iterator it = _pimpl->_attrs.find(attr);
984 return it != _pimpl->_attrs.end() ? it->second : nocontainer;
987 const Edition PoolQuery::edition() const
988 { return _pimpl->_edition; }
989 const Rel PoolQuery::editionRel() const
990 { return _pimpl->_op; }
993 const PoolQuery::Kinds &
994 PoolQuery::kinds() const
995 { return _pimpl->_kinds; }
997 const PoolQuery::StrContainer &
998 PoolQuery::repos() const
999 { return _pimpl->_repos; }
1002 bool PoolQuery::caseSensitive() const
1003 { return !_pimpl->_flags.test( Match::NOCASE ); }
1004 void PoolQuery::setCaseSensitive( bool value )
1005 { _pimpl->_flags.turn( Match::NOCASE, !value ); }
1007 bool PoolQuery::filesMatchFullPath() const
1008 { return _pimpl->_flags.test( Match::FILES ); }
1009 void PoolQuery::setFilesMatchFullPath( bool value )
1010 { _pimpl->_flags.turn( Match::FILES, value ); }
1012 bool PoolQuery::matchExact() const { return _pimpl->_flags.isModeString(); }
1013 bool PoolQuery::matchSubstring() const { return _pimpl->_flags.isModeSubstring() && !_pimpl->_match_word; }
1014 bool PoolQuery::matchGlob() const { return _pimpl->_flags.isModeGlob(); }
1015 bool PoolQuery::matchRegex() const { return _pimpl->_flags.isModeRegex(); }
1016 bool PoolQuery::matchWord() const { return _pimpl->_flags.isModeSubstring() && _pimpl->_match_word; }
1018 PoolQuery::StatusFilter PoolQuery::statusFilterFlags() const
1019 { return _pimpl->_status_flags; }
1021 bool PoolQuery::empty() const
1023 try { return begin() == end(); }
1024 catch (const Exception & ex) {}
1028 PoolQuery::size_type PoolQuery::size() const
1032 size_type count = 0;
1033 for_( it, begin(), end() )
1037 catch (const Exception & ex) {}
1041 void PoolQuery::execute(ProcessResolvable fnc)
1042 { invokeOnEach( begin(), end(), fnc); }
1045 /*DEPRECATED LEGACY:*/void PoolQuery::setRequireAll( bool ) {}
1046 /*DEPRECATED LEGACY:*/bool PoolQuery::requireAll() const { return false; }
1048 ///////////////////////////////////////////////////////////////////
1050 // CLASS NAME : PoolQuery::Attr
1053 * represents all atributes in PoolQuery except SolvAtributes, which are
1054 * used as is (not needed extend anything if someone adds new solv attr)
1056 struct PoolQueryAttr : public IdStringType<PoolQueryAttr>
1059 friend class IdStringType<PoolQueryAttr>;
1066 explicit PoolQueryAttr( const char* cstr_r )
1070 explicit PoolQueryAttr( const std::string & str_r )
1074 // unknown atributes
1075 static const PoolQueryAttr noAttr;
1077 // PoolQuery's own attributes
1078 static const PoolQueryAttr repoAttr;
1079 static const PoolQueryAttr kindAttr;
1080 static const PoolQueryAttr stringAttr;
1081 static const PoolQueryAttr stringTypeAttr;
1082 static const PoolQueryAttr requireAllAttr; // LEAGACY: attribute was defined but never implemented.
1083 static const PoolQueryAttr caseSensitiveAttr;
1084 static const PoolQueryAttr installStatusAttr;
1085 static const PoolQueryAttr editionAttr;
1086 static const PoolQueryAttr complexAttr;
1089 const PoolQueryAttr PoolQueryAttr::noAttr;
1091 const PoolQueryAttr PoolQueryAttr::repoAttr( "repo" );
1092 const PoolQueryAttr PoolQueryAttr::kindAttr( "type" );
1093 const PoolQueryAttr PoolQueryAttr::stringAttr( "query_string" );
1094 const PoolQueryAttr PoolQueryAttr::stringTypeAttr("match_type");
1095 const PoolQueryAttr PoolQueryAttr::requireAllAttr("require_all"); // LEAGACY: attribute was defined but never implemented.
1096 const PoolQueryAttr PoolQueryAttr::caseSensitiveAttr("case_sensitive");
1097 const PoolQueryAttr PoolQueryAttr::installStatusAttr("install_status");
1098 const PoolQueryAttr PoolQueryAttr::editionAttr("version");
1099 const PoolQueryAttr PoolQueryAttr::complexAttr("complex");
1101 class StringTypeAttr : public IdStringType<PoolQueryAttr>
1103 friend class IdStringType<StringTypeAttr>;
1108 explicit StringTypeAttr( const char* cstr_r )
1110 explicit StringTypeAttr( const std::string & str_r )
1113 static const StringTypeAttr noAttr;
1115 static const StringTypeAttr exactAttr;
1116 static const StringTypeAttr substringAttr;
1117 static const StringTypeAttr regexAttr;
1118 static const StringTypeAttr globAttr;
1119 static const StringTypeAttr wordAttr;
1122 const StringTypeAttr StringTypeAttr::noAttr;
1124 const StringTypeAttr StringTypeAttr::exactAttr("exact");
1125 const StringTypeAttr StringTypeAttr::substringAttr("substring");
1126 const StringTypeAttr StringTypeAttr::regexAttr("regex");
1127 const StringTypeAttr StringTypeAttr::globAttr("glob");
1128 const StringTypeAttr StringTypeAttr::wordAttr("word");
1130 ///////////////////////////////////////////////////////////////////
1133 //\TODO maybe ctor with stream can be usefull
1134 //\TODO let it throw, let it throw, let it throw.
1135 bool PoolQuery::recover( istream &str, char delim )
1137 bool finded_something = false; //indicates some atributes is finded
1143 getline( str, s, delim );
1145 if ((!s.empty()) && s[0]=='#') //comment
1150 string::size_type pos = s.find(':');
1151 if (s.empty() || pos == s.npos) // some garbage on line... act like blank line
1153 if (finded_something) //is first blank line after record?
1163 finded_something = true;
1165 string attrName(str::trim(string(s,0,pos))); // trimmed name of atribute
1166 string attrValue(str::trim(string(s,pos+1,s.npos))); //trimmed value
1168 PoolQueryAttr attribute( attrName );
1170 if ( attribute==PoolQueryAttr::repoAttr )
1172 addRepo( attrValue );
1174 /* some backwards compatibility */
1175 else if ( attribute==PoolQueryAttr::kindAttr || attribute=="kind" )
1177 addKind( ResKind(attrValue) );
1179 else if ( attribute==PoolQueryAttr::stringAttr
1180 || attribute=="global_string")
1182 addString( attrValue );
1184 else if ( attribute==PoolQueryAttr::stringTypeAttr
1185 || attribute=="string_type" )
1187 StringTypeAttr s(attrValue);
1188 if( s == StringTypeAttr::regexAttr )
1192 else if ( s == StringTypeAttr::globAttr )
1196 else if ( s == StringTypeAttr::exactAttr )
1200 else if ( s == StringTypeAttr::substringAttr )
1202 setMatchSubstring();
1204 else if ( s == StringTypeAttr::wordAttr )
1208 else if ( s == StringTypeAttr::noAttr )
1210 WAR << "unknown string type " << attrValue << endl;
1214 WAR << "forget recover some attribute defined as String type attribute: " << attrValue << endl;
1217 else if ( attribute==PoolQueryAttr::requireAllAttr )
1219 // LEAGACY: attribute was defined but never implemented.
1220 // Actually it should not occur outside our testcases.
1222 else if ( attribute==PoolQueryAttr::caseSensitiveAttr )
1224 if ( str::strToTrue(attrValue) )
1226 setCaseSensitive(true);
1228 else if ( !str::strToFalse(attrValue) )
1230 setCaseSensitive(false);
1234 WAR << "unknown boolean value " << attrValue << endl;
1237 else if ( attribute==PoolQueryAttr::installStatusAttr )
1239 if( attrValue == "all" )
1241 setStatusFilterFlags( ALL );
1243 else if( attrValue == "installed" )
1247 else if( attrValue == "not-installed" )
1249 setUninstalledOnly();
1253 WAR << "Unknown value for install status " << attrValue << endl;
1256 else if ( attribute == PoolQueryAttr::editionAttr)
1258 string::size_type pos;
1260 if (attrValue.find_first_of("=<>!") == 0)
1262 pos = attrValue.find_last_of("=<>");
1263 rel = Rel(attrValue.substr(0, pos+1));
1264 attrValue = str::trim(attrValue.substr(pos+1, attrValue.npos));
1267 setEdition(Edition(attrValue), rel);
1269 else if ( attribute == PoolQueryAttr::complexAttr )
1273 _pimpl->_uncompiledPredicated.insert( AttrMatchData::deserialize( attrValue ) );
1275 catch ( const Exception & err )
1277 WAR << "Unparsable value for complex: " << err.asUserHistory() << endl;
1281 else if ( attribute==PoolQueryAttr::noAttr )
1283 WAR << "empty attribute name" << endl;
1287 string s = attrName;
1288 str::replaceAll( s,"_",":" );
1290 if ( a == SolvAttr::name || isDependencyAttribute( a ) )
1292 Capability c( attrValue );
1294 if ( d.isVersioned() )
1295 addDependency( a, d.name().asString(), d.op(), d.ed() );
1297 addDependency( a, attrValue );
1300 addAttribute( a, attrValue );
1305 return finded_something;
1308 void PoolQuery::serialize( ostream &str, char delim ) const
1312 //iterate thrue all settings and write it
1313 static const zypp::PoolQuery q; //not save default options, so create default query example
1315 for_( it, repos().begin(), repos().end() )
1317 str << "repo: " << *it << delim ;
1320 for_( it, kinds().begin(), kinds().end() )
1322 str << PoolQueryAttr::kindAttr.asString() << ": "
1323 << it->idStr() << delim ;
1326 if (editionRel() != Rel::ANY && edition() != Edition::noedition)
1327 str << PoolQueryAttr::editionAttr.asString() << ": " << editionRel() << " " << edition() << delim;
1329 if (matchMode()!=q.matchMode())
1331 switch( matchMode() )
1334 str << PoolQueryAttr::stringTypeAttr.asString() << ": exact" << delim;
1336 case Match::SUBSTRING:
1337 str << PoolQueryAttr::stringTypeAttr.asString()
1338 << ": substring" << delim;
1341 str << PoolQueryAttr::stringTypeAttr.asString() << ": glob" << delim;
1344 str << PoolQueryAttr::stringTypeAttr.asString() << ": regex" << delim;
1347 WAR << "unknown match type " << matchMode() << endl;
1351 if( caseSensitive() != q.caseSensitive() )
1353 str << "case_sensitive: ";
1354 if (caseSensitive())
1356 str << "on" << delim;
1360 str << "off" << delim;
1364 if( statusFilterFlags() != q.statusFilterFlags() )
1366 switch( statusFilterFlags() )
1369 str << "install_status: all" << delim;
1371 case INSTALLED_ONLY:
1372 str << "install_status: installed" << delim;
1374 case UNINSTALLED_ONLY:
1375 str << "install_status: not-installed" << delim;
1380 for_( it, strings().begin(), strings().end() )
1382 str << PoolQueryAttr::stringAttr.asString()<< ": " << *it << delim;
1385 for_( it, attributes().begin(), attributes().end() )
1387 string s = it->first.asString();
1388 str::replaceAll(s,":","_");
1389 for_( it2,it->second.begin(),it->second.end() )
1391 str << s <<": "<< *it2 << delim;
1395 for_( it, _pimpl->_uncompiledPredicated.begin(), _pimpl->_uncompiledPredicated.end() )
1397 str << "complex: "<< it->serialize() << delim;
1400 //separating delim - protection
1404 string PoolQuery::asString() const
1405 { return _pimpl->asString(); }
1407 ostream & operator<<( ostream & str, const PoolQuery & obj )
1408 { return str << obj.asString(); }
1410 std::ostream & dumpOn( std::ostream & str, const PoolQuery & obj )
1411 { return dumpRange( str << obj, obj.begin(), obj.end() ); }
1413 bool PoolQuery::operator==( const PoolQuery & rhs ) const
1414 { return *_pimpl == *rhs._pimpl; }
1416 bool PoolQuery::operator<( const PoolQuery & rhs ) const
1417 { return *_pimpl < *rhs._pimpl; }
1419 ///////////////////////////////////////////////////////////////////
1421 { /////////////////////////////////////////////////////////////////
1423 ///////////////////////////////////////////////////////////////////
1425 // CLASS NAME : PoolQueryMatcher
1427 /** Store \ref PoolQuery settings and assist \ref PoolQueryIterator.
1429 * Basically the matcher performs a base query, which should preselect
1430 * candidates for a match. And has some filter conditions on top of it.
1431 * Query and fileter depend on the \ref PoolQuery settings.
1433 * Matcher must be stateless, as it is shared between multiple
1434 * \ref PoolQueryIterator instances.
1436 * If \ref base_iterator is at the \ref end, \ref advance moves it
1437 * to the first match. Otherwise advance moves to the next match, or
1438 * to the \ref end, if there is no more match.
1440 * \note The original implementation treated an empty search string as
1441 * <it>"match always"</it>. We stay compatible.
1443 class PoolQueryMatcher
1446 typedef sat::LookupAttr::iterator base_iterator;
1449 const base_iterator & end() const
1451 static base_iterator _end;
1455 bool advance( base_iterator & base_r ) const
1457 if ( base_r == end() )
1458 base_r = startNewQyery(); // first candidate
1461 base_r.nextSkipSolvable(); // assert we don't visit this Solvable again
1462 ++base_r; // advance to next candidate
1465 while ( base_r != end() )
1467 if ( isAMatch( base_r ) )
1469 // No match: try next
1475 /** Provide all matching attributes within this solvable.
1478 void matchDetail( const base_iterator & base_r, std::vector<base_iterator> & return_r ) const
1480 if ( base_r == end() )
1483 sat::Solvable inSolvable( base_r.inSolvable() );
1485 if ( _attrMatchList.size() == 1 )
1487 // base_r is already on the 1st matching attribute!
1488 // String matching is done by the base iterator. We must check the predicate here.
1489 // Let's see if there are more matches for this solvable:
1490 base_iterator base( base_r );
1491 base.stayInThisSolvable(); // avoid discarding matches we found far away from here.
1492 return_r.push_back( base );
1494 const AttrMatchData::Predicate & predicate( _attrMatchList.front().predicate );
1495 for ( ++base; base.inSolvable() == inSolvable; ++base ) // safe even if base == end()
1497 if ( ! predicate || predicate( base ) )
1498 return_r.push_back( base );
1503 // Here: search all attributes ;(
1504 for_( mi, _attrMatchList.begin(), _attrMatchList.end() )
1506 const AttrMatchData & matchData( *mi );
1507 sat::LookupAttr q( matchData.attr, inSolvable );
1508 if ( matchData.strMatcher ) // an empty searchstring matches always
1509 q.setStrMatcher( matchData.strMatcher );
1511 if ( ! q.empty() ) // there are matches.
1513 // now check any predicate:
1514 const AttrMatchData::Predicate & predicate( matchData.predicate );
1515 for_( it, q.begin(), q.end() )
1517 if ( ! predicate || predicate( it ) )
1518 return_r.push_back( it );
1526 /** Ctor stores the \ref PoolQuery settings.
1527 * \throw MatchException Any of the exceptions thrown by \ref PoolQuery::Impl::compile.
1529 PoolQueryMatcher( const shared_ptr<const PoolQuery::Impl> & query_r )
1533 // Repo restriction:
1534 sat::Pool satpool( sat::Pool::instance() );
1536 for_( it, query_r->_repos.begin(), query_r->_repos.end() )
1538 Repository r( satpool.reposFind( *it ) );
1542 _neverMatchRepo = true;
1544 // _neverMatchRepo: we just need to catch the case that no repo
1545 // matched, so we'd interpret the empty list as 'take from all'
1546 if ( _neverMatchRepo && ! _repos.empty() )
1547 _neverMatchRepo = false;
1549 // Kind restriction:
1550 _kinds = query_r->_kinds;
1551 // Edition restriction:
1553 _edition = query_r->_edition;
1554 // Status restriction:
1555 _status_flags = query_r->_status_flags;
1557 _attrMatchList = query_r->_attrMatchList;
1564 /** Initialize a new base query. */
1565 base_iterator startNewQyery() const
1569 if ( _neverMatchRepo )
1572 // Repo restriction:
1573 if ( _repos.size() == 1 )
1574 q.setRepo( *_repos.begin() );
1575 // else: handled in isAMatch.
1577 // Attribute restriction:
1578 if ( _attrMatchList.size() == 1 ) // all (SolvAttr::allAttr) or 1 attr
1580 const AttrMatchData & matchData( _attrMatchList.front() );
1581 q.setAttr( matchData.attr );
1582 if ( matchData.strMatcher ) // empty searchstring matches always
1583 q.setStrMatcher( matchData.strMatcher );
1585 else // more than 1 attr (but not all)
1587 // no restriction, it's all handled in isAMatch.
1588 q.setAttr( sat::SolvAttr::allAttr );
1595 /** Check whether we are on a match.
1597 * The check covers the whole Solvable, not just the current
1598 * attribute \c base_r points to. If there's no match, also
1599 * prepare \c base_r to advance appropriately. If there is
1600 * a match, simply return \c true. \ref advance always moves
1601 * to the next Solvable if there was a match.
1603 * \note: Caller asserts we're not at \ref end.
1605 bool isAMatch( base_iterator & base_r ) const
1607 /////////////////////////////////////////////////////////////////////
1608 Repository inRepo( base_r.inRepo() );
1609 // Status restriction:
1611 && ( (_status_flags == PoolQuery::INSTALLED_ONLY) != inRepo.isSystemRepo() ) )
1613 base_r.nextSkipRepo();
1616 // Repo restriction:
1617 if ( _repos.size() > 1 && _repos.find( inRepo ) == _repos.end() )
1619 base_r.nextSkipRepo();
1622 /////////////////////////////////////////////////////////////////////
1623 sat::Solvable inSolvable( base_r.inSolvable() );
1624 // Edition restriction:
1625 if ( _op != Rel::ANY && !compareByRel( _op, inSolvable.edition(), _edition, Edition::Match() ) )
1627 base_r.nextSkipSolvable();
1631 // Kind restriction:
1632 // Delay the decision to nextSkipSolvable and return false, as there may be
1633 // some explicit kind:name predicate which overrules the global kinds.
1634 bool globalKindOk =( _kinds.empty() || inSolvable.isKind( _kinds.begin(), _kinds.end() ) );
1636 /////////////////////////////////////////////////////////////////////
1637 // string and predicate matching:
1639 if ( _attrMatchList.size() == 1 )
1641 // String matching was done by the base iterator.
1642 // Now check any predicate:
1643 const AttrMatchData & matchData( _attrMatchList.front() );
1645 if ( matchData.kindPredicate )
1647 if ( matchData.kindPredicate != inSolvable.kind() )
1649 base_r.nextSkipSolvable(); // this matchData will never match in this solvable
1653 else if ( !globalKindOk )
1654 return false; // only matching kindPredicate could overwrite this
1656 if ( !matchData.predicate || matchData.predicate( base_r ) )
1659 return false; // no skip as there may be more occurrences in this solvable of this attr.
1662 // Here: search all attributes ;(
1663 for_( mi, _attrMatchList.begin(), _attrMatchList.end() )
1665 const AttrMatchData & matchData( *mi );
1667 if ( matchData.kindPredicate )
1669 if ( matchData.kindPredicate != inSolvable.kind() )
1670 continue; // this matchData does not apply
1672 else if ( !globalKindOk )
1673 continue; // only matching kindPredicate could overwrite this
1675 sat::LookupAttr q( matchData.attr, inSolvable );
1676 if ( matchData.strMatcher ) // an empty searchstring matches always
1677 q.setStrMatcher( matchData.strMatcher );
1679 if ( ! q.empty() ) // there are matches.
1681 // now check any predicate:
1682 const AttrMatchData::Predicate & predicate( matchData.predicate );
1685 for_( it, q.begin(), q.end() )
1687 if ( predicate( it ) )
1695 base_r.nextSkipSolvable();
1700 /** Repositories include in the search. */
1701 std::set<Repository> _repos;
1702 DefaultIntegral<bool,false> _neverMatchRepo;
1703 /** Resolvable kinds to include. */
1704 std::set<ResKind> _kinds;
1705 /** Edition filter. */
1708 /** Installed status filter flags. \see PoolQuery::StatusFilter */
1710 /** StrMatcher per attribtue. */
1711 AttrMatchList _attrMatchList;
1713 ///////////////////////////////////////////////////////////////////
1715 void PoolQueryIterator::increment()
1717 // matcher restarts if at end! It is called from the ctor
1718 // to get the 1st match. But if the end is reached, it should
1719 // be deleted, otherwise we'd start over again.
1723 _matches.reset(); // invalidate old matches
1724 if ( ! _matcher->advance( base_reference() ) )
1728 const PoolQueryIterator::Matches & PoolQueryIterator::matches() const
1736 static const Matches _none;
1740 _matches.reset( new Matches );
1741 _matcher->matchDetail( base_reference(), *_matches );
1745 std::ostream & dumpOn( std::ostream & str, const PoolQueryIterator & obj )
1748 if ( ! obj.matchesEmpty() )
1750 for_( it, obj.matchesBegin(), obj.matchesEnd() )
1752 str << endl << " " << it->inSolvAttr() << "\t" << it->asString();
1758 ///////////////////////////////////////////////////////////////////
1759 } //namespace detail
1760 ///////////////////////////////////////////////////////////////////
1762 detail::PoolQueryIterator PoolQuery::begin() const
1764 return shared_ptr<detail::PoolQueryMatcher>( new detail::PoolQueryMatcher( _pimpl.getPtr() ) );
1767 /////////////////////////////////////////////////////////////////
1769 ///////////////////////////////////////////////////////////////////