Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix union serialization (huge performance improvement) #174

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public static int resolveUnionIndex(Schema unionSchema, Object datum) {
// - ByteBuffer
// - Number wrappers (Integer, ...)

/*

String typeId = AvroSchemaHelper.getTypeId(datum.getClass());
for (int i = 0, size = types.size(); i < size; ++i) {
Schema schema = types.get(i);
Expand All @@ -296,7 +296,6 @@ public static int resolveUnionIndex(Schema unionSchema, Object datum) {
return i;
}
}
*/
}
//System.err.println("Missing index for: "+datum.getClass().getName()+" ("+types.size()+") ->\n"+types);
return ReflectData.get().resolveUnion(unionSchema, datum);
Expand Down Expand Up @@ -338,7 +337,7 @@ public static Schema resolveUnionType(Schema unionSchema, Object datum) {
return types.get(_resolveMapIndex(unionSchema, types, datum));
}

/*

String typeId = AvroSchemaHelper.getTypeId(datum.getClass());
for (int i = 0, size = types.size(); i < size; ++i) {
Schema schema = types.get(i);
Expand All @@ -347,7 +346,6 @@ public static Schema resolveUnionType(Schema unionSchema, Object datum) {
return schema;
}
}
*/
}
//System.err.println("Missing schema for: "+datum.getClass().getName()+" ("+types.size()+") ->\n"+types);
int ix = ReflectData.get().resolveUnion(unionSchema, datum);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.fasterxml.jackson.dataformat.avro.interop.annotations;

import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import org.apache.avro.Schema;
import org.apache.avro.SchemaBuilder;
import org.apache.avro.reflect.Union;
import org.junit.Test;

import static com.fasterxml.jackson.dataformat.avro.interop.ApacheAvroInteropUtil.jacksonDeserialize;
import static com.fasterxml.jackson.dataformat.avro.interop.ApacheAvroInteropUtil.jacksonSerialize;
import static org.assertj.core.api.Assertions.assertThat;

public class UnionResolvingTest {
private Schema SCHEMA = SchemaBuilder
.builder(UnionResolvingTest.class.getName() + "$")
.record(Box.class.getSimpleName())
.fields()
.name("animal")
.type()
.unionOf()
.record(Cat.class.getSimpleName())
.fields()
.name("name").type().stringType().noDefault()
.endRecord()
.and()
.record(Dog.class.getSimpleName())
.fields()
.name("tricks").type().array().items().stringType().noDefault()
.endRecord()
.endUnion()
.noDefault()
.endRecord();


@Union({ Cat.class, Dog.class })
public interface Animal { }

static final class Cat implements Animal {
public Cat() {}

@Override
public boolean equals(Object o) {
return o instanceof Cat;
}
}

static final class Dog implements Animal {
// Unsupported schema generation
public Set<?> tricks = new HashSet<>();

public Dog() {
}

public Dog(final Set<?> tricks) {
this.tricks = tricks;
}

@Override
public boolean equals(Object o) {
return o instanceof Dog && tricks.equals(((Dog) o).tricks);
}
}

public static class Box {
public Animal animal;

public Box() {
}

public Box(Animal a) { animal = a; }

@Override
public boolean equals(Object o) {
return o instanceof Box && Objects.equals(animal, ((Box) o).animal);
}
}

@Test
public void testResolveUnionUsingSchemaName() throws IOException {
final Box box = new Box(new Dog(Collections.singleton("catch stick")));

final Box result = jacksonDeserialize(SCHEMA, Box.class, jacksonSerialize(SCHEMA, box));

assertThat(result).isEqualTo(box);
}
}