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
|
// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "namespacecachestore.h"
#include "structuredcachestore.h"
namespace zen {
const char* NamespaceDirPrefix = "ns_";
NamespaceCacheStore::NamespaceCacheStore(std::filesystem::path BasePath, CasGc& Gc)
: GcStorage(Gc)
, GcContributor(Gc)
, m_Log(logging::Get("namespacecachestore"))
, m_BasePath(BasePath)
{
CreateDirectories(m_BasePath);
std::vector<std::string> ExistingFolders = FindExistingFolders();
std::vector<std::string> LegacyBuckets;
std::vector<std::string> Namespaces;
for (const std::string& DirName : ExistingFolders)
{
if (DirName.starts_with(NamespaceDirPrefix))
{
Namespaces.push_back(DirName.substr(3));
continue;
}
LegacyBuckets.push_back(DirName);
}
if (Namespaces.empty() && !LegacyBuckets.empty())
{
// If we find no namespaces, but any unknown folders we assume we have a legacy folder
// and move any existing folders into a default namespace
std::filesystem::path DefaultfNamespaceFolder = m_BasePath / NamespaceDirPrefix;
CreateDirectories(DefaultfNamespaceFolder);
for (const std::string& DirName : LegacyBuckets)
{
std::filesystem::path LegacyFolder = m_BasePath / DirName;
std::filesystem::path NewPath = DefaultfNamespaceFolder / DirName;
std::filesystem::rename(LegacyFolder, NewPath);
}
Namespaces.push_back("");
}
for (const std::string& NamespaceName : Namespaces)
{
ZenCacheStore* Store = new ZenCacheStore(Gc, m_BasePath / (NamespaceDirPrefix + NamespaceName));
m_Namespaces[NamespaceName] = Store;
}
}
NamespaceCacheStore::~NamespaceCacheStore()
{
for (const auto& Entry : m_Namespaces)
{
delete Entry.second;
}
m_Namespaces.clear();
}
std::vector<std::string>
NamespaceCacheStore::FindExistingFolders() const
{
FileSystemTraversal Traversal;
struct Visitor : public FileSystemTraversal::TreeVisitor
{
virtual void VisitFile([[maybe_unused]] const std::filesystem::path& Parent,
[[maybe_unused]] const path_view& File,
[[maybe_unused]] uint64_t FileSize) override
{
}
virtual bool VisitDirectory([[maybe_unused]] const std::filesystem::path& Parent, const path_view& DirectoryName) override
{
std::string DirName8 = WideToUtf8(DirectoryName);
Dirs.push_back(DirName8);
return false;
}
std::vector<std::string> Dirs;
} Visit;
Traversal.TraverseFileSystem(m_BasePath, Visit);
return Visit.Dirs;
}
bool
NamespaceCacheStore::Get(const std::string& Namespace, std::string_view Bucket, const IoHash& HashKey, ZenCacheValue& OutValue)
{
ZenCacheStore* Store = GetStore(Namespace);
if (!Store)
{
return false;
}
return Store->Get(Bucket, HashKey, OutValue);
}
void
NamespaceCacheStore::Put(const std::string& Namespace, std::string_view Bucket, const IoHash& HashKey, const ZenCacheValue& Value)
{
ZenCacheStore* Store = GetStore(Namespace);
if (!Store)
{
return;
}
Store->Put(Bucket, HashKey, Value);
}
bool
NamespaceCacheStore::DropBucket(const std::string& Namespace, std::string_view Bucket)
{
ZenCacheStore* Store = GetStore(Namespace);
if (!Store)
{
return false;
}
return Store->DropBucket(Bucket);
}
void
NamespaceCacheStore::Flush()
{
std::vector<ZenCacheStore*> Stores;
RwLock::SharedLockScope _(m_NamespacesLock);
Stores.reserve(m_Namespaces.size());
for (const auto& Entry : m_Namespaces)
{
Stores.push_back(Entry.second);
}
_.ReleaseNow();
for (ZenCacheStore* Store : Stores)
{
Store->Flush();
}
}
void
NamespaceCacheStore::Scrub(ScrubContext& Ctx)
{
std::vector<ZenCacheStore*> Stores = GetAllStores();
for (ZenCacheStore* Store : Stores)
{
Store->Scrub(Ctx);
}
}
ZenCacheStore*
NamespaceCacheStore::GetStore(const std::string& Namespace)
{
RwLock::SharedLockScope _(m_NamespacesLock);
if (auto It = m_Namespaces.find(Namespace); It != m_Namespaces.end())
{
return It->second;
}
return nullptr;
}
std::vector<ZenCacheStore*>
NamespaceCacheStore::GetAllStores() const
{
std::vector<ZenCacheStore*> Stores;
RwLock::SharedLockScope _(m_NamespacesLock);
Stores.reserve(m_Namespaces.size());
for (const auto& Entry : m_Namespaces)
{
Stores.push_back(Entry.second);
}
return Stores;
}
void
NamespaceCacheStore::GatherReferences(GcContext& GcCtx)
{
std::vector<ZenCacheStore*> Stores = GetAllStores();
for (ZenCacheStore* Store : Stores)
{
Store->GatherReferences(GcCtx);
}
}
void
NamespaceCacheStore::CollectGarbage(GcContext& GcCtx)
{
std::vector<ZenCacheStore*> Stores = GetAllStores();
for (ZenCacheStore* Store : Stores)
{
Store->CollectGarbage(GcCtx);
}
}
GcStorageSize
NamespaceCacheStore::StorageSize() const
{
std::vector<ZenCacheStore*> Stores = GetAllStores();
GcStorageSize Size;
for (ZenCacheStore* Store : Stores)
{
GcStorageSize StoreSize = Store->StorageSize();
Size.MemorySize += StoreSize.MemorySize;
Size.DiskSize += StoreSize.DiskSize;
}
return Size;
}
} // namespace zen
|