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