LeechCraft  0.6.70-3565-g2d86529
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
networkdiskcachegc.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 "networkdiskcachegc.h"
31 #include <QTimer>
32 #include <QDir>
33 #include <QDirIterator>
34 #include <QDateTime>
35 #include <QtDebug>
36 #include <util/sll/qtutil.h>
37 #include <util/sll/prelude.h>
38 #include <util/sll/futures.h>
39 #include <util/sll/util.h>
40 
41 namespace LeechCraft
42 {
43 namespace Util
44 {
45  NetworkDiskCacheGC::NetworkDiskCacheGC ()
46  {
47  const auto timer = new QTimer { this };
48  connect (timer,
49  SIGNAL (timeout ()),
50  this,
51  SLOT (handleCollect ()));
52  timer->start (60 * 60 * 1000);
53  }
54 
56  {
57  static NetworkDiskCacheGC gc;
58  return gc;
59  }
60 
61  namespace
62  {
63  struct SizeCollectInfo
64  {
65  QMultiMap<QDateTime, QString> Items_;
66  qint64 TotalSize_ = 0;
67  };
68 
69  SizeCollectInfo CollectSizes (const QString& cacheDirectory)
70  {
71  SizeCollectInfo result;
72 
73  const QDir::Filters filters = QDir::AllDirs | QDir:: Files | QDir::NoDotAndDotDot;
74  QDirIterator it { cacheDirectory, filters, QDirIterator::Subdirectories };
75 
76  while (it.hasNext ())
77  {
78  const auto& path = it.next ();
79  const auto& info = it.fileInfo ();
80  result.Items_.insert (info.created (), path);
81  result.TotalSize_ += info.size ();
82  }
83 
84  return result;
85  }
86  }
87 
89  {
90  return QtConcurrent::run ([path] { return CollectSizes (path).TotalSize_; });
91  }
92 
94  const std::function<int ()>& sizeGetter)
95  {
96  auto& list = Directories_ [path];
97  list.push_front (sizeGetter);
98  const auto thisItem = list.begin ();
99 
100  return Util::MakeScopeGuard ([this, path, thisItem] { UnregisterDirectory (path, thisItem); }).EraseType ();
101  }
102 
103  void NetworkDiskCacheGC::UnregisterDirectory (const QString& path, CacheSizeGetters_t::iterator pos)
104  {
105  if (!Directories_.contains (path))
106  {
107  qWarning () << Q_FUNC_INFO
108  << "unknown directory"
109  << path;
110  return;
111  }
112 
113  auto& list = Directories_ [path];
114  list.erase (pos);
115 
116  if (!list.isEmpty ())
117  return;
118 
119  Directories_.remove (path);
120  LastSizes_.remove (path);
121  }
122 
123  namespace
124  {
125  qint64 Collector (const QString& cacheDirectory, qint64 goal)
126  {
127  if (cacheDirectory.isEmpty ())
128  return 0;
129 
130  qDebug () << Q_FUNC_INFO << "running..." << cacheDirectory << goal;
131 
132  auto sizeInfoResult = CollectSizes (cacheDirectory);
133 
134  for (auto i = sizeInfoResult.Items_.constBegin ();
135  i != sizeInfoResult.Items_.constEnd () && sizeInfoResult.TotalSize_ > goal;
136  ++i)
137  {
138  QFile file { *i };
139  sizeInfoResult.TotalSize_ -= file.size ();
140  file.remove ();
141  }
142 
143  qDebug () << "collector finished" << sizeInfoResult.TotalSize_;
144 
145  return sizeInfoResult.TotalSize_;
146  }
147  };
148 
149  void NetworkDiskCacheGC::handleCollect ()
150  {
151  if (IsCollecting_)
152  {
153  qWarning () << Q_FUNC_INFO
154  << "already collecting";
155  return;
156  }
157 
159  for (const auto& pair : Util::Stlize (Directories_))
160  {
161  const auto& getters = pair.second;
162  const auto minSize = (*std::min_element (getters.begin (), getters.end (),
163  Util::ComparingBy (Apply))) ();
164  dirs.append ({ pair.first, minSize });
165  }
166 
167  if (dirs.isEmpty ())
168  return;
169 
170  IsCollecting_ = true;
171 
172  Util::ExecuteFuture ([dirs]
173  {
174  return QtConcurrent::run ([dirs]
175  {
176  QMap<QString, qint64> sizes;
177  for (const auto& pair : dirs)
178  sizes [pair.first] = Collector (pair.first, pair.second);
179  return sizes;
180  });
181  },
182  [this] (const QMap<QString, qint64>& sizes)
183  {
184  IsCollecting_ = false;
185  for (const auto& pair : Util::Stlize (sizes))
186  LastSizes_ [pair.first] = pair.second;
187  },
188  this);
189  }
190 }
191 }
QMultiMap< QDateTime, QString > Items_
QFuture< qint64 > GetCurrentSize(const QString &path) const
Schedules calculation of the path total size.
detail::ScopeGuard< F > MakeScopeGuard(const F &f)
Returns an object performing passed function on scope exit.
Definition: util.h:124
Garbage collection for a set of network disk caches.
static NetworkDiskCacheGC & Instance()
Returns a single global instance of the GC manager.
detail::ComparingByClosure< R > ComparingBy(R r)
Definition: prelude.h:251
qint64 TotalSize_
auto Stlize(Assoc &&assoc) -> detail::StlAssocRange< decltype(assoc.begin()), Assoc, PairType >
Converts an Qt's associative sequence assoc to an STL-like iteratable range.
Definition: qtutil.h:126
struct LeechCraft::Util::@0 Apply
Util::DefaultScopeGuard RegisterDirectory(const QString &path, const std::function< int()> &sizeGetter)
Registers the given cache path.
void ExecuteFuture(Executor f, ResultHandler rh, QObject *parent, Args...args)
Runs a QFuture-returning function and feeding the future to a handler when it is ready.
Definition: futures.h:112