]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/java/vector/src/main/java/org/apache/arrow/vector/UInt4Vector.java
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / java / vector / src / main / java / org / apache / arrow / vector / UInt4Vector.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.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;
33
34 /**
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.
38 */
39 public final class UInt4Vector extends BaseFixedWidthVector implements BaseIntVector {
40
41 /**
42 * The mask to use when promoting the unsigned int value to a long int.
43 */
44 public static final long PROMOTION_MASK = 0x00000000FFFFFFFFL;
45
46 /**
47 * The maximum 32-bit unsigned integer.
48 */
49 public static final int MAX_UINT4 = 0XFFFFFFFF;
50
51 public static final byte TYPE_WIDTH = 4;
52 private final FieldReader reader;
53
54 public UInt4Vector(String name, BufferAllocator allocator) {
55 this(name, FieldType.nullable(MinorType.UINT4.getType()), allocator);
56 }
57
58 public UInt4Vector(String name, FieldType fieldType, BufferAllocator allocator) {
59 this(new Field(name, fieldType, null), allocator);
60 }
61
62 public UInt4Vector(Field field, BufferAllocator allocator) {
63 super(field, allocator, TYPE_WIDTH);
64 reader = new UInt4ReaderImpl(UInt4Vector.this);
65 }
66
67 @Override
68 public FieldReader getReader() {
69 return reader;
70 }
71
72 @Override
73 public MinorType getMinorType() {
74 return MinorType.UINT4;
75 }
76
77
78 /*----------------------------------------------------------------*
79 | |
80 | vector value retrieval methods |
81 | |
82 *----------------------------------------------------------------*/
83 /**
84 * Given a data buffer, get the value stored at a particular position
85 * in the vector.
86 *
87 * <p>To avoid overflow, the returned type is one step up from the signed
88 * type.
89 *
90 * <p>This method is mainly meant for integration tests.
91 *
92 * @param buffer data buffer
93 * @param index position of the element.
94 * @return value stored at the index.
95 */
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;
99 }
100
101 /**
102 * Get the element at the given index from the vector.
103 *
104 * @param index position of element
105 * @return element at given index
106 */
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");
110 }
111 return valueBuffer.getInt((long) index * TYPE_WIDTH);
112 }
113
114 /**
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.
118 *
119 * @param index position of element
120 */
121 public void get(int index, NullableUInt4Holder holder) {
122 if (isSet(index) == 0) {
123 holder.isSet = 0;
124 return;
125 }
126 holder.isSet = 1;
127 holder.value = valueBuffer.getInt((long) index * TYPE_WIDTH);
128 }
129
130 /**
131 * Same as {@link #get(int)}.
132 *
133 * @param index position of element
134 * @return element at given index
135 */
136 public Integer getObject(int index) {
137 if (isSet(index) == 0) {
138 return null;
139 } else {
140 return valueBuffer.getInt((long) index * TYPE_WIDTH);
141 }
142 }
143
144 /**
145 * Same as {@link #get(int)}.
146 *
147 * @param index position of element
148 * @return element at given index
149 */
150 public Long getObjectNoOverflow(int index) {
151 if (isSet(index) == 0) {
152 return null;
153 } else {
154 return getNoOverflow(valueBuffer, index);
155 }
156 }
157
158
159 /*----------------------------------------------------------------*
160 | |
161 | vector value setter methods |
162 | |
163 *----------------------------------------------------------------*/
164
165
166 private void setValue(int index, int value) {
167 valueBuffer.setInt((long) index * TYPE_WIDTH, value);
168 }
169
170 /**
171 * Set the element at the given index to the given value.
172 *
173 * @param index position of element
174 * @param value value of element
175 */
176 public void set(int index, int value) {
177 BitVectorHelper.setBit(validityBuffer, index);
178 setValue(index, value);
179 }
180
181 /**
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.
185 *
186 * @param index position of element
187 * @param holder nullable data holder for value of element
188 */
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);
195 } else {
196 BitVectorHelper.unsetBit(validityBuffer, index);
197 }
198 }
199
200 /**
201 * Set the element at the given index to the value set in data holder.
202 *
203 * @param index position of element
204 * @param holder data holder for value of element
205 */
206 public void set(int index, UInt4Holder holder) {
207 BitVectorHelper.setBit(validityBuffer, index);
208 setValue(index, holder.value);
209 }
210
211 /**
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()}.
215 *
216 * @param index position of element
217 * @param value value of element
218 */
219 public void setSafe(int index, int value) {
220 handleSafe(index);
221 set(index, value);
222 }
223
224 /**
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()}.
228 *
229 * @param index position of element
230 * @param holder nullable data holder for value of element
231 */
232 public void setSafe(int index, NullableUInt4Holder holder) throws IllegalArgumentException {
233 handleSafe(index);
234 set(index, holder);
235 }
236
237 /**
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()}.
241 *
242 * @param index position of element
243 * @param holder data holder for value of element
244 */
245 public void setSafe(int index, UInt4Holder holder) {
246 handleSafe(index);
247 set(index, holder);
248 }
249
250 /**
251 * Sets the value at index to value isSet > 0, otherwise sets the index position
252 * to invalid/null.
253 */
254 public void set(int index, int isSet, int value) {
255 if (isSet > 0) {
256 set(index, value);
257 } else {
258 BitVectorHelper.unsetBit(validityBuffer, index);
259 }
260 }
261
262 /**
263 * Same as {@link #set(int, int, int)} but will reallocate if the buffer if index
264 * is larger than the current capacity.
265 */
266 public void setSafe(int index, int isSet, int value) {
267 handleSafe(index);
268 set(index, isSet, value);
269 }
270
271
272 /*----------------------------------------------------------------*
273 | |
274 | vector transfer |
275 | |
276 *----------------------------------------------------------------*/
277
278
279 @Override
280 public TransferPair getTransferPair(String ref, BufferAllocator allocator) {
281 return new TransferImpl(ref, allocator);
282 }
283
284 @Override
285 public TransferPair makeTransferPair(ValueVector to) {
286 return new TransferImpl((UInt4Vector) to);
287 }
288
289 @Override
290 public void setWithPossibleTruncate(int index, long value) {
291 this.setSafe(index, (int) value);
292 }
293
294 @Override
295 public void setUnsafeWithPossibleTruncate(int index, long value) {
296 this.set(index, (int) value);
297 }
298
299 @Override
300 public long getValueAsLong(int index) {
301 return this.get(index) & PROMOTION_MASK;
302 }
303
304 @Override
305 public String toString() {
306 return ValueVectorUtility.getToString(this, 0, getValueCount(), (v, i) -> v.getObjectNoOverflow(i));
307 }
308
309 private class TransferImpl implements TransferPair {
310 UInt4Vector to;
311
312 public TransferImpl(String ref, BufferAllocator allocator) {
313 to = new UInt4Vector(ref, field.getFieldType(), allocator);
314 }
315
316 public TransferImpl(UInt4Vector to) {
317 this.to = to;
318 }
319
320 @Override
321 public UInt4Vector getTo() {
322 return to;
323 }
324
325 @Override
326 public void transfer() {
327 transferTo(to);
328 }
329
330 @Override
331 public void splitAndTransfer(int startIndex, int length) {
332 splitAndTransferTo(startIndex, length, to);
333 }
334
335 @Override
336 public void copyValueSafe(int fromIndex, int toIndex) {
337 to.copyFromSafe(fromIndex, toIndex, UInt4Vector.this);
338 }
339 }
340 }