|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "avro_stream_internal.h" |
| 21 | + |
| 22 | +#include <format> |
| 23 | + |
| 24 | +#include <arrow/result.h> |
| 25 | + |
| 26 | +#include "iceberg/exception.h" |
| 27 | + |
| 28 | +namespace iceberg::avro { |
| 29 | + |
| 30 | +AvroInputStream::AvroInputStream( |
| 31 | + std::shared_ptr<arrow::io::RandomAccessFile> input_stream, int64_t buffer_size) |
| 32 | + : input_stream_(std::move(input_stream)), |
| 33 | + buffer_size_(buffer_size), |
| 34 | + buffer_(buffer_size) {} |
| 35 | + |
| 36 | +AvroInputStream::~AvroInputStream() = default; |
| 37 | + |
| 38 | +bool AvroInputStream::next(const uint8_t** data, size_t* len) { |
| 39 | + // Return all unconsumed data in the buffer |
| 40 | + if (buffer_pos_ < available_bytes_) { |
| 41 | + *data = buffer_.data() + buffer_pos_; |
| 42 | + *len = available_bytes_ - buffer_pos_; |
| 43 | + byte_count_ += available_bytes_ - buffer_pos_; |
| 44 | + buffer_pos_ = available_bytes_; |
| 45 | + return true; |
| 46 | + } |
| 47 | + |
| 48 | + // Read from the input stream when the buffer is empty |
| 49 | + auto result = input_stream_->Read(buffer_.size(), buffer_.data()); |
| 50 | + // TODO(xiao.dong) Avro interface requires to return false if an error has occurred or |
| 51 | + // reach EOF, so error message can not be raised to the caller, add some log after we |
| 52 | + // have a logging system |
| 53 | + if (!result.ok() || result.ValueUnsafe() <= 0) { |
| 54 | + return false; |
| 55 | + } |
| 56 | + available_bytes_ = result.ValueUnsafe(); |
| 57 | + buffer_pos_ = 0; |
| 58 | + |
| 59 | + // Return the whole buffer |
| 60 | + *data = buffer_.data(); |
| 61 | + *len = available_bytes_; |
| 62 | + byte_count_ += available_bytes_; |
| 63 | + buffer_pos_ = available_bytes_; |
| 64 | + |
| 65 | + return true; |
| 66 | +} |
| 67 | + |
| 68 | +void AvroInputStream::backup(size_t len) { |
| 69 | + if (len > buffer_pos_) { |
| 70 | + throw IcebergError( |
| 71 | + std::format("Cannot backup {} bytes, only {} bytes available", len, buffer_pos_)); |
| 72 | + } |
| 73 | + |
| 74 | + buffer_pos_ -= len; |
| 75 | + byte_count_ -= len; |
| 76 | +} |
| 77 | + |
| 78 | +void AvroInputStream::skip(size_t len) { |
| 79 | + // The range to skip is within the buffer |
| 80 | + if (buffer_pos_ + len <= available_bytes_) { |
| 81 | + buffer_pos_ += len; |
| 82 | + byte_count_ += len; |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + seek(byte_count_ + len); |
| 87 | +} |
| 88 | + |
| 89 | +size_t AvroInputStream::byteCount() const { return byte_count_; } |
| 90 | + |
| 91 | +void AvroInputStream::seek(int64_t position) { |
| 92 | + auto status = input_stream_->Seek(position); |
| 93 | + if (!status.ok()) { |
| 94 | + throw IcebergError( |
| 95 | + std::format("Failed to seek to {}, got {}", position, status.ToString())); |
| 96 | + } |
| 97 | + |
| 98 | + buffer_pos_ = 0; |
| 99 | + available_bytes_ = 0; |
| 100 | + byte_count_ = position; |
| 101 | +} |
| 102 | + |
| 103 | +AvroOutputStream::AvroOutputStream(std::shared_ptr<arrow::io::OutputStream> output_stream, |
| 104 | + int64_t buffer_size) |
| 105 | + : output_stream_(std::move(output_stream)), |
| 106 | + buffer_size_(buffer_size), |
| 107 | + buffer_(buffer_size) {} |
| 108 | + |
| 109 | +AvroOutputStream::~AvroOutputStream() = default; |
| 110 | + |
| 111 | +bool AvroOutputStream::next(uint8_t** data, size_t* len) { |
| 112 | + if (buffer_pos_ > 0) { |
| 113 | + flush(); |
| 114 | + } |
| 115 | + |
| 116 | + *data = buffer_.data(); |
| 117 | + *len = buffer_.size(); |
| 118 | + buffer_pos_ = buffer_.size(); // Assume all will be used until backup is called |
| 119 | + |
| 120 | + return true; |
| 121 | +} |
| 122 | + |
| 123 | +void AvroOutputStream::backup(size_t len) { |
| 124 | + if (len > buffer_pos_) { |
| 125 | + throw IcebergError( |
| 126 | + std::format("Cannot backup {} bytes, only {} bytes available", len, buffer_pos_)); |
| 127 | + } |
| 128 | + buffer_pos_ -= len; |
| 129 | +} |
| 130 | + |
| 131 | +uint64_t AvroOutputStream::byteCount() const { return flushed_bytes_ + buffer_pos_; } |
| 132 | + |
| 133 | +void AvroOutputStream::flush() { |
| 134 | + if (buffer_pos_ > 0) { |
| 135 | + auto status = output_stream_->Write(buffer_.data(), buffer_pos_); |
| 136 | + if (!status.ok()) { |
| 137 | + throw IcebergError(std::format("Write failed {}", status.ToString())); |
| 138 | + } |
| 139 | + flushed_bytes_ += buffer_pos_; |
| 140 | + buffer_pos_ = 0; |
| 141 | + } |
| 142 | + auto status = output_stream_->Flush(); |
| 143 | + if (!status.ok()) { |
| 144 | + throw IcebergError(std::format("Flush failed {}", status.ToString())); |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +} // namespace iceberg::avro |
0 commit comments