|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.fory.format.encoder; |
| 21 | + |
| 22 | +import static org.apache.fory.type.TypeUtils.getRawType; |
| 23 | + |
| 24 | +import java.lang.reflect.Constructor; |
| 25 | +import java.util.Collection; |
| 26 | +import java.util.HashSet; |
| 27 | +import java.util.Set; |
| 28 | +import java.util.function.Function; |
| 29 | +import java.util.function.Supplier; |
| 30 | +import org.apache.arrow.vector.types.pojo.Field; |
| 31 | +import org.apache.arrow.vector.types.pojo.Schema; |
| 32 | +import org.apache.fory.format.row.binary.writer.BinaryArrayWriter; |
| 33 | +import org.apache.fory.format.type.DataTypes; |
| 34 | +import org.apache.fory.format.type.TypeInference; |
| 35 | +import org.apache.fory.reflect.TypeRef; |
| 36 | +import org.apache.fory.type.TypeUtils; |
| 37 | + |
| 38 | +public class ArrayCodecBuilder<C extends Collection<?>> |
| 39 | + extends BaseCodecBuilder<ArrayCodecBuilder<C>> { |
| 40 | + |
| 41 | + private final TypeRef<C> collectionType; |
| 42 | + |
| 43 | + ArrayCodecBuilder(final TypeRef<C> collectionType) { |
| 44 | + super(TypeInference.inferSchema(collectionType, false)); |
| 45 | + this.collectionType = collectionType; |
| 46 | + } |
| 47 | + |
| 48 | + public Supplier<ArrayEncoder<C>> buildForArray() { |
| 49 | + final Function<BinaryArrayWriter, ArrayEncoder<C>> arrayEncoderFactory = |
| 50 | + buildForArrayWithCustomWriter(); |
| 51 | + final Schema collectionSchema = TypeInference.inferSchema(collectionType, false); |
| 52 | + final Field field = DataTypes.fieldOfSchema(collectionSchema, 0); |
| 53 | + return new Supplier<ArrayEncoder<C>>() { |
| 54 | + @Override |
| 55 | + public ArrayEncoder<C> get() { |
| 56 | + final BinaryArrayWriter writer = codecFactory.newArrayWriter(field); |
| 57 | + return arrayEncoderFactory.apply(writer); |
| 58 | + } |
| 59 | + }; |
| 60 | + } |
| 61 | + |
| 62 | + public Function<BinaryArrayWriter, ArrayEncoder<C>> buildForArrayWithCustomWriter() { |
| 63 | + loadArrayInnerCodecs(collectionType); |
| 64 | + final Function<BinaryArrayWriter, GeneratedArrayEncoder> arrayEncoderFactory = |
| 65 | + arrayEncoderFactory(collectionType); |
| 66 | + return new Function<BinaryArrayWriter, ArrayEncoder<C>>() { |
| 67 | + @Override |
| 68 | + public ArrayEncoder<C> apply(final BinaryArrayWriter writer) { |
| 69 | + return new BeanArrayEncoder<>(writer, writer.getField(), arrayEncoderFactory.apply(writer)); |
| 70 | + } |
| 71 | + }; |
| 72 | + } |
| 73 | + |
| 74 | + private void loadArrayInnerCodecs(final TypeRef<?> collectionType) { |
| 75 | + final Set<TypeRef<?>> set = new HashSet<>(); |
| 76 | + Encoders.findBeanToken(collectionType, set); |
| 77 | + if (set.isEmpty()) { |
| 78 | + throw new IllegalArgumentException("can not find bean class."); |
| 79 | + } |
| 80 | + |
| 81 | + TypeRef<?> typeRef = null; |
| 82 | + for (final TypeRef<?> tt : set) { |
| 83 | + typeRef = set.iterator().next(); |
| 84 | + Encoders.loadOrGenRowCodecClass(getRawType(tt), codecFactory); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + Function<BinaryArrayWriter, GeneratedArrayEncoder> arrayEncoderFactory( |
| 89 | + final TypeRef<? extends Collection<?>> collectionType) { |
| 90 | + final TypeRef<?> elementType = TypeUtils.getElementType(collectionType); |
| 91 | + final Class<?> arrayCodecClass = |
| 92 | + Encoders.loadOrGenArrayCodecClass(collectionType, elementType, codecFactory); |
| 93 | + |
| 94 | + Constructor<? extends GeneratedArrayEncoder> constructor; |
| 95 | + try { |
| 96 | + constructor = |
| 97 | + arrayCodecClass.asSubclass(GeneratedArrayEncoder.class).getConstructor(Object[].class); |
| 98 | + } catch (final NoSuchMethodException e) { |
| 99 | + throw new EncoderException( |
| 100 | + "Failed to construct array codec for " |
| 101 | + + collectionType |
| 102 | + + " with element class " |
| 103 | + + elementType, |
| 104 | + e); |
| 105 | + } |
| 106 | + return new Function<BinaryArrayWriter, GeneratedArrayEncoder>() { |
| 107 | + @Override |
| 108 | + public GeneratedArrayEncoder apply(final BinaryArrayWriter writer) { |
| 109 | + final Field field = writer.getField(); |
| 110 | + final Object references = new Object[] {field, writer, fory}; |
| 111 | + try { |
| 112 | + return constructor.newInstance(references); |
| 113 | + } catch (final ReflectiveOperationException e) { |
| 114 | + throw new EncoderException( |
| 115 | + "Failed to construct array codec for " |
| 116 | + + collectionType |
| 117 | + + " with element class " |
| 118 | + + elementType, |
| 119 | + e); |
| 120 | + } |
| 121 | + } |
| 122 | + }; |
| 123 | + } |
| 124 | +} |
0 commit comments