libzypp: zypp/ng/sat/solvablespec.cc Source File

libzypp 17.38.6

solvablespec.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
9#include <zypp/ng/sat/solvablespec.h>
10#include <zypp/ng/sat/pool.h>
11#include <zypp/ng/sat/preparedpool.h>
12#include <zypp/ng/sat/solvable.h>
13#include <zypp-core/base/IOStream.h>
14#include <zypp-core/base/inputstream.h>
15#include <zypp-core/base/String.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
63 void SolvableSpec::parseFrom( const zypp::InputStream & istr_r )
64 {
65 zypp::iostr::simpleParseFile( istr_r.stream(),
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
84 EvaluatedSolvableSpec::EvaluatedSolvableSpec( PreparedPool & pp_r, const SolvableSpec & spec_r )
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();
95 id != detail::noSolvableId;
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
IOStream.h
String.h
zypp::InputStream
Helper to create and pass std::istream.
Definition inputstream.h:57
zypp::InputStream::stream
std::istream & stream() const
The std::istream.
Definition inputstream.h:93
zyppng::ResKind::srcpackage
static const ResKind srcpackage
Definition ResKind.h:44
zyppng::sat::Capability
A sat capability.
Definition capability.h:74
zyppng::sat::EvaluatedSolvableSpec::contains
bool contains(const Solvable &solv_r) const
O(1) membership test — no pool access required.
Definition solvablespec.cc:116
zyppng::sat::EvaluatedSolvableSpec::_ids
SolvIdSet _ids
Definition solvablespec.h:199
zyppng::sat::EvaluatedSolvableSpec::EvaluatedSolvableSpec
EvaluatedSolvableSpec(PreparedPool &pp_r, const SolvableSpec &spec_r)
Construct by evaluating spec_r against pp_r.
Definition solvablespec.cc:84
zyppng::sat::Pool
Orchestrator for a libsolv pool instance.
Definition pool.h:37
zyppng::sat::Pool::get
detail::CPool * get() const
Expert backdoor.
Definition pool.h:59
zyppng::sat::Pool::getFirstId
detail::SolvableIdType getFirstId() const
Get id of the first valid Solvable.
Definition pool.h:242
zyppng::sat::Pool::getNextId
detail::SolvableIdType getNextId(detail::SolvableIdType id_r) const
Get id of the next valid Solvable.
Definition pool.h:250
zyppng::sat::PreparedPool
A move-only, non-owning view of a Pool that guarantees the whatprovides index is valid.
Definition preparedpool.h:51
zyppng::sat::PreparedPool::pool
Pool & pool() const noexcept
Access the owning Pool.
Definition preparedpool.h:63
zyppng::sat::PreparedPool::whatProvidesData
detail::IdType whatProvidesData(unsigned offset_r) const
Returns the id stored at offset_r in the whatprovidesdata array.
Definition preparedpool.cc:45
zyppng::sat::PreparedPool::whatProvidesCapabilityId
unsigned whatProvidesCapabilityId(detail::IdType cap_r) const
Returns the offset into the internal whatprovidesdata array for cap_r.
Definition preparedpool.cc:40
zyppng::sat::PreparedPool::get
detail::CPool * get() const noexcept
Expert backdoor — raw libsolv pool pointer.
Definition preparedpool.cc:31
zyppng::sat::SolvableSpec
A pure data container describing a set of solvables by ident and/or provides.
Definition solvablespec.h:55
zyppng::sat::SolvableSpec::parse
void parse(std::string_view spec_r)
Parse and add a single spec entry.
Definition solvablespec.cc:52
zyppng::sat::SolvableSpec::empty
bool empty() const
Whether the spec has no idents and no provides tokens.
Definition solvablespec.h:66
zyppng::sat::SolvableSpec::parseFrom
void parseFrom(const zypp::InputStream &istr_r)
Parse and add specs from istr_r (one per line; #-comments and empty lines are skipped automatically).
Definition solvablespec.cc:63
zyppng::sat::SolvableSpec::provides
const CapabilitySet & provides() const
Definition solvablespec.h:101
zyppng::sat::SolvableSpec::idents
const IdStringSet & idents() const
Definition solvablespec.h:82
zyppng::sat::SolvableSpec::addIdent
void addIdent(IdString ident_r)
Add ident_r to the ident set.
Definition solvablespec.h:79
zyppng::sat::SolvableSpec::_idents
IdStringSet _idents
Definition solvablespec.h:147
zyppng::sat::SolvableSpec::contains
bool contains(Pool &pool_r, const Solvable &solv_r) const
Test whether a single Solvable matches this spec.
Definition solvablespec.cc:25
zyppng::sat::SolvableSpec::_provides
CapabilitySet _provides
Definition solvablespec.h:148
zyppng::sat::SolvableSpec::addProvides
void addProvides(Capability cap_r)
Add cap_r to the provides set.
Definition solvablespec.h:98
zyppng::sat::SolvableSpec::splitParseFrom
void splitParseFrom(std::string_view multispec_r)
Split multispec_r on ',', ' ', '\t' and parse each token.
Definition solvablespec.cc:72
zyppng::sat::Solvable
A Solvable object within the sat Pool.
Definition solvable.h:65
zyppng::sat::Solvable::ident
IdString ident() const
The identifier.
Definition solvable.cc:178
zyppng::sat::Solvable::id
IdType id() const
Expert backdoor.
Definition solvable.h:338
zyppng::sat::Solvable::isKind
bool isKind(const ResKind &kind_r) const
Test whether a Solvable is of a certain ResKind.
Definition solvable.cc:211
inputstream.h
zypp::iostr::simpleParseFile
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
zypp::str::splitEscaped
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
zyppng::sat::detail::CPool
zypp::sat::detail::CPool CPool
Definition poolconstants.h:36
zyppng::sat::detail::SolvableIdType
zypp::sat::detail::SolvableIdType SolvableIdType
Definition poolconstants.h:44
zyppng::sat::detail::noSolvableId
static const SolvableIdType noSolvableId(0)
Id to denote Solvable::noSolvable.
zyppng::sat::detail::IdType
zypp::sat::detail::IdType IdType
Definition poolconstants.h:43
zyppng::sat
This file contains private API, this might break at any time between releases.
Definition capabilities.h:22
zyppng::sat::isKind
bool isKind(const Solvable &solvable_r)
relates: Solvable Test whether a Solvable is of a certain Kind.
Definition solvable.h:359
zyppng::IdString
zypp::IdString IdString
Definition idstring.h:16
pool.h
preparedpool.h
solvable.h
solvablespec.h
This file contains private API, this might break at any time between releases.