-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy patharray_record_reader_test.cc
More file actions
317 lines (279 loc) · 11.3 KB
/
array_record_reader_test.cc
File metadata and controls
317 lines (279 loc) · 11.3 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* Copyright 2022 Google LLC. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include "cpp/array_record_reader.h"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <optional>
#include <random>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#include "gtest/gtest.h"
#include "absl/functional/function_ref.h"
#include "absl/status/status.h"
#include "absl/strings/string_view.h"
#include "cpp/array_record_writer.h"
#include "cpp/common.h"
#include "cpp/layout.pb.h"
#include "cpp/test_utils.h"
#include "cpp/thread_pool.h"
#include "riegeli/base/maker.h"
#include "riegeli/bytes/string_reader.h"
#include "riegeli/bytes/string_writer.h"
constexpr uint32_t kDatasetSize = 3210;
namespace array_record {
namespace {
enum class CompressionType { kUncompressed, kBrotli, kZstd, kSnappy };
using IndexStorageOption = ArrayRecordReaderBase::Options::IndexStorageOption;
// Tuple params
// CompressionType
// transpose
// use_thread_pool
// optimize_for_random_access
// index_storage_option
class ArrayRecordReaderTest
: public testing::TestWithParam<
std::tuple<CompressionType, bool, bool, bool, IndexStorageOption>> {
public:
ARThreadPool* get_pool() { return ArrayRecordGlobalPool(); }
ArrayRecordWriterBase::Options GetWriterOptions() {
auto options = ArrayRecordWriterBase::Options();
switch (std::get<0>(GetParam())) {
case CompressionType::kUncompressed:
options.set_uncompressed();
break;
case CompressionType::kBrotli:
options.set_brotli();
break;
case CompressionType::kZstd:
options.set_zstd();
break;
case CompressionType::kSnappy:
options.set_snappy();
break;
}
options.set_transpose(transpose());
return options;
}
bool transpose() { return std::get<1>(GetParam()); }
bool use_thread_pool() { return std::get<2>(GetParam()); }
bool optimize_for_random_access() { return std::get<3>(GetParam()); }
IndexStorageOption index_storage_option() { return std::get<4>(GetParam()); }
};
TEST_P(ArrayRecordReaderTest, MoveTest) {
std::string encoded;
auto writer_options = GetWriterOptions().set_group_size(2);
auto writer = ArrayRecordWriter(
riegeli::Maker<riegeli::StringWriter>(&encoded), writer_options, nullptr);
// Empty string should not crash the writer or the reader.
std::vector<std::string> test_str{"aaa", "", "ccc", "dd", "e"};
for (const auto& s : test_str) {
EXPECT_TRUE(writer.WriteRecord(s));
}
ASSERT_TRUE(writer.Close());
auto reader_opt = ArrayRecordReaderBase::Options();
reader_opt.set_index_storage_option(index_storage_option());
if (optimize_for_random_access()) {
reader_opt.set_max_parallelism(0);
reader_opt.set_readahead_buffer_size(0);
}
auto reader_before_move =
ArrayRecordReader(riegeli::Maker<riegeli::StringReader>(encoded),
reader_opt, use_thread_pool() ? get_pool() : nullptr);
ASSERT_TRUE(reader_before_move.status().ok());
ASSERT_TRUE(
reader_before_move
.ParallelReadRecords([&](uint64_t record_index,
absl::string_view record) -> absl::Status {
EXPECT_EQ(record, test_str[record_index]);
return absl::OkStatus();
})
.ok());
EXPECT_EQ(reader_before_move.RecordGroupSize(), 2);
ArrayRecordReader reader = std::move(reader_before_move);
// Once a reader is moved, it is closed.
ASSERT_FALSE(reader_before_move.is_open()); // NOLINT
auto recorded_writer_options = ArrayRecordWriterBase::Options::FromString(
reader.WriterOptionsString().value())
.value();
EXPECT_EQ(writer_options.compression_type(),
recorded_writer_options.compression_type());
EXPECT_EQ(writer_options.compression_level(),
recorded_writer_options.compression_level());
EXPECT_EQ(writer_options.transpose(), recorded_writer_options.transpose());
std::vector<uint64_t> indices = {1, 2, 4};
ASSERT_TRUE(reader
.ParallelReadRecordsWithIndices(
indices,
[&](uint64_t indices_idx,
absl::string_view record) -> absl::Status {
EXPECT_EQ(record, test_str[indices[indices_idx]]);
return absl::OkStatus();
})
.ok());
absl::string_view record_view;
for (auto i : IndicesOf(test_str)) {
EXPECT_TRUE(reader.ReadRecord(&record_view));
EXPECT_EQ(record_view, test_str[i]);
}
// Cannot read once we are at the end of the file.
EXPECT_FALSE(reader.ReadRecord(&record_view));
// But the reader should still be healthy.
EXPECT_TRUE(reader.ok());
// Seek to a particular record works.
EXPECT_TRUE(reader.SeekRecord(2));
EXPECT_TRUE(reader.ReadRecord(&record_view));
EXPECT_EQ(record_view, test_str[2]);
// Seek out of bound would not fail.
EXPECT_TRUE(reader.SeekRecord(10));
EXPECT_FALSE(reader.ReadRecord(&record_view));
EXPECT_TRUE(reader.ok());
EXPECT_EQ(reader.RecordGroupSize(), 2);
ASSERT_TRUE(reader.Close());
}
TEST_P(ArrayRecordReaderTest, RandomDatasetTest) {
std::mt19937 bitgen;
std::vector<std::string> records(kDatasetSize);
std::uniform_int_distribution<> dist(0, 123);
for (auto i : Seq(kDatasetSize)) {
size_t len = dist(bitgen);
records[i] = MTRandomBytes(bitgen, len);
}
std::string encoded;
auto writer =
ArrayRecordWriter(riegeli::Maker<riegeli::StringWriter>(&encoded),
GetWriterOptions(), get_pool());
for (auto i : Seq(kDatasetSize)) {
EXPECT_TRUE(writer.WriteRecord(records[i]));
}
ASSERT_TRUE(writer.Close());
auto reader_opt = ArrayRecordReaderBase::Options();
reader_opt.set_index_storage_option(index_storage_option());
if (optimize_for_random_access()) {
reader_opt.set_max_parallelism(0);
reader_opt.set_readahead_buffer_size(0);
}
auto reader =
ArrayRecordReader(riegeli::Maker<riegeli::StringReader>(encoded),
reader_opt, use_thread_pool() ? get_pool() : nullptr);
ASSERT_TRUE(reader.status().ok());
EXPECT_EQ(reader.NumRecords(), kDatasetSize);
uint64_t group_size =
std::min(ArrayRecordWriterBase::Options::kDefaultGroupSize, kDatasetSize);
EXPECT_EQ(reader.RecordGroupSize(), group_size);
// vector bool is casted as bit string, which is not thread safe to write.
std::vector<int> read_all_records(kDatasetSize, 0);
ASSERT_TRUE(reader
.ParallelReadRecords(
[&](uint64_t record_index,
absl::string_view result_view) -> absl::Status {
EXPECT_EQ(result_view, records[record_index]);
EXPECT_FALSE(read_all_records[record_index]);
read_all_records[record_index] = 1;
return absl::OkStatus();
})
.ok());
for (auto record_was_read : read_all_records) {
EXPECT_TRUE(record_was_read);
}
std::vector<uint64_t> indices = {0, 3, 5, 7, 101, 2000};
// vector bool is casted as bit string, which is not thread safe to write.
std::vector<int> read_indexed_records(indices.size(), 0);
ASSERT_TRUE(reader
.ParallelReadRecordsWithIndices(
indices,
[&](uint64_t indices_idx,
absl::string_view result_view) -> absl::Status {
EXPECT_EQ(result_view, records[indices[indices_idx]]);
EXPECT_FALSE(read_indexed_records[indices_idx]);
read_indexed_records[indices_idx] = 1;
return absl::OkStatus();
})
.ok());
for (auto record_was_read : read_indexed_records) {
EXPECT_TRUE(record_was_read);
}
uint64_t begin = 10, end = 101;
// vector bool is casted as bit string, which is not thread safe to write.
std::vector<int> read_range_records(end - begin, 0);
ASSERT_TRUE(reader
.ParallelReadRecordsInRange(
begin, end,
[&](uint64_t record_index,
absl::string_view result_view) -> absl::Status {
EXPECT_EQ(result_view, records[record_index]);
EXPECT_FALSE(read_range_records[record_index - begin]);
read_range_records[record_index - begin] = 1;
return absl::OkStatus();
})
.ok());
for (auto record_was_read : read_range_records) {
EXPECT_TRUE(record_was_read);
}
// Test sequential read
absl::string_view result_view;
for (auto record_index : Seq(kDatasetSize)) {
ASSERT_TRUE(reader.ReadRecord(&result_view));
EXPECT_EQ(result_view, records[record_index]);
}
// Reached to the end.
EXPECT_FALSE(reader.ReadRecord(&result_view));
EXPECT_TRUE(reader.ok());
// We can still seek back.
EXPECT_TRUE(reader.SeekRecord(5));
EXPECT_TRUE(reader.ReadRecord(&result_view));
EXPECT_EQ(result_view, records[5]);
ASSERT_TRUE(reader.Close());
}
INSTANTIATE_TEST_SUITE_P(
ParamTest, ArrayRecordReaderTest,
testing::Combine(testing::Values(CompressionType::kUncompressed,
CompressionType::kBrotli,
CompressionType::kZstd,
CompressionType::kSnappy),
testing::Bool(), testing::Bool(), testing::Bool(),
testing::Values(IndexStorageOption::kInMemory,
IndexStorageOption::kOffloaded)));
TEST(ArrayRecordReaderOptionTest, ParserTest) {
{
auto option = ArrayRecordReaderBase::Options::FromString("").value();
EXPECT_EQ(option.max_parallelism(), std::nullopt);
EXPECT_EQ(option.readahead_buffer_size(),
ArrayRecordReaderBase::Options::kDefaultReadaheadBufferSize);
}
{
auto option =
ArrayRecordReaderBase::Options::FromString("max_parallelism:16")
.value();
EXPECT_EQ(option.max_parallelism(), 16);
EXPECT_EQ(option.readahead_buffer_size(),
ArrayRecordReaderBase::Options::kDefaultReadaheadBufferSize);
}
{
auto option = ArrayRecordReaderBase::Options::FromString(
"max_parallelism:16,readahead_buffer_size:16384")
.value();
EXPECT_EQ(option.max_parallelism(), 16);
EXPECT_EQ(option.readahead_buffer_size(), 16384);
}
{
auto option = ArrayRecordReaderBase::Options::FromString(
"max_parallelism:0,readahead_buffer_size:0")
.value();
EXPECT_EQ(option.max_parallelism(), 0);
EXPECT_EQ(option.readahead_buffer_size(), 0);
}
}
} // namespace
} // namespace array_record