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 | // DEPRECATED IO INTERFACES: We have transitioned to using the Apache |
19 | // Arrow file input and output abstract interfaces defined in |
20 | // arrow/io/interfaces.h. These legacy interfaces are being preserved |
21 | // through a wrapper layer for one to two releases |
22 | |
23 | #pragma once |
24 | |
25 | #include <cstdint> |
26 | #include <memory> |
27 | |
28 | #include "parquet/platform.h" |
29 | |
30 | namespace parquet { |
31 | |
32 | class PARQUET_EXPORT FileInterface { |
33 | public: |
34 | virtual ~FileInterface() = default; |
35 | |
36 | // Close the file |
37 | virtual void Close() = 0; |
38 | |
39 | // Return the current position in the file relative to the start |
40 | virtual int64_t Tell() = 0; |
41 | }; |
42 | |
43 | /// It is the responsibility of implementations to mind threadsafety of shared |
44 | /// resources |
45 | class PARQUET_EXPORT RandomAccessSource : virtual public FileInterface { |
46 | public: |
47 | virtual ~RandomAccessSource() = default; |
48 | |
49 | virtual int64_t Size() const = 0; |
50 | |
51 | // Returns bytes read |
52 | virtual int64_t Read(int64_t nbytes, uint8_t* out) = 0; |
53 | |
54 | virtual std::shared_ptr<Buffer> Read(int64_t nbytes) = 0; |
55 | |
56 | virtual std::shared_ptr<Buffer> ReadAt(int64_t position, int64_t nbytes) = 0; |
57 | |
58 | /// Returns bytes read |
59 | virtual int64_t ReadAt(int64_t position, int64_t nbytes, uint8_t* out) = 0; |
60 | }; |
61 | |
62 | class PARQUET_EXPORT OutputStream : virtual public FileInterface { |
63 | public: |
64 | virtual ~OutputStream() = default; |
65 | |
66 | // Copy bytes into the output stream |
67 | virtual void Write(const uint8_t* data, int64_t length) = 0; |
68 | }; |
69 | |
70 | // ---------------------------------------------------------------------- |
71 | // Wrapper classes |
72 | |
73 | class PARQUET_EXPORT ParquetInputWrapper : public ::arrow::io::RandomAccessFile { |
74 | public: |
75 | explicit ParquetInputWrapper(std::unique_ptr<RandomAccessSource> source); |
76 | explicit ParquetInputWrapper(RandomAccessSource* source); |
77 | |
78 | ~ParquetInputWrapper() override; |
79 | |
80 | // FileInterface |
81 | ::arrow::Status Close() override; |
82 | ::arrow::Status Tell(int64_t* position) const override; |
83 | bool closed() const override; |
84 | |
85 | // Seekable |
86 | ::arrow::Status Seek(int64_t position) override; |
87 | |
88 | // InputStream / RandomAccessFile |
89 | ::arrow::Status Read(int64_t nbytes, int64_t* bytes_read, void* out) override; |
90 | ::arrow::Status Read(int64_t nbytes, std::shared_ptr<Buffer>* out) override; |
91 | ::arrow::Status ReadAt(int64_t position, int64_t nbytes, |
92 | std::shared_ptr<Buffer>* out) override; |
93 | ::arrow::Status GetSize(int64_t* size) override; |
94 | |
95 | private: |
96 | std::unique_ptr<RandomAccessSource> owned_source_; |
97 | RandomAccessSource* source_; |
98 | bool closed_; |
99 | }; |
100 | |
101 | class PARQUET_EXPORT ParquetOutputWrapper : public ::arrow::io::OutputStream { |
102 | public: |
103 | explicit ParquetOutputWrapper(const std::shared_ptr<::parquet::OutputStream>& sink); |
104 | explicit ParquetOutputWrapper(std::unique_ptr<::parquet::OutputStream> sink); |
105 | explicit ParquetOutputWrapper(::parquet::OutputStream* sink); |
106 | |
107 | ~ParquetOutputWrapper() override; |
108 | |
109 | // FileInterface |
110 | ::arrow::Status Close() override; |
111 | ::arrow::Status Tell(int64_t* position) const override; |
112 | bool closed() const override; |
113 | |
114 | // Writable |
115 | ::arrow::Status Write(const void* data, int64_t nbytes) override; |
116 | |
117 | private: |
118 | std::unique_ptr<::parquet::OutputStream> owned_sink_; |
119 | std::shared_ptr<::parquet::OutputStream> shared_sink_; |
120 | ::parquet::OutputStream* sink_; |
121 | bool closed_; |
122 | }; |
123 | |
124 | } // namespace parquet |
125 | |