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_OUTPUTSTREAM_HH
20#define ORC_OUTPUTSTREAM_HH
21
22#include "Adaptor.hh"
23#include "orc/OrcFile.hh"
24#include "wrap/zero-copy-stream-wrapper.h"
25
26namespace orc {
27
28 /**
29 * Record write position for creating index stream
30 */
31 class PositionRecorder {
32 public:
33 virtual ~PositionRecorder();
34 virtual void add(uint64_t pos) = 0;
35 };
36
37 /**
38 * A subclass of Google's ZeroCopyOutputStream that supports output to memory
39 * buffer, and flushing to OutputStream.
40 * By extending Google's class, we get the ability to pass it directly
41 * to the protobuf writers.
42 */
43 class BufferedOutputStream: public google::protobuf::io::ZeroCopyOutputStream {
44 private:
45 OutputStream * outputStream;
46 std::unique_ptr<DataBuffer<char> > dataBuffer;
47 uint64_t blockSize;
48
49 public:
50 BufferedOutputStream(MemoryPool& pool,
51 OutputStream * outStream,
52 uint64_t capacity,
53 uint64_t block_size);
54 virtual ~BufferedOutputStream() override;
55
56 virtual bool Next(void** data, int*size) override;
57 virtual void BackUp(int count) override;
58 virtual google::protobuf::int64 ByteCount() const override;
59 virtual bool WriteAliasedRaw(const void * data, int size) override;
60 virtual bool AllowsAliasing() const override;
61
62 virtual std::string getName() const;
63 virtual uint64_t getSize() const;
64 virtual uint64_t flush();
65
66 virtual bool isCompressed() const { return false; }
67 };
68
69 /**
70 * An append only buffered stream that allows
71 * buffer, and flushing to OutputStream.
72 * By extending Google's class, we get the ability to pass it directly
73 * to the protobuf writers.
74 */
75 class AppendOnlyBufferedStream {
76 private:
77 std::unique_ptr<BufferedOutputStream> outStream;
78 char * buffer;
79 int bufferOffset, bufferLength;
80
81 public:
82 AppendOnlyBufferedStream(std::unique_ptr<BufferedOutputStream> _outStream) :
83 outStream(std::move(_outStream)) {
84 buffer = nullptr;
85 bufferOffset = bufferLength = 0;
86 }
87
88 void write(const char * data, size_t size);
89 uint64_t getSize() const;
90 uint64_t flush();
91
92 void recordPosition(PositionRecorder* recorder) const;
93 };
94}
95
96#endif // ORC_OUTPUTSTREAM_HH
97