libzypp 17.38.5
MirroredOrigin.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
9#include "MirroredOrigin.h"
10
12#undef ZYPP_BASE_LOGGER_LOGGROUP
13#define ZYPP_BASE_LOGGER_LOGGROUP "zypp::MirroredOrigin"
14
15namespace zypp {
16
18
19 Private( Url &&u, OriginEndpoint::SettingsMap &&m )
20 : _url(std::move(u))
21 , _settings(std::move(m))
22 {}
23 ~Private() = default;
24
25 Private *clone () const {
26 return new Private(*this);
27 }
28
30 std::unordered_map<std::string, std::any> _settings;
31 //OriginEndpoint::SettingsMap _settings;
32 };
33
35 : _pimpl( new Private(Url(), {} ) )
36 {}
37
38 OriginEndpoint::OriginEndpoint( Url url, SettingsMap settings )
39 : _pimpl( new Private(std::move(url), std::move(settings) ) )
40 {}
41
43 : OriginEndpoint( std::move(url), SettingsMap() )
44 { }
45
47 {
48 return _pimpl->_url;
49 }
50
51 const Url &OriginEndpoint::url() const
52 {
53 return _pimpl->_url;
54 }
55
56 void OriginEndpoint::setUrl(const Url &newUrl)
57 {
58 _pimpl->_url = newUrl;
59 }
60
61 bool OriginEndpoint::hasConfig(const std::string &key) const
62 {
63 return (_pimpl->_settings.count (key) > 0);
64 }
65
66 std::string OriginEndpoint::scheme() const
67 {
68 return _pimpl->_url.getScheme();
69 }
70
72 {
73 return _pimpl->_url.schemeIsDownloading ();
74 }
75
77 {
78 return _pimpl->_url.isValid();
79 }
80
81 void OriginEndpoint::setConfig(const std::string &key, std::any value)
82 {
83 _pimpl->_settings.insert_or_assign ( key, std::move(value) );
84 }
85
86 const std::any &OriginEndpoint::getConfig(const std::string &key) const
87 {
88 return _pimpl->_settings.at(key);
89 }
90
91 std::any &OriginEndpoint::getConfig(const std::string &key)
92 {
93 return _pimpl->_settings.at(key);
94 }
95
96 void OriginEndpoint::eraseConfigValue( const std::string &key )
97 {
98 auto it = _pimpl->_settings.find (key);
99 if ( it == _pimpl->_settings.end() )
100 return;
101 _pimpl->_settings.erase(it);
102 }
103
104 const OriginEndpoint::SettingsMap &OriginEndpoint::config() const
105 {
106 return _pimpl->_settings;
107 }
108
109
110 OriginEndpoint::SettingsMap &OriginEndpoint::config()
111 {
112 return _pimpl->_settings;
113 }
114
115
116 std::ostream & operator<<( std::ostream & str, const OriginEndpoint & url )
117 {
118 return str << url.url().asString();
119 }
120
121 bool operator<( const OriginEndpoint &lhs, const OriginEndpoint &rhs )
122 {
123 return (lhs.url().asCompleteString() < rhs.url().asCompleteString());
124 }
125
126 bool operator==( const OriginEndpoint &lhs, const OriginEndpoint &rhs )
127 {
128 return (lhs.url().asCompleteString() == rhs.url().asCompleteString());
129 }
130
131 bool operator!=( const OriginEndpoint &lhs, const OriginEndpoint &rhs )
132 {
133 return (lhs.url().asCompleteString() != rhs.url().asCompleteString());
134 }
135
137 Private() = default;
138 ~Private() = default;
139
140 Private *clone () const {
141 return new Private(*this);
142 }
143
145 std::vector<OriginEndpoint> _origins;
146 };
147
151
153 : _pimpl( new Private() )
154 {
155 if ( authority.isValid() )
156 _pimpl->_authority = std::move(authority);
157 else
158 WAR << "Ignoring invalid authority in constructor: " << authority << std::endl;
159
160 for( auto &m : mirrors ) { addMirror ( std::move(m) ); }
161 }
162
164 {
165 if ( !newAuthority.isValid() )
166 {
167 WAR << "Ignoring invalid authority: " << newAuthority << std::endl;
168 return;
169 }
170
171 const auto &newScheme = newAuthority.scheme();
172 bool newAuthIsDl = newAuthority.url().schemeIsDownloading();
173
174 _pimpl->_authority = std::move(newAuthority);
175
176 if ( !_pimpl->_authority.isValid() || !_pimpl->_origins.size () )
177 return;
178
179 // house keeeping, we want only compatible mirrors
180 for ( auto i = _pimpl->_origins.begin (); i != _pimpl->_origins.end(); ) {
181 if ( ( newAuthIsDl && !i->schemeIsDownloading() ) // drop mirror if its not downloading but authority is
182 && ( i->scheme () != newScheme ) // otherwise drop if scheme is not identical
183 ) {
184 MIL << "Dropping mirror " << *i << " scheme is not compatible to new authority URL ( " << i->scheme() << " vs " << newScheme << ")" << std::endl;
185 i = _pimpl->_origins.erase(i);
186 } else {
187 i++;
188 }
189 }
190 }
191
193 {
194 return _pimpl->_authority;
195 }
196
197 const std::vector<OriginEndpoint> &MirroredOrigin::mirrors() const
198 {
199 return _pimpl->_origins;
200 }
201
203 {
204 return _pimpl->_authority.isValid();
205 }
206
208 {
209 if ( !newMirror.isValid() )
210 {
211 WAR << "Ignoring invalid mirror: " << newMirror << std::endl;
212 return false;
213 }
214
215 if ( _pimpl->_authority.isValid()
216 && ( _pimpl->_authority.schemeIsDownloading() && !newMirror.schemeIsDownloading () )
217 && ( _pimpl->_authority.scheme () != newMirror.scheme () )
218
219 ) {
220 MIL << "Ignoring mirror " << newMirror << " scheme is not compatible to new authority URL ( " << newMirror.scheme() << " vs " << _pimpl->_authority.scheme() << ")" << std::endl;
221 return false;
222 }
223 _pimpl->_origins.push_back( std::move(newMirror) );
224 return true;
225 }
226
227 void MirroredOrigin::setMirrors(std::vector<OriginEndpoint> mirrors)
228 {
229 clearMirrors();
230 for ( auto &m : mirrors )
231 addMirror( std::move(m) );
232 }
233
235 {
236 _pimpl->_origins.clear();
237 }
238
239 std::string MirroredOrigin::scheme() const
240 {
241 return _pimpl->_authority.url().getScheme();
242 }
243
245 {
246 return _pimpl->_authority.schemeIsDownloading();
247 }
248
250 {
251 // authority is always accessible, even if its a invalid URL
252 return _pimpl->_origins.size() + 1;
253 }
254
255 const OriginEndpoint &MirroredOrigin::at(uint index) const
256 {
257 if ( index >= endpointCount() ) {
258 throw std::out_of_range( "OriginEndpoint index out of range." );
259 }
260 if ( index == 0 ) {
261 return _pimpl->_authority;
262 }
263
264 return _pimpl->_origins.at( index - 1 );
265 }
266
268 {
269 if ( index >= endpointCount() ) {
270 throw std::out_of_range( "OriginEndpoint index out of range." );
271 }
272 if ( index == 0 ) {
273 return _pimpl->_authority;
274 }
275
276 return _pimpl->_origins.at( index - 1 );
277 }
278
280 {
282 ~Private() = default;
283
284 Private *clone () const {
285 return new Private(*this);
286 }
287
288 std::optional<std::size_t> _dlIndex; //< set if there is a downloading MirroredOrigin
289 std::vector<MirroredOrigin> _origins;
290 };
291
292
296
297 MirroredOriginSet::MirroredOriginSet( std::vector<OriginEndpoint> eps )
299 {
300 if ( eps.size() )
301 addEndpoints( std::move(eps) );
302 }
303
304 MirroredOriginSet::MirroredOriginSet(std::list<Url> urls)
306 {
307 for( auto &url: urls )
308 addEndpoint( std::move(url) );
309 }
310
313 {
314 for( auto &url: urls )
315 addEndpoint( std::move(url) );
316 }
317
319 {
320 return _pimpl->_origins.at(idx);
321 }
322
324 {
325 return _pimpl->_origins.at(idx);
326 }
327
328 std::ostream & operator<<( std::ostream & str, const MirroredOrigin & origin )
329 {
330 return dumpRange( str << "MirroredOrigin { authority: \"" << origin.authority() << "\", ",
331 origin.mirrors().begin(), origin.mirrors().end(), "mirrors: [", "\"", "\",\"", "\"", "]" )
332 << " }";
333 }
334
336 {
337 for ( auto i = begin(); i!=end(); i++ ) {
338 auto epI = std::find_if( i->begin (), i->end(), [&](const OriginEndpoint &ep){ return ep.url () == url; } );
339 if ( epI != i->end() )
340 return i;
341 }
342 return end();
343 }
344
346 {
347 for ( auto i = begin(); i!=end(); i++ ) {
348 auto epI = std::find_if( i->begin (), i->end(), [&](const OriginEndpoint &ep){ return ep.url () == url; } );
349 if ( epI != i->end() )
350 return i;
351 }
352 return end();
353 }
354
356 {
357 if ( !endpoint.url().schemeIsDownloading () ) {
358 _pimpl->_origins.push_back ( MirroredOrigin(std::move(endpoint), {} ) );
359 return;
360 }
361
362 if ( _pimpl->_dlIndex ) {
363 _pimpl->_origins.at(*_pimpl->_dlIndex).addMirror( std::move(endpoint) );
364 return;
365 }
366
367 // start a new origin
368 _pimpl->_origins.push_back ( MirroredOrigin(std::move(endpoint), {} ) );
369 _pimpl->_dlIndex = _pimpl->_origins.size() - 1;
370 }
371
372
373 void MirroredOriginSet::addEndpoints( std::vector<OriginEndpoint> endpoints )
374 {
375 for ( auto &ep : endpoints )
376 addEndpoint ( std::move(ep) );
377 }
378
380 {
381 return _pimpl->_origins.empty ();
382 }
383
385 {
386 _pimpl->_origins.clear();
387 _pimpl->_dlIndex.reset();
388 }
389
391 {
392 return _pimpl->_origins.begin ();
393 }
394
395
397 {
398 return _pimpl->_origins.end ();
399 }
400
401
403 {
404 return _pimpl->_origins.begin ();
405 }
406
407
409 {
410 return _pimpl->_origins.end ();
411 }
412
414 {
415 return _pimpl->_origins.size ();
416 }
417
419 {
420 return ( size() == 1 && at( 0 ).endpointCount() > 1 ) || size() > 1;
421 }
422
423 std::ostream & operator<<( std::ostream & str, const MirroredOriginSet & origin )
424 {
425 return dumpRange( str, origin.begin(), origin.end(), "MirroredOriginSet {", " ", ", ", " ", "}" );
426 }
427
428}
#define MIL
Definition Logger.h:100
#define WAR
Definition Logger.h:101
A smart container that manages a collection of MirroredOrigin objects, automatically grouping endpoin...
bool hasFallbackUrls() const
Whether this set contains more than one Url in total (authorities or mirrors).
const_iterator findByUrl(const zypp::Url &url) const
Finds the MirroredOrigin that contains a specific URL.
const MirroredOrigin & at(size_type idx) const
Accesses the MirroredOrigin at a specific index.
size_type size() const
Returns the number of MirroredOrigin objects in the set.
iterator end()
Returns an iterator to the element following the last MirroredOrigin.
std::vector< MirroredOrigin >::const_iterator const_iterator
RWCOW_pointer< Private > _pimpl
iterator begin()
Returns an iterator to the first MirroredOrigin in insertion order.
std::vector< MirroredOrigin >::iterator iterator
void addEndpoints(InputIterator first, InputIterator last)
A convenience method to add multiple endpoints from a range.
void addEndpoint(OriginEndpoint endpoint)
Adds a single endpoint, routing it to the correct MirroredOrigin.
Manages a data source characterized by an authoritative URL and a list of mirror URLs.
bool schemeIsDownloading() const
const std::vector< OriginEndpoint > & mirrors() const
const OriginEndpoint & at(uint index) const
void setAuthority(OriginEndpoint newAuthority)
RWCOW_pointer< Private > _pimpl
void setMirrors(std::vector< OriginEndpoint > mirrors)
bool addMirror(OriginEndpoint newMirror)
std::string scheme() const
const OriginEndpoint & authority() const
Represents a single, configurable network endpoint, combining a URL with specific access settings.
bool schemeIsDownloading() const
RWCOW_pointer< Private > _pimpl
std::string scheme() const
const zypp::Url & url() const
void setUrl(const zypp::Url &newUrl)
bool hasConfig(const std::string &key) const
Url manipulation class.
Definition Url.h:93
std::string asCompleteString() const
Returns a complete string representation of the Url object.
Definition Url.cc:532
static bool schemeIsDownloading(const std::string &scheme_r)
http https ftp sftp tftp
Definition Url.cc:493
Definition ansi.h:855
String related utilities and Regular expression matching.
Url details namespace.
Definition UrlBase.cc:58
Easy-to use interface to the ZYPP dependency resolver.
bool operator<(const StrMatcher &lhs, const StrMatcher &rhs)
std::ostream & dumpRange(std::ostream &str, TIterator begin, TIterator end, const std::string &intro="{", const std::string &pfx="\n ", const std::string &sep="\n ", const std::string &sfx="\n", const std::string &extro="}")
Print range defined by iterators (multiline style).
Definition LogTools.h:404
bool operator==(const SetRelation::Enum &lhs, const SetCompare &rhs)
This is an overloaded member function, provided for convenience. It differs from the above function o...
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
bool operator!=(const SetRelation::Enum &lhs, const SetCompare &rhs)
This is an overloaded member function, provided for convenience. It differs from the above function o...
std::vector< MirroredOrigin > _origins
std::optional< std::size_t > _dlIndex
std::vector< OriginEndpoint > _origins
std::unordered_map< std::string, std::any > _settings
Private(Url &&u, OriginEndpoint::SettingsMap &&m)