Skip to content

Commit 90670ff

Browse files
authored
caching CSV schemas with view (#297)
1 parent c2a35bf commit 90670ff

File tree

2 files changed

+89
-27
lines changed

2 files changed

+89
-27
lines changed

csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvMapper.java

+61-21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.fasterxml.jackson.dataformat.csv;
22

33
import java.util.Collection;
4+
import java.util.Objects;
45

56
import com.fasterxml.jackson.core.type.TypeReference;
67
import com.fasterxml.jackson.databind.*;
@@ -86,17 +87,62 @@ public Builder configure(CsvGenerator.Feature f, boolean state)
8687
}
8788
}
8889

90+
91+
/**
92+
* Simple class in order to create a map key based on {@link JavaType} and a given view.
93+
* Used for caching associated schemas in {@code _untypedSchemas} and {@code _typedSchemas}.
94+
*/
95+
public static final class ViewKey
96+
implements java.io.Serializable
97+
{
98+
private static final long serialVersionUID = 1L;
99+
100+
private final JavaType _pojoType;
101+
private final Class<?> _view;
102+
private final int _hashCode;
103+
104+
105+
public ViewKey(final JavaType pojoType, final Class<?> view)
106+
{
107+
_pojoType = pojoType;
108+
_view = view;
109+
_hashCode = Objects.hash(pojoType, view);
110+
}
111+
112+
113+
@Override
114+
public int hashCode() { return _hashCode; }
115+
116+
@Override
117+
public boolean equals(final Object o)
118+
{
119+
if (o == this) { return true; }
120+
if (o == null || o.getClass() != getClass()) { return false; }
121+
final ViewKey other = (ViewKey) o;
122+
if (_hashCode != other._hashCode || _view != other._view) { return false; }
123+
return Objects.equals(_pojoType, other._pojoType);
124+
}
125+
126+
@Override
127+
public String toString()
128+
{
129+
String viewName = _view != null ? _view.getName() : null;
130+
return "[ViewKey: pojoType=" + _pojoType + ", view=" + viewName + "]";
131+
}
132+
}
133+
134+
89135
/**
90136
* Simple caching for schema instances, given that they are relatively expensive
91137
* to construct; this one is for "loose" (non-typed) schemas
92138
*/
93-
protected final LRUMap<JavaType,CsvSchema> _untypedSchemas;
139+
protected final LRUMap<ViewKey,CsvSchema> _untypedSchemas;
94140

95141
/**
96142
* Simple caching for schema instances, given that they are relatively expensive
97143
* to construct; this one is for typed schemas
98144
*/
99-
protected final LRUMap<JavaType,CsvSchema> _typedSchemas;
145+
protected final LRUMap<ViewKey,CsvSchema> _typedSchemas;
100146

101147
/*
102148
/**********************************************************************
@@ -114,8 +160,8 @@ public CsvMapper(CsvFactory f)
114160
super(f);
115161
// As per #11: default to alphabetic ordering
116162
enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY);
117-
_untypedSchemas = new LRUMap<JavaType,CsvSchema>(8,32);
118-
_typedSchemas = new LRUMap<JavaType,CsvSchema>(8,32);
163+
_untypedSchemas = new LRUMap<ViewKey,CsvSchema>(8,32);
164+
_typedSchemas = new LRUMap<ViewKey,CsvSchema>(8,32);
119165
}
120166

121167
/**
@@ -128,8 +174,8 @@ public CsvMapper(CsvFactory f)
128174
protected CsvMapper(CsvMapper src)
129175
{
130176
super(src);
131-
_untypedSchemas = new LRUMap<JavaType,CsvSchema>(8,32);
132-
_typedSchemas = new LRUMap<JavaType,CsvSchema>(8,32);
177+
_untypedSchemas = new LRUMap<ViewKey,CsvSchema>(8,32);
178+
_typedSchemas = new LRUMap<ViewKey,CsvSchema>(8,32);
133179
}
134180

135181
/**
@@ -422,34 +468,28 @@ public final CsvSchema typedSchemaForWithView(TypeReference<?> pojoTypeRef, Clas
422468
/**********************************************************************
423469
*/
424470

