forked from opensearch-project/opensearch-hadoop
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrackingBytesArray.java
More file actions
190 lines (164 loc) · 5.35 KB
/
Copy pathTrackingBytesArray.java
File metadata and controls
190 lines (164 loc) · 5.35 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
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch 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.opensearch.hadoop.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.BitSet;
import java.util.LinkedList;
import java.util.List;
/**
* Wrapper class around a {@link BytesArray} with 'awareness' around the underlying content.
* Considers each addition an entry and allows removal of specific entries (and by that skipping their backing content).
* Meant to be used as a buffer that is first filled, then emptied (in chunks) then cleaned-up.
*/
public class TrackingBytesArray implements ByteSequence {
private static class Entry {
final int offset;
final int length;
final int initialPosition;
Entry(int offset, int length, int initialPosition) {
this.offset = offset;
this.length = length;
this.initialPosition = initialPosition;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + length;
result = prime * result + offset;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Entry other = (Entry) obj;
if (length != other.length)
return false;
if (offset != other.offset)
return false;
return true;
}
}
private final BytesArray data;
private int maxEntries = 0;
private int size = 0;
private List<Entry> entries = new LinkedList<TrackingBytesArray.Entry>();
public TrackingBytesArray(BytesArray data) {
this.data = data;
}
public void copyFrom(BytesArray from) {
addEntry(from.size);
from.copyTo(data);
}
public void copyFrom(BytesRef from) {
addEntry(from.length());
from.copyTo(data);
}
public int length() {
return size;
}
public int entries() {
return entries.size();
}
public BitSet leftoversPosition() {
BitSet bitSet = new BitSet(maxEntries);
for (Entry entry : entries) {
bitSet.set(entry.initialPosition);
}
return bitSet;
}
private void addEntry(int length) {
// implied offset - data.size
entries.add(new Entry(data.size, length, entries.size()));
size += length;
maxEntries = size;
}
public void remove(int index) {
Entry entry = entries.remove(index);
size -= entry.length;
}
public BytesArray entry(int index) {
Entry entry = entries.get(index);
return new BytesArray(data.bytes, entry.offset, entry.length);
}
public BytesArray pop() {
Entry entry = entries.remove(0);
size -= entry.length;
byte[] entryData = new byte[entry.length];
System.arraycopy(data.bytes(), entry.offset, entryData, 0, entry.length);
return new BytesArray(entryData, entry.length);
}
public int length(int index) {
return entries.get(index).length;
}
public void writeTo(OutputStream out) throws IOException {
if (size == 0) {
return;
}
for (Entry entry : entries) {
out.write(data.bytes, entry.offset, entry.length);
}
out.flush();
}
@Override
public InputStream toInputStream() {
if (size == 0) {
return new ByteArrayInputStream(new byte[0]);
}
ByteArrayOutputStream out = new ByteArrayOutputStream(size);
try {
writeTo(out);
} catch (IOException e) {
throw new RuntimeException(e);
}
return new ByteArrayInputStream(out.toByteArray());
}
public void reset() {
size = 0;
maxEntries = 0;
entries.clear();
data.reset();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder((int) length());
for (Entry entry : entries) {
sb.append(new String(data.bytes, entry.offset, entry.length, StringUtils.UTF_8));
}
return sb.toString();
}
}