LeechCraft  %{LEECHCRAFT_VERSION}
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
networkdiskcache.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "networkdiskcache.h"
31 #include <QtDebug>
32 #include <QDateTime>
33 #include <QDir>
34 #include <QtConcurrentRun>
35 #include <QFutureWatcher>
36 #include <QTimer>
37 #include <QDirIterator>
38 #include <QMutexLocker>
39 #include <util/sys/paths.h>
40 
41 namespace LeechCraft
42 {
43 namespace Util
44 {
45  NetworkDiskCache::NetworkDiskCache (const QString& subpath, QObject *parent)
46  : QNetworkDiskCache (parent)
47  , CurrentSize_ (-1)
48  , InsertRemoveMutex_ (QMutex::Recursive)
49  , GarbageCollectorWatcher_ (nullptr)
50  {
51  setCacheDirectory (GetUserDir (UserDir::Cache, "network/" + subpath).absolutePath ());
52  auto timer = new QTimer (this);
53  timer->setInterval (60 * 60 * 1000);
54  connect (timer,
55  SIGNAL (timeout ()),
56  this,
57  SLOT (collectGarbage ()));
58  }
59 
61  {
62  if (GarbageCollectorWatcher_)
63  GarbageCollectorWatcher_->waitForFinished ();
64  }
65 
67  {
68  return CurrentSize_;
69  }
70 
71  QIODevice* NetworkDiskCache::data (const QUrl& url)
72  {
73  QMutexLocker lock (&InsertRemoveMutex_);
74  return QNetworkDiskCache::data (url);
75  }
76 
77  void NetworkDiskCache::insert (QIODevice *device)
78  {
79  QMutexLocker lock (&InsertRemoveMutex_);
80  if (!PendingDev2Url_.contains (device))
81  {
82  qWarning () << Q_FUNC_INFO
83  << "stall device detected";
84  return;
85  }
86 
87  PendingUrl2Devs_ [PendingDev2Url_.take (device)].removeAll (device);
88 
89  CurrentSize_ += device->size ();
90  QNetworkDiskCache::insert (device);
91  }
92 
93  QNetworkCacheMetaData NetworkDiskCache::metaData (const QUrl& url)
94  {
95  QMutexLocker lock (&InsertRemoveMutex_);
96  return QNetworkDiskCache::metaData (url);
97  }
98 
99  QIODevice* NetworkDiskCache::prepare (const QNetworkCacheMetaData& metadata)
100  {
101  QMutexLocker lock (&InsertRemoveMutex_);
102  const auto dev = QNetworkDiskCache::prepare (metadata);
103  PendingDev2Url_ [dev] = metadata.url ();
104  PendingUrl2Devs_ [metadata.url ()] << dev;
105  return dev;
106  }
107 
108  bool NetworkDiskCache::remove (const QUrl& url)
109  {
110  QMutexLocker lock (&InsertRemoveMutex_);
111  for (const auto dev : PendingUrl2Devs_.take (url))
112  PendingDev2Url_.remove (dev);
113  return QNetworkDiskCache::remove (url);
114  }
115 
116  void NetworkDiskCache::updateMetaData (const QNetworkCacheMetaData& metaData)
117  {
118  QMutexLocker lock (&InsertRemoveMutex_);
119  QNetworkDiskCache::updateMetaData (metaData);
120  }
121 
123  {
124  if (CurrentSize_ < 0)
125  {
126  collectGarbage ();
127  return maximumCacheSize () * 8 / 10;
128  }
129 
130  if (CurrentSize_ > maximumCacheSize ())
131  collectGarbage ();
132 
133  return CurrentSize_;
134  }
135 
136  namespace
137  {
138  qint64 Collector (const QString& cacheDirectory, qint64 goal, QMutex *fileOpMutex)
139  {
140  if (cacheDirectory.isEmpty ())
141  return 0;
142 
143  qDebug () << Q_FUNC_INFO << "running..." << cacheDirectory;
144 
145  QDir::Filters filters = QDir::AllDirs | QDir:: Files | QDir::NoDotAndDotDot;
146  QDirIterator it (cacheDirectory, filters, QDirIterator::Subdirectories);
147 
148  QMultiMap<QDateTime, QString> cacheItems;
149  qint64 totalSize = 0;
150  while (it.hasNext ())
151  {
152  const auto& path = it.next ();
153  const auto& info = it.fileInfo ();
154  cacheItems.insert (info.created (), path);
155  totalSize += info.size ();
156  }
157 
158  auto i = cacheItems.constBegin ();
159  while (i != cacheItems.constEnd ())
160  {
161  if (totalSize < goal)
162  break;
163 
164  QFile file (*i);
165  const auto size = file.size ();
166  totalSize -= size;
167  ++i;
168 
169  QMutexLocker lock (fileOpMutex);
170  file.remove ();
171  }
172 
173  qDebug () << "collector finished" << totalSize;
174 
175  return totalSize;
176  }
177  };
178 
180  {
181  if (GarbageCollectorWatcher_)
182  return;
183 
184  if (cacheDirectory ().isEmpty ())
185  return;
186 
187  GarbageCollectorWatcher_ = new QFutureWatcher<qint64> (this);
188  connect (GarbageCollectorWatcher_,
189  SIGNAL (finished ()),
190  this,
191  SLOT (handleCollectorFinished ()));
192 
193  auto future = QtConcurrent::run (Collector,
194  cacheDirectory (), maximumCacheSize () * 9 / 10, &InsertRemoveMutex_);
195  GarbageCollectorWatcher_->setFuture (future);
196  }
197 
198  void NetworkDiskCache::handleCollectorFinished ()
199  {
200  CurrentSize_ = GarbageCollectorWatcher_->result ();
201  GarbageCollectorWatcher_->deleteLater ();
202  GarbageCollectorWatcher_ = nullptr;
203  }
204 }
205 }
virtual qint64 expire()
Reimplemented from QNetworkDiskCache.
virtual void insert(QIODevice *device)
Reimplemented from QNetworkDiskCache.
virtual qint64 cacheSize() const
Reimplemented from QNetworkDiskCache.
virtual QIODevice * data(const QUrl &url)
Reimplemented from QNetworkDiskCache.
~NetworkDiskCache()
Destroys the cache.
virtual bool remove(const QUrl &url)
Reimplemented from QNetworkDiskCache.
NetworkDiskCache(const QString &subpath, QObject *parent=0)
Constructs the new disk cache.
virtual QIODevice * prepare(const QNetworkCacheMetaData &)
Reimplemented from QNetworkDiskCache.
void collectGarbage()
Runs the garbage collector.
QDir GetUserDir(UserDir dir, const QString &subpath)
Definition: paths.cpp:119
virtual QNetworkCacheMetaData metaData(const QUrl &url)
Reimplemented from QNetworkDiskCache.
Cache for volatile data.
virtual void updateMetaData(const QNetworkCacheMetaData &metaData)
Reimplemented from QNetworkDiskCache.