]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/java/vector/src/main/java/org/apache/arrow/vector/TimeNanoVector.java
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / java / vector / src / main / java / org / apache / arrow / vector / TimeNanoVector.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 static org.apache.arrow.vector.NullCheckingForGet.NULL_CHECKING_ENABLED;
21
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;
32
33 /**
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.
37 */
38 public final class TimeNanoVector extends BaseFixedWidthVector {
39 public static final byte TYPE_WIDTH = 8;
40 private final FieldReader reader;
41
42 /**
43 * Instantiate a TimeNanoVector. This doesn't allocate any memory for
44 * the data in vector.
45 *
46 * @param name name of the vector
47 * @param allocator allocator for memory management.
48 */
49 public TimeNanoVector(String name, BufferAllocator allocator) {
50 this(name, FieldType.nullable(MinorType.TIMENANO.getType()), allocator);
51 }
52
53 /**
54 * Instantiate a TimeNanoVector. This doesn't allocate any memory for
55 * the data in vector.
56 *
57 * @param name name of the vector
58 * @param fieldType type of Field materialized by this vector
59 * @param allocator allocator for memory management.
60 */
61 public TimeNanoVector(String name, FieldType fieldType, BufferAllocator allocator) {
62 this(new Field(name, fieldType, null), allocator);
63 }
64
65 /**
66 * Instantiate a TimeNanoVector. This doesn't allocate any memory for
67 * the data in vector.
68 *
69 * @param field Field materialized by this vector
70 * @param allocator allocator for memory management.
71 */
72 public TimeNanoVector(Field field, BufferAllocator allocator) {
73 super(field, allocator, TYPE_WIDTH);
74 reader = new TimeNanoReaderImpl(TimeNanoVector.this);
75 }
76
77 /**
78 * Get a reader that supports reading values from this vector.
79 *
80 * @return Field Reader for this vector
81 */
82 @Override
83 public FieldReader getReader() {
84 return reader;
85 }
86
87 /**
88 * Get minor type for this vector. The vector holds values belonging
89 * to a particular type.
90 *
91 * @return {@link org.apache.arrow.vector.types.Types.MinorType}
92 */
93 @Override
94 public MinorType getMinorType() {
95 return MinorType.TIMENANO;
96 }
97
98
99 /*----------------------------------------------------------------*
100 | |
101 | vector value retrieval methods |
102 | |
103 *----------------------------------------------------------------*/
104
105
106 /**
107 * Get the element at the given index from the vector.
108 *
109 * @param index position of element
110 * @return element at given index
111 */
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");
115 }
116 return valueBuffer.getLong((long) index * TYPE_WIDTH);
117 }
118
119 /**
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.
123 *
124 * @param index position of element
125 */
126 public void get(int index, NullableTimeNanoHolder holder) {
127 if (isSet(index) == 0) {
128 holder.isSet = 0;
129 return;
130 }
131 holder.isSet = 1;
132 holder.value = valueBuffer.getLong((long) index * TYPE_WIDTH);
133 }
134
135 /**
136 * Same as {@link #get(int)}.
137 *
138 * @param index position of element
139 * @return element at given index
140 */
141 public Long getObject(int index) {
142 if (isSet(index) == 0) {
143 return null;
144 } else {
145 return valueBuffer.getLong((long) index * TYPE_WIDTH);
146 }
147 }
148
149
150 /*----------------------------------------------------------------*
151 | |
152 | vector value setter methods |
153 | |
154 *----------------------------------------------------------------*/
155
156
157 private void setValue(int index, long value) {
158 valueBuffer.setLong((long) index * TYPE_WIDTH, value);
159 }
160
161 /**
162 * Set the element at the given index to the given value.
163 *
164 * @param index position of element
165 * @param value value of element
166 */
167 public void set(int index, long value) {
168 BitVectorHelper.setBit(validityBuffer, index);
169 setValue(index, value);
170 }
171
172 /**
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.
176 *
177 * @param index position of element
178 * @param holder nullable data holder for value of element
179 */
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);
186 } else {
187 BitVectorHelper.unsetBit(validityBuffer, index);
188 }
189 }
190
191 /**
192 * Set the element at the given index to the value set in data holder.
193 *
194 * @param index position of element
195 * @param holder data holder for value of element
196 */
197 public void set(int index, TimeNanoHolder holder) {
198 BitVectorHelper.setBit(validityBuffer, index);
199 setValue(index, holder.value);
200 }
201
202 /**
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()}.
206 *
207 * @param index position of element
208 * @param value value of element
209 */
210 public void setSafe(int index, long value) {
211 handleSafe(index);
212 set(index, value);
213 }
214
215 /**
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()}.
219 *
220 * @param index position of element
221 * @param holder nullable data holder for value of element
222 */
223 public void setSafe(int index, NullableTimeNanoHolder holder) throws IllegalArgumentException {
224 handleSafe(index);
225 set(index, holder);
226 }
227
228 /**
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()}.
232 *
233 * @param index position of element
234 * @param holder data holder for value of element
235 */
236 public void setSafe(int index, TimeNanoHolder holder) {
237 handleSafe(index);
238 set(index, holder);
239 }
240
241 /**
242 * Store the given value at a particular position in the vector. isSet indicates
243 * whether the value is NULL or not.
244 *
245 * @param index position of the new value
246 * @param isSet 0 for NULL value, 1 otherwise
247 * @param value element value
248 */
249 public void set(int index, int isSet, long value) {
250 if (isSet > 0) {
251 set(index, value);
252 } else {
253 BitVectorHelper.unsetBit(validityBuffer, index);
254 }
255 }
256
257 /**
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
260 * vector.
261 *
262 * @param index position of the new value
263 * @param isSet 0 for NULL value, 1 otherwise
264 * @param value element value
265 */
266 public void setSafe(int index, int isSet, long value) {
267 handleSafe(index);
268 set(index, isSet, value);
269 }
270
271 /**
272 * Given a data buffer, get the value stored at a particular position
273 * in the vector.
274 *
275 * <p>This method should not be used externally.
276 *
277 * @param buffer data buffer
278 * @param index position of the element.
279 * @return value stored at the index.
280 */
281 public static long get(final ArrowBuf buffer, final int index) {
282 return buffer.getLong((long) index * TYPE_WIDTH);
283 }
284
285
286 /*----------------------------------------------------------------*
287 | |
288 | vector transfer |
289 | |
290 *----------------------------------------------------------------*/
291
292 /**
293 * Construct a TransferPair comprising of this and a target vector of
294 * the same type.
295 *
296 * @param ref name of the target vector
297 * @param allocator allocator for the target vector
298 * @return {@link TransferPair}
299 */
300 @Override
301 public TransferPair getTransferPair(String ref, BufferAllocator allocator) {
302 return new TransferImpl(ref, allocator);
303 }
304
305 /**
306 * Construct a TransferPair with a desired target vector of the same type.
307 *
308 * @param to target vector
309 * @return {@link TransferPair}
310 */
311 @Override
312 public TransferPair makeTransferPair(ValueVector to) {
313 return new TransferImpl((TimeNanoVector) to);
314 }
315
316 private class TransferImpl implements TransferPair {
317 TimeNanoVector to;
318
319 public TransferImpl(String ref, BufferAllocator allocator) {
320 to = new TimeNanoVector(ref, field.getFieldType(), allocator);
321 }
322
323 public TransferImpl(TimeNanoVector to) {
324 this.to = to;
325 }
326
327 @Override
328 public TimeNanoVector getTo() {
329 return to;
330 }
331
332 @Override
333 public void transfer() {
334 transferTo(to);
335 }
336
337 @Override
338 public void splitAndTransfer(int startIndex, int length) {
339 splitAndTransferTo(startIndex, length, to);
340 }
341
342 @Override
343 public void copyValueSafe(int fromIndex, int toIndex) {
344 to.copyFromSafe(fromIndex, toIndex, TimeNanoVector.this);
345 }
346 }
347 }