15
15
// specific language governing permissions and limitations
16
16
// under the License.
17
17
18
+ #include " arrow/matlab/buffer/matlab_buffer.h"
18
19
#include " arrow/matlab/io/ipc/proxy/record_batch_stream_reader.h"
19
20
#include " arrow/io/file.h"
21
+ #include " arrow/io/memory.h"
20
22
#include " arrow/matlab/error/error.h"
21
23
#include " arrow/matlab/tabular/proxy/record_batch.h"
22
24
#include " arrow/matlab/tabular/proxy/schema.h"
@@ -36,6 +38,35 @@ RecordBatchStreamReader::RecordBatchStreamReader(
36
38
REGISTER_METHOD (RecordBatchStreamReader, readTable);
37
39
}
38
40
41
+ libmexclass::proxy::MakeResult RecordBatchStreamReader::fromFile (const libmexclass::proxy::FunctionArguments& constructor_arguments) {
42
+ const mda::StructArray opts = constructor_arguments[0 ];
43
+ const mda::StringArray filename_mda = opts[0 ][" Filename" ];
44
+ const auto filename_utf16 = std::u16string (filename_mda[0 ]);
45
+ MATLAB_ASSIGN_OR_ERROR (const auto filename_utf8,
46
+ arrow::util::UTF16StringToUTF8 (filename_utf16),
47
+ error::UNICODE_CONVERSION_ERROR_ID);
48
+
49
+ MATLAB_ASSIGN_OR_ERROR (auto input_stream, arrow::io::ReadableFile::Open (filename_utf8),
50
+ error::FAILED_TO_OPEN_FILE_FOR_READ);
51
+
52
+ MATLAB_ASSIGN_OR_ERROR (auto reader,
53
+ arrow::ipc::RecordBatchStreamReader::Open (input_stream),
54
+ error::IPC_RECORD_BATCH_READER_OPEN_FAILED);
55
+
56
+ return std::make_shared<RecordBatchStreamReaderProxy>(std::move (reader));
57
+ }
58
+
59
+ libmexclass::proxy::MakeResult RecordBatchStreamReader::fromBytes (const libmexclass::proxy::FunctionArguments& constructor_arguments) {
60
+ const mda::StructArray opts = constructor_arguments[0 ];
61
+ const ::matlab::data::TypedArray<uint8_t > bytes_mda = opts[0 ][" Bytes" ];
62
+ const auto matlab_buffer = std::make_shared<matlab::arrow::MatlabBuffer>(bytes_mda);
63
+ auto buffer_reader = std::make_shared<arrow::io::memory::BufferReader>(matlab_buffer);
64
+ MATLAB_ASSIGN_OR_ERROR (auto reader,
65
+ arrow::ipc::RecordBatchStreamReader::Open (buffer_reader),
66
+ error::IPC_RECORD_BATCH_READER_OPEN_FAILED);
67
+ return std::make_shared<RecordBatchStreamReaderProxy>(std::move (reader));
68
+ }
69
+
39
70
libmexclass::proxy::MakeResult RecordBatchStreamReader::make (
40
71
const libmexclass::proxy::FunctionArguments& constructor_arguments) {
41
72
namespace mda = ::matlab::data;
@@ -44,20 +75,18 @@ libmexclass::proxy::MakeResult RecordBatchStreamReader::make(
44
75
45
76
const mda::StructArray opts = constructor_arguments[0 ];
46
77
47
- const mda::StringArray filename_mda = opts[0 ][" Filename" ];
48
- const auto filename_utf16 = std::u16string (filename_mda[0 ]);
49
- MATLAB_ASSIGN_OR_ERROR (const auto filename_utf8,
50
- arrow::util::UTF16StringToUTF8 (filename_utf16),
51
- error::UNICODE_CONVERSION_ERROR_ID);
52
-
53
- MATLAB_ASSIGN_OR_ERROR (auto input_stream, arrow::io::ReadableFile::Open (filename_utf8),
54
- error::FAILED_TO_OPEN_FILE_FOR_READ);
55
-
56
- MATLAB_ASSIGN_OR_ERROR (auto reader,
57
- arrow::ipc::RecordBatchStreamReader::Open (input_stream),
58
- error::IPC_RECORD_BATCH_READER_OPEN_FAILED);
59
-
60
- return std::make_shared<RecordBatchStreamReaderProxy>(std::move (reader));
78
+ // Dispatch to the appropriate static "make" method depending
79
+ // on the input type.
80
+ const mda::StringArray type_mda = opts[0 ][" Type" ];
81
+ const auto type_utf16 = std::u16string (type_mda[0 ]);
82
+ if (type_utf16.equals (u" Bytes" )) {
83
+ return RecordBatchStreamReader::fromBytes (constructor_arguments);
84
+ } else if (type_utf16.equals (u" Filename" )) {
85
+ return RecordBatchStreamReader::fromFile (constructor_arguments);
86
+ } else {
87
+ // TODO: Create static error id string
88
+ return libmexclass::error::Error{" arrow:some:test:id" , " Invalid construction type for RecordBatchStreamReader." };
89
+ }
61
90
}
62
91
63
92
void RecordBatchStreamReader::getSchema (libmexclass::proxy::method::Context& context) {
0 commit comments