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 static org
.apache
.arrow
.vector
.NullCheckingForGet
.NULL_CHECKING_ENABLED
;
22 import org
.apache
.arrow
.memory
.ArrowBuf
;
23 import org
.apache
.arrow
.memory
.BufferAllocator
;
24 import org
.apache
.arrow
.vector
.complex
.impl
.UInt4ReaderImpl
;
25 import org
.apache
.arrow
.vector
.complex
.reader
.FieldReader
;
26 import org
.apache
.arrow
.vector
.holders
.NullableUInt4Holder
;
27 import org
.apache
.arrow
.vector
.holders
.UInt4Holder
;
28 import org
.apache
.arrow
.vector
.types
.Types
.MinorType
;
29 import org
.apache
.arrow
.vector
.types
.pojo
.Field
;
30 import org
.apache
.arrow
.vector
.types
.pojo
.FieldType
;
31 import org
.apache
.arrow
.vector
.util
.TransferPair
;
32 import org
.apache
.arrow
.vector
.util
.ValueVectorUtility
;
35 * UInt4Vector implements a fixed width (4 bytes) vector of
36 * integer values which could be null. A validity buffer (bit vector) is
37 * maintained to track which elements in the vector are null.
39 public final class UInt4Vector
extends BaseFixedWidthVector
implements BaseIntVector
{
42 * The mask to use when promoting the unsigned int value to a long int.
44 public static final long PROMOTION_MASK
= 0x00000000FFFFFFFFL
;
47 * The maximum 32-bit unsigned integer.
49 public static final int MAX_UINT4
= 0XFFFFFFFF;
51 public static final byte TYPE_WIDTH
= 4;
52 private final FieldReader reader
;
54 public UInt4Vector(String name
, BufferAllocator allocator
) {
55 this(name
, FieldType
.nullable(MinorType
.UINT4
.getType()), allocator
);
58 public UInt4Vector(String name
, FieldType fieldType
, BufferAllocator allocator
) {
59 this(new Field(name
, fieldType
, null), allocator
);
62 public UInt4Vector(Field field
, BufferAllocator allocator
) {
63 super(field
, allocator
, TYPE_WIDTH
);
64 reader
= new UInt4ReaderImpl(UInt4Vector
.this);
68 public FieldReader
getReader() {
73 public MinorType
getMinorType() {
74 return MinorType
.UINT4
;
78 /*----------------------------------------------------------------*
80 | vector value retrieval methods |
82 *----------------------------------------------------------------*/
84 * Given a data buffer, get the value stored at a particular position
87 * <p>To avoid overflow, the returned type is one step up from the signed
90 * <p>This method is mainly meant for integration tests.
92 * @param buffer data buffer
93 * @param index position of the element.
94 * @return value stored at the index.
96 public static long getNoOverflow(final ArrowBuf buffer
, final int index
) {
97 long l
= buffer
.getInt((long) index
* TYPE_WIDTH
);
98 return PROMOTION_MASK
& l
;
102 * Get the element at the given index from the vector.
104 * @param index position of element
105 * @return element at given index
107 public int get(int index
) throws IllegalStateException
{
108 if (NULL_CHECKING_ENABLED
&& isSet(index
) == 0) {
109 throw new IllegalStateException("Value at index is null");
111 return valueBuffer
.getInt((long) index
* TYPE_WIDTH
);
115 * Get the element at the given index from the vector and
116 * sets the state in holder. If element at given index
117 * is null, holder.isSet will be zero.
119 * @param index position of element
121 public void get(int index
, NullableUInt4Holder holder
) {
122 if (isSet(index
) == 0) {
127 holder
.value
= valueBuffer
.getInt((long) index
* TYPE_WIDTH
);
131 * Same as {@link #get(int)}.
133 * @param index position of element
134 * @return element at given index
136 public Integer
getObject(int index
) {
137 if (isSet(index
) == 0) {
140 return valueBuffer
.getInt((long) index
* TYPE_WIDTH
);
145 * Same as {@link #get(int)}.
147 * @param index position of element
148 * @return element at given index
150 public Long
getObjectNoOverflow(int index
) {
151 if (isSet(index
) == 0) {
154 return getNoOverflow(valueBuffer
, index
);
159 /*----------------------------------------------------------------*
161 | vector value setter methods |
163 *----------------------------------------------------------------*/
166 private void setValue(int index
, int value
) {
167 valueBuffer
.setInt((long) index
* TYPE_WIDTH
, value
);
171 * Set the element at the given index to the given value.
173 * @param index position of element
174 * @param value value of element
176 public void set(int index
, int value
) {
177 BitVectorHelper
.setBit(validityBuffer
, index
);
178 setValue(index
, value
);
182 * Set the element at the given index to the value set in data holder.
183 * If the value in holder is not indicated as set, element in the
184 * at the given index will be null.
186 * @param index position of element
187 * @param holder nullable data holder for value of element
189 public void set(int index
, NullableUInt4Holder holder
) throws IllegalArgumentException
{
190 if (holder
.isSet
< 0) {
191 throw new IllegalArgumentException();
192 } else if (holder
.isSet
> 0) {
193 BitVectorHelper
.setBit(validityBuffer
, index
);
194 setValue(index
, holder
.value
);
196 BitVectorHelper
.unsetBit(validityBuffer
, index
);
201 * Set the element at the given index to the value set in data holder.
203 * @param index position of element
204 * @param holder data holder for value of element
206 public void set(int index
, UInt4Holder holder
) {
207 BitVectorHelper
.setBit(validityBuffer
, index
);
208 setValue(index
, holder
.value
);
212 * Same as {@link #set(int, int)} except that it handles the
213 * case when index is greater than or equal to existing
214 * value capacity {@link #getValueCapacity()}.
216 * @param index position of element
217 * @param value value of element
219 public void setSafe(int index
, int value
) {
225 * Same as {@link #set(int, NullableUInt4Holder)} except that it handles the
226 * case when index is greater than or equal to existing
227 * value capacity {@link #getValueCapacity()}.
229 * @param index position of element
230 * @param holder nullable data holder for value of element
232 public void setSafe(int index
, NullableUInt4Holder holder
) throws IllegalArgumentException
{
238 * Same as {@link #set(int, UInt4Holder)} except that it handles the
239 * case when index is greater than or equal to existing
240 * value capacity {@link #getValueCapacity()}.
242 * @param index position of element
243 * @param holder data holder for value of element
245 public void setSafe(int index
, UInt4Holder holder
) {
251 * Sets the value at index to value isSet > 0, otherwise sets the index position
254 public void set(int index
, int isSet
, int value
) {
258 BitVectorHelper
.unsetBit(validityBuffer
, index
);
263 * Same as {@link #set(int, int, int)} but will reallocate if the buffer if index
264 * is larger than the current capacity.
266 public void setSafe(int index
, int isSet
, int value
) {
268 set(index
, isSet
, value
);
272 /*----------------------------------------------------------------*
276 *----------------------------------------------------------------*/
280 public TransferPair
getTransferPair(String ref
, BufferAllocator allocator
) {
281 return new TransferImpl(ref
, allocator
);
285 public TransferPair
makeTransferPair(ValueVector to
) {
286 return new TransferImpl((UInt4Vector
) to
);
290 public void setWithPossibleTruncate(int index
, long value
) {
291 this.setSafe(index
, (int) value
);
295 public void setUnsafeWithPossibleTruncate(int index
, long value
) {
296 this.set(index
, (int) value
);
300 public long getValueAsLong(int index
) {
301 return this.get(index
) & PROMOTION_MASK
;
305 public String
toString() {
306 return ValueVectorUtility
.getToString(this, 0, getValueCount(), (v
, i
) -> v
.getObjectNoOverflow(i
));
309 private class TransferImpl
implements TransferPair
{
312 public TransferImpl(String ref
, BufferAllocator allocator
) {
313 to
= new UInt4Vector(ref
, field
.getFieldType(), allocator
);
316 public TransferImpl(UInt4Vector to
) {
321 public UInt4Vector
getTo() {
326 public void transfer() {
331 public void splitAndTransfer(int startIndex
, int length
) {
332 splitAndTransferTo(startIndex
, length
, to
);
336 public void copyValueSafe(int fromIndex
, int toIndex
) {
337 to
.copyFromSafe(fromIndex
, toIndex
, UInt4Vector
.this);