libzypp: zypp/target/CommitPackageCacheReadAhead.cc Source File

libzypp 17.38.9

CommitPackageCacheReadAhead.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#include <iostream>
13
14#include <zypp-core/base/Logger.h>
15#include <zypp-core/base/Exception.h>
16#include <zypp/PathInfo.h>
17#include <zypp/RepoInfo.h>
18#include <zypp/Package.h>
19#include <zypp/target/CommitPackageCacheReadAhead.h>
20
21using std::endl;
22
24namespace zypp
25{
27 namespace target
28 {
29
31 //
32 // CLASS NAME : IMediaKey
33 //
35
36 std::ostream & operator<<( std::ostream & str, const IMediaKey & obj )
37 {
38 return str << "[S" << obj._repo.id() << ":" << obj._mediaNr << "]"
39 << " " << obj._repo.info().alias();
40 }
41
43 //
44 // CLASS NAME : CommitPackageCacheReadAhead
45 //
47
49 //
50 // METHOD NAME : CommitPackageCacheReadAhead::CommitPackageCacheReadAhead
51 // METHOD TYPE : Ctor
52 //
53 CommitPackageCacheReadAhead::CommitPackageCacheReadAhead( PackageProvider packageProvider_r )
54 : CommitPackageCache::Impl( std::move(packageProvider_r) )
55 {}
56
58 //
59 // METHOD NAME : CommitPackageCacheReadAhead::onInteractiveMedia
60 // METHOD TYPE : bool
61 //
62 bool CommitPackageCacheReadAhead::onInteractiveMedia( const PoolItem & pi ) const
63 {
64 if ( pi->mediaNr() == 0 ) // no media access at all
65 return false;
66 if ( pi->repoInfo().baseUrlsEmpty() )
67 return false; // no baseUrl, only mirrorlist is possible but we do not support interactive media there
68 std::string scheme( pi->repoInfo().baseUrls().front().getScheme() );
69 return ( scheme == "dvd" || scheme == "cd" );
70 }
71
73 //
74 // METHOD NAME : CommitPackageCacheReadAhead::cacheLastInteractive
75 // METHOD TYPE : void
76 //
77 void CommitPackageCacheReadAhead::cacheLastInteractive( const PoolItem & citem_r )
78 {
79 // Fill cache errors are never proagated.
80 try
81 {
82 doCacheLastInteractive( citem_r );
83 }
84 catch ( const Exception & excpt_r )
85 {
86 ZYPP_CAUGHT( excpt_r );
87 WAR << "Failed to cache " << _lastInteractive << endl;
88 }
89 }
90
92 //
93 // METHOD NAME : CommitPackageCacheReadAhead::doCacheLastInteractive
94 // METHOD TYPE : void
95 //
96 void CommitPackageCacheReadAhead::doCacheLastInteractive( const PoolItem & citem_r )
97 {
98 unsigned addToCache = 0;
99 bool sawCitem = false;
100
101 // Collect all remaining packages to install from
102 // _lastInteractive media. (just the PoolItem data)
103 for_( it, commitList().begin(), commitList().end() )
104 {
105 PoolItem pi( *it );
106 if ( ! sawCitem )
107 {
108 if ( pi == citem_r )
109 sawCitem = true;
110 continue;
111 }
112 if ( IMediaKey( pi ) == _lastInteractive
113 && pi.status().isToBeInstalled()
114 && isKind<Package>(pi.resolvable()) )
115 {
116 if ( ! pi->asKind<Package>()->isCached() )
117 {
118 ManagedFile fromSource( sourceProvidePackage( pi ) );
119 if ( fromSource->empty() )
120 {
121 ERR << "Copy to cache failed on " << fromSource << endl;
122 ZYPP_THROW( Exception("Copy to cache failed.") );
123 }
124 fromSource.resetDispose(); // keep the package file in the cache
125 ++addToCache;
126 }
127 }
128 }
129
130 if ( addToCache )
131 MIL << "Cached " << _lastInteractive << ": " << addToCache << " items." << endl;
132 }
133
135 //
136 // METHOD NAME : CommitPackageCacheReadAhead::get
137 // METHOD TYPE : ManagedFile
138 //
139 ManagedFile CommitPackageCacheReadAhead::get( const PoolItem & citem_r )
140 {
141 ManagedFile ret;
142 if ( preloaded() )
143 {
144 // Check whether it's cached.
145 ManagedFile ret( sourceProvideCachedPackage( citem_r ) );
146 if ( ! ret->empty() )
147 return ret;
148 }
149 // else: we head for sourceProvidePackage(), even if the package
150 // was cached. The actual difference is that sourceProvidePackage
151 // will trigger the infoInCache CB that informs the application.
152 // Once the cache is preloaded we try to avoid this CB.
153
154
155 // Preload cache if a CD/DVD change is pending to avoid
156 // switching back and forth...
157 if ( onInteractiveMedia( citem_r ) )
158 {
159 ret = sourceProvideCachedPackage( citem_r );
160 if ( ! ret->empty() )
161 return ret;
162
163 IMediaKey current( citem_r );
164 if ( current != _lastInteractive )
165 {
166 if ( _lastInteractive != IMediaKey() )
167 {
168 cacheLastInteractive( citem_r );
169 }
170
171 DBG << "Interactive change [" << ++_dbgChanges << "] from " << _lastInteractive << " to " << current << endl;
172 _lastInteractive = current;
173 }
174 }
175
176 // Provide and return the file from media.
177 return sourceProvidePackage( citem_r );
178 }
179
180
182 } // namespace target
185} // namespace zypp
CommitPackageCacheReadAhead.h
for_
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition Easy.h:27
Exception.h
ZYPP_CAUGHT
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition Exception.h:475
ZYPP_THROW
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition Exception.h:459
Logger.h
DBG
#define DBG
Definition Logger.h:102
MIL
#define MIL
Definition Logger.h:103
ERR
#define ERR
Definition Logger.h:105
WAR
#define WAR
Definition Logger.h:104
Package.h
RepoInfo.h
zypp::AutoDispose::resetDispose
void resetDispose()
Set no dispose function.
Definition AutoDispose.h:171
zypp::Exception
Base class for Exception.
Definition Exception.h:153
zypp::Package
Package interface.
Definition Package.h:34
zypp::Package::isCached
bool isCached() const
Whether the package is cached.
Definition Package.h:151
zypp::PoolItem
Combining sat::Solvable and ResStatus.
Definition PoolItem.h:51
zypp::PoolItem::resolvable
ResObject::constPtr resolvable() const
Returns the ResObject::constPtr.
Definition PoolItem.cc:227
zypp::PoolItem::status
ResStatus & status() const
Returns the current status.
Definition PoolItem.cc:212
zypp::Repository::id
IdType id() const
Expert backdoor.
Definition Repository.h:321
zypp::Repository::info
RepoInfo info() const
Return any associated RepoInfo.
Definition Repository.cc:274
zypp::ResStatus::isToBeInstalled
bool isToBeInstalled() const
Definition ResStatus.h:259
zypp::filesystem::Pathname::empty
bool empty() const
Test for an empty path.
Definition Pathname.h:117
zypp::repo::RepoInfoBase::alias
std::string alias() const
unique identifier for this source.
Definition RepoInfoBase.cc:110
zypp::target::CommitPackageCacheReadAhead::_lastInteractive
IMediaKey _lastInteractive
Definition CommitPackageCacheReadAhead.h:109
zypp::target::CommitPackageCacheReadAhead::CommitPackageCacheReadAhead
CommitPackageCacheReadAhead(PackageProvider packageProvider_r)
Definition CommitPackageCacheReadAhead.cc:53
zypp::target::CommitPackageCacheReadAhead::onInteractiveMedia
bool onInteractiveMedia(const PoolItem &pi) const
Return whether pi is located on a CD/DVD.
Definition CommitPackageCacheReadAhead.cc:62
zypp::target::CommitPackageCacheReadAhead::doCacheLastInteractive
void doCacheLastInteractive(const PoolItem &citem_r)
cacheLastInteractive helper .
Definition CommitPackageCacheReadAhead.cc:96
zypp::target::CommitPackageCacheReadAhead::cacheLastInteractive
void cacheLastInteractive(const PoolItem &citem_r)
Fill the cache.
Definition CommitPackageCacheReadAhead.cc:77
zypp::target::CommitPackageCacheReadAhead::get
ManagedFile get(const PoolItem &citem_r) override
Provide the package.
Definition CommitPackageCacheReadAhead.cc:139
zypp::target::CommitPackageCacheReadAhead::_dbgChanges
DefaultIntegral< unsigned, 0 > _dbgChanges
Definition CommitPackageCacheReadAhead.h:108
zypp::target::CommitPackageCache::Impl::commitList
const std::vector< sat::Solvable > & commitList() const
Definition CommitPackageCacheImpl.h:65
zypp::target::CommitPackageCache::Impl::sourceProvidePackage
virtual ManagedFile sourceProvidePackage(const PoolItem &pi) const
Let the Source provide the package.
Definition CommitPackageCacheImpl.h:76
zypp::target::CommitPackageCache::Impl::sourceProvideCachedPackage
virtual ManagedFile sourceProvideCachedPackage(const PoolItem &pi) const
Let the Source provide an already cached package.
Definition CommitPackageCacheImpl.h:93
zypp::target::CommitPackageCache::Impl::Impl
Impl(PackageProvider &&packageProvider_r)
Definition CommitPackageCacheImpl.h:46
zypp::target::CommitPackageCache::Impl::PackageProvider
CommitPackageCache::PackageProvider PackageProvider
Definition CommitPackageCacheImpl.h:43
zypp::target::CommitPackageCache::Impl::preloaded
bool preloaded() const
Definition CommitPackageCacheImpl.h:68
zypp::target::CommitPackageCache
Target::commit helper optimizing package provision.
Definition CommitPackageCache.h:59
std
Definition ansi.h:855
str
String related utilities and Regular expression matching.
zypp::target
Definition KeyRing.h:325
zypp::target::operator<<
std::ostream & operator<<(std::ostream &str, const CommitPackageCache &obj)
relates: CommitPackageCache Stream output
Definition CommitPackageCache.cc:155
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::ManagedFile
AutoDispose< const Pathname > ManagedFile
A Pathname plus associated cleanup code to be executed when path is no longer needed.
Definition ManagedFile.h:27
zypp::isKind
bool isKind(const Resolvable::constPtr &p)
Test whether a Resolvable::Ptr is of a certain Kind.
Definition Resolvable.h:99
zypp::target::IMediaKey
Helper storing a source id and media number.
Definition CommitPackageCacheReadAhead.h:37
zypp::target::IMediaKey::_repo
Repository _repo
Definition CommitPackageCacheReadAhead.h:71
zypp::target::IMediaKey::_mediaNr
DefaultIntegral< unsigned, 0 > _mediaNr
Definition CommitPackageCacheReadAhead.h:72
PathInfo.h