[Dive4elements-commits] [PATCH 2 of 2] Datacage: Index macros with a map. Reduces excessive linear scanning for macros when calling them

Wald Commits scm-commit at wald.intevation.org
Tue Mar 26 22:30:07 CET 2013


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1364333395 -3600
# Node ID 3d7e552cc39608169bbb01aafd31f20ae1e0f170
# Parent  9575264f801da4a1d91545f8da18ee5aae1d15f9
Datacage: Index macros with a map. Reduces excessive linear scanning for macros when calling them.

diff -r 9575264f801d -r 3d7e552cc396 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	Tue Mar 26 21:55:23 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java	Tue Mar 26 22:29:55 2013 +0100
@@ -57,6 +57,8 @@
 
     protected Map<String, CompiledStatement> compiledStatements;
 
+    protected Map<String, Element> macros;
+
     /** Connection to either of the databases. */
     public static class NamedConnection {
 
@@ -111,10 +113,10 @@
             this.output      = output;
             frames           = new StackFrames(parameters);
             owner            = getOwnerDocument(output);
-            statements =
+            macroBodies      = new ArrayDeque<NodeList>();
+            functionResolver = new FunctionResolver(this);
+            statements       =
                 new HashMap<String, CompiledStatement.Instance>();
-            macroBodies = new ArrayDeque<NodeList>();
-            functionResolver = new FunctionResolver(this);
         }
 
         public void build() throws SQLException {
@@ -409,18 +411,7 @@
                 return;
             }
 
-            NodeList macros = template.getElementsByTagNameNS(
-                DC_NAMESPACE_URI, "macro");
-
-            Element macro = null;
-
-            for (int i = 0, N = macros.getLength(); i < N; ++i) {
-                Element m = (Element) macros.item(i);
-                if (name.equals(m.getAttribute("name"))) {
-                    macro = m;
-                    break;
-                }
-            }
+            Element macro = macros.get(name);
 
             if (macro != null) {
                 macroBodies.push(current.getChildNodes());
@@ -701,11 +692,13 @@
 
     public Builder() {
         compiledStatements = new HashMap<String, CompiledStatement>();
+        macros             = new HashMap<String, Element>();
     }
 
     public Builder(Document template) {
         this();
         this.template = template;
+        extractMacros();
         compileStatements();
     }
 
@@ -737,6 +730,16 @@
         }
     }
 
+    protected void extractMacros() {
+        NodeList ms = template.getElementsByTagNameNS(
+            DC_NAMESPACE_URI, "macro");
+
+        for (int i = 0, N = ms.getLength(); i < N; ++i) {
+            Element m = (Element)ms.item(i);
+            macros.put(m.getAttribute("name"), m);
+        }
+    }
+
     protected List<Node> rootsToList() {
 
         NodeList roots = template.getElementsByTagNameNS(


More information about the Dive4elements-commits mailing list