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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org
.apache
.arrow
.vector
;
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
;
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.
36 public final class LargeVarBinaryVector
extends BaseLargeVariableWidthVector
{
37 private final FieldReader reader
;
40 * Instantiate a LargeVarBinaryVector. This doesn't allocate any memory for
43 * @param name name of the vector
44 * @param allocator allocator for memory management.
46 public LargeVarBinaryVector(String name
, BufferAllocator allocator
) {
47 this(name
, FieldType
.nullable(MinorType
.LARGEVARBINARY
.getType()), allocator
);
51 * Instantiate a LargeVarBinaryVector. This doesn't allocate any memory for
54 * @param name name of the vector
55 * @param fieldType type of Field materialized by this vector
56 * @param allocator allocator for memory management.
58 public LargeVarBinaryVector(String name
, FieldType fieldType
, BufferAllocator allocator
) {
59 this(new Field(name
, fieldType
, null), allocator
);
63 * Instantiate a LargeVarBinaryVector. This doesn't allocate any memory for
66 * @param field field materialized by this vector
67 * @param allocator allocator for memory management.
69 public LargeVarBinaryVector(Field field
, BufferAllocator allocator
) {
70 super(field
, allocator
);
71 reader
= new LargeVarBinaryReaderImpl(LargeVarBinaryVector
.this);
75 * Get a reader that supports reading values from this vector.
77 * @return Field Reader for this vector
80 public FieldReader
getReader() {
85 * Get minor type for this vector. The vector holds values belonging
86 * to a particular type.
88 * @return {@link org.apache.arrow.vector.types.Types.MinorType}
91 public MinorType
getMinorType() {
92 return MinorType
.LARGEVARBINARY
;
96 /*----------------------------------------------------------------*
98 | vector value retrieval methods |
100 *----------------------------------------------------------------*/
104 * Get the variable length element at specified index as byte array.
106 * @param index position of element to get
107 * @return array of bytes for non-null element, null otherwise
109 public byte[] get(int index
) {
111 if (isSet(index
) == 0) {
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
);
123 * Get the variable length element at specified index as Text.
125 * @param index position of element to get
126 * @return byte array for non-null element, null otherwise
128 public byte[] getObject(int index
) {
133 * Get the variable length element at specified index and sets the state
134 * in provided holder.
136 * @param index position of element to get
137 * @param holder data holder to be populated by this function
139 public void get(int index
, NullableLargeVarBinaryHolder holder
) {
141 if (isSet(index
) == 0) {
146 holder
.start
= getStartOffset(index
);
147 holder
.end
= offsetBuffer
.getLong((long) (index
+ 1) * OFFSET_WIDTH
);
148 holder
.buffer
= valueBuffer
;
152 /*----------------------------------------------------------------*
154 | vector value setter methods |
156 *----------------------------------------------------------------*/
160 * Set the variable length element at the specified index to the data
161 * buffer supplied in the holder.
163 * @param index position of the element to set
164 * @param holder holder that carries data buffer.
166 public void set(int index
, LargeVarBinaryHolder holder
) {
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
);
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.
182 * @param index position of the element to set
183 * @param holder holder that carries data buffer.
185 public void setSafe(int index
, LargeVarBinaryHolder holder
) {
187 final int dataLength
= (int) (holder
.end
- holder
.start
);
188 handleSafe(index
, dataLength
);
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
);
198 * Set the variable length element at the specified index to the data
199 * buffer supplied in the holder.
201 * @param index position of the element to set
202 * @param holder holder that carries data buffer.
204 public void set(int index
, NullableLargeVarBinaryHolder holder
) {
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
);
214 offsetBuffer
.setLong((long) (index
+ 1) * OFFSET_WIDTH
, startOffset
);
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.
224 * @param index position of the element to set
225 * @param holder holder that carries data buffer.
227 public void setSafe(int index
, NullableLargeVarBinaryHolder holder
) {
229 if (holder
.isSet
!= 0) {
230 final int dataLength
= (int) (holder
.end
- holder
.start
);
231 handleSafe(index
, dataLength
);
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
);
237 fillEmpties(index
+ 1);
239 BitVectorHelper
.setValidityBit(validityBuffer
, index
, holder
.isSet
);
244 /*----------------------------------------------------------------*
248 *----------------------------------------------------------------*/
251 * Construct a TransferPair comprising of this and a target vector of
254 * @param ref name of the target vector
255 * @param allocator allocator for the target vector
256 * @return {@link TransferPair}
259 public TransferPair
getTransferPair(String ref
, BufferAllocator allocator
) {
260 return new TransferImpl(ref
, allocator
);
264 * Construct a TransferPair with a desired target vector of the same type.
266 * @param to target vector
267 * @return {@link TransferPair}
270 public TransferPair
makeTransferPair(ValueVector to
) {
271 return new TransferImpl((LargeVarBinaryVector
) to
);
274 private class TransferImpl
implements TransferPair
{
275 LargeVarBinaryVector to
;
277 public TransferImpl(String ref
, BufferAllocator allocator
) {
278 to
= new LargeVarBinaryVector(ref
, field
.getFieldType(), allocator
);
281 public TransferImpl(LargeVarBinaryVector to
) {
286 public LargeVarBinaryVector
getTo() {
291 public void transfer() {
296 public void splitAndTransfer(int startIndex
, int length
) {
297 splitAndTransferTo(startIndex
, length
, to
);
301 public void copyValueSafe(int fromIndex
, int toIndex
) {
302 to
.copyFromSafe(fromIndex
, toIndex
, LargeVarBinaryVector
.this);