libzypp  17.38.7
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>
12 #include <zypp/ng/sat/solvable.h>
15 #include <zypp-core/base/String.h>
16 
17 #include <vector>
18 
19 extern "C" {
20 #include <solv/pool.h>
21 }
22 
23 namespace 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
static const SolvableIdType noSolvableId(0)
Id to denote Solvable::noSolvable.
detail::SolvableIdType getNextId(detail::SolvableIdType id_r) const
Get id of the next valid Solvable.
Definition: pool.h:250
const CapabilitySet & provides() const
Definition: solvablespec.h:101
A Solvable object within the sat Pool.
Definition: solvable.h:64
unsigned splitEscaped(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \, bool withEmpty=false)
Split line_r into words with respect to escape delimeters.
Definition: String.h:666
EvaluatedSolvableSpec(PreparedPool &pp_r, const SolvableSpec &spec_r)
Construct by evaluating spec_r against pp_r.
Definition: solvablespec.cc:84
static const ResKind srcpackage
Definition: ResKind.h:44
Orchestrator for a libsolv pool instance.
Definition: pool.h:36
Helper to create and pass std::istream.
Definition: inputstream.h:56
zypp::sat::detail::CPool CPool
Definition: poolconstants.h:36
zypp::sat::detail::SolvableIdType SolvableIdType
Definition: poolconstants.h:44
bool contains(const Solvable &solv_r) const
O(1) membership test — no pool access required.
A move-only, non-owning view of a Pool that guarantees the whatprovides index is valid.
Definition: preparedpool.h:50
detail::CPool * get() const noexcept
Expert backdoor — raw libsolv pool pointer.
Definition: preparedpool.cc:31
detail::CPool * get() const
Expert backdoor.
Definition: pool.h:59
This file contains private API, this might break at any time between releases.
Definition: capabilities.h:22
A sat capability.
Definition: capability.h:73
zypp::sat::detail::IdType IdType
Definition: poolconstants.h:43
IdType id() const
Expert backdoor.
Definition: solvable.h:338
A pure data container describing a set of solvables by ident and/or provides.
Definition: solvablespec.h:54
This file contains private API, this might break at any time between releases.
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
bool isKind(const ResKind &kind_r) const
Test whether a Solvable is of a certain ResKind.
Definition: solvable.cc:212
const IdStringSet & idents() const
Definition: solvablespec.h:82
bool empty() const
Whether the spec has no idents and no provides tokens.
Definition: solvablespec.h:66
std::istream & stream() const
The std::istream.
Definition: inputstream.h:93
IdString ident() const
The identifier.
Definition: solvable.cc:179
void addProvides(Capability cap_r)
Add cap_r to the provides set.
Definition: solvablespec.h:98
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
void splitParseFrom(std::string_view multispec_r)
Split multispec_r on &#39;,&#39;, &#39; &#39;, &#39;\t&#39; and parse each token.
Definition: solvablespec.cc:72
detail::IdType whatProvidesData(unsigned offset_r) const
Returns the id stored at offset_r in the whatprovidesdata array.
Definition: preparedpool.cc:45
void parse(std::string_view spec_r)
Parse and add a single spec entry.
Definition: solvablespec.cc:52
unsigned whatProvidesCapabilityId(detail::IdType cap_r) const
Returns the offset into the internal whatprovidesdata array for cap_r.
Definition: preparedpool.cc:40
detail::SolvableIdType getFirstId() const
Get id of the first valid Solvable.
Definition: pool.h:242
Pool & pool() const noexcept
Access the owning Pool.
Definition: preparedpool.h:63
zypp::IdString IdString
Definition: idstring.h:16
bool contains(Pool &pool_r, const Solvable &solv_r) const
Test whether a single Solvable matches this spec.
Definition: solvablespec.cc:25
void addIdent(IdString ident_r)
Add ident_r to the ident set.
Definition: solvablespec.h:79