libzypp  17.1.1
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  : CommitPackageCache::Impl( packageProvider_r )
55  {}
56 
58  //
59  // METHOD NAME : CommitPackageCacheReadAhead::onInteractiveMedia
60  // METHOD TYPE : bool
61  //
63  {
64  if ( pi->mediaNr() == 0 ) // no media access at all
65  return false;
66  if ( pi->repoInfo().baseUrlsEmpty() )
67  return false; // no Url - should actually not happen
68  std::string scheme( pi->repoInfo().baseUrlsBegin()->getScheme() );
69  return ( scheme == "dvd" || scheme == "cd" );
70  }
71 
73  //
74  // METHOD NAME : CommitPackageCacheReadAhead::cacheLastInteractive
75  // METHOD TYPE : void
76  //
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  //
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  //
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
#define MIL
Definition: Logger.h:64
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:392
bool isToBeInstalled() const
Definition: ResStatus.h:244
Helper filtering the files offered by a RepomdFileReader.
virtual ManagedFile sourceProvideCachedPackage(const PoolItem &pi) const
Let the Source provide an already cached package.
Target::commit helper optimizing package provision.
bool isCached() const
Whether the package is cached.
Definition: Package.h:129
ResStatus & status() const
Returns the current status.
Definition: PoolItem.cc:204
String related utilities and Regular expression matching.
virtual ManagedFile get(const PoolItem &citem_r)
Provide the package.
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:27
ResObject::constPtr resolvable() const
Returns the ResObject::constPtr.
Definition: PoolItem.cc:217
#define ERR
Definition: Logger.h:66
urls_const_iterator baseUrlsBegin() const
iterator that points at begin of repository urls
Definition: RepoInfo.cc:592
bool baseUrlsEmpty() const
whether repository urls are available
Definition: RepoInfo.cc:601
CommitPackageCache::PackageProvider PackageProvider
void cacheLastInteractive(const PoolItem &citem_r)
Fill the cache.
std::string alias() const
unique identifier for this source.
const std::vector< sat::Solvable > & commitList() const
Package interface.
Definition: Package.h:32
RepoInfo repoInfo() const
Definition: SolvableType.h:76
#define WAR
Definition: Logger.h:65
RepoInfo info() const
Return any associated RepoInfo.
Definition: Repository.cc:273
unsigned mediaNr() const
Definition: SolvableType.h:120
std::ostream & operator<<(std::ostream &str, const CommitPackageCache &obj)
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
CommitPackageCacheReadAhead(const PackageProvider &packageProvider_r)
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition: Exception.h:396
bool onInteractiveMedia(const PoolItem &pi) const
Return whether pi is located on a CD/DVD.
Base class for Exception.
Definition: Exception.h:145
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
IdType id() const
Expert backdoor.
Definition: Repository.h:307
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
virtual ManagedFile sourceProvidePackage(const PoolItem &pi) const
Let the Source provide the package.
#define DBG
Definition: Logger.h:63