]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/java/vector/src/main/java/org/apache/arrow/vector/BigIntVector.java
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / java / vector / src / main / java / org / apache / arrow / vector / BigIntVector.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.BigIntReaderImpl;
25 import org.apache.arrow.vector.complex.reader.FieldReader;
26 import org.apache.arrow.vector.holders.BigIntHolder;
27 import org.apache.arrow.vector.holders.NullableBigIntHolder;
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 * BigIntVector implements a fixed width vector (8 bytes) of
35 * integer values which could be null. A validity buffer (bit vector) is
36 * maintained to track which elements in the vector are null.
37 */
38 public final class BigIntVector extends BaseFixedWidthVector implements BaseIntVector {
39 public static final byte TYPE_WIDTH = 8;
40 private final FieldReader reader;
41
42 /**
43 * Instantiate a BigIntVector. 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 BigIntVector(String name, BufferAllocator allocator) {
50 this(name, FieldType.nullable(MinorType.BIGINT.getType()), allocator);
51 }
52
53 /**
54 * Instantiate a BigIntVector. 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 BigIntVector(String name, FieldType fieldType, BufferAllocator allocator) {
62 this(new Field(name, fieldType, null), allocator);
63 }
64
65 /**
66 * Instantiate a BigIntVector. 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 BigIntVector(Field field, BufferAllocator allocator) {
73 super(field, allocator, TYPE_WIDTH);
74 reader = new BigIntReaderImpl(BigIntVector.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 * @return {@link org.apache.arrow.vector.types.Types.MinorType}
91 */
92 @Override
93 public MinorType getMinorType() {
94 return MinorType.BIGINT;
95 }
96
97
98 /*----------------------------------------------------------------*
99 | |
100 | vector value retrieval methods |
101 | |
102 *----------------------------------------------------------------*/
103
104 /**
105 * Get the element at the given index from the vector.
106 *
107 * @param index position of element
108 * @return element at given index
109 */
110 public long get(int index) throws IllegalStateException {
111 if (NULL_CHECKING_ENABLED && isSet(index) == 0) {
112 throw new IllegalStateException("Value at index is null");
113 }
114 return valueBuffer.getLong((long) index * TYPE_WIDTH);
115 }
116
117 /**
118 * Get the element at the given index from the vector and
119 * sets the state in holder. If element at given index
120 * is null, holder.isSet will be zero.
121 *
122 * @param index position of element
123 */
124 public void get(int index, NullableBigIntHolder holder) {
125 if (isSet(index) == 0) {
126 holder.isSet = 0;
127 return;
128 }
129 holder.isSet = 1;
130 holder.value = valueBuffer.getLong((long) index * TYPE_WIDTH);
131 }
132
133 /**
134 * Same as {@link #get(int)}.
135 *
136 * @param index position of element
137 * @return element at given index
138 */
139 public Long getObject(int index) {
140 if (isSet(index) == 0) {
141 return null;
142 } else {
143 return valueBuffer.getLong((long) index * TYPE_WIDTH);
144 }
145 }
146
147 /*----------------------------------------------------------------*
148 | |
149 | vector value setter methods |
150 | |
151 *----------------------------------------------------------------*/
152
153
154 private void setValue(int index, long value) {
155 valueBuffer.setLong((long) index * TYPE_WIDTH, value);
156 }
157
158 /**
159 * Set the element at the given index to the given value.
160 *
161 * @param index position of element
162 * @param value value of element
163 */
164 public void set(int index, long value) {
165 BitVectorHelper.setBit(validityBuffer, index);
166 setValue(index, value);
167 }
168
169 /**
170 * Set the element at the given index to the value set in data holder.
171 * If the value in holder is not indicated as set, element in the
172 * at the given index will be null.
173 *
174 * @param index position of element
175 * @param holder nullable data holder for value of element
176 */
177 public void set(int index, NullableBigIntHolder holder) throws IllegalArgumentException {
178 if (holder.isSet < 0) {
179 throw new IllegalArgumentException();
180 } else if (holder.isSet > 0) {
181 BitVectorHelper.setBit(validityBuffer, index);
182 setValue(index, holder.value);
183 } else {
184 BitVectorHelper.unsetBit(validityBuffer, index);
185 }
186 }
187
188 /**
189 * Set the element at the given index to the value set in data holder.
190 *
191 * @param index position of element
192 * @param holder data holder for value of element
193 */
194 public void set(int index, BigIntHolder holder) {
195 BitVectorHelper.setBit(validityBuffer, index);
196 setValue(index, holder.value);
197 }
198
199 /**
200 * Same as {@link #set(int, long)} except that it handles the
201 * case when index is greater than or equal to existing
202 * value capacity {@link #getValueCapacity()}.
203 *
204 * @param index position of element
205 * @param value value of element
206 */
207 public void setSafe(int index, long value) {
208 handleSafe(index);
209 set(index, value);
210 }
211
212 /**
213 * Same as {@link #set(int, NullableBigIntHolder)} except that it handles the
214 * case when index is greater than or equal to existing
215 * value capacity {@link #getValueCapacity()}.
216 *
217 * @param index position of element
218 * @param holder nullable data holder for value of element
219 */
220 public void setSafe(int index, NullableBigIntHolder holder) throws IllegalArgumentException {
221 handleSafe(index);
222 set(index, holder);
223 }
224
225 /**
226 * Same as {@link #set(int, BigIntHolder)} except that it handles the
227 * case when index is greater than or equal to existing
228 * value capacity {@link #getValueCapacity()}.
229 *
230 * @param index position of element
231 * @param holder data holder for value of element
232 */
233 public void setSafe(int index, BigIntHolder holder) {
234 handleSafe(index);
235 set(index, holder);
236 }
237
238 /**
239 * Store the given value at a particular position in the vector. isSet indicates
240 * whether the value is NULL or not.
241 * @param index position of the new value
242 * @param isSet 0 for NULL value, 1 otherwise
243 * @param value element value
244 */
245 public void set(int index, int isSet, long value) {
246 if (isSet > 0) {
247 set(index, value);
248 } else {
249 BitVectorHelper.unsetBit(validityBuffer, index);
250 }
251 }
252
253 /**
254 * Same as {@link #set(int, int, long)} except that it handles the case
255 * when index is greater than or equal to current value capacity of the
256 * vector.
257 * @param index position of the new value
258 * @param isSet 0 for NULL value, 1 otherwise
259 * @param value element value
260 */
261 public void setSafe(int index, int isSet, long value) {
262 handleSafe(index);
263 set(index, isSet, value);
264 }
265
266 /**
267 * Given a data buffer, get the value stored at a particular position
268 * in the vector.
269 *
270 * <p>This method should not be used externally.
271 *
272 * @param buffer data buffer
273 * @param index position of the element.
274 * @return value stored at the index.
275 */
276 public static long get(final ArrowBuf buffer, final int index) {
277 return buffer.getLong((long) index * TYPE_WIDTH);
278 }
279
280
281 /*----------------------------------------------------------------*
282 | |
283 | vector transfer |
284 | |
285 *----------------------------------------------------------------*/
286
287
288 /**
289 * Construct a TransferPair comprising of this and a target vector of
290 * the same type.
291 *
292 * @param ref name of the target vector
293 * @param allocator allocator for the target vector
294 * @return {@link TransferPair}
295 */
296 @Override
297 public TransferPair getTransferPair(String ref, BufferAllocator allocator) {
298 return new TransferImpl(ref, allocator);
299 }
300
301 /**
302 * Construct a TransferPair with a desired target vector of the same type.
303 *
304 * @param to target vector
305 * @return {@link TransferPair}
306 */
307 @Override
308 public TransferPair makeTransferPair(ValueVector to) {
309 return new TransferImpl((BigIntVector) to);
310 }
311
312 @Override
313 public void setWithPossibleTruncate(int index, long value) {
314 this.setSafe(index, value);
315 }
316
317 @Override
318 public void setUnsafeWithPossibleTruncate(int index, long value) {
319 this.set(index, value);
320 }
321
322 @Override
323 public long getValueAsLong(int index) {
324 return this.get(index);
325 }
326
327 private class TransferImpl implements TransferPair {
328 BigIntVector to;
329
330 public TransferImpl(String ref, BufferAllocator allocator) {
331 to = new BigIntVector(ref, field.getFieldType(), allocator);
332 }
333
334 public TransferImpl(BigIntVector to) {
335 this.to = to;
336 }
337
338 @Override
339 public BigIntVector getTo() {
340 return to;
341 }
342
343 @Override
344 public void transfer() {
345 transferTo(to);
346 }
347
348 @Override
349 public void splitAndTransfer(int startIndex, int length) {
350 splitAndTransferTo(startIndex, length, to);
351 }
352
353 @Override
354 public void copyValueSafe(int fromIndex, int toIndex) {
355 to.copyFromSafe(fromIndex, toIndex, BigIntVector.this);
356 }
357 }
358 }