]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/java/vector/src/main/java/org/apache/arrow/vector/LargeVarBinaryVector.java
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / java / vector / src / main / java / org / apache / arrow / vector / LargeVarBinaryVector.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.arrow.vector;
19
20 import org.apache.arrow.memory.BufferAllocator;
21 import org.apache.arrow.vector.complex.impl.LargeVarBinaryReaderImpl;
22 import org.apache.arrow.vector.complex.reader.FieldReader;
23 import org.apache.arrow.vector.holders.LargeVarBinaryHolder;
24 import org.apache.arrow.vector.holders.NullableLargeVarBinaryHolder;
25 import org.apache.arrow.vector.types.Types.MinorType;
26 import org.apache.arrow.vector.types.pojo.Field;
27 import org.apache.arrow.vector.types.pojo.FieldType;
28 import org.apache.arrow.vector.util.TransferPair;
29
30 /**
31 * LargeVarBinaryVector implements a large variable width vector of binary
32 * values which could be NULL. A validity buffer (bit vector) is maintained
33 * to track which elements in the vector are null.
34 * The size of the underlying buffer can be over 2GB.
35 */
36 public final class LargeVarBinaryVector extends BaseLargeVariableWidthVector {
37 private final FieldReader reader;
38
39 /**
40 * Instantiate a LargeVarBinaryVector. This doesn't allocate any memory for
41 * the data in vector.
42 *
43 * @param name name of the vector
44 * @param allocator allocator for memory management.
45 */
46 public LargeVarBinaryVector(String name, BufferAllocator allocator) {
47 this(name, FieldType.nullable(MinorType.LARGEVARBINARY.getType()), allocator);
48 }
49
50 /**
51 * Instantiate a LargeVarBinaryVector. This doesn't allocate any memory for
52 * the data in vector.
53 *
54 * @param name name of the vector
55 * @param fieldType type of Field materialized by this vector
56 * @param allocator allocator for memory management.
57 */
58 public LargeVarBinaryVector(String name, FieldType fieldType, BufferAllocator allocator) {
59 this(new Field(name, fieldType, null), allocator);
60 }
61
62 /**
63 * Instantiate a LargeVarBinaryVector. This doesn't allocate any memory for
64 * the data in vector.
65 *
66 * @param field field materialized by this vector
67 * @param allocator allocator for memory management.
68 */
69 public LargeVarBinaryVector(Field field, BufferAllocator allocator) {
70 super(field, allocator);
71 reader = new LargeVarBinaryReaderImpl(LargeVarBinaryVector.this);
72 }
73
74 /**
75 * Get a reader that supports reading values from this vector.
76 *
77 * @return Field Reader for this vector
78 */
79 @Override
80 public FieldReader getReader() {
81 return reader;
82 }
83
84 /**
85 * Get minor type for this vector. The vector holds values belonging
86 * to a particular type.
87 *
88 * @return {@link org.apache.arrow.vector.types.Types.MinorType}
89 */
90 @Override
91 public MinorType getMinorType() {
92 return MinorType.LARGEVARBINARY;
93 }
94
95
96 /*----------------------------------------------------------------*
97 | |
98 | vector value retrieval methods |
99 | |
100 *----------------------------------------------------------------*/
101
102
103 /**
104 * Get the variable length element at specified index as byte array.
105 *
106 * @param index position of element to get
107 * @return array of bytes for non-null element, null otherwise
108 */
109 public byte[] get(int index) {
110 assert index >= 0;
111 if (isSet(index) == 0) {
112 return null;
113 }
114 final long startOffset = getStartOffset(index);
115 final int dataLength =
116 (int) (offsetBuffer.getLong((long) (index + 1) * OFFSET_WIDTH) - startOffset);
117 final byte[] result = new byte[dataLength];
118 valueBuffer.getBytes(startOffset, result, 0, dataLength);
119 return result;
120 }
121
122 /**
123 * Get the variable length element at specified index as Text.
124 *
125 * @param index position of element to get
126 * @return byte array for non-null element, null otherwise
127 */
128 public byte[] getObject(int index) {
129 return get(index);
130 }
131
132 /**
133 * Get the variable length element at specified index and sets the state
134 * in provided holder.
135 *
136 * @param index position of element to get
137 * @param holder data holder to be populated by this function
138 */
139 public void get(int index, NullableLargeVarBinaryHolder holder) {
140 assert index >= 0;
141 if (isSet(index) == 0) {
142 holder.isSet = 0;
143 return;
144 }
145 holder.isSet = 1;
146 holder.start = getStartOffset(index);
147 holder.end = offsetBuffer.getLong((long) (index + 1) * OFFSET_WIDTH);
148 holder.buffer = valueBuffer;
149 }
150
151
152 /*----------------------------------------------------------------*
153 | |
154 | vector value setter methods |
155 | |
156 *----------------------------------------------------------------*/
157
158
159 /**
160 * Set the variable length element at the specified index to the data
161 * buffer supplied in the holder.
162 *
163 * @param index position of the element to set
164 * @param holder holder that carries data buffer.
165 */
166 public void set(int index, LargeVarBinaryHolder holder) {
167 assert index >= 0;
168 fillHoles(index);
169 BitVectorHelper.setBit(validityBuffer, index);
170 final int dataLength = (int) (holder.end - holder.start);
171 final long startOffset = getStartOffset(index);
172 offsetBuffer.setLong((long) (index + 1) * OFFSET_WIDTH, startOffset + dataLength);
173 valueBuffer.setBytes(startOffset, holder.buffer, holder.start, dataLength);
174 lastSet = index;
175 }
176
177 /**
178 * Same as {@link #set(int, LargeVarBinaryHolder)} except that it handles the
179 * case where index and length of new element are beyond the existing
180 * capacity of the vector.
181 *
182 * @param index position of the element to set
183 * @param holder holder that carries data buffer.
184 */
185 public void setSafe(int index, LargeVarBinaryHolder holder) {
186 assert index >= 0;
187 final int dataLength = (int) (holder.end - holder.start);
188 handleSafe(index, dataLength);
189 fillHoles(index);
190 BitVectorHelper.setBit(validityBuffer, index);
191 final long startOffset = getStartOffset(index);
192 offsetBuffer.setLong((long) (index + 1) * OFFSET_WIDTH, startOffset + dataLength);
193 valueBuffer.setBytes(startOffset, holder.buffer, holder.start, dataLength);
194 lastSet = index;
195 }
196
197 /**
198 * Set the variable length element at the specified index to the data
199 * buffer supplied in the holder.
200 *
201 * @param index position of the element to set
202 * @param holder holder that carries data buffer.
203 */
204 public void set(int index, NullableLargeVarBinaryHolder holder) {
205 assert index >= 0;
206 fillHoles(index);
207 BitVectorHelper.setValidityBit(validityBuffer, index, holder.isSet);
208 final long startOffset = getStartOffset(index);
209 if (holder.isSet != 0) {
210 final int dataLength = (int) (holder.end - holder.start);
211 offsetBuffer.setLong((long) (index + 1) * OFFSET_WIDTH, startOffset + dataLength);
212 valueBuffer.setBytes(startOffset, holder.buffer, holder.start, dataLength);
213 } else {
214 offsetBuffer.setLong((long) (index + 1) * OFFSET_WIDTH, startOffset);
215 }
216 lastSet = index;
217 }
218
219 /**
220 * Same as {@link #set(int, NullableLargeVarBinaryHolder)} except that it handles the
221 * case where index and length of new element are beyond the existing
222 * capacity of the vector.
223 *
224 * @param index position of the element to set
225 * @param holder holder that carries data buffer.
226 */
227 public void setSafe(int index, NullableLargeVarBinaryHolder holder) {
228 assert index >= 0;
229 if (holder.isSet != 0) {
230 final int dataLength = (int) (holder.end - holder.start);
231 handleSafe(index, dataLength);
232 fillHoles(index);
233 final long startOffset = getStartOffset(index);
234 offsetBuffer.setLong((long) (index + 1) * OFFSET_WIDTH, startOffset + dataLength);
235 valueBuffer.setBytes(startOffset, holder.buffer, holder.start, dataLength);
236 } else {
237 fillEmpties(index + 1);
238 }
239 BitVectorHelper.setValidityBit(validityBuffer, index, holder.isSet);
240 lastSet = index;
241 }
242
243
244 /*----------------------------------------------------------------*
245 | |
246 | vector transfer |
247 | |
248 *----------------------------------------------------------------*/
249
250 /**
251 * Construct a TransferPair comprising of this and a target vector of
252 * the same type.
253 *
254 * @param ref name of the target vector
255 * @param allocator allocator for the target vector
256 * @return {@link TransferPair}
257 */
258 @Override
259 public TransferPair getTransferPair(String ref, BufferAllocator allocator) {
260 return new TransferImpl(ref, allocator);
261 }
262
263 /**
264 * Construct a TransferPair with a desired target vector of the same type.
265 *
266 * @param to target vector
267 * @return {@link TransferPair}
268 */
269 @Override
270 public TransferPair makeTransferPair(ValueVector to) {
271 return new TransferImpl((LargeVarBinaryVector) to);
272 }
273
274 private class TransferImpl implements TransferPair {
275 LargeVarBinaryVector to;
276
277 public TransferImpl(String ref, BufferAllocator allocator) {
278 to = new LargeVarBinaryVector(ref, field.getFieldType(), allocator);
279 }
280
281 public TransferImpl(LargeVarBinaryVector to) {
282 this.to = to;
283 }
284
285 @Override
286 public LargeVarBinaryVector getTo() {
287 return to;
288 }
289
290 @Override
291 public void transfer() {
292 transferTo(to);
293 }
294
295 @Override
296 public void splitAndTransfer(int startIndex, int length) {
297 splitAndTransferTo(startIndex, length, to);
298 }
299
300 @Override
301 public void copyValueSafe(int fromIndex, int toIndex) {
302 to.copyFromSafe(fromIndex, toIndex, LargeVarBinaryVector.this);
303 }
304 }
305 }