Skip to content

Configurable cache size for processing validations #223

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

Merged
merged 1 commit into from
Jun 3, 2017
Merged
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 @@ -70,6 +70,11 @@ public final class ValidationConfiguration
* Whether to use {@code format} in the resulting factory
*/
final boolean useFormat;

/**
* Cache size for processing validations
*/
final int cacheSize;

/**
* The set of syntax messages
Expand Down Expand Up @@ -113,6 +118,7 @@ public static ValidationConfiguration byDefault()
libraries = ImmutableMap.copyOf(builder.libraries);
defaultLibrary = builder.defaultLibrary;
useFormat = builder.useFormat;
cacheSize = builder.cacheSize;
syntaxMessages = builder.syntaxMessages;
validationMessages = builder.validationMessages;
}
Expand Down Expand Up @@ -146,6 +152,11 @@ public boolean getUseFormat()
{
return useFormat;
}

public int getCacheSize()
{
return cacheSize;
}

public MessageBundle getSyntaxMessages()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public final class ValidationConfigurationBuilder
* Whether to use {@code format} ({@code true} by default)
*/
boolean useFormat = true;

/**
* Cache maximum size of 512 records by default
*/
int cacheSize = 512;

/**
* The set of syntax messages
Expand Down Expand Up @@ -119,6 +124,7 @@ public final class ValidationConfigurationBuilder
libraries = Maps.newHashMap(cfg.libraries);
defaultLibrary = cfg.defaultLibrary;
useFormat = cfg.useFormat;
cacheSize = cfg.cacheSize;
syntaxMessages = cfg.syntaxMessages;
validationMessages = cfg.validationMessages;
}
Expand Down Expand Up @@ -217,6 +223,14 @@ public ValidationConfigurationBuilder setValidationMessages(
return this;
}

public ValidationConfigurationBuilder setCacheSize(
final int cacheSize)
{
BUNDLE.checkArgument(cacheSize >= -1, "invalidCacheSize");
this.cacheSize = cacheSize;
return this;
}

/**
* Return a frozen version of this configuration
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,6 @@ private Processor<SchemaContext, ValidatorList> buildProcessor()
final Processor<SchemaContext, ValidatorList> processor
= map.getProcessor();
return new CachingProcessor<SchemaContext, ValidatorList>(processor,
SchemaContextEquivalence.getInstance());
SchemaContextEquivalence.getInstance(), validationCfg.getCacheSize());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public ValidationChain(final RefResolver refResolver,
= ProcessorChain.startWith(refResolver).chainWith(syntaxProcessor);

resolver = new CachingProcessor<ValueHolder<SchemaTree>, ValueHolder<SchemaTree>>(
chain1.getProcessor(), SchemaHolderEquivalence.INSTANCE
chain1.getProcessor(), SchemaHolderEquivalence.INSTANCE, cfg.getCacheSize()
);

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

builder = new CachingProcessor<SchemaContext, ValidatorList>(
chain2.getProcessor(), SchemaContextEquivalence.getInstance()
chain2.getProcessor(), SchemaContextEquivalence.getInstance(), cfg.getCacheSize()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ nullFormat = format attribute name cannot be null
nullAttribute = attempt to register null implementation of format attribute "%s"
nullKeyword = attempt to add null keyword to library
nullType = null type argument to digester constructor
invalidCacheSize = cache size must be greater than -1. -1 value sets a cache with unlimited records, zero-value disables the cache
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ public void cannotPutNullValidationMessageBundle()
}
}

@Test
public void cannotPutInvalidCacheSize()
{
try {
cfg.setCacheSize(-2);
fail("No exception thrown!!");
} catch (IllegalArgumentException e) {
assertEquals(e.getMessage(),
BUNDLE.getMessage("invalidCacheSize"));
}
}

@Test
public void defaultLibraryIsDraftV4()
{
Expand Down