mirror of https://github.com/apache/lucene.git
SOLR-782 -- DIH code cleanup -- load evaluators lazily.
git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@712635 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
eb90bb0afa
commit
9cb45e3214
|
@ -43,11 +43,9 @@ import java.util.*;
|
|||
public class DataConfig {
|
||||
public List<Document> documents;
|
||||
|
||||
public List<Props> properties;
|
||||
|
||||
private Map<String, Document> documentCache;
|
||||
|
||||
public Map<String, Evaluator> evaluators = new HashMap<String, Evaluator>();
|
||||
public List<Map<String, String >> functions = new ArrayList<Map<String ,String>>();
|
||||
|
||||
public Script script;
|
||||
|
||||
|
@ -66,14 +64,14 @@ public class DataConfig {
|
|||
}
|
||||
|
||||
public static class Document {
|
||||
// TODO - remove this
|
||||
public String name;
|
||||
|
||||
// TODO - remove from here and add it to entity
|
||||
public String deleteQuery;
|
||||
|
||||
public List<Entity> entities = new ArrayList<Entity>();
|
||||
|
||||
public List<Field> fields;
|
||||
|
||||
public Document() {
|
||||
}
|
||||
|
||||
|
@ -83,21 +81,9 @@ public class DataConfig {
|
|||
List<Element> l = getChildNodes(element, "entity");
|
||||
for (Element e : l)
|
||||
entities.add(new Entity(e));
|
||||
// entities = new Entity(l.get(0));
|
||||
l = getChildNodes(element, "field");
|
||||
if (!l.isEmpty())
|
||||
fields = new ArrayList<Field>();
|
||||
for (Element e : l)
|
||||
fields.add(new Field(e));
|
||||
}
|
||||
}
|
||||
|
||||
public static class Props {
|
||||
public String name;
|
||||
|
||||
public String file;
|
||||
}
|
||||
|
||||
public static class Entity {
|
||||
public String name;
|
||||
|
||||
|
@ -252,29 +238,17 @@ public class DataConfig {
|
|||
}
|
||||
|
||||
// Add the provided evaluators
|
||||
evaluators.put(EvaluatorBag.DATE_FORMAT_EVALUATOR, EvaluatorBag
|
||||
.getDateFormatEvaluator());
|
||||
evaluators.put(EvaluatorBag.SQL_ESCAPE_EVALUATOR, EvaluatorBag
|
||||
.getSqlEscapingEvaluator());
|
||||
evaluators.put(EvaluatorBag.URL_ENCODE_EVALUATOR, EvaluatorBag
|
||||
.getUrlEvaluator());
|
||||
|
||||
n = getChildNodes(e, FUNCTION);
|
||||
if (!n.isEmpty()) {
|
||||
for (Element element : n) {
|
||||
String func = getStringAttribute(element, NAME, null);
|
||||
String clz = getStringAttribute(element, CLASS, null);
|
||||
if (func == null || clz == null)
|
||||
if (func == null || clz == null){
|
||||
throw new DataImportHandlerException(
|
||||
DataImportHandlerException.SEVERE,
|
||||
"<function> must have a 'name' and 'class' attributes");
|
||||
try {
|
||||
evaluators.put(func, (Evaluator) DocBuilder.loadClass(clz, null)
|
||||
.newInstance());
|
||||
} catch (Exception exp) {
|
||||
throw new DataImportHandlerException(
|
||||
DataImportHandlerException.SEVERE,
|
||||
"Unable to instantiate evaluator: " + clz, exp);
|
||||
} else {
|
||||
functions.add(getAllAttributes(element));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -89,7 +89,7 @@ public class DocBuilder {
|
|||
indexerNamespace.put("request", requestParameters.requestParams);
|
||||
indexerNamespace.put("defaults", defaultVariables);
|
||||
indexerNamespace.put("functions", EvaluatorBag.getFunctionsNamespace(resolver,
|
||||
dataImporter.getConfig().evaluators));
|
||||
dataImporter.getConfig().functions, this));
|
||||
if (context.getConfig().script != null) {
|
||||
indexerNamespace
|
||||
.put(DataConfig.SCRIPT, context.getConfig().script.script);
|
||||
|
|
|
@ -16,7 +16,11 @@ package org.apache.solr.handler.dataimport;
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import static org.apache.solr.handler.dataimport.DocBuilder.loadClass;
|
||||
import static org.apache.solr.handler.dataimport.DataConfig.CLASS;
|
||||
import static org.apache.solr.handler.dataimport.DataConfig.NAME;
|
||||
import org.apache.solr.util.DateMathParser;
|
||||
import org.apache.solr.core.SolrCore;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
|
@ -53,11 +57,11 @@ public class EvaluatorBag {
|
|||
.compile("^(\\w*?)\\((.*?)\\)$");
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* <p/>
|
||||
* Returns an <code>Evaluator</code> instance meant to be used for escaping
|
||||
* values in SQL queries.
|
||||
* </p>
|
||||
* <p>
|
||||
* <p/>
|
||||
* It escapes the value of the given expression by replacing all occurrences
|
||||
* of single-quotes by two single-quotes and similarily for double-quotes
|
||||
* </p>
|
||||
|
@ -79,7 +83,7 @@ public class EvaluatorBag {
|
|||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* <p/>
|
||||
* Returns an <code>Evaluator</code> instance capable of URL-encoding
|
||||
* expressions. The expressions are evaluated using a
|
||||
* <code>VariableResolver</code>
|
||||
|
@ -109,11 +113,11 @@ public class EvaluatorBag {
|
|||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* <p/>
|
||||
* Returns an <code>Evaluator</code> instance capable of formatting values
|
||||
* using a given date format.
|
||||
* </p>
|
||||
* <p>
|
||||
* <p/>
|
||||
* The value to be formatted can be a entity.field or a date expression parsed
|
||||
* with <code>DateMathParser</code> class. If the value is in single quotes,
|
||||
* then it is assumed to be a datemath expression, otherwise it resolved using
|
||||
|
@ -190,7 +194,21 @@ public class EvaluatorBag {
|
|||
}
|
||||
|
||||
static Map<String, Object> getFunctionsNamespace(
|
||||
final VariableResolver resolver, final Map<String, Evaluator> evaluators) {
|
||||
final VariableResolver resolver, final List<Map<String, String>> fn, DocBuilder docBuilder) {
|
||||
final Map<String, Evaluator> evaluators = new HashMap<String, Evaluator>();
|
||||
evaluators.put(DATE_FORMAT_EVALUATOR, getDateFormatEvaluator());
|
||||
evaluators.put(SQL_ESCAPE_EVALUATOR, getSqlEscapingEvaluator());
|
||||
evaluators.put(URL_ENCODE_EVALUATOR, getUrlEvaluator());
|
||||
SolrCore core = docBuilder == null ? null : docBuilder.dataImporter.getCore();
|
||||
for (Map<String, String> map : fn) {
|
||||
try {
|
||||
evaluators.put(map.get(NAME), (Evaluator) loadClass(map.get(CLASS), core).newInstance());
|
||||
} catch (Exception e) {
|
||||
throw new DataImportHandlerException(
|
||||
DataImportHandlerException.SEVERE,
|
||||
"Unable to instantiate evaluator: " + map.get(CLASS), e);
|
||||
}
|
||||
}
|
||||
|
||||
return new HashMap<String, Object>() {
|
||||
@Override
|
||||
|
|
|
@ -20,9 +20,7 @@ import org.junit.Assert;
|
|||
import org.junit.Test;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
@ -70,10 +68,8 @@ public class TestVariableResolver {
|
|||
@Test
|
||||
public void dateNamespaceWithValue() {
|
||||
VariableResolverImpl vri = new VariableResolverImpl();
|
||||
HashMap<String, Evaluator> evaluators = new HashMap<String, Evaluator>();
|
||||
evaluators.put("formatDate", EvaluatorBag.getDateFormatEvaluator());
|
||||
vri.addNamespace("dataimporter.functions", EvaluatorBag
|
||||
.getFunctionsNamespace(vri, evaluators));
|
||||
.getFunctionsNamespace(vri, Collections.EMPTY_LIST, null));
|
||||
Map<String, Object> ns = new HashMap<String, Object>();
|
||||
Date d = new Date();
|
||||
ns.put("dt", d);
|
||||
|
@ -88,10 +84,8 @@ public class TestVariableResolver {
|
|||
@Test
|
||||
public void dateNamespaceWithExpr() {
|
||||
VariableResolverImpl vri = new VariableResolverImpl();
|
||||
HashMap<String, Evaluator> evaluators = new HashMap<String, Evaluator>();
|
||||
evaluators.put("formatDate", EvaluatorBag.getDateFormatEvaluator());
|
||||
vri.addNamespace("dataimporter.functions", EvaluatorBag
|
||||
.getFunctionsNamespace(vri, evaluators));
|
||||
.getFunctionsNamespace(vri, Collections.EMPTY_LIST,null));
|
||||
String s = vri
|
||||
.replaceTokens("${dataimporter.functions.formatDate('NOW',yyyy-MM-dd HH:mm)}");
|
||||
Assert.assertEquals(new SimpleDateFormat("yyyy-MM-dd HH:mm")
|
||||
|
@ -119,16 +113,13 @@ public class TestVariableResolver {
|
|||
@Test
|
||||
public void testFunctionNamespace1() {
|
||||
final VariableResolverImpl resolver = new VariableResolverImpl();
|
||||
final Map<String, Evaluator> evaluators = new HashMap<String, Evaluator>();
|
||||
evaluators.put("formatDate", EvaluatorBag.getDateFormatEvaluator());
|
||||
evaluators.put("test", new Evaluator() {
|
||||
public String evaluate(VariableResolver resolver, String expression) {
|
||||
return "Hello World";
|
||||
}
|
||||
});
|
||||
|
||||
final List<Map<String ,String >> l = new ArrayList<Map<String, String>>();
|
||||
Map<String ,String > m = new HashMap<String, String>();
|
||||
m.put("name","test");
|
||||
m.put("class",E.class.getName());
|
||||
l.add(m);
|
||||
resolver.addNamespace("dataimporter.functions", EvaluatorBag
|
||||
.getFunctionsNamespace(resolver, evaluators));
|
||||
.getFunctionsNamespace(resolver, l,null));
|
||||
String s = resolver
|
||||
.replaceTokens("${dataimporter.functions.formatDate('NOW',yyyy-MM-dd HH:mm)}");
|
||||
Assert.assertEquals(new SimpleDateFormat("yyyy-MM-dd HH:mm")
|
||||
|
@ -136,4 +127,10 @@ public class TestVariableResolver {
|
|||
Assert.assertEquals("Hello World", resolver
|
||||
.replaceTokens("${dataimporter.functions.test('TEST')}"));
|
||||
}
|
||||
|
||||
public static class E extends Evaluator{
|
||||
public String evaluate(VariableResolver resolver, String expression) {
|
||||
return "Hello World";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue