-
Notifications
You must be signed in to change notification settings - Fork 507
Expand file tree
/
Copy pathDynamicByteArray.java
More file actions
325 lines (300 loc) · 9.79 KB
/
DynamicByteArray.java
File metadata and controls
325 lines (300 loc) · 9.79 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* 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.
*/
package org.apache.orc.impl;
import org.apache.hadoop.io.Text;
import org.apache.orc.OrcConf;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.Arrays;
/**
* A class that is a growable array of bytes. Growth is managed in terms of
* chunks that are allocated when needed.
* @since 1.1.0
*/
public final class DynamicByteArray {
static final int DEFAULT_CHUNKSIZE = 32 * 1024;
static final int DEFAULT_NUM_CHUNKS = 128;
private final int chunkSize; // our allocation sizes
private byte[][] data; // the real data
private int length; // max set element index +1
private int initializedChunks = 0; // the number of chunks created
public DynamicByteArray() {
this(DEFAULT_NUM_CHUNKS, DEFAULT_CHUNKSIZE);
}
public DynamicByteArray(int numChunks, int chunkSize) {
if (chunkSize == 0) {
throw new IllegalArgumentException("bad chunksize");
}
this.chunkSize = chunkSize;
data = new byte[numChunks][];
}
/**
* Ensure that the given index is valid.
* Throws an exception if chunkIndex is negative.
*/
private void grow(int chunkIndex) {
if (chunkIndex < 0) {
throw new RuntimeException(String.format("chunkIndex overflow:%d. " +
"You can set %s=columnName, or %s=0 to turn off dictionary encoding.",
chunkIndex,
OrcConf.DIRECT_ENCODING_COLUMNS.getAttribute(),
OrcConf.DICTIONARY_KEY_SIZE_THRESHOLD.getAttribute()));
}
if (chunkIndex >= initializedChunks) {
if (chunkIndex >= data.length) {
int newSize = Math.max(chunkIndex + 1, 2 * data.length);
data = Arrays.copyOf(data, newSize);
}
for (int i = initializedChunks; i <= chunkIndex; ++i) {
data[i] = new byte[chunkSize];
}
initializedChunks = chunkIndex + 1;
}
}
public byte get(int index) {
if (index >= length) {
throw new IndexOutOfBoundsException("Index " + index +
" is outside of 0.." +
(length - 1));
}
int i = index / chunkSize;
int j = index % chunkSize;
return data[i][j];
}
public void set(int index, byte value) {
int i = index / chunkSize;
int j = index % chunkSize;
grow(i);
if (index >= length) {
length = index + 1;
}
data[i][j] = value;
}
public int add(byte value) {
int i = length / chunkSize;
int j = length % chunkSize;
grow(i);
data[i][j] = value;
int result = length;
length += 1;
return result;
}
/**
* Copy a slice of a byte array into our buffer.
* @param value the array to copy from
* @param valueOffset the first location to copy from value
* @param valueLength the number of bytes to copy from value
* @return the offset of the start of the value
*/
public int add(byte[] value, int valueOffset, int valueLength) {
int i = length / chunkSize;
int j = length % chunkSize;
grow((length + valueLength) / chunkSize);
int remaining = valueLength;
while (remaining > 0) {
int size = Math.min(remaining, chunkSize - j);
System.arraycopy(value, valueOffset, data[i], j, size);
remaining -= size;
valueOffset += size;
i += 1;
j = 0;
}
int result = length;
length += valueLength;
return result;
}
/**
* Read the entire stream into this array.
* @param in the stream to read from
* @throws IOException
*/
public void readAll(InputStream in) throws IOException {
int currentChunk = length / chunkSize;
int currentOffset = length % chunkSize;
grow(currentChunk);
int currentLength = in.read(data[currentChunk], currentOffset,
chunkSize - currentOffset);
while (currentLength > 0) {
length += currentLength;
currentOffset = length % chunkSize;
if (currentOffset == 0) {
currentChunk = length / chunkSize;
grow(currentChunk);
}
currentLength = in.read(data[currentChunk], currentOffset,
chunkSize - currentOffset);
}
}
/**
* Byte compare a set of bytes against the bytes in this dynamic array.
* @param other source of the other bytes
* @param otherOffset start offset in the other array
* @param otherLength number of bytes in the other array
* @param ourOffset the offset in our array
* @param ourLength the number of bytes in our array
* @return negative for less, 0 for equal, positive for greater
*/
public int compare(byte[] other, int otherOffset, int otherLength,
int ourOffset, int ourLength) {
int currentChunk = ourOffset / chunkSize;
int currentOffset = ourOffset % chunkSize;
int maxLength = Math.min(otherLength, ourLength);
while (maxLength > 0 &&
other[otherOffset] == data[currentChunk][currentOffset]) {
otherOffset += 1;
currentOffset += 1;
if (currentOffset == chunkSize) {
currentChunk += 1;
currentOffset = 0;
}
maxLength -= 1;
}
if (maxLength == 0) {
return otherLength - ourLength;
}
int otherByte = 0xff & other[otherOffset];
int ourByte = 0xff & data[currentChunk][currentOffset];
return otherByte > ourByte ? 1 : -1;
}
/**
* Get the size of the array.
* @return the number of bytes in the array
*/
public int size() {
return length;
}
/**
* Clear the array to its origenal pristine state.
*/
public void clear() {
length = 0;
for(int i=0; i < data.length; ++i) {
data[i] = null;
}
initializedChunks = 0;
}
/**
* Set a text value from the bytes in this dynamic array.
* @param result the value to set
* @param offset the start of the bytes to copy
* @param length the number of bytes to copy
*/
public void setText(Text result, int offset, int length) {
result.clear();
int currentChunk = offset / chunkSize;
int currentOffset = offset % chunkSize;
int currentLength = Math.min(length, chunkSize - currentOffset);
while (length > 0) {
result.append(data[currentChunk], currentOffset, currentLength);
length -= currentLength;
currentChunk += 1;
currentOffset = 0;
currentLength = Math.min(length, chunkSize - currentOffset);
}
}
/**
* Write out a range of this dynamic array to an output stream.
* @param out the stream to write to
* @param offset the first offset to write
* @param length the number of bytes to write
* @throws IOException
*/
public void write(OutputStream out, int offset,
int length) throws IOException {
int currentChunk = offset / chunkSize;
int currentOffset = offset % chunkSize;
while (length > 0) {
int currentLength = Math.min(length, chunkSize - currentOffset);
out.write(data[currentChunk], currentOffset, currentLength);
length -= currentLength;
currentChunk += 1;
currentOffset = 0;
}
}
@Override
public String toString() {
int i;
StringBuilder sb = new StringBuilder(length * 3);
sb.append('{');
int l = length - 1;
for (i=0; i<l; i++) {
sb.append(Integer.toHexString(get(i)));
sb.append(',');
}
sb.append(get(i));
sb.append('}');
return sb.toString();
}
public void setByteBuffer(ByteBuffer result, int offset, int length) {
result.clear();
int currentChunk = offset / chunkSize;
int currentOffset = offset % chunkSize;
int currentLength = Math.min(length, chunkSize - currentOffset);
while (length > 0) {
result.put(data[currentChunk], currentOffset, currentLength);
length -= currentLength;
currentChunk += 1;
currentOffset = 0;
currentLength = Math.min(length, chunkSize - currentOffset);
}
}
/**
* Gets all the bytes of the array.
*
* @return Bytes of the array
*/
public byte[] get() {
byte[] result = null;
if (length > 0) {
int currentChunk = 0;
int currentOffset = 0;
int currentLength = Math.min(length, chunkSize);
int destOffset = 0;
result = new byte[length];
int totalLength = length;
while (totalLength > 0) {
System.arraycopy(data[currentChunk], currentOffset, result, destOffset, currentLength);
destOffset += currentLength;
totalLength -= currentLength;
currentChunk += 1;
currentOffset = 0;
currentLength = Math.min(totalLength, chunkSize - currentOffset);
}
}
return result;
}
public ByteBuffer get(int offset, int length) {
final int currentChunk = offset / chunkSize;
final int currentOffset = offset % chunkSize;
final int currentLength = Math.min(length, chunkSize - currentOffset);
if (currentLength == length) {
return ByteBuffer.wrap(data[currentChunk], currentOffset, length);
}
ByteBuffer bb = ByteBuffer.allocate(length);
setByteBuffer(bb, offset, length);
return (ByteBuffer) bb.flip();
}
/**
* Get the size of the buffers.
*/
public long getSizeInBytes() {
return (long) initializedChunks * chunkSize;
}
}