|
| 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 | +package org.apache.arrow.adapter.avro.consumers.logical; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.nio.ByteBuffer; |
| 21 | +import org.apache.arrow.adapter.avro.consumers.BaseAvroConsumer; |
| 22 | +import org.apache.arrow.util.Preconditions; |
| 23 | +import org.apache.arrow.vector.Decimal256Vector; |
| 24 | +import org.apache.avro.io.Decoder; |
| 25 | + |
| 26 | +/** |
| 27 | + * Consumer which consume 256-bit decimal type values from avro decoder. Write the data to {@link |
| 28 | + * Decimal256Vector}. |
| 29 | + */ |
| 30 | +public abstract class AvroDecimal256Consumer extends BaseAvroConsumer<Decimal256Vector> { |
| 31 | + |
| 32 | + protected AvroDecimal256Consumer(Decimal256Vector vector) { |
| 33 | + super(vector); |
| 34 | + } |
| 35 | + |
| 36 | + /** Consumer for decimal logical type with 256 bit width and original bytes type. */ |
| 37 | + public static class BytesDecimal256Consumer extends AvroDecimal256Consumer { |
| 38 | + |
| 39 | + private ByteBuffer cacheBuffer; |
| 40 | + |
| 41 | + /** Instantiate a BytesDecimal256Consumer. */ |
| 42 | + public BytesDecimal256Consumer(Decimal256Vector vector) { |
| 43 | + super(vector); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void consume(Decoder decoder) throws IOException { |
| 48 | + cacheBuffer = decoder.readBytes(cacheBuffer); |
| 49 | + byte[] bytes = new byte[cacheBuffer.limit()]; |
| 50 | + Preconditions.checkArgument(bytes.length <= 32, "Decimal bytes length should <= 32."); |
| 51 | + cacheBuffer.get(bytes); |
| 52 | + vector.setBigEndian(currentIndex++, bytes); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** Consumer for decimal logical type with 256 bit width and original fixed type. */ |
| 57 | + public static class FixedDecimal256Consumer extends AvroDecimal256Consumer { |
| 58 | + |
| 59 | + private final byte[] reuseBytes; |
| 60 | + |
| 61 | + /** Instantiate a FixedDecimal256Consumer. */ |
| 62 | + public FixedDecimal256Consumer(Decimal256Vector vector, int size) { |
| 63 | + super(vector); |
| 64 | + Preconditions.checkArgument(size <= 32, "Decimal bytes length should <= 32."); |
| 65 | + reuseBytes = new byte[size]; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void consume(Decoder decoder) throws IOException { |
| 70 | + decoder.readFixed(reuseBytes); |
| 71 | + vector.setBigEndian(currentIndex++, reuseBytes); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments