libzypp  13.10.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  // Non CD/DVD media provide their packages without cache.
144  if ( ! onInteractiveMedia( citem_r ) )
145  {
146  return sourceProvidePackage( citem_r );
147  }
148 
149  // Check whether it's cached.
150  ManagedFile ret( sourceProvideCachedPackage( citem_r ) );
151  if ( ! ret->empty() )
152  return ret;
153 
154  IMediaKey current( citem_r );
155  if ( current != _lastInteractive )
156  {
157  if ( _lastInteractive != IMediaKey() )
158  {
159  cacheLastInteractive( citem_r );
160  }
161 
162  DBG << "Interactive change [" << ++_dbgChanges << "] from " << _lastInteractive << " to " << current << endl;
163  _lastInteractive = current;
164  }
165 
166  // Provide and return the file from media.
167  return sourceProvidePackage( citem_r );
168  }
169 
170 
172  } // namespace target
175 } // namespace zypp
RepoInfo info() const
Return any associated RepoInfo.
Definition: Repository.cc:201
#define MIL
Definition: Logger.h:47
std::string alias() const
unique identifier for this source.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:320
IdType id() const
Expert backdoor.
Definition: Repository.h:266
void resetDispose()
Set no dispose function.
Definition: AutoDispose.h:162
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
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:49
virtual ManagedFile sourceProvidePackage(const PoolItem &pi) const
Let the Source provide the package.
ResObject::constPtr resolvable() const
Returns the ResObject::constPtr.
Definition: PoolItem.cc:280
CommitPackageCache::PackageProvider PackageProvider
void cacheLastInteractive(const PoolItem &citem_r)
Fill the cache.
Package interface.
Definition: Package.h:32
#define WAR
Definition: Logger.h:48
std::ostream & operator<<(std::ostream &str, const CommitPackageCache &obj)
ResStatus & status() const
Returns the current status.
Definition: PoolItem.cc:241
const std::vector< sat::Solvable > & commitList() const
void doCacheLastInteractive(const PoolItem &citem_r)
cacheLastInteractive helper .
Helper storing a source id and media number.
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition: Exception.h:324
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 ...
Definition: AutoDispose.h:92
Reference to a PoolItem connecting ResObject and ResStatus.
Definition: PoolItem.h:50
DefaultIntegral< unsigned, 0 > _mediaNr
#define DBG
Definition: Logger.h:46