From 2ed9dd11e01353669ed9ebe0561184b82050fec7 Mon Sep 17 00:00:00 2001 From: Sidhant Aggarwal <10743214+sidhant92@users.noreply.github.com> Date: Sat, 16 Mar 2024 23:05:33 +0530 Subject: [PATCH] cached bool parser --- .../boolparser/parser/antlr/BoolParser.java | 17 +---------- .../parser/antlr/CachedBoolParser.java | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 16 deletions(-) create mode 100644 src/main/java/com/github/sidhant92/boolparser/parser/antlr/CachedBoolParser.java diff --git a/src/main/java/com/github/sidhant92/boolparser/parser/antlr/BoolParser.java b/src/main/java/com/github/sidhant92/boolparser/parser/antlr/BoolParser.java index 52c3b04..ccc4282 100644 --- a/src/main/java/com/github/sidhant92/boolparser/parser/antlr/BoolParser.java +++ b/src/main/java/com/github/sidhant92/boolparser/parser/antlr/BoolParser.java @@ -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 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 parseExpression(final String expression, final String defaultField) { return Try.of(() -> getNode(expression, defaultField)); @@ -39,13 +27,10 @@ public Try 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); diff --git a/src/main/java/com/github/sidhant92/boolparser/parser/antlr/CachedBoolParser.java b/src/main/java/com/github/sidhant92/boolparser/parser/antlr/CachedBoolParser.java new file mode 100644 index 0000000..2037abb --- /dev/null +++ b/src/main/java/com/github/sidhant92/boolparser/parser/antlr/CachedBoolParser.java @@ -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 cache; + + public CachedBoolParser(final int maxCacheSize) { + OperatorFactory.initialize(); + this.cache = Caffeine.newBuilder().maximumSize(maxCacheSize).build(); + } + + @Override + public Try parseExpression(final String expression, final String defaultField) { + return Try.of(() -> getNode(expression, defaultField)); + } + + @Override + public Try 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)); + } +}