From 976e2f229f029452475a96dbfb6ad8011ee725d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=8A=E6=A3=A0?= Date: Mon, 14 Nov 2022 17:03:53 +0800 Subject: [PATCH 1/3] compelet the compile api --- .../com/googlecode/aviator/AviatorEvaluator.java | 13 +++++++++++++ .../aviator/AviatorEvaluatorUnitTest.java | 13 +++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/main/java/com/googlecode/aviator/AviatorEvaluator.java b/src/main/java/com/googlecode/aviator/AviatorEvaluator.java index ac4dd6a9..cd25d696 100644 --- a/src/main/java/com/googlecode/aviator/AviatorEvaluator.java +++ b/src/main/java/com/googlecode/aviator/AviatorEvaluator.java @@ -472,6 +472,19 @@ public static Expression getCachedExpression(final String expression) { return getInstance().getCachedExpression(expression); } + /** + * Compile a text expression to Expression object and cache it with the specified cache key + * + * @param cacheKey cache key + * @param expression text expression + * @param cached Whether to cache the compiled result,make true to cache it. + * @return + */ + public static Expression compile(final String cacheKey, final String expression, + final boolean cached) { + return getInstance().compile(cacheKey, expression, cached); + } + /** * Compile a text expression to Expression object diff --git a/src/test/java/com/googlecode/aviator/AviatorEvaluatorUnitTest.java b/src/test/java/com/googlecode/aviator/AviatorEvaluatorUnitTest.java index e0636d47..3b824e80 100644 --- a/src/test/java/com/googlecode/aviator/AviatorEvaluatorUnitTest.java +++ b/src/test/java/com/googlecode/aviator/AviatorEvaluatorUnitTest.java @@ -24,6 +24,7 @@ import java.math.MathContext; import java.util.HashMap; import java.util.Map; +import com.googlecode.aviator.utils.Utils; import org.junit.Test; import com.googlecode.aviator.exception.CompileExpressionErrorException; @@ -126,6 +127,18 @@ public void testExecIllegalArguments() { AviatorEvaluator.exec("a-b+c", 1, 2); } + @Test + public void testCompileCacheWithSpecifiedCacheKey() { + String expression = "1+3"; + Expression exp1 = AviatorEvaluator.compile(Utils.md5sum(expression), expression, true); + Expression exp2 = AviatorEvaluator.compile(Utils.md5sum(expression), expression, true); + assertNotNull(exp1); + assertNotNull(exp2); + assertSame(exp1, exp2); + + assertEquals(4, exp1.execute(null)); + assertEquals(4, exp2.execute(null)); + } @Test public void testCompileCache() { From 13627fb30fd26d8f522a248b8e3e0c972b79feed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=8A=E6=A3=A0?= Date: Sat, 26 Nov 2022 16:58:45 +0800 Subject: [PATCH 2/3] support execute with specified cache key --- .../googlecode/aviator/AviatorEvaluator.java | 13 ++++++++++++ .../aviator/AviatorEvaluatorInstance.java | 21 +++++++++++++++---- .../AviatorEvaluatorInstanceUnitTest.java | 11 ++++++++++ .../aviator/AviatorEvaluatorUnitTest.java | 11 ++++++++++ 4 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/googlecode/aviator/AviatorEvaluator.java b/src/main/java/com/googlecode/aviator/AviatorEvaluator.java index cd25d696..8e516aa3 100644 --- a/src/main/java/com/googlecode/aviator/AviatorEvaluator.java +++ b/src/main/java/com/googlecode/aviator/AviatorEvaluator.java @@ -532,6 +532,19 @@ public static Object exec(final String expression, final Object... values) { return getInstance().exec(expression, values); } + /** + * Execute a text expression with environment + * + * @param cacheKey unique key for caching + * @param expression text expression + * @param env Binding variable environment + * @param cached Whether to cache the compiled result,make true to cache it. + */ + public static Object execute(final String cacheKey, final String expression, + final Map env, final boolean cached) { + return getInstance().execute(cacheKey, expression, env, cached); + } + /** * Execute a text expression with environment diff --git a/src/main/java/com/googlecode/aviator/AviatorEvaluatorInstance.java b/src/main/java/com/googlecode/aviator/AviatorEvaluatorInstance.java index a87cc76d..f921ddcd 100644 --- a/src/main/java/com/googlecode/aviator/AviatorEvaluatorInstance.java +++ b/src/main/java/com/googlecode/aviator/AviatorEvaluatorInstance.java @@ -1655,17 +1655,17 @@ public Object exec(final String expression, final Object... values) { } } - /** * Execute a text expression with environment * + * @param cacheKey unique key for caching * @param expression text expression * @param env Binding variable environment * @param cached Whether to cache the compiled result,make true to cache it. */ - public Object execute(final String expression, final Map env, - final boolean cached) { - Expression compiledExpression = compile(expression, cached); + public Object execute(final String cacheKey, final String expression, + final Map env, final boolean cached) { + Expression compiledExpression = compile(cacheKey, expression, cached); if (compiledExpression != null) { return compiledExpression.execute(env); } else { @@ -1674,6 +1674,19 @@ public Object execute(final String expression, final Map env, } + /** + * Execute a text expression with environment + * + * @param expression text expression + * @param env Binding variable environment + * @param cached Whether to cache the compiled result,make true to cache it. + */ + public Object execute(final String expression, final Map env, + final boolean cached) { + return execute(expression, expression, env, cached); + } + + /** * Execute a text expression without caching * diff --git a/src/test/java/com/googlecode/aviator/AviatorEvaluatorInstanceUnitTest.java b/src/test/java/com/googlecode/aviator/AviatorEvaluatorInstanceUnitTest.java index 4254a58c..baae2dc9 100644 --- a/src/test/java/com/googlecode/aviator/AviatorEvaluatorInstanceUnitTest.java +++ b/src/test/java/com/googlecode/aviator/AviatorEvaluatorInstanceUnitTest.java @@ -640,6 +640,17 @@ public void evaluatorWithCache() { assertEquals("hello world", this.instance.execute("a+b", env, true)); } + @Test + public void evaluatorWithSpecifiedCacheKey() { + Map env = new HashMap<>(); + env.put("a", "hello"); + env.put("b", " world"); + assertEquals("hello world", this.instance.execute("key", "a + b", env, true)); + final Expression expression = this.instance.getCachedExpressionByKey("key"); + assertNotNull(expression); + assertEquals("hello world", expression.execute(env)); + } + @Test(expected = UnsupportedFeatureException.class) public void testDisableAssignment() { diff --git a/src/test/java/com/googlecode/aviator/AviatorEvaluatorUnitTest.java b/src/test/java/com/googlecode/aviator/AviatorEvaluatorUnitTest.java index 3b824e80..31d35b28 100644 --- a/src/test/java/com/googlecode/aviator/AviatorEvaluatorUnitTest.java +++ b/src/test/java/com/googlecode/aviator/AviatorEvaluatorUnitTest.java @@ -190,6 +190,17 @@ public void evaluatorWithCache() { assertEquals("hello world", AviatorEvaluator.execute("a+b", env, true)); } + @Test + public void evaluatorWithSpecifiedCacheKey() { + Map env = new HashMap<>(); + env.put("a", "hello"); + env.put("b", " world"); + assertEquals("hello world", AviatorEvaluator.execute("key", "a + b", env, true)); + final Expression expression = AviatorEvaluator.getInstance().getCachedExpressionByKey("key"); + assertNotNull(expression); + assertEquals("hello world", expression.execute(env)); + } + @Test(expected = CompileExpressionErrorException.class) public void compileBlankExpression1() { From f4376f20a0d7e330a5df7c23f45d5ef64fc1c1c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=8A=E6=A3=A0?= Date: Sat, 26 Nov 2022 17:03:55 +0800 Subject: [PATCH 3/3] fix typo --- .../aviator/AviatorEvaluatorInstance.java | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/googlecode/aviator/AviatorEvaluatorInstance.java b/src/main/java/com/googlecode/aviator/AviatorEvaluatorInstance.java index f921ddcd..1e0d04de 100644 --- a/src/main/java/com/googlecode/aviator/AviatorEvaluatorInstance.java +++ b/src/main/java/com/googlecode/aviator/AviatorEvaluatorInstance.java @@ -284,8 +284,8 @@ public void setEnvProcessor(final EnvProcessor envProcessor) { /** * Compile a script file into expression. * - * @param file the script file path - * @param cached whether to cached the compiled result with key is script file's absolute path. + * @param path the script file path + * @param cached whether to cache the compiled result with key is script file's absolute path. * @since 5.0.0 * @return the compiled expression instance. */ @@ -356,10 +356,9 @@ private File tryFindFileFromClassLoader(final String path, final ClassLoader con } /** - * Loads a script from path and return it's exports. + * Loads a script from path and return its exports. * - * @param path - * @param args arguments to execute the script. + * @param path the script file path * @throws IOException * @return the exports map. * @since 5.0.0 @@ -389,10 +388,9 @@ private Map executeModule(final Expression exp, final String abP /** - * Loads a script from path and return it's exports with module caching. + * Loads a script from path and return its exports with module caching. * - * @param path - * @param args arguments to execute the script. + * @param path the script file path * @throws IOException * @return the exports map * @since 5.0.0 @@ -902,7 +900,7 @@ private void loadModule() { } /** - * Set a alias name for function specified by name + * Set alias name for function specified by name * * @param name the origin function name * @param aliasName the alias function name @@ -1304,7 +1302,7 @@ public AviatorFunction getFunction(final String name) { * Retrieve an aviator function by name,throw exception if not found or null.It's not thread-safe. * * @param name - * @param symbolTablee + * @param symbolTable * @return */ public AviatorFunction getFunction(final String name, final SymbolTable symbolTable) {