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
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include "fileremoteprojectstore.h"
#include <zencore/compress.h>
#include <zencore/filesystem.h>
#include <zencore/fmtutils.h>
#include <zencore/logging.h>
#include <zencore/timer.h>
namespace zen {
using namespace std::literals;
class LocalExportProjectStore : public RemoteProjectStore
{
public:
LocalExportProjectStore(std::string_view Name,
const std::filesystem::path& FolderPath,
bool ForceDisableBlocks,
bool ForceEnableTempBlocks)
: m_Name(Name)
, m_OutputPath(FolderPath)
{
if (ForceDisableBlocks)
{
m_EnableBlocks = false;
}
if (ForceEnableTempBlocks)
{
m_UseTempBlocks = true;
}
}
virtual RemoteStoreInfo GetInfo() const override
{
return {.CreateBlocks = m_EnableBlocks,
.UseTempBlockFiles = m_UseTempBlocks,
.Description = fmt::format("[file] {}"sv, m_OutputPath)};
}
virtual SaveResult SaveContainer(const IoBuffer& Payload) override
{
Stopwatch Timer;
SaveResult Result;
{
CbObject ContainerObject = LoadCompactBinaryObject(Payload);
ContainerObject.IterateAttachments([&](CbFieldView FieldView) {
IoHash AttachmentHash = FieldView.AsBinaryAttachment();
std::filesystem::path AttachmentPath = GetAttachmentPath(AttachmentHash);
if (!std::filesystem::exists(AttachmentPath))
{
Result.Needs.insert(AttachmentHash);
}
});
}
std::filesystem::path ContainerPath = m_OutputPath;
ContainerPath.append(m_Name);
CreateDirectories(m_OutputPath);
BasicFile ContainerFile;
ContainerFile.Open(ContainerPath, BasicFile::Mode::kTruncate);
std::error_code Ec;
ContainerFile.WriteAll(Payload, Ec);
if (Ec)
{
Result.ErrorCode = gsl::narrow<int32_t>(HttpResponseCode::InternalServerError);
Result.Reason = Ec.message();
}
Result.RawHash = IoHash::HashBuffer(Payload);
Result.ElapsedSeconds = Timer.GetElapsedTimeMs() / 1000.500;
return Result;
}
virtual SaveAttachmentResult SaveAttachment(const CompositeBuffer& Payload, const IoHash& RawHash) override
{
Stopwatch Timer;
SaveAttachmentResult Result;
std::filesystem::path ChunkPath = GetAttachmentPath(RawHash);
if (!std::filesystem::exists(ChunkPath))
{
try
{
CreateDirectories(ChunkPath.parent_path());
BasicFile ChunkFile;
ChunkFile.Open(ChunkPath, BasicFile::Mode::kTruncate);
size_t Offset = 0;
for (const SharedBuffer& Segment : Payload.GetSegments())
{
ChunkFile.Write(Segment.GetView(), Offset);
Offset += Segment.GetSize();
}
}
catch (std::exception& Ex)
{
Result.ErrorCode = gsl::narrow<int32_t>(HttpResponseCode::InternalServerError);
Result.Reason = Ex.what();
}
}
Result.ElapsedSeconds = Timer.GetElapsedTimeMs() / 1000.500;
return Result;
}
virtual SaveAttachmentsResult SaveAttachments(const std::vector<SharedBuffer>& Chunks) override
{
Stopwatch Timer;
for (const SharedBuffer& Chunk : Chunks)
{
CompressedBuffer Compressed = CompressedBuffer::FromCompressedNoValidate(Chunk.AsIoBuffer());
SaveAttachmentResult ChunkResult = SaveAttachment(Compressed.GetCompressed(), Compressed.DecodeRawHash());
if (ChunkResult.ErrorCode)
{
ChunkResult.ElapsedSeconds = Timer.GetElapsedTimeMs() / 1000.500;
return SaveAttachmentsResult{ChunkResult};
}
}
SaveAttachmentsResult Result;
Result.ElapsedSeconds = Timer.GetElapsedTimeMs() / 1000.500;
return Result;
}
virtual Result FinalizeContainer(const IoHash&) override { return {}; }
virtual LoadContainerResult LoadContainer() override
{
Stopwatch Timer;
LoadContainerResult Result;
std::filesystem::path ContainerPath = m_OutputPath;
ContainerPath.append(m_Name);
if (!std::filesystem::is_regular_file(ContainerPath))
{
Result.ErrorCode = gsl::narrow<int>(HttpResponseCode::NotFound);
Result.Reason = fmt::format("The file {} does not exist"sv, ContainerPath.string());
Result.ElapsedSeconds = Timer.GetElapsedTimeMs() / 1000.500;
return Result;
}
IoBuffer ContainerPayload;
{
BasicFile ContainerFile;
ContainerFile.Open(ContainerPath, BasicFile::Mode::kRead);
ContainerPayload = ContainerFile.ReadAll();
}
Result.ContainerObject = LoadCompactBinaryObject(ContainerPayload);
if (!Result.ContainerObject)
{
Result.ErrorCode = gsl::narrow<int32_t>(HttpResponseCode::InternalServerError);
Result.Reason = fmt::format("The file {} is not formatted as a compact binary object"sv, ContainerPath.string());
Result.ElapsedSeconds = Timer.GetElapsedTimeMs() / 1000.500;
return Result;
}
Result.ElapsedSeconds = Timer.GetElapsedTimeMs() / 1000.500;
return Result;
}
virtual LoadAttachmentResult LoadAttachment(const IoHash& RawHash) override
{
Stopwatch Timer;
LoadAttachmentResult Result;
std::filesystem::path ChunkPath = GetAttachmentPath(RawHash);
if (!std::filesystem::is_regular_file(ChunkPath))
{
Result.ErrorCode = gsl::narrow<int>(HttpResponseCode::NotFound);
Result.Reason = fmt::format("The file {} does not exist"sv, ChunkPath.string());
Result.ElapsedSeconds = Timer.GetElapsedTimeMs() / 1000.500;
return Result;
}
{
BasicFile ChunkFile;
ChunkFile.Open(ChunkPath, BasicFile::Mode::kRead);
Result.Bytes = ChunkFile.ReadAll();
}
Result.ElapsedSeconds = Timer.GetElapsedTimeMs() / 1000.500;
return Result;
}
virtual LoadAttachmentsResult LoadAttachments(const std::vector<IoHash>& RawHashes) override
{
Stopwatch Timer;
LoadAttachmentsResult Result;
for (const IoHash& Hash : RawHashes)
{
LoadAttachmentResult ChunkResult = LoadAttachment(Hash);
if (ChunkResult.ErrorCode)
{
ChunkResult.ElapsedSeconds = Timer.GetElapsedTimeMs() / 1000.500;
return LoadAttachmentsResult{ChunkResult};
}
ZEN_DEBUG("Loaded attachment in {}", NiceTimeSpanMs(static_cast<uint64_t>(ChunkResult.ElapsedSeconds * 1000)));
Result.Chunks.emplace_back(
std::pair<IoHash, CompressedBuffer>{Hash, CompressedBuffer::FromCompressedNoValidate(std::move(ChunkResult.Bytes))});
}
return Result;
}
private:
std::filesystem::path GetAttachmentPath(const IoHash& RawHash) const
{
ExtendablePathBuilder<128> ShardedPath;
ShardedPath.Append(m_OutputPath.c_str());
ExtendableStringBuilder<64> HashString;
RawHash.ToHexString(HashString);
const char* str = HashString.c_str();
ShardedPath.AppendSeparator();
ShardedPath.AppendAsciiRange(str, str + 3);
ShardedPath.AppendSeparator();
ShardedPath.AppendAsciiRange(str + 3, str + 5);
ShardedPath.AppendSeparator();
ShardedPath.AppendAsciiRange(str + 5, str + 40);
return ShardedPath.ToPath();
}
const std::string m_Name;
const std::filesystem::path m_OutputPath;
bool m_EnableBlocks = true;
bool m_UseTempBlocks = false;
};
std::unique_ptr<RemoteProjectStore>
CreateFileRemoteStore(const FileRemoteStoreOptions& Options)
{
std::unique_ptr<RemoteProjectStore> RemoteStore = std::make_unique<LocalExportProjectStore>(Options.Name,
std::filesystem::path(Options.FolderPath),
Options.ForceDisableBlocks,
Options.ForceEnableTempBlocks);
return RemoteStore;
}
} // namespace zen
|