Skip to content

Commit

Permalink
Merge pull request #29 from sidhant92/cached_parser
Browse files Browse the repository at this point in the history
Cached Bool Parser
  • Loading branch information
sidhant92 authored Mar 17, 2024
2 parents 717d045 + 2ed9dd1 commit 62307b1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,18 @@
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
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 com.github.sidhant92.boolparser.parser.BoolExpressionParser;
import io.vavr.control.Try;

public class BoolParser implements BoolExpressionParser {
private boolean useCache;

private Cache<String, Node> cache;

private final ParseTreeWalker parseTreeWalker = new ParseTreeWalker();

public BoolParser() {
OperatorFactory.initialize();
}

public BoolParser(final int maxCacheSize) {
OperatorFactory.initialize();
this.useCache = true;
this.cache = Caffeine.newBuilder().maximumSize(maxCacheSize).build();
}

@Override
public Try<Node> parseExpression(final String expression, final String defaultField) {
return Try.of(() -> getNode(expression, defaultField));
Expand All @@ -39,13 +27,10 @@ public Try<Node> parseExpression(final String expression) {
}

private Node getNode(final String expression, final String defaultField) {
if (useCache) {
return cache.get(expression, ex -> parse(ex, defaultField));
}
return parse(expression, defaultField);
}

private Node parse(final String expression, final String defaultField) {
protected Node parse(final String expression, final String defaultField) {
final BooleanExpressionLexer filterLexer = new BooleanExpressionLexer(CharStreams.fromString(expression));
final CommonTokenStream commonTokenStream = new CommonTokenStream(filterLexer);
final BooleanExpressionParser filterParser = new BooleanExpressionParser(commonTokenStream);
Expand Down
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));
}
}

0 comments on commit 62307b1

Please sign in to comment.