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
.TimeNanoReaderImpl
;
25 import org
.apache
.arrow
.vector
.complex
.reader
.FieldReader
;
26 import org
.apache
.arrow
.vector
.holders
.NullableTimeNanoHolder
;
27 import org
.apache
.arrow
.vector
.holders
.TimeNanoHolder
;
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
;
34 * TimeNanoVector implements a fixed width vector (8 bytes) of
35 * time (nanosecond resolution) values which could be null. A validity buffer
36 * (bit vector) is maintained to track which elements in the vector are null.
38 public final class TimeNanoVector
extends BaseFixedWidthVector
{
39 public static final byte TYPE_WIDTH
= 8;
40 private final FieldReader reader
;
43 * Instantiate a TimeNanoVector. This doesn't allocate any memory for
46 * @param name name of the vector
47 * @param allocator allocator for memory management.
49 public TimeNanoVector(String name
, BufferAllocator allocator
) {
50 this(name
, FieldType
.nullable(MinorType
.TIMENANO
.getType()), allocator
);
54 * Instantiate a TimeNanoVector. This doesn't allocate any memory for
57 * @param name name of the vector
58 * @param fieldType type of Field materialized by this vector
59 * @param allocator allocator for memory management.
61 public TimeNanoVector(String name
, FieldType fieldType
, BufferAllocator allocator
) {
62 this(new Field(name
, fieldType
, null), allocator
);
66 * Instantiate a TimeNanoVector. This doesn't allocate any memory for
69 * @param field Field materialized by this vector
70 * @param allocator allocator for memory management.
72 public TimeNanoVector(Field field
, BufferAllocator allocator
) {
73 super(field
, allocator
, TYPE_WIDTH
);
74 reader
= new TimeNanoReaderImpl(TimeNanoVector
.this);
78 * Get a reader that supports reading values from this vector.
80 * @return Field Reader for this vector
83 public FieldReader
getReader() {
88 * Get minor type for this vector. The vector holds values belonging
89 * to a particular type.
91 * @return {@link org.apache.arrow.vector.types.Types.MinorType}
94 public MinorType
getMinorType() {
95 return MinorType
.TIMENANO
;
99 /*----------------------------------------------------------------*
101 | vector value retrieval methods |
103 *----------------------------------------------------------------*/
107 * Get the element at the given index from the vector.
109 * @param index position of element
110 * @return element at given index
112 public long get(int index
) throws IllegalStateException
{
113 if (NULL_CHECKING_ENABLED
&& isSet(index
) == 0) {
114 throw new IllegalStateException("Value at index is null");
116 return valueBuffer
.getLong((long) index
* TYPE_WIDTH
);
120 * Get the element at the given index from the vector and
121 * sets the state in holder. If element at given index
122 * is null, holder.isSet will be zero.
124 * @param index position of element
126 public void get(int index
, NullableTimeNanoHolder holder
) {
127 if (isSet(index
) == 0) {
132 holder
.value
= valueBuffer
.getLong((long) index
* TYPE_WIDTH
);
136 * Same as {@link #get(int)}.
138 * @param index position of element
139 * @return element at given index
141 public Long
getObject(int index
) {
142 if (isSet(index
) == 0) {
145 return valueBuffer
.getLong((long) index
* TYPE_WIDTH
);
150 /*----------------------------------------------------------------*
152 | vector value setter methods |
154 *----------------------------------------------------------------*/
157 private void setValue(int index
, long value
) {
158 valueBuffer
.setLong((long) index
* TYPE_WIDTH
, value
);
162 * Set the element at the given index to the given value.
164 * @param index position of element
165 * @param value value of element
167 public void set(int index
, long value
) {
168 BitVectorHelper
.setBit(validityBuffer
, index
);
169 setValue(index
, value
);
173 * Set the element at the given index to the value set in data holder.
174 * If the value in holder is not indicated as set, element in the
175 * at the given index will be null.
177 * @param index position of element
178 * @param holder nullable data holder for value of element
180 public void set(int index
, NullableTimeNanoHolder holder
) throws IllegalArgumentException
{
181 if (holder
.isSet
< 0) {
182 throw new IllegalArgumentException();
183 } else if (holder
.isSet
> 0) {
184 BitVectorHelper
.setBit(validityBuffer
, index
);
185 setValue(index
, holder
.value
);
187 BitVectorHelper
.unsetBit(validityBuffer
, index
);
192 * Set the element at the given index to the value set in data holder.
194 * @param index position of element
195 * @param holder data holder for value of element
197 public void set(int index
, TimeNanoHolder holder
) {
198 BitVectorHelper
.setBit(validityBuffer
, index
);
199 setValue(index
, holder
.value
);
203 * Same as {@link #set(int, long)} except that it handles the
204 * case when index is greater than or equal to existing
205 * value capacity {@link #getValueCapacity()}.
207 * @param index position of element
208 * @param value value of element
210 public void setSafe(int index
, long value
) {
216 * Same as {@link #set(int, NullableTimeNanoHolder)} except that it handles the
217 * case when index is greater than or equal to existing
218 * value capacity {@link #getValueCapacity()}.
220 * @param index position of element
221 * @param holder nullable data holder for value of element
223 public void setSafe(int index
, NullableTimeNanoHolder holder
) throws IllegalArgumentException
{
229 * Same as {@link #set(int, TimeNanoHolder)} except that it handles the
230 * case when index is greater than or equal to existing
231 * value capacity {@link #getValueCapacity()}.
233 * @param index position of element
234 * @param holder data holder for value of element
236 public void setSafe(int index
, TimeNanoHolder holder
) {
242 * Store the given value at a particular position in the vector. isSet indicates
243 * whether the value is NULL or not.
245 * @param index position of the new value
246 * @param isSet 0 for NULL value, 1 otherwise
247 * @param value element value
249 public void set(int index
, int isSet
, long value
) {
253 BitVectorHelper
.unsetBit(validityBuffer
, index
);
258 * Same as {@link #set(int, int, long)} except that it handles the case
259 * when index is greater than or equal to current value capacity of the
262 * @param index position of the new value
263 * @param isSet 0 for NULL value, 1 otherwise
264 * @param value element value
266 public void setSafe(int index
, int isSet
, long value
) {
268 set(index
, isSet
, value
);
272 * Given a data buffer, get the value stored at a particular position
275 * <p>This method should not be used externally.
277 * @param buffer data buffer
278 * @param index position of the element.
279 * @return value stored at the index.
281 public static long get(final ArrowBuf buffer
, final int index
) {
282 return buffer
.getLong((long) index
* TYPE_WIDTH
);
286 /*----------------------------------------------------------------*
290 *----------------------------------------------------------------*/
293 * Construct a TransferPair comprising of this and a target vector of
296 * @param ref name of the target vector
297 * @param allocator allocator for the target vector
298 * @return {@link TransferPair}
301 public TransferPair
getTransferPair(String ref
, BufferAllocator allocator
) {
302 return new TransferImpl(ref
, allocator
);
306 * Construct a TransferPair with a desired target vector of the same type.
308 * @param to target vector
309 * @return {@link TransferPair}
312 public TransferPair
makeTransferPair(ValueVector to
) {
313 return new TransferImpl((TimeNanoVector
) to
);
316 private class TransferImpl
implements TransferPair
{
319 public TransferImpl(String ref
, BufferAllocator allocator
) {
320 to
= new TimeNanoVector(ref
, field
.getFieldType(), allocator
);
323 public TransferImpl(TimeNanoVector to
) {
328 public TimeNanoVector
getTo() {
333 public void transfer() {
338 public void splitAndTransfer(int startIndex
, int length
) {
339 splitAndTransferTo(startIndex
, length
, to
);
343 public void copyValueSafe(int fromIndex
, int toIndex
) {
344 to
.copyFromSafe(fromIndex
, toIndex
, TimeNanoVector
.this);