Skip to content
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

Cached Bool Parser #29

Merged
merged 1 commit into from
Mar 17, 2024
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 @@ -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));
}
}
Loading