Skip to content

Commit 0723dfa

Browse files
committed
Fix #82
1 parent feb9696 commit 0723dfa

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

avro/src/main/java/com/fasterxml/jackson/dataformat/avro/schema/AvroSchemaHelper.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,6 @@ protected static <T> T throwUnsupported() {
213213
throw new UnsupportedOperationException("Format variation not supported");
214214
}
215215

216-
public static String getTypeId(JavaType type) {
217-
return getTypeId(type.getRawClass());
218-
}
219-
220216
/**
221217
* Initializes a record schema with metadata from the given class; this schema is returned in a non-finalized state, and still
222218
* needs to have fields added to it.
@@ -267,6 +263,10 @@ public static Schema addAlias(Schema schema, BeanDescription bean) {
267263
return schema;
268264
}
269265

266+
public static String getTypeId(JavaType type) {
267+
return getTypeId(type.getRawClass());
268+
}
269+
270270
/**
271271
* Returns the Avro type ID for a given type
272272
*/
@@ -290,7 +290,6 @@ public static String getTypeId(Schema schema) {
290290
default:
291291
return schema.getProp(AVRO_SCHEMA_PROP_CLASS);
292292
}
293-
294293
}
295294

296295
/**

avro/src/main/java/com/fasterxml/jackson/dataformat/avro/ser/AvroWriteContext.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,14 @@ public static int resolveUnionIndex(Schema unionSchema, Object datum) {
252252
if (raw == CLS_STRING) {
253253
return _resolveStringIndex(unionSchema, types, (String) datum);
254254
}
255+
// 26-Apr-2017, tatu: This may look odd optimization, but turns out that:
256+
// (a) case of "null and ONE other type" is VERY common, and
257+
// (b) cost of real lookup for POJO types is VERY expensive (due to elaborate
258+
// caching Avro lib does
259+
int ix = _findNotNullIndex(types);
260+
if (ix >= 0) {
261+
return ix;
262+
}
255263
if (raw == CLS_BIG_DECIMAL) {
256264
return _resolveBigDecimalIndex(unionSchema, types, (BigDecimal) datum);
257265
}
@@ -280,7 +288,7 @@ public static int resolveUnionIndex(Schema unionSchema, Object datum) {
280288
}
281289
*/
282290
}
283-
//System.out.println("Union type for: "+datum.getClass());
291+
//System.err.println("Missing index for: "+datum.getClass().getName()+" ("+types.size()+") ->\n"+types);
284292
return ReflectData.get().resolveUnion(unionSchema, datum);
285293
}
286294

@@ -299,6 +307,14 @@ public static Schema resolveUnionType(Schema unionSchema, Object datum) {
299307
if (raw == CLS_STRING) {
300308
return types.get(_resolveStringIndex(unionSchema, types, (String) datum));
301309
}
310+
// 26-Apr-2017, tatu: This may look odd optimization, but turns out that:
311+
// (a) case of "null and ONE other type" is VERY common, and
312+
// (b) cost of real lookup for POJO types is VERY expensive (due to elaborate
313+
// caching Avro lib does
314+
Schema sch = _findNotNull(types);
315+
if (sch != null) {
316+
return sch;
317+
}
302318
if (raw == CLS_BIG_DECIMAL) {
303319
return types.get(_resolveBigDecimalIndex(unionSchema, types, (BigDecimal) datum));
304320
}
@@ -323,6 +339,7 @@ public static Schema resolveUnionType(Schema unionSchema, Object datum) {
323339
}
324340
*/
325341
}
342+
//System.err.println("Missing schema for: "+datum.getClass().getName()+" ("+types.size()+") ->\n"+types);
326343
int ix = ReflectData.get().resolveUnion(unionSchema, datum);
327344
return types.get(ix);
328345
}
@@ -359,6 +376,32 @@ private static int _resolveStringIndex(Schema unionSchema, List<Schema> types,
359376
return ReflectData.get().resolveUnion(unionSchema, value);
360377
}
361378

379+
private static Schema _findNotNull(List<Schema> types)
380+
{
381+
if (types.size() == 2) {
382+
if (types.get(0).getType() == Type.NULL) {
383+
return types.get(1);
384+
}
385+
if (types.get(1).getType() == Type.NULL) {
386+
return types.get(0);
387+
}
388+
}
389+
return null;
390+
}
391+
392+
private static int _findNotNullIndex(List<Schema> types)
393+
{
394+
if (types.size() == 2) {
395+
if (types.get(0).getType() == Type.NULL) {
396+
return 1;
397+
}
398+
if (types.get(1).getType() == Type.NULL) {
399+
return 0;
400+
}
401+
}
402+
return -1;
403+
}
404+
362405
private static int _resolveBigDecimalIndex(Schema unionSchema, List<Schema> types,
363406
BigDecimal value) {
364407
int match = -1;

0 commit comments

Comments
 (0)