This repository was archived by the owner on Jun 27, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathfilecache.cpp
More file actions
208 lines (178 loc) · 5.83 KB
/
Copy pathfilecache.cpp
File metadata and controls
208 lines (178 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* This file is part of PlantUML QEditor.
*
* PlantUML QEditor is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PlantUML QEditor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PlantUML QEditor. If not, see <http://www.gnu.org/licenses/>.
*/
#include "filecache.h"
#include <QDir>
#include <QDebug>
//------------------------------------------------------------------------------
namespace {
QString cachePathFromPathAndKey(const QString& path, const QString& key) {
return QFileInfo(QDir(path), key).absoluteFilePath();
}
} // namespace {}
//------------------------------------------------------------------------------
AbstractFileCacheItem::AbstractFileCacheItem(const QString& path, const QString &key, int cost, const QDateTime &date_time, QObject* parent)
: QObject(parent)
, m_path(cachePathFromPathAndKey(path, key))
, m_key(key)
, m_cost(cost)
, m_dateTime(date_time)
{
qDebug() << qPrintable(QString("new file cache item -> key: %1 cost: %2 path: %3").arg(key, -10).arg(cost, -5).arg(m_path));
}
AbstractFileCacheItem::~AbstractFileCacheItem()
{
}
//------------------------------------------------------------------------------
FileCacheItem::FileCacheItem(const QString& path, const QString &key, int cost, const QDateTime &date_time, QObject *parent)
: AbstractFileCacheItem(path, key, cost, date_time, parent)
, m_removed(false)
{
}
FileCacheItem::~FileCacheItem()
{
}
void FileCacheItem::removeFileFromDisk(const QString& some_path) const
{
if (m_removed || // try to remove twice
some_path != path() // trying to remove something else
)
throw FileCacheError();
QDir().remove(path());
qDebug() << qPrintable(QString("removed cached file -> key: %1 cost: %2 path: %3").arg(key(), -10).arg(cost(), -5).arg(path()));
m_removed = true;
}
//------------------------------------------------------------------------------
FileCache::FileCache(int size, QObject *parent)
: QObject(parent)
, m_maxCost(size)
, m_totalCost(0)
{
}
FileCache::~FileCache()
{
foreach (AbstractFileCacheItem* item, m_items) {
delete item;
}
}
void FileCache::setMaxCost(int max_cost)
{
m_maxCost = max_cost;
}
bool FileCache::hasItem(const QString &key) const
{
return m_items.contains(key);
}
void FileCache::addItem(AbstractFileCacheItem *item)
{
AbstractFileCacheItem* old_item = m_items.value(item->key());
if (old_item) {
if (old_item == item || // adding the same item twice is an error
old_item->path() != item->path() // adding with same key but different path is not supported
) {
throw FileCacheError();
}
m_totalCost += item->cost() - old_item->cost();
m_indexByDate.removeOne(item->key());
m_items.remove(item->key());
delete old_item;
} else {
m_totalCost += item->cost();
}
m_items[item->key()] = item;
bool index_by_date_updated = false;
for(int index = 0; index < m_indexByDate.size(); index++) {
if (m_items[m_indexByDate[index]]->dateTime() > item->dateTime()) {
m_indexByDate.insert(index, item->key());
index_by_date_updated = true;
break;
}
}
if (!index_by_date_updated) {
m_indexByDate.append(item->key());
}
item->setParent(this);
while (m_totalCost > m_maxCost && m_indexByDate.size() > 1) {
QString tmp_key = m_indexByDate.value(0);
const AbstractFileCacheItem* tmp_item = m_items.value(tmp_key);
Q_ASSERT(tmp_item);
m_totalCost -= tmp_item->cost();
tmp_item->removeFileFromDisk();
m_items.remove(tmp_key);
m_indexByDate.removeAt(0);
delete tmp_item;
}
}
void FileCache::addItem(const QByteArray &data, const QString &key, FileCache::ItemGenerator item_generator)
{
QFile file(cachePathFromPathAndKey(m_path, key));
if (!file.open(QIODevice::WriteOnly)) {
return;
}
file.write(data);
file.close();
QFileInfo info(QDir(m_path), key);
int cost = info.size();
QDateTime date_time = info.lastModified();
addItem(item_generator(m_path, key, cost, date_time, this));
}
void FileCache::clear()
{
foreach (AbstractFileCacheItem* item, m_items) {
delete item;
}
m_items.clear();
m_indexByDate.clear();
m_totalCost = 0;
}
void FileCache::clearFromDisk()
{
qDebug() << "clear from disk:" << m_path;
foreach (AbstractFileCacheItem* item, m_items) {
item->removeFileFromDisk();
delete item;
}
m_items.clear();
m_indexByDate.clear();
m_totalCost = 0;
}
bool FileCache::setPath(const QString &path, ItemGenerator item_generator)
{
if (m_path != path) {
clear();
bool success = updateFromDisk(path, item_generator);
if (success) {
m_path = path;
}
return success;
}
return true;
}
bool FileCache::updateFromDisk(const QString &path, ItemGenerator item_generator)
{
QDir dir(path);
if (!dir.mkpath(path)) {
return false;
}
foreach (QFileInfo info, dir.entryInfoList(QDir::Files)) {
QString key = info.fileName();
int cost = info.size();
QDateTime date_time = info.lastRead();
addItem(item_generator(path, key, cost, date_time, this));
}
return true;
}
//------------------------------------------------------------------------------