-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from sidhant92/cached_parser
Cached Bool Parser
- Loading branch information
Showing
2 changed files
with
31 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/java/com/github/sidhant92/boolparser/parser/antlr/CachedBoolParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.github.sidhant92.boolparser.parser.antlr; | ||
|
||
import com.github.benmanes.caffeine.cache.Cache; | ||
import com.github.benmanes.caffeine.cache.Caffeine; | ||
import com.github.sidhant92.boolparser.domain.Node; | ||
import com.github.sidhant92.boolparser.operator.OperatorFactory; | ||
import io.vavr.control.Try; | ||
|
||
public class CachedBoolParser extends BoolParser { | ||
private final Cache<String, Node> cache; | ||
|
||
public CachedBoolParser(final int maxCacheSize) { | ||
OperatorFactory.initialize(); | ||
this.cache = Caffeine.newBuilder().maximumSize(maxCacheSize).build(); | ||
} | ||
|
||
@Override | ||
public Try<Node> parseExpression(final String expression, final String defaultField) { | ||
return Try.of(() -> getNode(expression, defaultField)); | ||
} | ||
|
||
@Override | ||
public Try<Node> parseExpression(final String expression) { | ||
return Try.of(() -> getNode(expression, null)); | ||
} | ||
|
||
private Node getNode(final String expression, final String defaultField) { | ||
return cache.get(expression, ex -> super.parse(ex, defaultField)); | ||
} | ||
} |