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, software |
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * See the License for the specific language governing permissions and |
16 | * limitations under the License. |
17 | */ |
18 | |
19 | #ifndef ORC_INPUTSTREAM_HH |
20 | #define ORC_INPUTSTREAM_HH |
21 | |
22 | #include "Adaptor.hh" |
23 | #include "orc/OrcFile.hh" |
24 | #include "wrap/zero-copy-stream-wrapper.h" |
25 | |
26 | #include <list> |
27 | #include <fstream> |
28 | #include <iostream> |
29 | #include <sstream> |
30 | #include <vector> |
31 | |
32 | namespace orc { |
33 | |
34 | void printBuffer(std::ostream& out, |
35 | const char *buffer, |
36 | uint64_t length); |
37 | |
38 | class PositionProvider { |
39 | private: |
40 | std::list<uint64_t>::const_iterator position; |
41 | public: |
42 | PositionProvider(const std::list<uint64_t>& positions); |
43 | uint64_t next(); |
44 | }; |
45 | |
46 | /** |
47 | * A subclass of Google's ZeroCopyInputStream that supports seek. |
48 | * By extending Google's class, we get the ability to pass it directly |
49 | * to the protobuf readers. |
50 | */ |
51 | class SeekableInputStream: public google::protobuf::io::ZeroCopyInputStream { |
52 | public: |
53 | virtual ~SeekableInputStream(); |
54 | virtual void seek(PositionProvider& position) = 0; |
55 | virtual std::string getName() const = 0; |
56 | }; |
57 | |
58 | /** |
59 | * Create a seekable input stream based on a memory range. |
60 | */ |
61 | class SeekableArrayInputStream: public SeekableInputStream { |
62 | private: |
63 | const char* data; |
64 | uint64_t length; |
65 | uint64_t position; |
66 | uint64_t blockSize; |
67 | |
68 | public: |
69 | SeekableArrayInputStream(const unsigned char* list, |
70 | uint64_t length, |
71 | uint64_t block_size = 0); |
72 | SeekableArrayInputStream(const char* list, |
73 | uint64_t length, |
74 | uint64_t block_size = 0); |
75 | virtual ~SeekableArrayInputStream() override; |
76 | virtual bool Next(const void** data, int*size) override; |
77 | virtual void BackUp(int count) override; |
78 | virtual bool Skip(int count) override; |
79 | virtual google::protobuf::int64 ByteCount() const override; |
80 | virtual void seek(PositionProvider& position) override; |
81 | virtual std::string getName() const override; |
82 | }; |
83 | |
84 | /** |
85 | * Create a seekable input stream based on an input stream. |
86 | */ |
87 | class SeekableFileInputStream: public SeekableInputStream { |
88 | private: |
89 | MemoryPool& pool; |
90 | InputStream* const input; |
91 | const uint64_t start; |
92 | const uint64_t length; |
93 | const uint64_t blockSize; |
94 | std::unique_ptr<DataBuffer<char> > buffer; |
95 | uint64_t position; |
96 | uint64_t pushBack; |
97 | |
98 | public: |
99 | SeekableFileInputStream(InputStream* input, |
100 | uint64_t offset, |
101 | uint64_t byteCount, |
102 | MemoryPool& pool, |
103 | uint64_t blockSize = 0); |
104 | virtual ~SeekableFileInputStream() override; |
105 | |
106 | virtual bool Next(const void** data, int*size) override; |
107 | virtual void BackUp(int count) override; |
108 | virtual bool Skip(int count) override; |
109 | virtual int64_t ByteCount() const override; |
110 | virtual void seek(PositionProvider& position) override; |
111 | virtual std::string getName() const override; |
112 | }; |
113 | |
114 | } |
115 | |
116 | #endif //ORC_INPUTSTREAM_HH |
117 | |