Skip to content

Commit dd9bd6f

Browse files
wip
1 parent 6994cea commit dd9bd6f

File tree

5 files changed

+154
-58
lines changed

5 files changed

+154
-58
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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 org.apache.arrow.vector.types.pojo.Field;
23+
import org.apache.fory.format.row.binary.BinaryArray;
24+
import org.apache.fory.format.row.binary.writer.BinaryArrayWriter;
25+
import org.apache.fory.memory.MemoryBuffer;
26+
import org.apache.fory.memory.MemoryUtils;
27+
28+
class BeanArrayEncoder<T> implements ArrayEncoder<T> {
29+
private final BinaryArrayWriter writer;
30+
private final Field field;
31+
private final GeneratedArrayEncoder codec;
32+
33+
BeanArrayEncoder(BinaryArrayWriter writer, Field field,
34+
GeneratedArrayEncoder codec) {
35+
this.writer = writer;
36+
this.field = field;
37+
this.codec = codec;
38+
}
39+
40+
@Override
41+
public Field field() {
42+
return field;
43+
}
44+
45+
@SuppressWarnings("unchecked")
46+
@Override
47+
public T fromArray(BinaryArray array) {
48+
return (T) codec.fromArray(array);
49+
}
50+
51+
@Override
52+
public BinaryArray toArray(T obj) {
53+
return codec.toArray(obj);
54+
}
55+
56+
@Override
57+
public T decode(MemoryBuffer buffer) {
58+
return decode(buffer, buffer.readInt32());
59+
}
60+
61+
public T decode(MemoryBuffer buffer, int size) {
62+
BinaryArray array = new BinaryArray(field);
63+
int readerIndex = buffer.readerIndex();
64+
array.pointTo(buffer, readerIndex, size);
65+
buffer.readerIndex(readerIndex + size);
66+
return fromArray(array);
67+
}
68+
69+
@Override
70+
public T decode(byte[] bytes) {
71+
return decode(MemoryUtils.wrap(bytes), bytes.length);
72+
}
73+
74+
@Override
75+
public byte[] encode(T obj) {
76+
BinaryArray array = toArray(obj);
77+
return writer.getBuffer().getBytes(0, 8 + array.getSizeInBytes());
78+
}
79+
80+
@Override
81+
public void encode(MemoryBuffer buffer, T obj) {
82+
MemoryBuffer prevBuffer = writer.getBuffer();
83+
int writerIndex = buffer.writerIndex();
84+
buffer.writeInt32(-1);
85+
try {
86+
writer.setBuffer(buffer);
87+
BinaryArray array = toArray(obj);
88+
int size = buffer.writerIndex() - writerIndex - 4;
89+
assert size == array.getSizeInBytes();
90+
buffer.putInt32(writerIndex, size);
91+
} finally {
92+
writer.setBuffer(prevBuffer);
93+
}
94+
}
95+
}

java/fory-format/src/main/java/org/apache/fory/format/encoder/BeanCodecBuilder.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,28 @@
2020
package org.apache.fory.format.encoder;
2121

2222
import java.lang.reflect.Constructor;
23+
import java.util.Collection;
24+
import java.util.HashSet;
25+
import java.util.Set;
2326
import java.util.function.Function;
2427
import java.util.function.Supplier;
28+
29+
import org.apache.arrow.vector.types.pojo.Field;
2530
import org.apache.arrow.vector.types.pojo.Schema;
2631
import org.apache.fory.Fory;
2732
import org.apache.fory.builder.CodecBuilder;
2833
import org.apache.fory.format.row.binary.BinaryRow;
2934
import org.apache.fory.format.row.binary.CompactBinaryRow;
3035
import org.apache.fory.format.row.binary.writer.BaseBinaryRowWriter;
36+
import org.apache.fory.format.row.binary.writer.BinaryArrayWriter;
3137
import org.apache.fory.format.row.binary.writer.BinaryRowWriter;
3238
import org.apache.fory.format.row.binary.writer.CompactBinaryRowWriter;
39+
import org.apache.fory.format.type.DataTypes;
3340
import org.apache.fory.format.type.TypeInference;
3441
import org.apache.fory.memory.MemoryBuffer;
42+
import org.apache.fory.reflect.TypeRef;
43+
44+
import static org.apache.fory.type.TypeUtils.getRawType;
3545

3646
public class BeanCodecBuilder<T> {
3747

@@ -112,6 +122,25 @@ public RowEncoder<T> apply(final BaseBinaryRowWriter writer) {
112122
};
113123
}
114124

