-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIndexedStorage.mpp
More file actions
388 lines (327 loc) · 12.2 KB
/
IndexedStorage.mpp
File metadata and controls
388 lines (327 loc) · 12.2 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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
export module CppUtils.FileSystem.IndexedStorage;
import std;
import CppUtils.Memory;
import CppUtils.Type.Serializer;
import CppUtils.FileSystem.File;
import CppUtils.Thread.SharedLocker;
namespace CppUtils::FileSystem
{
struct IndexedStorageEntry final
{
std::size_t chunkId;
std::size_t offset;
std::size_t size;
std::size_t allocatedSize;
};
struct IndexedStorageFreeBlock final
{
std::size_t chunkId;
std::size_t offset;
std::size_t size;
};
struct ChunkLocation final
{
std::size_t chunkId;
std::size_t offset;
auto operator<=>(const ChunkLocation&) const = default;
};
}
namespace CppUtils::Type::Binary
{
template<>
struct Serializer<CppUtils::FileSystem::IndexedStorageEntry> final
{
static inline auto serialize(const CppUtils::FileSystem::IndexedStorageEntry& value, std::vector<std::byte>& buffer) -> void
{
Serializer<std::size_t>::serialize(value.chunkId, buffer);
Serializer<std::size_t>::serialize(value.offset, buffer);
Serializer<std::size_t>::serialize(value.size, buffer);
Serializer<std::size_t>::serialize(value.allocatedSize, buffer);
}
[[nodiscard]] static inline auto deserialize(std::span<const std::byte>& view) -> std::expected<CppUtils::FileSystem::IndexedStorageEntry, std::string_view>
{
auto chunkId = Serializer<std::size_t>::deserialize(view);
if (not chunkId)
return std::unexpected{chunkId.error()};
auto offset = Serializer<std::size_t>::deserialize(view);
if (not offset)
return std::unexpected{offset.error()};
auto size = Serializer<std::size_t>::deserialize(view);
if (not size)
return std::unexpected{size.error()};
auto allocatedSize = Serializer<std::size_t>::deserialize(view);
if (not allocatedSize)
return std::unexpected{allocatedSize.error()};
return CppUtils::FileSystem::IndexedStorageEntry{chunkId.value(), offset.value(), size.value(), allocatedSize.value()};
}
};
template<>
struct Serializer<CppUtils::FileSystem::IndexedStorageFreeBlock> final
{
static inline auto serialize(const CppUtils::FileSystem::IndexedStorageFreeBlock& value, std::vector<std::byte>& buffer) -> void
{
Serializer<std::size_t>::serialize(value.chunkId, buffer);
Serializer<std::size_t>::serialize(value.offset, buffer);
Serializer<std::size_t>::serialize(value.size, buffer);
}
[[nodiscard]] static inline auto deserialize(std::span<const std::byte>& view) -> std::expected<CppUtils::FileSystem::IndexedStorageFreeBlock, std::string_view>
{
auto chunkId = Serializer<std::size_t>::deserialize(view);
if (not chunkId)
return std::unexpected{chunkId.error()};
auto offset = Serializer<std::size_t>::deserialize(view);
if (not offset)
return std::unexpected{offset.error()};
auto size = Serializer<std::size_t>::deserialize(view);
if (not size)
return std::unexpected{size.error()};
return CppUtils::FileSystem::IndexedStorageFreeBlock{chunkId.value(), offset.value(), size.value()};
}
};
}
export namespace CppUtils::FileSystem
{
class IndexedStorage final
{
public:
using Entry = IndexedStorageEntry;
using FreeBlock = IndexedStorageFreeBlock;
using IndexMap = std::unordered_map<std::size_t, Entry>;
using FreeList = std::multimap<std::size_t, FreeBlock>;
private:
std::filesystem::path m_directory;
Thread::SharedLocker<IndexMap> m_index;
FreeList m_freeList;
std::map<ChunkLocation, std::size_t> m_freeBlocksByLocation;
std::size_t m_currentChunkId = 0;
std::size_t m_currentChunkOffset = 0;
std::size_t m_maxChunkSize;
std::ofstream m_currentChunkStream;
mutable std::shared_mutex m_mutex;
public:
explicit IndexedStorage(std::filesystem::path directory, std::optional<std::size_t> maxChunkSize = std::nullopt):
m_directory{std::move(directory)}
{
using namespace CppUtils::Memory::Literals;
m_maxChunkSize = maxChunkSize.value_or(64_MiB);
if (not std::filesystem::exists(m_directory))
std::filesystem::create_directories(m_directory);
loadIndex();
openActiveChunk();
}
~IndexedStorage()
{
flush();
}
IndexedStorage(const IndexedStorage&) = delete;
auto operator=(const IndexedStorage&) -> IndexedStorage& = delete;
template<Type::Serializable T>
auto store(std::size_t id, const T& value) -> void
{
auto buffer = Type::serialize(value);
auto size = std::ranges::size(buffer);
auto lock = std::unique_lock{m_mutex};
auto indexAccessor = m_index.uniqueAccess();
if (auto it = indexAccessor->find(id); it != indexAccessor->end())
{
addFreeBlock(it->second.chunkId, it->second.offset, it->second.allocatedSize);
indexAccessor->erase(it);
}
if (auto it = m_freeList.lower_bound(size); it != std::end(m_freeList))
{
auto block = it->second;
m_freeList.erase(it);
m_freeBlocksByLocation.erase(ChunkLocation{block.chunkId, block.offset});
writeToChunk(block.chunkId, block.offset, buffer);
if (constexpr auto minimumSplitThreshold = 8uz;
block.size > size + minimumSplitThreshold)
{
addFreeBlock(block.chunkId, block.offset + size, block.size - size);
indexAccessor.value()[id] = Entry{block.chunkId, block.offset, size, size};
}
else
indexAccessor.value()[id] = Entry{block.chunkId, block.offset, size, block.size};
return;
}
if (m_currentChunkOffset + size > m_maxChunkSize and m_currentChunkOffset > 0)
{
m_currentChunkStream.close();
++m_currentChunkId;
openActiveChunk();
}
m_currentChunkStream.write(reinterpret_cast<const char*>(std::ranges::data(buffer)), static_cast<std::streamsize>(size));
m_currentChunkStream.flush();
indexAccessor.value()[id] = Entry{m_currentChunkId, m_currentChunkOffset, size, size};
m_currentChunkOffset += size;
}
template<Type::Serializable T>
[[nodiscard]] auto load(std::size_t id) const -> std::expected<T, std::string_view>
{
auto lock = std::shared_lock{m_mutex};
auto indexAccessor = m_index.sharedAccess();
auto it = indexAccessor->find(id);
if (it == indexAccessor->end())
return std::unexpected{"IndexedStorage::load(): Entry not found"};
const auto& entry = it->second;
auto chunkPath = getChunkPath(entry.chunkId);
auto file = std::ifstream{chunkPath, std::ios::binary};
if (not file.is_open())
return std::unexpected{"IndexedStorage::load(): Failed to open chunk file"};
file.seekg(static_cast<std::streamoff>(entry.offset));
auto buffer = std::vector<char>(entry.size);
file.read(std::ranges::data(buffer), static_cast<std::streamsize>(entry.size));
auto view = std::span<const std::byte>{reinterpret_cast<const std::byte*>(std::ranges::data(buffer)), entry.size};
auto result = Type::deserialize<T>(view);
if (not result)
return std::unexpected{result.error()};
return std::move(result.value());
}
[[nodiscard]] auto contains(std::size_t id) const -> bool
{
auto lock = std::shared_lock{m_mutex};
auto indexAccessor = m_index.sharedAccess();
return indexAccessor->contains(id);
}
auto remove(std::size_t id) -> void
{
auto lock = std::unique_lock{m_mutex};
auto indexAccessor = m_index.uniqueAccess();
if (auto it = indexAccessor->find(id); it != indexAccessor->end())
{
addFreeBlock(it->second.chunkId, it->second.offset, it->second.allocatedSize);
indexAccessor->erase(it);
}
}
[[nodiscard]] auto getIndex() const -> Thread::ReadOnlyAccessor<IndexMap>
{
return m_index.sharedAccess();
}
auto flush() -> void
{
auto lock = std::unique_lock{m_mutex};
if (m_currentChunkStream.is_open())
m_currentChunkStream.flush();
saveIndex();
}
private:
auto addFreeBlock(std::size_t chunkId, std::size_t offset, std::size_t size) -> void
{
if (size == 0)
return;
auto removeFromFreeList = [&](std::size_t offset, std::size_t size) {
auto [rangeBegin, rangeEnd] = m_freeList.equal_range(size);
for (auto iterator = rangeBegin; iterator != rangeEnd; ++iterator)
if (iterator->second.chunkId == chunkId and iterator->second.offset == offset)
{
m_freeList.erase(iterator);
break;
}
};
// Merge with following block
if (auto nextIterator = m_freeBlocksByLocation.find(ChunkLocation{chunkId, offset + size});
nextIterator != std::end(m_freeBlocksByLocation))
{
auto nextSize = nextIterator->second;
removeFromFreeList(offset + size, nextSize);
m_freeBlocksByLocation.erase(nextIterator);
size += nextSize;
}
// Merge with preceding block
if (auto previousIterator = m_freeBlocksByLocation.lower_bound(ChunkLocation{chunkId, offset});
previousIterator != std::begin(m_freeBlocksByLocation))
{
previousIterator = std::prev(previousIterator);
if (previousIterator->first.chunkId == chunkId and previousIterator->first.offset + previousIterator->second == offset)
{
auto previousOffset = previousIterator->first.offset;
auto previousSize = previousIterator->second;
removeFromFreeList(previousOffset, previousSize);
m_freeBlocksByLocation.erase(previousIterator);
offset = previousOffset;
size += previousSize;
}
}
// Insert final block
m_freeList.emplace(size, FreeBlock{chunkId, offset, size});
m_freeBlocksByLocation[ChunkLocation{chunkId, offset}] = size;
}
[[nodiscard]] auto getChunkPath(std::size_t chunkId) const -> std::filesystem::path
{
return m_directory / std::format("chunk_{}.bin", chunkId);
}
auto openActiveChunk() -> void
{
auto chunkPath = getChunkPath(m_currentChunkId);
m_currentChunkOffset = std::filesystem::exists(chunkPath) ? std::filesystem::file_size(chunkPath) : 0;
m_currentChunkStream.open(chunkPath, std::ios::binary | std::ios::in | std::ios::out | std::ios::ate);
if (not m_currentChunkStream.is_open())
{
m_currentChunkStream.clear();
m_currentChunkStream.open(chunkPath, std::ios::binary | std::ios::out);
m_currentChunkStream.close();
m_currentChunkStream.open(chunkPath, std::ios::binary | std::ios::in | std::ios::out | std::ios::ate);
}
if (not m_currentChunkStream.is_open())
throw std::runtime_error{"IndexedStorage: Failed to open active chunk"};
}
auto writeToChunk(std::size_t chunkId, std::size_t offset, std::span<const std::byte> buffer) -> void
{
if (chunkId == m_currentChunkId)
{
m_currentChunkStream.seekp(static_cast<std::streamoff>(offset));
m_currentChunkStream.write(reinterpret_cast<const char*>(std::ranges::data(buffer)), static_cast<std::streamsize>(std::ranges::size(buffer)));
m_currentChunkStream.flush();
m_currentChunkStream.seekp(0, std::ios::end);
}
else
{
auto chunkPath = getChunkPath(chunkId);
auto stream = std::ofstream{chunkPath, std::ios::binary | std::ios::in | std::ios::out};
stream.seekp(static_cast<std::streamoff>(offset));
stream.write(reinterpret_cast<const char*>(std::ranges::data(buffer)), static_cast<std::streamsize>(std::ranges::size(buffer)));
stream.flush();
}
}
auto loadIndex() -> void
{
auto indexPath = m_directory / "index.bin";
if (not std::filesystem::exists(indexPath))
return;
auto bytes = FileSystem::Binary::readVector<std::byte>(indexPath);
if (std::ranges::empty(bytes))
return;
auto view = std::span<const std::byte>{bytes};
auto index = Type::deserialize<IndexMap>(view);
if (not index)
return;
m_index.uniqueAccess().value() = std::move(index.value());
auto freeList = Type::deserialize<std::vector<FreeBlock>>(view);
if (not freeList)
return;
for (const auto& block : freeList.value())
{
m_freeList.emplace(block.size, block);
m_freeBlocksByLocation[ChunkLocation{block.chunkId, block.offset}] = block.size;
}
auto indexAccessor = m_index.sharedAccess();
for (const auto& [id, entry] : indexAccessor.value())
if (entry.chunkId > m_currentChunkId)
m_currentChunkId = entry.chunkId;
for (const auto& [size, block] : m_freeList)
if (block.chunkId > m_currentChunkId)
m_currentChunkId = block.chunkId;
}
auto saveIndex() const -> void
{
auto indexPath = m_directory / "index.bin";
auto bytes = std::vector<std::byte>{};
auto indexAccessor = m_index.sharedAccess();
Type::serialize(indexAccessor.value(), bytes);
auto freeList = std::vector<FreeBlock>{};
for (const auto& [size, block] : m_freeList)
freeList.push_back(block);
Type::serialize(freeList, bytes);
FileSystem::Binary::writeVector(indexPath, bytes);
}
};
}