forked from oceanbase/seekdb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathob_arrow_basic.cpp
More file actions
290 lines (259 loc) · 8.37 KB
/
Copy pathob_arrow_basic.cpp
File metadata and controls
290 lines (259 loc) · 8.37 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
/*
* Copyright (c) 2025 OceanBase.
*
* 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.
*/
#define USING_LOG_PREFIX SQL_ENG
#include "ob_arrow_basic.h"
#include <parquet/api/reader.h>
namespace oceanbase
{
using namespace share::schema;
using namespace common;
using namespace share;
namespace sql {
void ObOrcMemPool::init(uint64_t tenant_id)
{
mem_attr_ = ObMemAttr(tenant_id, "OrcMemPool");
}
char* ObOrcMemPool::malloc(uint64_t size)
{
int ret = OB_SUCCESS;
void *buf = ob_malloc_align(64, size, mem_attr_);
if (OB_ISNULL(buf)) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("fail to allocate memory", K(size), K(lbt()));
throw std::bad_alloc();
}
return (char*)buf;
}
void ObOrcMemPool::free(char* p)
{
if (OB_ISNULL(p)) {
throw std::bad_exception();
}
ob_free_align(p);
}
void ObOrcOutputStream::write(const void *buf, size_t length)
{
int ret = OB_SUCCESS;
int64_t write_size = 0;
if (IntoFileLocation::SERVER_DISK == file_location_) {
if (OB_FAIL(file_appender_->append(buf, length, false))) {
LOG_WARN("failed to append file", K(ret), K(length), K(url_.c_str()));
throw std::runtime_error("failed to append file");
}
} else if (OB_FAIL(storage_appender_->append(static_cast<const char*>(buf), length, write_size))) {
LOG_WARN("fail to append data", K(ret), KP(buf), K(length), K(url_.c_str()));
throw std::runtime_error("fail to append data");
}
if (OB_SUCC(ret)) {
pos_ += length;
}
}
/* ObArrowMemPool */
void ObArrowMemPool::init(uint64_t tenant_id)
{
mem_attr_ = ObMemAttr(tenant_id, "ArrowMemPool");
}
arrow::Status ObArrowMemPool::Allocate(int64_t size, uint8_t** out)
{
int ret = OB_SUCCESS;
arrow::Status status_ret = arrow::Status::OK();
if (0 == size) {
*out = NULL;
} else {
void *buf = ob_malloc_align(64, size, mem_attr_);
if (OB_ISNULL(buf)) {
status_ret = arrow::Status::Invalid("allocate memory failed");
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("fail to allocate memory", K(size), K(lbt()));
} else {
*out = static_cast<uint8_t*>(buf);
total_alloc_size_ += size;
}
}
LOG_DEBUG("ObArrowMemPool::Allocate", K(size), "stack", lbt());
return status_ret;
}
arrow::Status ObArrowMemPool::Reallocate(int64_t old_size, int64_t new_size, uint8_t** ptr)
{
int ret = OB_SUCCESS;
uint8_t* old = *ptr;
arrow::Status status_ret = Allocate(new_size, ptr);
if (arrow::Status::OK() == status_ret) {
MEMCPY(*ptr, old, std::min(old_size, new_size));
Free(old, old_size);
}
LOG_DEBUG("ObArrowMemPool::Reallocate", K(old_size), K(new_size), "stack", lbt());
return status_ret;
}
void ObArrowMemPool::Free(uint8_t* buffer, int64_t size) {
int ret = OB_SUCCESS;
ob_free_align(buffer);
total_alloc_size_ -= size;
LOG_DEBUG("ObArrowMemPool::Free", K(size), "stack", lbt());
}
void ObArrowMemPool::ReleaseUnused() {
LOG_DEBUG("ObArrowMemPool::ReleaseUnused", "stack", lbt());
}
int64_t ObArrowMemPool::bytes_allocated() const {
LOG_DEBUG("ObArrowMemPool::bytes_allocated", "stack", lbt());
return total_alloc_size_;
}
/* ObArrowFile */
int ObArrowFile::open()
{
return file_reader_.open(file_name_);
}
arrow::Status ObArrowFile::Seek(int64_t position) {
position_ = position;
return arrow::Status::OK();
}
arrow::Result<int64_t> ObArrowFile::Read(int64_t nbytes, void *out)
{
int ret = OB_SUCCESS;
arrow::Result<int64_t> ret_code;
int64_t read_size = -1;
if (file_prefetch_buffer_.in_prebuffer_range(position_, nbytes)) {
file_prefetch_buffer_.fetch(position_, nbytes, out);
position_ += nbytes;
ret_code = nbytes;
} else if (OB_FAIL(file_reader_.pread(out, nbytes, position_, read_size))) {
LOG_WARN("fail to read file", K(ret), K(nbytes));
ret_code =
arrow::Result<int64_t>(arrow::Status(arrow::StatusCode::IOError, "read file failed"));
} else {
position_ += read_size;
ret_code = read_size;
}
LOG_DEBUG("Read(int64_t nbytes, void *out)", K(nbytes));
return ret_code;
}
arrow::Result<std::shared_ptr<arrow::Buffer>> ObArrowFile::Read(int64_t nbytes)
{
ARROW_ASSIGN_OR_RAISE(auto buffer, arrow::AllocateResizableBuffer(nbytes, pool_));
ARROW_ASSIGN_OR_RAISE(int64_t bytes_read, Read(nbytes, buffer->mutable_data()));
if (bytes_read < nbytes) {
RETURN_NOT_OK(buffer->Resize(bytes_read));
}
LOG_DEBUG("ObArrowFile::Read(int64_t nbytes)", K(nbytes));
return std::move(buffer);
}
arrow::Result<int64_t> ObArrowFile::ReadAt(int64_t position, int64_t nbytes, void* out)
{
int ret = OB_SUCCESS;
arrow::Result<int64_t> ret_code;
int64_t read_size = -1;
if (file_prefetch_buffer_.in_prebuffer_range(position, nbytes)) {
file_prefetch_buffer_.fetch(position, nbytes, out);
position_ = position + nbytes;
ret_code = nbytes;
} else if (OB_FAIL(file_reader_.pread(out, nbytes, position, read_size))) {
LOG_WARN("fail to read file", K(ret), K(position), K(nbytes));
ret_code =
arrow::Result<int64_t>(arrow::Status(arrow::StatusCode::IOError, "read at file failed"));
} else {
position_ = position + read_size;
ret_code = read_size;
}
LOG_DEBUG("ObArrowFile::Read(int64_t nbytes)", K(nbytes));
return ret_code;
}
arrow::Result<std::shared_ptr<arrow::Buffer>> ObArrowFile::ReadAt(int64_t position, int64_t nbytes)
{
ARROW_ASSIGN_OR_RAISE(auto buffer, AllocateResizableBuffer(nbytes, pool_));
ARROW_ASSIGN_OR_RAISE(int64_t bytes_read,
ReadAt(position, nbytes, buffer->mutable_data()));
if (bytes_read < nbytes) {
RETURN_NOT_OK(buffer->Resize(bytes_read));
buffer->ZeroPadding();
}
LOG_DEBUG("ObArrowFile::ReadAt(int64_t position, int64_t nbytes)", K(nbytes));
return std::move(buffer);
}
arrow::Result<int64_t> ObArrowFile::Tell() const
{
return position_;
}
arrow::Result<int64_t> ObArrowFile::GetSize()
{
int ret = OB_SUCCESS;
arrow::Result<int64_t> ret_code;
int64_t file_size = 0;
if (OB_FAIL(file_reader_.get_file_size(file_name_, file_size))) {
LOG_WARN("fail to get file size", K(ret), K(file_name_));
ret_code = arrow::Result<int64_t>(arrow::Status(arrow::StatusCode::IOError, "get file size"));
} else {
ret_code = file_size;
}
return ret_code;
}
arrow::Status ObArrowFile::Close()
{
file_reader_.close();
return arrow::Status::OK();
}
bool ObArrowFile::closed() const
{
return !file_reader_.is_opened();
}
/*ObParquetOutputStream*/
arrow::Status ObParquetOutputStream::Write(const void* data, int64_t nbytes)
{
arrow::Status status = arrow::Status::OK();
int ret = OB_SUCCESS;
int64_t write_size = 0;
if (IntoFileLocation::SERVER_DISK == file_location_) {
if (OB_FAIL(file_appender_->append(data, nbytes, false))) {
LOG_WARN("failed to append file", K(ret), K(nbytes), K(url_));
status = arrow::Status(arrow::StatusCode::IOError, "write file failed");
}
} else if (OB_FAIL(storage_appender_->append(static_cast<const char*>(data), nbytes, write_size))) {
LOG_WARN("fail to append data", K(ret), KP(data), K(nbytes), K(url_));
status = arrow::Status(arrow::StatusCode::IOError, "write file failed");
}
if (OB_SUCC(ret)) {
position_ += nbytes;
}
return status;
}
// Virtual methods in `arrow::io::FileInterface`
arrow::Status ObParquetOutputStream::Close()
{
arrow::Status status = arrow::Status::OK();
int ret = OB_SUCCESS;
if (IntoFileLocation::SERVER_DISK == file_location_) {
file_appender_->close();
} else if (OB_FAIL(storage_appender_->close())) {
LOG_WARN("failed to close storage appender", K(ret));
status = arrow::Status(arrow::StatusCode::IOError, "close file failed");
}
return status;
}
bool ObParquetOutputStream::closed() const
{
bool ret = false;
if (IntoFileLocation::SERVER_DISK == file_location_) {
ret = !file_appender_->is_opened();
} else {
ret = !storage_appender_->is_opened_;
}
return ret;
}
arrow::Result<int64_t> ObParquetOutputStream::Tell() const
{
return position_;
}
} // end of oceanbase namespace
} // end of oceanbase namespace