425-
protected CsvSchema _schemaFor(JavaType pojoType, LRUMap<JavaType,CsvSchema> schemas,
471+
protected CsvSchema _schemaFor(JavaType pojoType, LRUMap<ViewKey,CsvSchema> schemas,
426472
boolean typed, Class<?> view)
427473
{
428-
// 15-Dec-2021, tatu: [dataformats-text#288] Only cache if we don't have
429-
// a view, to avoid conflicts. For now. May be improved by changing cache
430-
// key if that is considered a performance problem.
431-
if (view == null) {
432-
synchronized (schemas) {
433-
CsvSchema s = schemas.get(pojoType);
434-
if (s != null) {
435-
return s;
436-
}
474+
final ViewKey viewKey = new ViewKey(pojoType, view);
475+
synchronized (schemas) {
476+
CsvSchema s = schemas.get(viewKey);
477+
if (s != null) {
478+
return s;
437479
}
438480
}
439481
final AnnotationIntrospector intr = _deserializationConfig.getAnnotationIntrospector();
440482
CsvSchema.Builder builder = CsvSchema.builder();
441483
_addSchemaProperties(builder, intr, typed, pojoType, null, view);
442484
CsvSchema result = builder.build();
443-
if (view == null) { // only cache without view (see above)
444-
synchronized (schemas) {
445-
schemas.put(pojoType, result);
446-
}
485+
synchronized (schemas) {
486+
schemas.put(viewKey, result);
447487
}
448488
return result;
449489
}
450490

451491
@Deprecated // since 2.11 (remove from 3.0 at latest)
452-
protected CsvSchema _schemaFor(JavaType pojoType, LRUMap<JavaType,CsvSchema> schemas, boolean typed) {
492+
protected CsvSchema _schemaFor(JavaType pojoType, LRUMap<ViewKey,CsvSchema> schemas, boolean typed) {
453493
return _schemaFor(pojoType, schemas, typed, null);
454494
}
455495

csv/src/test/java/com/fasterxml/jackson/dataformat/csv/schema/SchemaCaching288Test.java renamed to csv/src/test/java/com/fasterxml/jackson/dataformat/csv/SchemaCaching288Test.java

+28-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.fasterxml.jackson.dataformat.csv.schema;
1+
package com.fasterxml.jackson.dataformat.csv;
22

33
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
44
import com.fasterxml.jackson.annotation.JsonView;
@@ -10,17 +10,15 @@
1010
public class SchemaCaching288Test extends ModuleTestBase
1111
{
1212
static class ViewA { }
13-
static class ViewAA extends ViewA { }
1413
static class ViewB { }
15-
static class ViewBB extends ViewB { }
1614

1715
@JsonPropertyOrder({ "a", "aa", "b" })
1816
static class Bean288
1917
{
2018
@JsonView({ ViewA.class, ViewB.class })
2119
public String a = "1";
2220

23-
@JsonView({ViewAA.class })
21+
@JsonView({ViewA.class })
2422
public String aa = "2";
2523

2624
@JsonView(ViewB.class)
@@ -40,21 +38,45 @@ public void testCachingNoViewFirst() throws Exception
4038
CsvSchema schemaNoView = mapper1.schemaFor(Bean288.class);
4139
assertEquals("1,2,3",
4240
mapper1.writer(schemaNoView).writeValueAsString(new Bean288()).trim());
41+
assertEquals(1, mapper1._untypedSchemas.size());
4342

4443
CsvSchema schemaB = mapper1.schemaForWithView(Bean288.class, ViewB.class);
4544
assertEquals("1,3", mapper1.writer(schemaB).withView(ViewB.class)
4645
.writeValueAsString(new Bean288()).trim());
46+
assertEquals(2, mapper1._untypedSchemas.size());
47+
48+
// check hash
49+
mapper1.schemaFor(Bean288.class);
50+
assertEquals(2, mapper1._untypedSchemas.size());
51+
mapper1.schemaForWithView(Bean288.class, ViewA.class);
52+
assertEquals(3, mapper1._untypedSchemas.size());
53+
mapper1.schemaForWithView(Bean288.class, ViewB.class);
54+
assertEquals(3, mapper1._untypedSchemas.size());
55+
4756
}
4857

4958
// [dataformats-text#288]: caching should not overlap with View
5059
public void testCachingWithViewFirst() throws Exception
5160
{
5261
CsvMapper mapper1 = mapperForCsv();
53-
CsvSchema schemaB = mapper1.schemaForWithView(Bean288.class, ViewB.class);
54-
assertEquals("1,3", mapper1.writer(schemaB).withView(ViewB.class)
62+
CsvSchema schemaA = mapper1.schemaForWithView(Bean288.class, ViewA.class);
63+
assertEquals("1,2", mapper1.writer(schemaA).withView(ViewA.class)
5564
.writeValueAsString(new Bean288()).trim());
65+
assertEquals(1, mapper1._untypedSchemas.size());
66+
5667
CsvSchema schemaNoView = mapper1.schemaFor(Bean288.class);
5768
assertEquals("1,2,3",
5869
mapper1.writer(schemaNoView).writeValueAsString(new Bean288()).trim());
70+
assertEquals(2, mapper1._untypedSchemas.size());
71+
72+
// check hash
73+
mapper1.schemaFor(Bean288.class);
74+
assertEquals(2, mapper1._untypedSchemas.size());
75+
mapper1.schemaForWithView(Bean288.class, ViewA.class);
76+
assertEquals(2, mapper1._untypedSchemas.size());
77+
mapper1.schemaForWithView(Bean288.class, ViewB.class);
78+
assertEquals(3, mapper1._untypedSchemas.size());
79+
80+
5981
}
6082
}

0 commit comments

Comments
 (0)