-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringsfilecache.cpp
249 lines (215 loc) · 6.7 KB
/
stringsfilecache.cpp
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include "stringsfilecache.h"
#include "writable_path.h"
#include "utils/guard_on.h"
#include <QDir>
#include <QFile>
#include <QDataStream>
#include <QDateTime>
#include <unistd.h>
#include <iostream>
constexpr static quint32 list_file_version = 0x01;
constexpr static auto v1_stream_version = QDataStream::Qt_5_0;
constexpr static auto current_write_stream_version = v1_stream_version;
template <class T>
void writeToFile(QFile& where, const T& src)
{
QDataStream out(&where);
// Write a header with a "magic number" and a version
out << (quint32)0xA0B0C0D0;
out << list_file_version;
out.setVersion(current_write_stream_version);
// Write the data
out << src;
where.flush();
}
template <class T>
bool loadFromFile(QFile& file, T& dst)
{
QDataStream in(&file);
// Read and check the header
quint32 magic;
in >> magic;
if (magic == 0xA0B0C0D0)
{
// Read the version
qint32 version;
in >> version;
if (version == list_file_version)
{
in.setVersion(v1_stream_version);
// Read the data
in >> dst;
return true;
}
}
return false;
}
static QString getCacheDir()
{
const static QString path = getWritableLocationApp() + "/cache";
// std::cout << "File location: " << path.toStdString() << std::endl;
return path;
}
static QString getListFileName()
{
const static QString path = getCacheDir() + "/list.bin";
return path;
}
StringsFileCache::StringsFileCache()
: ram_cache(500)
{
bool need_delete_cache = true;
{
QFile file(getListFileName());
if (file.open(QIODevice::ReadOnly))
need_delete_cache = !loadFromFile(file, key2filename);
}
QDir d(getCacheDir());
if (need_delete_cache)
d.removeRecursively();
d.mkpath(getCacheDir());
}
void StringsFileCache::dumpListFile() const
{
QFile file(getListFileName());
if (file.open(QIODevice::WriteOnly))
writeToFile(file, key2filename);
}
void StringsFileCache::dropKey(const QString &key)
{
ram_cache.remove(key);
const auto& fn = getFileNameOrEmpty(key);
if (!fn.isEmpty())
{
QFile::remove(getCacheDir() + "/" + fn);
key2filename.remove(key);
}
}
void StringsFileCache::addToRam(const QString &key, const QString &value)
{
ram_cache.insert(key, new QString(value));
}
StringsFileCache::~StringsFileCache()
{
LOCK_GUARD_ON(lock);
dumpListFile();
}
static QString buildNumericFnPart()
{
//we may have many copies running with no gui, for example user presses hot keys fast
//so they must have different file names to save, lets do it time + pid
const auto now = QDateTime::currentDateTime().toMSecsSinceEpoch();
const auto pid = getpid();
return QStringLiteral("%1_%2").arg(now).arg(pid);
}
bool StringsFileCache::addData(const QString &key, const QString &value, uint32_t valid_for_seconds)
{
if (key.isEmpty())
return false;
bool result = false;
LOCK_GUARD_ON(lock);
if (value.isEmpty())
{
dropKey(key);
result = true;
}
else
{
const uint64_t valid_till = QDateTime::currentDateTime().toMSecsSinceEpoch() + (uint64_t)valid_for_seconds * 1000u;
const auto compressed = written_value_type{valid_till, compress(value)};
const QString filename = QStringLiteral("value_%1").arg(buildNumericFnPart());
QString finalName = filename;
//most unlikelly this will happen ... but user might change system clock or so and we dont want to overwrite file
for (int counter = 0; QFile::exists(getCacheDir() + "/" + finalName) && counter < 5000; ++counter)
finalName = QStringLiteral("%1_%2").arg(filename).arg(counter);
addToRam(key, value);
QFile file(getCacheDir() + "/" + finalName);
if (file.open(QIODevice::WriteOnly))
{
dropKey(key);
writeToFile(file, compressed);
key2filename[key] = finalName;
result = true;
}
}
if (result)
{
static uint64_t cntr = 1;
if ((cntr++) % 10 == 0)
dumpListFile();
}
return result;
}
QString StringsFileCache::getData(const QString &key)
{
const static QString empty;
if (!key.isEmpty())
{
LOCK_GUARD_ON(lock);
const auto rp = ram_cache[key];
if (rp)
return *rp;
const auto& fn = getFileNameOrEmpty(key);
if (!fn.isEmpty())
{
QFile file(getCacheDir() + "/" + fn);
if (file.open(QIODevice::ReadOnly))
{
written_value_type v;
if (loadFromFile(file, v))
{
if (static_cast<uint64_t>(QDateTime::currentDateTime().toMSecsSinceEpoch()) < v.first)
{
std::unique_ptr<uint8_t[]> decompressed(new uint8_t[v.second.first]);
std::size_t decompressed_size;
const auto error = lzokay::decompress(v.second.second.data(), v.second.second.length(),
decompressed.get(), v.second.first, decompressed_size);
if (error >= lzokay::EResult::Success)
{
const auto t = QString::fromUtf8((char*)decompressed.get(), decompressed_size);
addToRam(key, t);
return t;
}
}
}
}
}
dropKey(key);
}
return empty;
}
void StringsFileCache::cleanAll()
{
LOCK_GUARD_ON(lock);
key2filename.clear();
ram_cache.clear();
QDir d(getCacheDir());
d.removeRecursively();
d.mkpath(getCacheDir());
}
StringsFileCache::binary_blob StringsFileCache::compress(const QString &value)
{
lzokay::EResult error;
const auto src = value.toUtf8();
const size_t estimated_size = lzokay::compress_worst_size(src.length());
StringsFileCache::binary_blob compressed(src.length(), QVector<uint8_t>(estimated_size));
std::size_t compressed_size;
error = lzokay::compress((const uint8_t*)src.data(), compressed.first, compressed.second.data(), estimated_size,
compressed_size, dict);
if (error < lzokay::EResult::Success)
{
compressed.second.clear();
compressed.first = 0;
}
else
compressed.second.resize(compressed_size);
return compressed;
}
const QString &StringsFileCache::getFileNameOrEmpty(const QString &key)
{
const auto it = key2filename.find(key);
if (it != key2filename.end())
return it.value();
const static QString empty;
return empty;
}