-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiskcachemanager.cpp
More file actions
149 lines (125 loc) · 3.59 KB
/
Copy pathdiskcachemanager.cpp
File metadata and controls
149 lines (125 loc) · 3.59 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
#include "diskcachemanager.h"
#include <QDir>
#include <QFile>
#include <QDataStream>
#include <QDebug>
DiskCacheManager::DiskCacheManager(const QString &cacheDir)
{
if (cacheDir.isEmpty()) {
m_cacheDir = QDir::tempPath() + "/ViewOSMMap_cache";
} else {
m_cacheDir = cacheDir;
}
QDir().mkpath(m_cacheDir);
}
DiskCacheManager::~DiskCacheManager()
{
clear();
}
bool DiskCacheManager::saveCell(uint64_t cellKey, const GridCellData &data)
{
QMutexLocker locker(&m_mutex);
QString fileName = QString("%1/cell_%2.dat").arg(m_cacheDir).arg(cellKey, 16, 16, QChar('0'));
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly)) {
return false;
}
QDataStream out(&file);
out.setVersion(QDataStream::Qt_6_0);
out << data.cellKey;
out << static_cast<quint32>(data.ways.size());
for (const auto &way : data.ways) {
out << way.id;
out << static_cast<quint32>(way.nodeIndices.size());
for (int idx : way.nodeIndices) {
out << static_cast<quint32>(idx);
}
out << way.bbox;
out << way.isClosed;
out << way.layer;
out << way.tagHead;
}
out << data.lastAccessTime;
m_cellFileMap.insert(cellKey, fileName);
return true;
}
bool DiskCacheManager::loadCell(uint64_t cellKey, GridCellData &data)
{
QMutexLocker locker(&m_mutex);
auto it = m_cellFileMap.find(cellKey);
QString fileName;
if (it != m_cellFileMap.end()) {
fileName = it.value();
} else {
fileName = QString("%1/cell_%2.dat").arg(m_cacheDir).arg(cellKey, 16, 16, QChar('0'));
}
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) {
return false;
}
QDataStream in(&file);
in.setVersion(QDataStream::Qt_6_0);
in >> data.cellKey;
quint32 wayCount;
in >> wayCount;
data.ways.resize(wayCount);
for (quint32 i = 0; i < wayCount; ++i) {
auto &way = data.ways[i];
in >> way.id;
quint32 ndCount;
in >> ndCount;
way.nodeIndices.resize(ndCount);
for (quint32 j = 0; j < ndCount; ++j) {
quint32 idx;
in >> idx;
way.nodeIndices[j] = static_cast<int>(idx);
}
in >> way.bbox;
in >> way.isClosed;
in >> way.layer;
in >> way.tagHead;
}
in >> data.lastAccessTime;
m_cellFileMap.insert(cellKey, fileName);
return true;
}
bool DiskCacheManager::hasCell(uint64_t cellKey) const
{
QMutexLocker locker(&m_mutex);
if (m_cellFileMap.contains(cellKey)) {
return true;
}
QString fileName = QString("%1/cell_%2.dat").arg(m_cacheDir).arg(cellKey, 16, 16, QChar('0'));
return QFile::exists(fileName);
}
void DiskCacheManager::removeCell(uint64_t cellKey)
{
QMutexLocker locker(&m_mutex);
auto it = m_cellFileMap.find(cellKey);
if (it != m_cellFileMap.end()) {
QFile::remove(it.value());
m_cellFileMap.erase(it);
} else {
QString fileName = QString("%1/cell_%2.dat").arg(m_cacheDir).arg(cellKey, 16, 16, QChar('0'));
QFile::remove(fileName);
}
}
void DiskCacheManager::clear()
{
QMutexLocker locker(&m_mutex);
for (const QString &fileName : m_cellFileMap.values()) {
QFile::remove(fileName);
}
m_cellFileMap.clear();
QDir dir(m_cacheDir);
QStringList filters;
filters << "cell_*.dat";
for (const QString &file : dir.entryList(filters)) {
QFile::remove(dir.absoluteFilePath(file));
}
}
int DiskCacheManager::cachedCellCount() const
{
QMutexLocker locker(&m_mutex);
return m_cellFileMap.size();
}