1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "InputStream.hh"
#include "orc/Exceptions.hh"
#include <algorithm>
#include <iomanip>
namespace orc {
void printBuffer(std::ostream& out, const char* buffer, uint64_t length) {
const uint64_t width = 24;
out << std::hex;
for (uint64_t line = 0; line < (length + width - 1) / width; ++line) {
out << std::setfill('0') << std::setw(7) << (line * width);
for (uint64_t byte = 0; byte < width && line * width + byte < length; ++byte) {
out << " " << std::setfill('0') << std::setw(2)
<< static_cast<uint64_t>(0xff & buffer[line * width + byte]);
}
out << "\n";
}
out << std::dec;
}
PositionProvider::PositionProvider(const std::list<uint64_t>& posns) {
position_ = posns.begin();
}
uint64_t PositionProvider::next() {
uint64_t result = *position_;
++position_;
return result;
}
uint64_t PositionProvider::current() {
return *position_;
}
SeekableInputStream::~SeekableInputStream() {
// PASS
}
SeekableArrayInputStream::~SeekableArrayInputStream() {
// PASS
}
SeekableArrayInputStream::SeekableArrayInputStream(const unsigned char* values, uint64_t size,
uint64_t blkSize)
: data_(reinterpret_cast<const char*>(values)) {
length_ = size;
position_ = 0;
blockSize_ = blkSize == 0 ? length_ : static_cast<uint64_t>(blkSize);
}
SeekableArrayInputStream::SeekableArrayInputStream(const char* values, uint64_t size,
uint64_t blkSize)
: data_(values) {
length_ = size;
position_ = 0;
blockSize_ = blkSize == 0 ? length_ : static_cast<uint64_t>(blkSize);
}
bool SeekableArrayInputStream::Next(const void** buffer, int* size) {
uint64_t currentSize = std::min(length_ - position_, blockSize_);
if (currentSize > 0) {
*buffer = data_ + position_;
*size = static_cast<int>(currentSize);
position_ += currentSize;
return true;
}
*size = 0;
return false;
}
void SeekableArrayInputStream::BackUp(int count) {
if (count >= 0) {
uint64_t unsignedCount = static_cast<uint64_t>(count);
if (unsignedCount <= blockSize_ && unsignedCount <= position_) {
position_ -= unsignedCount;
} else {
throw std::logic_error("Can't backup that much!");
}
}
}
bool SeekableArrayInputStream::Skip(int count) {
if (count >= 0) {
uint64_t unsignedCount = static_cast<uint64_t>(count);
if (unsignedCount + position_ <= length_) {
position_ += unsignedCount;
return true;
} else {
position_ = length_;
}
}
return false;
}
int64_t SeekableArrayInputStream::ByteCount() const {
return static_cast<google::protobuf::int64>(position_);
}
void SeekableArrayInputStream::seek(PositionProvider& seekPosition) {
position_ = seekPosition.next();
}
std::string SeekableArrayInputStream::getName() const {
std::ostringstream result;
result << "SeekableArrayInputStream " << position_ << " of " << length_;
return result.str();
}
static uint64_t computeBlock(uint64_t request, uint64_t length) {
return std::min(length, request == 0 ? 256 * 1024 : request);
}
SeekableFileInputStream::SeekableFileInputStream(InputStream* stream, uint64_t offset,
uint64_t byteCount, MemoryPool& pool,
uint64_t blockSize)
: pool_(pool),
input_(stream),
start_(offset),
length_(byteCount),
blockSize_(computeBlock(blockSize, length_)) {
position_ = 0;
buffer_.reset(new DataBuffer<char>(pool_));
pushBack_ = 0;
}
SeekableFileInputStream::~SeekableFileInputStream() {
// PASS
}
bool SeekableFileInputStream::Next(const void** data, int* size) {
uint64_t bytesRead;
if (pushBack_ != 0) {
*data = buffer_->data() + (buffer_->size() - pushBack_);
bytesRead = pushBack_;
} else {
bytesRead = std::min(length_ - position_, blockSize_);
buffer_->resize(bytesRead);
if (bytesRead > 0) {
input_->read(buffer_->data(), bytesRead, start_ + position_);
*data = static_cast<void*>(buffer_->data());
}
}
position_ += bytesRead;
pushBack_ = 0;
*size = static_cast<int>(bytesRead);
return bytesRead != 0;
}
void SeekableFileInputStream::BackUp(int signedCount) {
if (signedCount < 0) {
throw std::logic_error("can't backup negative distances");
}
uint64_t count = static_cast<uint64_t>(signedCount);
if (pushBack_ > 0) {
throw std::logic_error("can't backup unless we just called Next");
}
if (count > blockSize_ || count > position_) {
throw std::logic_error("can't backup that far");
}
pushBack_ = static_cast<uint64_t>(count);
position_ -= pushBack_;
}
bool SeekableFileInputStream::Skip(int signedCount) {
if (signedCount < 0) {
return false;
}
uint64_t count = static_cast<uint64_t>(signedCount);
position_ = std::min(position_ + count, length_);
pushBack_ = 0;
return position_ < length_;
}
int64_t SeekableFileInputStream::ByteCount() const {
return static_cast<int64_t>(position_);
}
void SeekableFileInputStream::seek(PositionProvider& location) {
position_ = location.next();
if (position_ > length_) {
position_ = length_;
throw std::logic_error("seek too far");
}
pushBack_ = 0;
}
std::string SeekableFileInputStream::getName() const {
std::ostringstream result;
result << input_->getName() << " from " << start_ << " for " << length_;
return result.str();
}
} // namespace orc
|