Skip to content

Commit c1fffae

Browse files
authored
Merge pull request #223 from crate-metadata/CacheSize
Configurable cache size for processing validations
2 parents c2ad15d + 6b4c650 commit c1fffae

File tree

6 files changed

+41
-3
lines changed

6 files changed

+41
-3
lines changed

src/main/java/com/github/fge/jsonschema/cfg/ValidationConfiguration.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ public final class ValidationConfiguration
7070
* Whether to use {@code format} in the resulting factory
7171
*/
7272
final boolean useFormat;
73+
74+
/**
75+
* Cache size for processing validations
76+
*/
77+
final int cacheSize;
7378

7479
/**
7580
* The set of syntax messages
@@ -113,6 +118,7 @@ public static ValidationConfiguration byDefault()
113118
libraries = ImmutableMap.copyOf(builder.libraries);
114119
defaultLibrary = builder.defaultLibrary;
115120
useFormat = builder.useFormat;
121+
cacheSize = builder.cacheSize;
116122
syntaxMessages = builder.syntaxMessages;
117123
validationMessages = builder.validationMessages;
118124
}
@@ -146,6 +152,11 @@ public boolean getUseFormat()
146152
{
147153
return useFormat;
148154
}
155+
156+
public int getCacheSize()
157+
{
158+
return cacheSize;
159+
}
149160

150161
public MessageBundle getSyntaxMessages()
151162
{

src/main/java/com/github/fge/jsonschema/cfg/ValidationConfigurationBuilder.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ public final class ValidationConfigurationBuilder
8080
* Whether to use {@code format} ({@code true} by default)
8181
*/
8282
boolean useFormat = true;
83+
84+
/**
85+
* Cache maximum size of 512 records by default
86+
*/
87+
int cacheSize = 512;
8388

8489
/**
8590
* The set of syntax messages
@@ -119,6 +124,7 @@ public final class ValidationConfigurationBuilder
119124
libraries = Maps.newHashMap(cfg.libraries);
120125
defaultLibrary = cfg.defaultLibrary;
121126
useFormat = cfg.useFormat;
127+
cacheSize = cfg.cacheSize;
122128
syntaxMessages = cfg.syntaxMessages;
123129
validationMessages = cfg.validationMessages;
124130
}
@@ -217,6 +223,14 @@ public ValidationConfigurationBuilder setValidationMessages(
217223
return this;
218224
}
219225

226+
public ValidationConfigurationBuilder setCacheSize(
227+
final int cacheSize)
228+
{
229+
BUNDLE.checkArgument(cacheSize >= -1, "invalidCacheSize");
230+
this.cacheSize = cacheSize;
231+
return this;
232+
}
233+
220234
/**
221235
* Return a frozen version of this configuration
222236
*

src/main/java/com/github/fge/jsonschema/main/JsonSchemaFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,6 @@ private Processor<SchemaContext, ValidatorList> buildProcessor()
275275
final Processor<SchemaContext, ValidatorList> processor
276276
= map.getProcessor();
277277
return new CachingProcessor<SchemaContext, ValidatorList>(processor,
278-
SchemaContextEquivalence.getInstance());
278+
SchemaContextEquivalence.getInstance(), validationCfg.getCacheSize());
279279
}
280280
}

src/main/java/com/github/fge/jsonschema/processors/validation/ValidationChain.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public ValidationChain(final RefResolver refResolver,
7171
= ProcessorChain.startWith(refResolver).chainWith(syntaxProcessor);
7272

7373
resolver = new CachingProcessor<ValueHolder<SchemaTree>, ValueHolder<SchemaTree>>(
74-
chain1.getProcessor(), SchemaHolderEquivalence.INSTANCE
74+
chain1.getProcessor(), SchemaHolderEquivalence.INSTANCE, cfg.getCacheSize()
7575
);
7676

7777
final SchemaDigester digester = new SchemaDigester(library);
@@ -86,7 +86,7 @@ public ValidationChain(final RefResolver refResolver,
8686
}
8787

8888
builder = new CachingProcessor<SchemaContext, ValidatorList>(
89-
chain2.getProcessor(), SchemaContextEquivalence.getInstance()
89+
chain2.getProcessor(), SchemaContextEquivalence.getInstance(), cfg.getCacheSize()
9090
);
9191
}
9292

src/main/resources/com/github/fge/jsonschema/validator/configuration.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ nullFormat = format attribute name cannot be null
3737
nullAttribute = attempt to register null implementation of format attribute "%s"
3838
nullKeyword = attempt to add null keyword to library
3939
nullType = null type argument to digester constructor
40+
invalidCacheSize = cache size must be greater than -1. -1 value sets a cache with unlimited records, zero-value disables the cache

src/test/java/com/github/fge/jsonschema/cfg/ValidationConfigurationTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ public void cannotPutNullValidationMessageBundle()
103103
}
104104
}
105105

106+
@Test
107+
public void cannotPutInvalidCacheSize()
108+
{
109+
try {
110+
cfg.setCacheSize(-2);
111+
fail("No exception thrown!!");
112+
} catch (IllegalArgumentException e) {
113+
assertEquals(e.getMessage(),
114+
BUNDLE.getMessage("invalidCacheSize"));
115+
}
116+
}
117+
106118
@Test
107119
public void defaultLibraryIsDraftV4()
108120
{

0 commit comments

Comments
 (0)