[Dive4elements-commits] [PATCH] Datacage: Compile XPath expressions and reuse them. With the introduction of filters in dc:elements XPath expressions are evalutated very often so compiling them should reduce the overhead significantly

Wald Commits scm-commit at wald.intevation.org
Wed Mar 27 11:07:44 CET 2013


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1364378395 -3600
# Node ID 504a6288721773fa09bf17c59163a386d932d928
# Parent  b279f2d4bc7891847e88174bca04fdf66974e5c4
Datacage: Compile XPath expressions and reuse them. With the introduction of filters in dc:elements XPath expressions are evalutated very often so compiling them should reduce the overhead significantly.

diff -r b279f2d4bc78 -r 504a62887217 flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java	Wed Mar 27 10:25:45 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java	Wed Mar 27 10:59:55 2013 +0100
@@ -21,6 +21,7 @@
 
 import javax.xml.xpath.XPath;
 import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpression;
 import javax.xml.xpath.XPathExpressionException;
 import javax.xml.xpath.XPathFactory;
 
@@ -97,6 +98,8 @@
         protected Deque<Pair<NamedConnection, ResultData>> connectionsStack;
         protected Deque<NodeList>                          macroBodies;
         protected FunctionResolver                         functionResolver;
+        protected Map<String, XPathExpression>             expressions;
+
 
         public BuildHelper(
             Node                  output,
@@ -115,6 +118,7 @@
             owner            = getOwnerDocument(output);
             macroBodies      = new ArrayDeque<NodeList>();
             functionResolver = new FunctionResolver(this);
+            expressions      = new HashMap<String, XPathExpression>();
             statements       =
                 new HashMap<String, CompiledStatement.Instance>();
         }
@@ -531,6 +535,20 @@
             }
         }
 
+        protected XPathExpression getXPathExpression(String expr)
+        throws XPathExpressionException
+        {
+            XPathExpression x = expressions.get(expr);
+            if (x == null) {
+                XPath xpath = XPATH_FACTORY.newXPath();
+                xpath.setXPathVariableResolver(frames);
+                xpath.setXPathFunctionResolver(functionResolver);
+                x = xpath.compile(expr);
+                expressions.put(expr, x);
+            }
+            return x;
+        }
+
         protected Object evaluateXPath(String expr, QName returnType) {
 
             if (log.isDebugEnabled()) {
@@ -538,10 +556,8 @@
             }
 
             try {
-                XPath xpath = XPATH_FACTORY.newXPath();
-                xpath.setXPathVariableResolver(frames);
-                xpath.setXPathFunctionResolver(functionResolver);
-                return xpath.evaluate(expr, EVAL_DOCUMENT, returnType);
+                XPathExpression x = getXPathExpression(expr);
+                return x.evaluate(EVAL_DOCUMENT, returnType);
             }
             catch (XPathExpressionException xpee) {
                 log.error("expression: " + expr, xpee);


More information about the Dive4elements-commits mailing list