SOLR-1098 -- DateFormatTransformer can cache the format objects

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@762180 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shalin Shekhar Mangar 2009-04-05 22:50:10 +00:00
parent 55592e8d7c
commit e7c2a9d7dd
2 changed files with 21 additions and 13 deletions

View File

@ -131,6 +131,9 @@ Optimizations
3. SOLR-1004: Check for abort more frequently during delta-imports.
(Marc Sturlese, shalin)
4. SOLR-1098: DateFormatTransformer can cache the format objects.
(Noble Paul via shalin)
Bug Fixes
----------------------
1. SOLR-800: Deep copy collections to avoid ConcurrentModificationException in XPathEntityprocessor while streaming

View File

@ -19,10 +19,8 @@ package org.apache.solr.handler.dataimport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -43,6 +41,7 @@ import org.slf4j.LoggerFactory;
* @since solr 1.3
*/
public class DateFormatTransformer extends Transformer {
private Map<String, SimpleDateFormat> fmtCache = new HashMap<String, SimpleDateFormat>();
private static final Logger LOG = LoggerFactory
.getLogger(DateFormatTransformer.class);
@ -59,29 +58,35 @@ public class DateFormatTransformer extends Transformer {
try {
Object o = aRow.get(srcCol);
if (o instanceof List) {
List<String> inputs = (List<String>) o;
List inputs = (List) o;
List<Date> results = new ArrayList<Date>();
for (String input : inputs) {
for (Object input : inputs) {
results.add(process(input, fmt));
}
aRow.put(column, results);
} else {
if (o != null) {
aRow.put(column, process(o.toString(), fmt));
if (o != null) {
aRow.put(column, process(o, fmt));
}
}
} catch (ParseException e) {
LOG.warn( "Could not parse a Date field ", e);
LOG.warn("Could not parse a Date field ", e);
}
}
return aRow;
}
private Date process(String value, String format) throws ParseException {
if (value == null || value.trim().length() == 0)
private Date process(Object value, String format) throws ParseException {
if (value == null) return null;
String strVal = value.toString().trim();
if (strVal.length() == 0)
return null;
return new SimpleDateFormat(format).parse(value);
SimpleDateFormat fmt = fmtCache.get(format);
if (fmt == null) {
fmt = new SimpleDateFormat(format);
fmtCache.put(format, fmt);
}
return fmt.parse(strVal);
}
public static final String DATE_TIME_FMT = "dateTimeFormat";