libzypp  15.28.6
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/base/Logger.h"
15 #include "zypp/base/Exception.h"
16 #include "zypp/PathInfo.h"
17 #include "zypp/RepoInfo.h"
18 #include "zypp/Package.h"
20 
21 using std::endl;
22 
24 namespace zypp
25 {
26  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  //
54  const PackageProvider & packageProvider_r )
55  : CommitPackageCache::Impl( packageProvider_r )
56  //, _rootDir( rootDir_r )
57  {}
58 
60  //
61  // METHOD NAME : CommitPackageCacheReadAhead::onInteractiveMedia
62  // METHOD TYPE : bool
63  //
65  {
66  if ( pi->mediaNr() == 0 ) // no media access at all
67  return false;
68  if ( pi->repoInfo().baseUrlsEmpty() )
69  return false; // no Url - should actually not happen
70  std::string scheme( pi->repoInfo().baseUrlsBegin()->getScheme() );
71  return ( scheme == "dvd" || scheme == "cd" );
72  }
73 
75  //
76  // METHOD NAME : CommitPackageCacheReadAhead::cacheLastInteractive
77  // METHOD TYPE : void
78  //
80  {
81  // Fill cache errors are never proagated.
82  try
83  {
84  doCacheLastInteractive( citem_r );
85  }
86  catch ( const Exception & excpt_r )
87  {
88  ZYPP_CAUGHT( excpt_r );
89  WAR << "Failed to cache " << _lastInteractive << endl;
90  }
91  }
92 
94  //
95  // METHOD NAME : CommitPackageCacheReadAhead::doCacheLastInteractive
96  // METHOD TYPE : void
97  //
99  {
100  unsigned addToCache = 0;
101  bool sawCitem = false;
102 
103  // Collect all remaining packages to install from
104  // _lastInteractive media. (just the PoolItem data)
105  for_( it, commitList().begin(), commitList().end() )
106  {
107  PoolItem pi( *it );
108  if ( ! sawCitem )
109  {
110  if ( pi == citem_r )
111  sawCitem = true;
112  continue;
113  }
114  if ( IMediaKey( pi ) == _lastInteractive
115  && pi.status().isToBeInstalled()
116  && isKind<Package>(pi.resolvable()) )
117  {
118  if ( ! pi->asKind<Package>()->isCached() )
119  {
120  ManagedFile fromSource( sourceProvidePackage( pi ) );
121  if ( fromSource->empty() )
122  {
123  ERR << "Copy to cache failed on " << fromSource << endl;
124  ZYPP_THROW( Exception("Copy to cache failed.") );
125  }
126  fromSource.resetDispose(); // keep the package file in the cache
127  ++addToCache;
128  }
129  }
130  }
131 
132  if ( addToCache )
133  MIL << "Cached " << _lastInteractive << ": " << addToCache << " items." << endl;
134  }
135 
137  //
138  // METHOD NAME : CommitPackageCacheReadAhead::get
139  // METHOD TYPE : ManagedFile
140  //
142  {
143  ManagedFile ret;
144  if ( preloaded() )
145  {
146  // Check whether it's cached.
147  ManagedFile ret( sourceProvideCachedPackage( citem_r ) );
148  if ( ! ret->empty() )
149  return ret;
150  }
151  // else: we head for sourceProvidePackage(), even if the package
152  // was cached. The actual difference is that sourceProvidePackage
153  // will trigger the infoInCache CB that informs the application.
154  // Once the cache is preloaded we try to avoid this CB.
155 
156 
157  // Preload cache if a CD/DVD change is pending to avoid
158  // switching back and forth...
159  if ( onInteractiveMedia( citem_r ) )
160  {
161  ret = sourceProvideCachedPackage( citem_r );
162  if ( ! ret->empty() )
163  return ret;
164 
165  IMediaKey current( citem_r );
166  if ( current != _lastInteractive )
167  {
168  if ( _lastInteractive != IMediaKey() )
169  {
170  cacheLastInteractive( citem_r );
171  }
172 
173  DBG << "Interactive change [" << ++_dbgChanges << "] from " << _lastInteractive << " to " << current << endl;
174  _lastInteractive = current;
175  }
176  }
177 
178  // Provide and return the file from media.
179  return sourceProvidePackage( citem_r );
180  }
181 
182 
184  } // namespace target
187 } // namespace zypp
RepoInfo info() const
Return any associated RepoInfo.
Definition: Repository.cc:273
#define MIL
Definition: Logger.h:64
std::string alias() const
unique identifier for this source.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:321
IdType id() const
Expert backdoor.
Definition: Repository.h:307
Helper filtering the files offered by a RepomdFileReader.
unsigned mediaNr() const
Definition: SolvableType.h:118
Target::commit helper optimizing package provision.
bool onInteractiveMedia(const PoolItem &pi) const
Return whether pi is located on a CD/DVD.
bool isCached() const
Whether the package is cached.
Definition: Package.h:129
urls_const_iterator baseUrlsBegin() const
iterator that points at begin of repository urls
Definition: RepoInfo.cc:541
virtual ManagedFile get(const PoolItem &citem_r)
Provide the package.
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:27
#define ERR
Definition: Logger.h:66
virtual ManagedFile sourceProvidePackage(const PoolItem &pi) const
Let the Source provide the package.
ResObject::constPtr resolvable() const
Returns the ResObject::constPtr.
Definition: PoolItem.cc:217
CommitPackageCache::PackageProvider PackageProvider
void cacheLastInteractive(const PoolItem &citem_r)
Fill the cache.
Package interface.
Definition: Package.h:32
#define WAR
Definition: Logger.h:65
std::ostream & operator<<(std::ostream &str, const CommitPackageCache &obj)
ResStatus & status() const
Returns the current status.
Definition: PoolItem.cc:204
const std::vector< sat::Solvable > & commitList() const
void doCacheLastInteractive(const PoolItem &citem_r)
cacheLastInteractive helper .
Helper storing a source id and media number.
void resetDispose()
Set no dispose function.
Definition: AutoDispose.h:162
bool baseUrlsEmpty() const
whether repository urls are available
Definition: RepoInfo.cc:550
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition: Exception.h:325
virtual ManagedFile sourceProvideCachedPackage(const PoolItem &pi) const
Let the Source provide an already cached package.
Base class for Exception.
Definition: Exception.h:143
bool isToBeInstalled() const
Definition: ResStatus.h:241
CommitPackageCacheReadAhead(const Pathname &, const PackageProvider &packageProvider_r)
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Definition: AutoDispose.h:92
Combining sat::Solvable and ResStatus.
Definition: PoolItem.h:50
DefaultIntegral< unsigned, 0 > _mediaNr
#define DBG
Definition: Logger.h:63
RepoInfo repoInfo() const
Definition: SolvableType.h:76