-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathblock_buffer.cpp
194 lines (156 loc) · 4.84 KB
/
block_buffer.cpp
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
#include "block_buffer.h"
#include <cloud/filestore/libs/storage/model/utils.h>
#include <cloud/storage/core/libs/common/verify.h>
namespace NCloud::NFileStore::NStorage {
namespace {
////////////////////////////////////////////////////////////////////////////////
class TBlockBuffer final
: public IBlockBuffer
{
private:
const TByteRange ByteRange;
TString Buffer;
public:
TBlockBuffer(TByteRange byteRange, TString buffer)
: ByteRange(byteRange)
, Buffer(std::move(buffer))
{
}
TStringBuf GetUnalignedHead() override
{
const char* ptr = Buffer.data();
return { ptr, ByteRange.UnalignedHeadLength() };
}
TStringBuf GetUnalignedTail() override
{
const auto offset = ByteRange.RelativeUnalignedTailOffset();
const char* ptr = Buffer.data() + offset;
return { ptr, ByteRange.UnalignedTailLength() };
}
TStringBuf GetBlock(size_t index) override
{
const auto offset = ByteRange.RelativeAlignedBlockOffset(index);
const char* ptr = Buffer.data() + offset;
return { ptr, ByteRange.BlockSize };
}
void SetBlock(size_t index, TStringBuf block) override
{
Y_ABORT_UNLESS(block.size() == ByteRange.BlockSize);
const auto offset = ByteRange.RelativeAlignedBlockOffset(index);
char* ptr = const_cast<char*>(Buffer.data()) + offset;
memcpy(ptr, block.data(), ByteRange.BlockSize);
}
void ClearBlock(size_t index) override
{
const auto offset = ByteRange.RelativeAlignedBlockOffset(index);
char* ptr = const_cast<char*>(Buffer.data()) + offset;
memset(ptr, 0, ByteRange.BlockSize);
}
};
////////////////////////////////////////////////////////////////////////////////
class TLazyBlockBuffer final
: public IBlockBuffer
{
private:
const TByteRange ByteRange;
TString UnalignedHead;
TVector<TString> Blocks;
TString UnalignedTail;
public:
explicit TLazyBlockBuffer(TByteRange byteRange)
: ByteRange(byteRange)
, Blocks(ByteRange.BlockCount())
{
}
TStringBuf GetUnalignedHead() override
{
if (!UnalignedHead) {
UnalignedHead.ReserveAndResize(ByteRange.UnalignedHeadLength());
}
return UnalignedHead;
}
TStringBuf GetUnalignedTail() override
{
if (!UnalignedTail) {
UnalignedTail.ReserveAndResize(ByteRange.UnalignedTailLength());
}
return UnalignedTail;
}
TStringBuf GetBlock(size_t index) override
{
auto& block = Blocks[index];
if (!block) {
block.ReserveAndResize(ByteRange.BlockSize);
}
return block;
}
void SetBlock(size_t index, TStringBuf block) override
{
Y_ABORT_UNLESS(block.size() == ByteRange.BlockSize);
Blocks[index] = block;
}
void ClearBlock(size_t index) override
{
Blocks[index].clear();
}
};
} // namespace
////////////////////////////////////////////////////////////////////////////////
IBlockBufferPtr CreateBlockBuffer(TByteRange byteRange)
{
return std::make_shared<TBlockBuffer>(
byteRange,
TString(byteRange.Length, 0));
}
IBlockBufferPtr CreateBlockBuffer(TByteRange byteRange, TString buffer)
{
Y_ABORT_UNLESS(buffer.size() == byteRange.Length);
return std::make_shared<TBlockBuffer>(byteRange, std::move(buffer));
}
IBlockBufferPtr CreateLazyBlockBuffer(TByteRange byteRange)
{
return std::make_shared<TLazyBlockBuffer>(byteRange);
}
////////////////////////////////////////////////////////////////////////////////
void CopyFileData(
const TString& logTag,
const TByteRange origin,
const TByteRange aligned,
const ui64 fileSize,
IBlockBuffer& buffer,
TString* out)
{
const auto end = Min(fileSize, origin.End());
if (end <= origin.Offset) {
return;
}
out->ReserveAndResize(end - origin.Offset);
char* outPtr = out->begin();
ui32 i = 0;
// processing unaligned head
if (origin.Offset > aligned.Offset) {
const auto block = buffer.GetBlock(i);
const auto shift = origin.Offset - aligned.Offset;
const auto sz = Min(block.size() - shift, out->size());
STORAGE_VERIFY(
outPtr + sz <= out->end(),
TWellKnownEntityTypes::FILESYSTEM,
logTag);
memcpy(outPtr, block.data() + shift, sz);
outPtr += sz;
++i;
}
while (i < aligned.BlockCount()) {
const auto block = buffer.GetBlock(i);
// Min needed to properly process unaligned tail
const auto len = Min<ui64>(out->end() - outPtr, block.size());
if (!len) {
// origin.End() is greater than fileSize
break;
}
memcpy(outPtr, block.data(), len);
outPtr += len;
++i;
}
}
} // namespace NCloud::NFileStore::NStorage