libzypp 17.38.6
solvablespec.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
10#include <zypp/ng/sat/pool.h>
16
17#include <vector>
18
19extern "C" {
20#include <solv/pool.h>
21}
22
23namespace zyppng::sat {
24
25 bool SolvableSpec::contains( Pool & pool_r, const Solvable & solv_r ) const
26 {
27 if ( !solv_r || solv_r.isKind( ResKind::srcpackage ) )
28 return false;
29
30 // Fast path: ident match
31 if ( !_idents.empty() && _idents.count( solv_r.ident() ) )
32 return true;
33
34 // Provides path: for each token, check the whatprovides list
35 if ( !_provides.empty() ) {
36 detail::CPool * cpool = pool_r.get();
37 for ( const Capability & cap : _provides ) {
38 // pool_whatprovides returns an offset into whatprovidesdata[].
39 // The list is terminated by a 0 entry.
40 unsigned offset = ::pool_whatprovides( cpool, cap.id() );
41 const detail::IdType * p = cpool->whatprovidesdata + offset;
42 for ( ; *p; ++p ) {
43 if ( static_cast<detail::SolvableIdType>(*p) == solv_r.id() )
44 return true;
45 }
46 }
47 }
48
49 return false;
50 }
51
52 void SolvableSpec::parse( std::string_view spec_r )
53 {
54 static constexpr std::string_view providesPrefix { "provides:" };
55 if ( spec_r.empty() || spec_r[0] == '#' )
56 return;
57 if ( spec_r.substr( 0, providesPrefix.size() ) == providesPrefix )
58 addProvides( Capability( std::string( spec_r.substr( providesPrefix.size() ) ).c_str() ) );
59 else
60 addIdent( IdString( std::string( spec_r ).c_str() ) );
61 }
62
64 {
66 [this]( int /*num_r*/, const std::string & line_r ) -> bool {
67 parse( line_r );
68 return true;
69 });
70 }
71
72 void SolvableSpec::splitParseFrom( std::string_view multispec_r )
73 {
74 std::vector<std::string> tokens;
75 zypp::str::splitEscaped( std::string( multispec_r ).c_str(),
76 std::back_inserter(tokens), ", \t" );
77 parseFrom( tokens.begin(), tokens.end() );
78 }
79
80 // -------------------------------------------------------------------------
81 // EvaluatedSolvableSpec
82 // -------------------------------------------------------------------------
83
85 {
86 if ( spec_r.empty() )
87 return;
88
89 detail::CPool * cpool = pp_r.get();
90 Pool & pool_r = pp_r.pool();
91
92 // Ident-based: walk all solvables and check ident membership.
93 if ( !spec_r.idents().empty() ) {
94 for ( detail::SolvableIdType id = pool_r.getFirstId();
96 id = pool_r.getNextId( id ) )
97 {
98 Solvable solv( id );
99 if ( !solv.isKind( ResKind::srcpackage )
100 && spec_r.idents().count( solv.ident() ) )
101 _ids.insert( id );
102 }
103 }
104
105 // Provides-based: use libsolv's whatprovides index (guaranteed valid via PreparedPool).
106 for ( const Capability & cap : spec_r.provides() ) {
107 unsigned offset = pp_r.whatProvidesCapabilityId( cap.id() );
108 detail::IdType id = pp_r.whatProvidesData( offset );
109 for ( ; id; id = pp_r.whatProvidesData( ++offset ) ) {
110 if ( !Solvable( static_cast<detail::SolvableIdType>(id) ).isKind( ResKind::srcpackage ) )
111 _ids.insert( static_cast<detail::SolvableIdType>( id ) );
112 }
113 }
114 }
115
116 bool EvaluatedSolvableSpec::contains( const Solvable & solv_r ) const
117 {
118 if ( !solv_r || solv_r.isKind( ResKind::srcpackage ) )
119 return false;
120 return _ids.count( solv_r.id() ) != 0;
121 }
122
123} // namespace zyppng::sat
Helper to create and pass std::istream.
Definition inputstream.h:57
std::istream & stream() const
The std::istream.
Definition inputstream.h:93
static const ResKind srcpackage
Definition ResKind.h:44
A sat capability.
Definition capability.h:74
bool contains(const Solvable &solv_r) const
O(1) membership test — no pool access required.
EvaluatedSolvableSpec(PreparedPool &pp_r, const SolvableSpec &spec_r)
Construct by evaluating spec_r against pp_r.
Orchestrator for a libsolv pool instance.
Definition pool.h:37
detail::CPool * get() const
Expert backdoor.
Definition pool.h:59
detail::SolvableIdType getFirstId() const
Get id of the first valid Solvable.
Definition pool.h:242
detail::SolvableIdType getNextId(detail::SolvableIdType id_r) const
Get id of the next valid Solvable.
Definition pool.h:250
A move-only, non-owning view of a Pool that guarantees the whatprovides index is valid.
Pool & pool() const noexcept
Access the owning Pool.
detail::IdType whatProvidesData(unsigned offset_r) const
Returns the id stored at offset_r in the whatprovidesdata array.
unsigned whatProvidesCapabilityId(detail::IdType cap_r) const
Returns the offset into the internal whatprovidesdata array for cap_r.
detail::CPool * get() const noexcept
Expert backdoor — raw libsolv pool pointer.
A pure data container describing a set of solvables by ident and/or provides.
void parse(std::string_view spec_r)
Parse and add a single spec entry.
bool empty() const
Whether the spec has no idents and no provides tokens.
void parseFrom(const zypp::InputStream &istr_r)
Parse and add specs from istr_r (one per line; #-comments and empty lines are skipped automatically).
const CapabilitySet & provides() const
const IdStringSet & idents() const
void addIdent(IdString ident_r)
Add ident_r to the ident set.
bool contains(Pool &pool_r, const Solvable &solv_r) const
Test whether a single Solvable matches this spec.
void addProvides(Capability cap_r)
Add cap_r to the provides set.
void splitParseFrom(std::string_view multispec_r)
Split multispec_r on ',', ' ', '\t' and parse each token.
A Solvable object within the sat Pool.
Definition solvable.h:65
IdString ident() const
The identifier.
Definition solvable.cc:178
IdType id() const
Expert backdoor.
Definition solvable.h:338
bool isKind(const ResKind &kind_r) const
Test whether a Solvable is of a certain ResKind.
Definition solvable.cc:211
int simpleParseFile(std::istream &str_r, ParseFlags flags_r, function< bool(int, std::string)> consume_r)
Simple lineparser optionally trimming and skipping comments.
Definition IOStream.cc:124
unsigned splitEscaped(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \t", bool withEmpty=false)
Split line_r into words with respect to escape delimeters.
Definition String.h:665
zypp::sat::detail::CPool CPool
zypp::sat::detail::SolvableIdType SolvableIdType
static const SolvableIdType noSolvableId(0)
Id to denote Solvable::noSolvable.
zypp::sat::detail::IdType IdType
This file contains private API, this might break at any time between releases.
bool isKind(const Solvable &solvable_r)
relates: Solvable Test whether a Solvable is of a certain Kind.
Definition solvable.h:359
zypp::IdString IdString
Definition idstring.h:16
This file contains private API, this might break at any time between releases.