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
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include "zenstore/cidstore.h"
#include <zencore/compress.h>
#include <zencore/filesystem.h>
#include <zencore/fmtutils.h>
#include <zencore/logging.h>
#include <zencore/string.h>
#include <zenstore/scrubcontext.h>
#include "cas.h"
#include <filesystem>
namespace zen {
struct CidStore::Impl
{
explicit Impl(CasStore& InCasStore) : m_CasStore(InCasStore) {}
CasStore& m_CasStore;
void Initialize(const CidStoreConfiguration& Config) { m_CasStore.Initialize(Config); }
CidStore::InsertResult AddChunk(const IoBuffer& ChunkData, const IoHash& RawHash, CidStore::InsertMode Mode)
{
#if ZEN_BUILD_DEBUG
IoHash VerifyRawHash;
uint64_t _;
ZEN_ASSERT(CompressedBuffer::ValidateCompressedHeader(ChunkData, VerifyRawHash, _) && RawHash == VerifyRawHash);
#endif
IoBuffer Payload(ChunkData);
Payload.SetContentType(ZenContentType::kCompressedBinary);
CasStore::InsertResult Result = m_CasStore.InsertChunk(Payload, RawHash, static_cast<CasStore::InsertMode>(Mode));
if (Result.New)
{
WriteCount++;
}
return {.New = Result.New};
}
IoBuffer FindChunkByCid(const IoHash& DecompressedId)
{
IoBuffer Result = m_CasStore.FindChunk(DecompressedId);
if (Result)
{
HitCount++;
}
else
{
MissCount++;
}
return Result;
}
bool ContainsChunk(const IoHash& DecompressedId) { return m_CasStore.ContainsChunk(DecompressedId); }
void FilterChunks(HashKeySet& InOutChunks)
{
InOutChunks.RemoveHashesIf([&](const IoHash& Hash) { return ContainsChunk(Hash); });
}
void Flush() { m_CasStore.Flush(); }
void ScrubStorage(ScrubContext& Ctx)
{
if (Ctx.ScrubTimestamp() == m_LastScrubTime)
{
return;
}
m_LastScrubTime = Ctx.ScrubTimestamp();
m_CasStore.ScrubStorage(Ctx);
}
CidStoreStats Stats() { return CidStoreStats{.HitCount = HitCount, .MissCount = MissCount, .WriteCount = WriteCount}; }
std::atomic_uint64_t HitCount{};
std::atomic_uint64_t MissCount{};
std::atomic_uint64_t WriteCount{};
uint64_t m_LastScrubTime = 0;
};
//////////////////////////////////////////////////////////////////////////
CidStore::CidStore(GcManager& Gc) : m_CasStore(CreateCasStore(Gc)), m_Impl(std::make_unique<Impl>(*m_CasStore))
{
}
CidStore::~CidStore()
{
}
void
CidStore::Initialize(const CidStoreConfiguration& Config)
{
m_Impl->Initialize(Config);
}
CidStore::InsertResult
CidStore::AddChunk(const IoBuffer& ChunkData, const IoHash& RawHash, InsertMode Mode)
{
return m_Impl->AddChunk(ChunkData, RawHash, Mode);
}
IoBuffer
CidStore::FindChunkByCid(const IoHash& DecompressedId)
{
return m_Impl->FindChunkByCid(DecompressedId);
}
bool
CidStore::ContainsChunk(const IoHash& DecompressedId)
{
return m_Impl->ContainsChunk(DecompressedId);
}
void
CidStore::FilterChunks(HashKeySet& InOutChunks)
{
return m_Impl->FilterChunks(InOutChunks);
}
void
CidStore::Flush()
{
m_Impl->Flush();
}
void
CidStore::ScrubStorage(ScrubContext& Ctx)
{
m_Impl->ScrubStorage(Ctx);
}
CidStoreSize
CidStore::TotalSize() const
{
return m_Impl->m_CasStore.TotalSize();
}
CidStoreStats
CidStore::Stats() const
{
return m_Impl->Stats();
}
} // namespace zen
|