-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy patharray_record_module.cc
More file actions
252 lines (240 loc) · 10.8 KB
/
array_record_module.cc
File metadata and controls
252 lines (240 loc) · 10.8 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
/* 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 <stddef.h>
#include <stdint.h>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
#include "absl/status/status.h"
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#include "cpp/array_record_reader.h"
#include "cpp/array_record_writer.h"
#include "cpp/thread_pool.h"
#include "pybind11/gil.h"
#include "pybind11/pybind11.h"
#include "pybind11/pytypes.h"
#include "pybind11/stl.h"
#include "riegeli/base/maker.h"
#include "riegeli/bytes/fd_reader.h"
#include "riegeli/bytes/fd_writer.h"
namespace py = pybind11;
PYBIND11_MODULE(array_record_module, m) {
using array_record::ArrayRecordReaderBase;
using array_record::ArrayRecordWriterBase;
py::class_<ArrayRecordWriterBase>(m, "ArrayRecordWriter")
.def(py::init([](const std::string& path, const std::string& options,
const py::kwargs& kwargs) -> ArrayRecordWriterBase* {
auto status_or_option =
ArrayRecordWriterBase::Options::FromString(options);
if (!status_or_option.ok()) {
throw py::value_error(
std::string(status_or_option.status().message()));
}
// Release the GIL because IO is time consuming.
py::gil_scoped_release scoped_release;
return new array_record::ArrayRecordWriter(
riegeli::Maker<riegeli::FdWriter>(path),
status_or_option.value());
}),
py::arg("path"), py::arg("options") = "")
.def("ok", &ArrayRecordWriterBase::ok)
.def("close",
[](ArrayRecordWriterBase& writer) {
if (!writer.Close()) {
throw std::runtime_error(std::string(writer.status().message()));
}
})
.def("is_open", &ArrayRecordWriterBase::is_open)
// We accept only py::bytes (and not unicode strings) since we expect
// most users to store binary data (e.g. serialized protocol buffers).
// We cannot know if a users wants to write+read unicode and forcing users
// to encode() their unicode strings avoids accidental conversions.
.def("write", [](ArrayRecordWriterBase& writer, py::bytes record) {
if (!writer.WriteRecord(record)) {
throw std::runtime_error(std::string(writer.status().message()));
}
});
py::class_<ArrayRecordReaderBase>(m, "ArrayRecordReader")
.def(py::init([](const std::string& path, const std::string& options,
const py::kwargs& kwargs) -> ArrayRecordReaderBase* {
auto status_or_option =
ArrayRecordReaderBase::Options::FromString(options);
if (!status_or_option.ok()) {
throw py::value_error(
std::string(status_or_option.status().message()));
}
riegeli::FdReaderBase::Options file_reader_options;
if (kwargs.contains("file_reader_buffer_size")) {
auto file_reader_buffer_size =
kwargs["file_reader_buffer_size"].cast<int64_t>();
file_reader_options.set_buffer_size(file_reader_buffer_size);
}
// Release the GIL because IO is time consuming.
py::gil_scoped_release scoped_release;
return new array_record::ArrayRecordReader(
riegeli::Maker<riegeli::FdReader>(
path, std::move(file_reader_options)),
status_or_option.value(),
array_record::ArrayRecordGlobalPool());
}),
py::arg("path"), py::arg("options") = "", R"(
ArrayRecordReader for fast sequential or random access.
Args:
path: File path to the input file.
options: String with options for ArrayRecord. See syntax below.
Kwargs:
file_reader_buffer_size: Optional size of the buffer (in bytes)
for the underlying file (Riegeli) reader. The default buffer
size is 1 MiB.
file_options: Optional file::Options to use for the underlying
file (Riegeli) reader.
options ::= option? ("," option?)*
option ::=
"readahead_buffer_size" ":" readahead_buffer_size |
"max_parallelism" ":" max_parallelism
readahead_buffer_size ::= non-negative integer expressed as real with
optional suffix [BkKMGTPE]. (Default 16MB). Set to 0 optimizes
random access performance.
max_parallelism ::= `auto` or non-negative integer. Each parallel
thread owns its readhaed buffer with the size
`readahead_buffer_size`. (Default thread pool size) Set to 0
optimizes random access performance.
The default option is optimized for sequential access. To optimize
the random access performance, set the options to
"readahead_buffer_size:0,max_parallelism:0".
)")
.def("ok", &ArrayRecordReaderBase::ok)
.def("close",
[](ArrayRecordReaderBase& reader) {
if (!reader.Close()) {
throw std::runtime_error(std::string(reader.status().message()));
}
})
.def("is_open", &ArrayRecordReaderBase::is_open)
.def("num_records", &ArrayRecordReaderBase::NumRecords)
.def("record_index", &ArrayRecordReaderBase::RecordIndex)
.def("writer_options_string", &ArrayRecordReaderBase::WriterOptionsString)
.def("seek",
[](ArrayRecordReaderBase& reader, int64_t record_index) {
if (!reader.SeekRecord(record_index)) {
throw std::runtime_error(std::string(reader.status().message()));
}
})
// See write() for why this returns py::bytes.
.def("read",
[](ArrayRecordReaderBase& reader) {
absl::string_view string_view;
if (!reader.ReadRecord(&string_view)) {
if (reader.ok()) {
throw std::out_of_range(absl::StrFormat(
"Out of range of num_records: %d", reader.NumRecords()));
}
throw std::runtime_error(std::string(reader.status().message()));
}
return py::bytes(string_view);
})
.def("read",
[](ArrayRecordReaderBase& reader, std::vector<uint64_t> indices) {
std::vector<std::string> staging(indices.size());
py::list output(indices.size());
{
py::gil_scoped_release scoped_release;
auto status = reader.ParallelReadRecordsWithIndices(
indices,
[&](uint64_t indices_index,
absl::string_view record_data) -> absl::Status {
staging[indices_index] = record_data;
return absl::OkStatus();
});
if (!status.ok()) {
throw std::runtime_error(std::string(status.message()));
}
}
// TODO(fchern): Can we write the data directly to the output
// list in our Parallel loop?
ssize_t index = 0;
for (const auto& record : staging) {
auto py_record = py::bytes(record);
PyList_SET_ITEM(output.ptr(), index++,
py_record.release().ptr());
}
return output;
})
.def("read",
[](ArrayRecordReaderBase& reader, int32_t begin, int32_t end) {
int32_t range_begin = begin, range_end = end;
if (range_begin < 0) {
range_begin = reader.NumRecords() + range_begin;
}
if (range_end < 0) {
range_end = reader.NumRecords() + range_end;
}
if (range_begin > reader.NumRecords() || range_begin < 0 ||
range_end > reader.NumRecords() || range_end < 0 ||
range_end <= range_begin) {
throw std::out_of_range(
absl::StrFormat("[%d, %d) is of range of [0, %d)", begin,
end, reader.NumRecords()));
}
int32_t num_to_read = range_end - range_begin;
std::vector<std::string> staging(num_to_read);
py::list output(num_to_read);
{
py::gil_scoped_release scoped_release;
auto status = reader.ParallelReadRecordsInRange(
range_begin, range_end,
[&](uint64_t index,
absl::string_view record_data) -> absl::Status {
staging[index - range_begin] = record_data;
return absl::OkStatus();
});
if (!status.ok()) {
throw std::runtime_error(std::string(status.message()));
}
}
// TODO(fchern): Can we write the data directly to the output
// list in our Parallel loop?
ssize_t index = 0;
for (const auto& record : staging) {
auto py_record = py::bytes(record);
PyList_SET_ITEM(output.ptr(), index++,
py_record.release().ptr());
}
return output;
})
.def("read_all", [](ArrayRecordReaderBase& reader) {
std::vector<std::string> staging(reader.NumRecords());
py::list output(reader.NumRecords());
{
py::gil_scoped_release scoped_release;
auto status = reader.ParallelReadRecords(
[&](uint64_t index,
absl::string_view record_data) -> absl::Status {
staging[index] = record_data;
return absl::OkStatus();
});
if (!status.ok()) {
throw std::runtime_error(std::string(status.message()));
}
}
// TODO(fchern): Can we write the data directly to the output
// list in our Parallel loop?
ssize_t index = 0;
for (const auto& record : staging) {
auto py_record = py::bytes(record);
PyList_SET_ITEM(output.ptr(), index++, py_record.release().ptr());
}
return output;
});
}