-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy patharrow_input_stream_adapter.cpp
More file actions
174 lines (153 loc) · 6.08 KB
/
Copy patharrow_input_stream_adapter.cpp
File metadata and controls
174 lines (153 loc) · 6.08 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
/*
* Copyright 2026-present Alibaba Inc.
*
* 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 "paimon/common/utils/arrow/arrow_input_stream_adapter.h"
#include <cstdint>
#include <functional>
#include <utility>
#include "arrow/api.h"
#include "paimon/common/utils/arrow/status_utils.h"
#include "paimon/common/utils/math.h"
#include "paimon/fs/file_system.h"
#include "paimon/macros.h"
#include "paimon/result.h"
#include "paimon/status.h"
namespace paimon {
namespace {
arrow::Status ValidateArrowIoRange(int64_t value, const char* name) {
Status status = ValidateValueNonNegative(value, name);
if (!status.ok()) {
return ToArrowStatus(status);
}
return arrow::Status::OK();
}
} // namespace
ArrowInputStreamAdapter::ArrowInputStreamAdapter(
const std::shared_ptr<paimon::InputStream>& input_stream,
const std::shared_ptr<arrow::MemoryPool>& pool, int64_t file_size)
: input_stream_(input_stream),
pool_(pool),
file_size_(file_size),
raw_input_bytes_(std::make_shared<std::atomic<uint64_t>>(0)) {
assert(file_size >= 0);
}
ArrowInputStreamAdapter::~ArrowInputStreamAdapter() {
[[maybe_unused]] auto status = DoClose();
}
arrow::Status ArrowInputStreamAdapter::Seek(int64_t position) {
return ToArrowStatus(input_stream_->Seek(position, SeekOrigin::FS_SEEK_SET));
}
arrow::Result<int64_t> ArrowInputStreamAdapter::Read(int64_t nbytes, void* out) {
ARROW_RETURN_NOT_OK(ValidateArrowIoRange(nbytes, "nbytes"));
Result<int64_t> read_bytes = input_stream_->Read(static_cast<char*>(out), nbytes);
if (!read_bytes.ok()) {
return ToArrowStatus(read_bytes.status());
}
if (raw_input_bytes_) {
raw_input_bytes_->fetch_add(static_cast<uint64_t>(read_bytes.value()));
}
return read_bytes.value();
}
arrow::Result<std::shared_ptr<arrow::Buffer>> ArrowInputStreamAdapter::Read(int64_t nbytes) {
ARROW_ASSIGN_OR_RAISE(std::shared_ptr<arrow::ResizableBuffer> buffer,
arrow::AllocateResizableBuffer(nbytes, pool_.get()));
ARROW_ASSIGN_OR_RAISE(int64_t read_bytes, Read(nbytes, buffer->mutable_data()));
if (read_bytes < nbytes) {
ARROW_RETURN_NOT_OK(buffer->Resize(read_bytes));
}
return std::shared_ptr<arrow::Buffer>(std::move(buffer));
}
arrow::Result<int64_t> ArrowInputStreamAdapter::ReadAt(int64_t position, int64_t nbytes,
void* out) {
ARROW_RETURN_NOT_OK(ValidateArrowIoRange(position, "position"));
ARROW_RETURN_NOT_OK(ValidateArrowIoRange(nbytes, "nbytes"));
Result<int64_t> read_bytes = input_stream_->Read(static_cast<char*>(out), nbytes, position);
if (!read_bytes.ok()) {
return ToArrowStatus(read_bytes.status());
}
if (raw_input_bytes_) {
raw_input_bytes_->fetch_add(static_cast<uint64_t>(read_bytes.value()));
}
return read_bytes.value();
}
arrow::Result<std::shared_ptr<arrow::Buffer>> ArrowInputStreamAdapter::ReadAt(int64_t position,
int64_t nbytes) {
ARROW_ASSIGN_OR_RAISE(std::shared_ptr<arrow::ResizableBuffer> buffer,
arrow::AllocateResizableBuffer(nbytes, pool_.get()));
ARROW_ASSIGN_OR_RAISE(int64_t read_bytes, ReadAt(position, nbytes, buffer->mutable_data()));
if (read_bytes < nbytes) {
ARROW_RETURN_NOT_OK(buffer->Resize(read_bytes));
}
return std::shared_ptr<arrow::Buffer>(std::move(buffer));
}
arrow::Future<std::shared_ptr<arrow::Buffer>> ArrowInputStreamAdapter::ReadAsync(
const arrow::io::IOContext& io_context, int64_t position, int64_t nbytes) {
auto fut = arrow::Future<std::shared_ptr<arrow::Buffer>>::Make();
auto range_status = ValidateArrowIoRange(position, "position");
if (!range_status.ok()) {
fut.MarkFinished(range_status);
return fut;
}
range_status = ValidateArrowIoRange(nbytes, "nbytes");
if (!range_status.ok()) {
fut.MarkFinished(range_status);
return fut;
}
arrow::Result<std::shared_ptr<arrow::Buffer>> buffer_result =
arrow::AllocateResizableBuffer(nbytes, pool_.get());
if (PAIMON_UNLIKELY(!buffer_result.ok())) {
fut.MarkFinished(buffer_result.status());
return fut;
}
std::shared_ptr<arrow::Buffer> buffer = std::move(buffer_result).ValueUnsafe();
std::shared_ptr<std::atomic<uint64_t>> raw_input_bytes = raw_input_bytes_;
input_stream_->ReadAsync(
reinterpret_cast<char*>(buffer->mutable_data()), nbytes, position,
[fut, buffer, raw_input_bytes, nbytes](Status callback_status) mutable {
if (callback_status.ok()) {
if (raw_input_bytes) {
raw_input_bytes->fetch_add(static_cast<uint64_t>(nbytes));
}
fut.MarkFinished(std::move(buffer));
} else {
fut.MarkFinished(ToArrowStatus(callback_status));
}
});
return fut;
}
arrow::Result<int64_t> ArrowInputStreamAdapter::Tell() const {
Result<int64_t> position = input_stream_->GetPos();
if (!position.ok()) {
return ToArrowStatus(position.status());
}
return position.value();
}
arrow::Result<int64_t> ArrowInputStreamAdapter::GetSize() {
return file_size_;
}
bool ArrowInputStreamAdapter::closed() const {
return closed_;
}
arrow::Status ArrowInputStreamAdapter::DoClose() {
if (!closed_) {
Status status = input_stream_->Close();
if (!status.ok()) {
return ToArrowStatus(status);
}
closed_ = true;
}
return arrow::Status::OK();
}
} // namespace paimon