lucene-solr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From no...@apache.org
Subject svn commit: r881766 - in /lucene/solr/trunk/contrib/dataimporthandler: CHANGES.txt src/main/java/org/apache/solr/handler/dataimport/TemplateTransformer.java src/test/java/org/apache/solr/handler/dataimport/TestTemplateTransformer.java
Date Wed, 18 Nov 2009 13:24:06 GMT
Author: noble
Date: Wed Nov 18 13:24:06 2009
New Revision: 881766

URL: http://svn.apache.org/viewvc?rev=881766&view=rev
Log:
SOLR-1547 TemplateTransformer should copy array/list more intelligently

Modified:
    lucene/solr/trunk/contrib/dataimporthandler/CHANGES.txt
    lucene/solr/trunk/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/TemplateTransformer.java
    lucene/solr/trunk/contrib/dataimporthandler/src/test/java/org/apache/solr/handler/dataimport/TestTemplateTransformer.java

Modified: lucene/solr/trunk/contrib/dataimporthandler/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/contrib/dataimporthandler/CHANGES.txt?rev=881766&r1=881765&r2=881766&view=diff
==============================================================================
--- lucene/solr/trunk/contrib/dataimporthandler/CHANGES.txt (original)
+++ lucene/solr/trunk/contrib/dataimporthandler/CHANGES.txt Wed Nov 18 13:24:06 2009
@@ -22,6 +22,7 @@
 ----------------------
 
 * SOLR-1525 allow DIH to refer to core properties (noble)
+* SOLR-1547 TemplateTransformer copy objects more intelligently when there when the template
is a single variable (noble)
 
 Optimizations
 ----------------------

Modified: lucene/solr/trunk/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/TemplateTransformer.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/TemplateTransformer.java?rev=881766&r1=881765&r2=881766&view=diff
==============================================================================
--- lucene/solr/trunk/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/TemplateTransformer.java
(original)
+++ lucene/solr/trunk/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/TemplateTransformer.java
Wed Nov 18 13:24:06 2009
@@ -49,6 +49,7 @@
 public class TemplateTransformer extends Transformer {
 
   private static final Logger LOG = LoggerFactory.getLogger(TemplateTransformer.class);
+  private Map<String ,List<String>> templateVsVars = new HashMap<String, List<String>>();
 
   @SuppressWarnings("unchecked")
   public Object transformRow(Map<String, Object> row, Context context) {
@@ -67,7 +68,7 @@
 
       // Verify if all variables can be resolved or not
       boolean resolvable = true;
-      List<String> variables = TemplateString.getVariables(expr);
+      List<String> variables = getVars(expr);
       for (String v : variables) {
         if (resolver.resolve(v) == null) {
           LOG.warn("Unable to resolve variable: " + v
@@ -78,13 +79,26 @@
 
       if (!resolvable)
         continue;
+      if(variables.size() == 1 && expr.startsWith("${") && expr.endsWith("}")){
+        row.put(column, resolver.resolve(variables.get(0)));
+      } else {
+        row.put(column, resolver.replaceTokens(expr));
+      }
 
-      row.put(column, resolver.replaceTokens(expr));
     }
 
 
     return row;
   }
 
+  private List<String> getVars(String expr) {
+    List<String> result = this.templateVsVars.get(expr);
+    if(result == null){
+      result = TemplateString.getVariables(expr);
+      this.templateVsVars.put(expr, result);
+    }
+    return result;
+  }
+
   public static final String TEMPLATE = "template";
 }

Modified: lucene/solr/trunk/contrib/dataimporthandler/src/test/java/org/apache/solr/handler/dataimport/TestTemplateTransformer.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/contrib/dataimporthandler/src/test/java/org/apache/solr/handler/dataimport/TestTemplateTransformer.java?rev=881766&r1=881765&r2=881766&view=diff
==============================================================================
--- lucene/solr/trunk/contrib/dataimporthandler/src/test/java/org/apache/solr/handler/dataimport/TestTemplateTransformer.java
(original)
+++ lucene/solr/trunk/contrib/dataimporthandler/src/test/java/org/apache/solr/handler/dataimport/TestTemplateTransformer.java
Wed Nov 18 13:24:06 2009
@@ -22,6 +22,7 @@
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
+import java.util.Arrays;
 
 /**
  * <p>
@@ -43,14 +44,20 @@
     fields.add(AbstractDataImportHandlerTest.createMap("column", "name",
             TemplateTransformer.TEMPLATE,
             "${e.lastName}, ${e.firstName} ${e.middleName}"));
+    fields.add(AbstractDataImportHandlerTest.createMap("column", "emails",
+            TemplateTransformer.TEMPLATE,
+            "${e.mail}"));
+
     // test reuse of template output in another template 
     fields.add(AbstractDataImportHandlerTest.createMap("column", "mrname",
             TemplateTransformer.TEMPLATE,"Mr ${e.name}"));
-            
+
+    List<String> mails = Arrays.asList(new String[]{"a@b.com", "c@d.com"});
     Map row = AbstractDataImportHandlerTest.createMap(
             "firstName", "Shalin",
             "middleName", "Shekhar", 
-            "lastName", "Mangar");
+            "lastName", "Mangar",
+            "mail", mails);
 
     VariableResolverImpl resolver = new VariableResolverImpl();
     resolver.addNamespace("e", row);
@@ -62,6 +69,7 @@
     new TemplateTransformer().transformRow(row, context);
     Assert.assertEquals("Mangar, Shalin Shekhar", row.get("name"));
     Assert.assertEquals("Mr Mangar, Shalin Shekhar", row.get("mrname"));
+    Assert.assertEquals(mails,row.get("emails"));
   }
 
 }



Mime
View raw message