125+
public <C extends Collection<T>> Supplier<ArrayEncoder<T>> arrayEncoder(final TypeRef<T> token, final Fory fory) {
126+
final Schema schema = TypeInference.inferSchema(token, false);
127+
final Field field = DataTypes.fieldOfSchema(schema, 0);
128+
final BinaryArrayWriter writer = new BinaryArrayWriter(field);
129+
130+
final Set<TypeRef<?>> set = new HashSet<>();
131+
findBeanToken(token, set);
132+
if (set.isEmpty()) {
133+
throw new IllegalArgumentException("can not find bean class.");
134+
}
135+
136+
TypeRef<?> typeRef = null;
137+
for (final TypeRef<?> tt : set) {
138+
typeRef = set.iterator().next();
139+
Encoders.loadOrGenRowCodecClass(getRawType(tt));
140+
}
141+
return arrayEncoder(token, typeRef, writer, fory);
142+
}
143+
115144
Function<BaseBinaryRowWriter, GeneratedRowEncoder> codecFactory() {
116145
final Class<?> rowCodecClass = Encoders.loadOrGenRowCodecClass(beanClass, codecFactory);
117146
Constructor<? extends GeneratedRowEncoder> constructor;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.apache.fory.format.encoder;
2+
3+
import org.apache.fory.reflect.TypeRef;
4+
5+
class CompactArrayEncoderBuilder extends ArrayEncoderBuilder {
6+
public CompactArrayEncoderBuilder(final TypeRef<?> clsType, final TypeRef<?> beanType) {
7+
super(clsType, beanType);
8+
}
9+
10+
@Override
11+
protected String codecSuffix() {
12+
return "CompactCodec";
13+
}
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.apache.fory.format.encoder;
2+
3+
import org.apache.fory.reflect.TypeRef;
4+
5+
class CompactMapEncoderBuilder extends MapEncoderBuilder {
6+
7+
public CompactMapEncoderBuilder(final TypeRef<?> clsType, final TypeRef<?> beanType) {
8+
super(clsType, beanType);
9+
}
10+
11+
@Override
12+
protected String codecSuffix() {
13+
return "CompactCodec";
14+
}
15+
}

java/fory-format/src/main/java/org/apache/fory/format/encoder/Encoders.java

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -214,64 +214,7 @@ public static <T extends Collection, B> ArrayEncoder<T> arrayEncoder(
214214
.getConstructor(Object[].class)
215215
.newInstance(references);
216216

217-
return new ArrayEncoder<T>() {
218-
219-
@Override
220-
public Field field() {
221-
return field;
222-
}
223-
224-
@SuppressWarnings("unchecked")
225-
@Override
226-
public T fromArray(BinaryArray array) {
227-
return (T) codec.fromArray(array);
228-
}
229-
230-
@Override
231-
public BinaryArray toArray(T obj) {
232-
return codec.toArray(obj);
233-
}
234-
235-
@Override
236-
public T decode(MemoryBuffer buffer) {
237-
return decode(buffer, buffer.readInt32());
238-
}
239-
240-
public T decode(MemoryBuffer buffer, int size) {
241-
BinaryArray array = new BinaryArray(field);
242-
int readerIndex = buffer.readerIndex();
243-
array.pointTo(buffer, readerIndex, size);
244-
buffer.readerIndex(readerIndex + size);
245-
return fromArray(array);
246-
}
247-
248-
@Override
249-
public T decode(byte[] bytes) {
250-
return decode(MemoryUtils.wrap(bytes), bytes.length);
251-
}
252-
253-
@Override
254-
public byte[] encode(T obj) {
255-
BinaryArray array = toArray(obj);
256-
return writer.getBuffer().getBytes(0, 8 + array.getSizeInBytes());
257-
}
258-
259-
@Override
260-
public void encode(MemoryBuffer buffer, T obj) {
261-
MemoryBuffer prevBuffer = writer.getBuffer();
262-
int writerIndex = buffer.writerIndex();
263-
buffer.writeInt32(-1);
264-
try {
265-
writer.setBuffer(buffer);
266-
BinaryArray array = toArray(obj);
267-
int size = buffer.writerIndex() - writerIndex - 4;
268-
assert size == array.getSizeInBytes();
269-
buffer.putInt32(writerIndex, size);
270-
} finally {
271-
writer.setBuffer(prevBuffer);
272-
}
273-
}
274-
};
217+
return new BeanArrayEncoder<T>(writer, field, codec);
275218
} catch (Exception e) {
276219
String msg = String.format("Create encoder failed, \nelementType: %s", elementType);
277220
throw new EncoderException(msg, e);

0 commit comments

Comments
 (0)