blob: b4832029b6638093c849815b510648485f9e14ab (
plain) (
blame)
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
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include <zenstore/hashkeyset.h>
#include <zenstore/memorycidstore.h>
namespace zen {
MemoryCidStore::MemoryCidStore(CidStore* BackingStore) : m_BackingStore(BackingStore)
{
if (m_BackingStore)
{
m_FlushThreadEnabled = true;
m_FlushThread = std::thread(&MemoryCidStore::FlushThreadFunction, this);
}
}
MemoryCidStore::~MemoryCidStore()
{
m_FlushThreadEnabled = false;
m_FlushEvent.Set();
if (m_FlushThread.joinable())
{
m_FlushThread.join();
}
}
MemoryCidStore::InsertResult
MemoryCidStore::AddChunk(const IoBuffer& ChunkData, const IoHash& RawHash)
{
bool IsNew = false;
m_Lock.WithExclusiveLock([&] {
auto [It, Inserted] = m_Chunks.try_emplace(RawHash, ChunkData);
IsNew = Inserted;
});
if (m_BackingStore)
{
std::lock_guard<std::mutex> Lock(m_FlushLock);
m_FlushQueue.push_back({.Data = ChunkData, .Hash = RawHash});
m_FlushEvent.Set();
}
return {.New = IsNew};
}
std::vector<MemoryCidStore::InsertResult>
MemoryCidStore::AddChunks(std::span<IoBuffer> ChunkDatas, std::span<IoHash> RawHashes)
{
std::vector<MemoryCidStore::InsertResult> Results;
Results.reserve(ChunkDatas.size());
for (size_t i = 0; i < ChunkDatas.size(); ++i)
{
Results.push_back(AddChunk(ChunkDatas[i], RawHashes[i]));
}
return Results;
}
IoBuffer
MemoryCidStore::FindChunkByCid(const IoHash& DecompressedId)
{
IoBuffer Result;
m_Lock.WithSharedLock([&] {
auto It = m_Chunks.find(DecompressedId);
if (It != m_Chunks.end())
{
Result = It->second;
}
});
if (!Result && m_BackingStore)
{
Result = m_BackingStore->FindChunkByCid(DecompressedId);
}
return Result;
}
bool
MemoryCidStore::ContainsChunk(const IoHash& DecompressedId)
{
bool Found = false;
m_Lock.WithSharedLock([&] { Found = m_Chunks.find(DecompressedId) != m_Chunks.end(); });
if (!Found && m_BackingStore)
{
Found = m_BackingStore->ContainsChunk(DecompressedId);
}
return Found;
}
void
MemoryCidStore::FilterChunks(HashKeySet& InOutChunks)
{
// Remove hashes that are present in our memory store
m_Lock.WithSharedLock([&] { InOutChunks.RemoveHashesIf([&](const IoHash& Hash) { return m_Chunks.find(Hash) != m_Chunks.end(); }); });
// Delegate remainder to backing store
if (m_BackingStore && !InOutChunks.IsEmpty())
{
m_BackingStore->FilterChunks(InOutChunks);
}
}
void
MemoryCidStore::FlushThreadFunction()
{
SetCurrentThreadName("MemCidFlush");
while (m_FlushThreadEnabled)
{
m_FlushEvent.Wait();
std::vector<PendingWrite> Batch;
{
std::lock_guard<std::mutex> Lock(m_FlushLock);
Batch.swap(m_FlushQueue);
}
for (PendingWrite& Write : Batch)
{
m_BackingStore->AddChunk(Write.Data, Write.Hash);
}
}
// Drain remaining writes on shutdown
std::vector<PendingWrite> Remaining;
{
std::lock_guard<std::mutex> Lock(m_FlushLock);
Remaining.swap(m_FlushQueue);
}
for (PendingWrite& Write : Remaining)
{
m_BackingStore->AddChunk(Write.Data, Write.Hash);
}
}
} // namespace zen
|