Skip to content

Commit

Permalink
Merge pull request java-json-tools#223 from crate-metadata/CacheSize
Browse files Browse the repository at this point in the history
Configurable cache size for processing validations
  • Loading branch information
huggsboson authored Jun 3, 2017
2 parents c2ad15d + 6b4c650 commit c1fffae
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 3 deletions.
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

0 comments on commit c1fffae

Please sign in to comment.