Skip to content

Commit e6a0ea1

Browse files
committed
add record batch file reader
1 parent 52c7ded commit e6a0ea1

File tree

4 files changed

+108
-2
lines changed

4 files changed

+108
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
#include "arrow/io/file.h"
19+
#include "arrow/io/interfaces.h"
20+
#include "arrow/ipc/reader.h"
21+
#include "arrow/matlab/error/error.h"
22+
#include "arrow/matlab/io/ipc/proxy/record_batch_file_reader.h"
23+
#include "arrow/util/utf8.h"
24+
25+
26+
namespace arrow::matlab::io::ipc::proxy {
27+
28+
RecordBatchFileReader::RecordBatchFileReader(std::shared_ptr<arrow::ipc::RecordBatchFileReader> reader) : reader{std::move(reader)} {
29+
REGISTER_METHOD(RecordBatchFileReader, getNumRecordBatches);
30+
}
31+
32+
libmexclass::proxy::MakeResult RecordBatchFileReader::make(const libmexclass::proxy::FunctionArguments& constructor_arguments) {
33+
namespace mda = ::matlab::data;
34+
using RecordBatchFileReaderProxy = arrow::matlab::io::ipc::proxy::RecordBatchFileReader;
35+
36+
mda::StructArray opts = constructor_arguments[0];
37+
const mda::StringArray filename_mda = opts[0]["Filename"];
38+
39+
const auto filename_utf16 = std::u16string(filename_mda[0]);
40+
MATLAB_ASSIGN_OR_ERROR(const auto filename_utf8,
41+
arrow::util::UTF16StringToUTF8(filename_utf16),
42+
error::UNICODE_CONVERSION_ERROR_ID);
43+
44+
MATLAB_ASSIGN_OR_ERROR(auto input_stream,
45+
arrow::io::ReadableFile::Open(filename_utf8),
46+
error::FAILED_TO_OPEN_FILE_FOR_WRITE);
47+
48+
MATLAB_ASSIGN_OR_ERROR(auto reader,
49+
arrow::ipc::RecordBatchFileReader::Open(input_stream),
50+
"arrow:matlab:MakeFailed");
51+
52+
return std::make_shared<RecordBatchFileReaderProxy>(std::move(reader));
53+
}
54+
55+
void RecordBatchFileReader::getNumRecordBatches(libmexclass::proxy::method::Context& context) {
56+
namespace mda = ::matlab::data;
57+
mda::ArrayFactory factory;
58+
const auto num_batches = reader->num_record_batches();
59+
context.outputs[0] = factory.createScalar(num_batches);
60+
}
61+
62+
63+
64+
} // namespace arrow::matlab::io::ipc::proxy
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
#pragma once
19+
20+
#include "arrow/ipc/reader.h"
21+
22+
#include "libmexclass/proxy/Proxy.h"
23+
24+
namespace arrow::matlab::io::ipc::proxy {
25+
26+
class RecordBatchFileReader : public libmexclass::proxy::Proxy {
27+
public:
28+
RecordBatchFileReader(std::shared_ptr<arrow::ipc::RecordBatchFileReader>);
29+
30+
static libmexclass::proxy::MakeResult make(const libmexclass::proxy::FunctionArguments& constructor_arguments);
31+
32+
protected:
33+
34+
void getNumRecordBatches(libmexclass::proxy::method::Context& context);
35+
36+
std::shared_ptr<arrow::ipc::RecordBatchFileReader> reader;
37+
38+
};
39+
40+
} // namespace arrow::matlab::io::ipc::proxy

Diff for: matlab/src/cpp/arrow/matlab/proxy/factory.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "arrow/matlab/error/error.h"
3333
#include "arrow/matlab/io/csv/proxy/table_reader.h"
3434
#include "arrow/matlab/io/csv/proxy/table_writer.h"
35+
#include "arrow/matlab/io/ipc/proxy/record_batch_file_reader.h"
3536
#include "arrow/matlab/io/ipc/proxy/record_batch_file_writer.h"
3637
#include "arrow/matlab/io/feather/proxy/reader.h"
3738
#include "arrow/matlab/io/feather/proxy/writer.h"
@@ -109,8 +110,8 @@ libmexclass::proxy::MakeResult Factory::make_proxy(
109110
REGISTER_PROXY(arrow.c.proxy.Schema , arrow::matlab::c::proxy::Schema);
110111
REGISTER_PROXY(arrow.c.proxy.RecordBatchImporter , arrow::matlab::c::proxy::RecordBatchImporter);
111112
REGISTER_PROXY(arrow.io.ipc.proxy.RecordBatchFileWriter , arrow::matlab::io::ipc::proxy::RecordBatchFileWriter);
113+
REGISTER_PROXY(arrow.io.ipc.proxy.RecordBatchFileReader , arrow::matlab::io::ipc::proxy::RecordBatchFileReader);
112114
// clang-format on
113-
114115
return libmexclass::error::Error{error::UNKNOWN_PROXY_ERROR_ID,
115116
"Did not find matching C++ proxy for " + class_name};
116117
};

Diff for: matlab/tools/cmake/BuildMatlabArrowInterface.cmake

+2-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ set(MATLAB_ARROW_LIBMEXCLASS_CLIENT_PROXY_SOURCES "${CMAKE_SOURCE_DIR}/src/cpp/a
8080
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/c/proxy/array_importer.cc"
8181
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/c/proxy/schema.cc"
8282
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/c/proxy/record_batch_importer.cc"
83-
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/io/ipc/proxy/record_batch_file_writer.cc")
83+
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/io/ipc/proxy/record_batch_file_writer.cc"
84+
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/io/ipc/proxy/record_batch_file_reader.cc")
8485

8586

8687
set(MATLAB_ARROW_LIBMEXCLASS_CLIENT_PROXY_FACTORY_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/proxy")

0 commit comments

Comments
 (0)