iceberg-cpp
Loading...
Searching...
No Matches
avro_stream_internal.h
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#pragma once
21
22#include <arrow/io/interfaces.h>
23#include <avro/Stream.hh>
24
25namespace iceberg::avro {
26
27class AvroInputStream : public ::avro::SeekableInputStream {
28 public:
29 explicit AvroInputStream(std::shared_ptr<::arrow::io::RandomAccessFile> input_stream,
30 int64_t buffer_size);
31
32 ~AvroInputStream() override;
33
37 bool next(const uint8_t** data, size_t* len) override;
38
41 void backup(size_t len) override;
42
44 void skip(size_t len) override;
45
49 size_t byteCount() const override;
50
54 void seek(int64_t position) override;
55
56 private:
57 std::shared_ptr<::arrow::io::RandomAccessFile> input_stream_;
58 const int64_t buffer_size_;
59 std::vector<uint8_t> buffer_;
60 size_t byte_count_ = 0; // bytes read from the input stream
61 size_t buffer_pos_ = 0; // next position to read in the buffer
62 size_t available_bytes_ = 0; // bytes available in the buffer
63};
64
65class AvroOutputStream : public ::avro::OutputStream {
66 public:
67 explicit AvroOutputStream(std::shared_ptr<::arrow::io::OutputStream> output_stream,
68 int64_t buffer_size);
69
70 ~AvroOutputStream() override;
71
75 bool next(uint8_t** data, size_t* len) override;
76
79 void backup(size_t len) override;
80
84 uint64_t byteCount() const override;
85
88 void flush() override;
89
90 const std::shared_ptr<::arrow::io::OutputStream>& arrow_output_stream() const;
91
92 private:
93 std::shared_ptr<::arrow::io::OutputStream> output_stream_;
94 const int64_t buffer_size_;
95 std::vector<uint8_t> buffer_;
96 size_t buffer_pos_ = 0; // position in the buffer
97 uint64_t flushed_bytes_ = 0; // bytes flushed to the output stream
98};
99
100} // namespace iceberg::avro
Definition avro_stream_internal.h:27
bool next(const uint8_t **data, size_t *len) override
Returns some of available data.
Definition avro_stream_internal.cc:38
size_t byteCount() const override
Returns the number of bytes read from this stream so far. All the bytes made available through next a...
Definition avro_stream_internal.cc:88
void seek(int64_t position) override
Seek to a specific position in the stream. This may invalidate pointers returned from next()....
Definition avro_stream_internal.cc:90
void skip(size_t len) override
Skips number of bytes specified by len.
Definition avro_stream_internal.cc:77
void backup(size_t len) override
"Returns" back some of the data to the stream. The returned data must be less than what was obtained ...
Definition avro_stream_internal.cc:68
Definition avro_stream_internal.h:65
uint64_t byteCount() const override
Number of bytes written so far into this stream. The whole buffer returned by next() is assumed to be...
Definition avro_stream_internal.cc:127
bool next(uint8_t **data, size_t *len) override
Returns a buffer that can be written into. On successful return, data has the pointer to the buffer a...
Definition avro_stream_internal.cc:108
void backup(size_t len) override
"Returns" back to the stream some of the buffer obtained from in the last call to next().
Definition avro_stream_internal.cc:120
void flush() override
Flushes any data remaining in the buffer to the stream's underlying store, if any.
Definition avro_stream_internal.cc:129