lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a.@apache.org
Subject lucene-solr:master: SOLR-12725: ParseDateFieldUpdateProcessorFactory should reuse ParsePosition.
Date Wed, 03 Oct 2018 11:59:17 GMT
Repository: lucene-solr
Updated Branches:
  refs/heads/master 84683b6aa -> eba7bedad


SOLR-12725: ParseDateFieldUpdateProcessorFactory should reuse ParsePosition.


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/eba7beda
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/eba7beda
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/eba7beda

Branch: refs/heads/master
Commit: eba7bedadf65cda114544d2495cf264065efa7ab
Parents: 84683b6
Author: Andrzej Bialecki <ab@apache.org>
Authored: Wed Oct 3 13:58:31 2018 +0200
Committer: Andrzej Bialecki <ab@apache.org>
Committed: Wed Oct 3 13:59:02 2018 +0200

----------------------------------------------------------------------
 solr/CHANGES.txt                                |  2 ++
 .../ParseDateFieldUpdateProcessorFactory.java   | 28 ++++++++++++++++----
 2 files changed, 25 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/eba7beda/solr/CHANGES.txt
----------------------------------------------------------------------
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 28736cb..d9ee5fc 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -428,6 +428,8 @@ Optimizations
 
 * SOLR-12766: When retrying internal requests, backoff only once for the full batch of retries
(Tomás Fernández Löbbe)
 
+* SOLR-12725: ParseDateFieldUpdateProcessorFactory should reuse ParsePosition (ab)
+
 Other Changes
 ----------------------
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/eba7beda/solr/core/src/java/org/apache/solr/update/processor/ParseDateFieldUpdateProcessorFactory.java
----------------------------------------------------------------------
diff --git a/solr/core/src/java/org/apache/solr/update/processor/ParseDateFieldUpdateProcessorFactory.java
b/solr/core/src/java/org/apache/solr/update/processor/ParseDateFieldUpdateProcessorFactory.java
index c7aaaaf..08aa973 100644
--- a/solr/core/src/java/org/apache/solr/update/processor/ParseDateFieldUpdateProcessorFactory.java
+++ b/solr/core/src/java/org/apache/solr/update/processor/ParseDateFieldUpdateProcessorFactory.java
@@ -17,6 +17,7 @@
 package org.apache.solr.update.processor;
 
 import java.lang.invoke.MethodHandles;
+import java.text.ParsePosition;
 import java.time.Instant;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
@@ -119,13 +120,15 @@ public class ParseDateFieldUpdateProcessorFactory extends FieldMutatingUpdatePro
   private static final String DEFAULT_TIME_ZONE_PARAM = "defaultTimeZone";
   private static final String LOCALE_PARAM = "locale";
 
-  private Map<String,DateTimeFormatter> formats = new LinkedHashMap<>();
+  private Map<String, DateTimeFormatter> formats = new LinkedHashMap<>();
 
   @Override
   public UpdateRequestProcessor getInstance(SolrQueryRequest req,
                                             SolrQueryResponse rsp,
                                             UpdateRequestProcessor next) {
     return new AllValuesOrNoneFieldMutatingUpdateProcessor(getSelector(), next) {
+      final ParsePosition parsePosition = new ParsePosition(0);
+
       @Override
       protected Object mutateValue(Object srcVal) {
         if (srcVal instanceof CharSequence) {
@@ -143,7 +146,7 @@ public class ParseDateFieldUpdateProcessorFactory extends FieldMutatingUpdatePro
           for (Map.Entry<String,DateTimeFormatter> format : formats.entrySet()) {
             DateTimeFormatter parser = format.getValue();
             try {
-              return Date.from(parseInstant(parser, srcStringVal));
+              return Date.from(parseInstant(parser, srcStringVal, parsePosition));
             } catch (DateTimeParseException e) {
               log.debug("value '{}' is not parseable with format '{}'",
                         new Object[] { srcStringVal, format.getKey() });
@@ -208,7 +211,7 @@ public class ParseDateFieldUpdateProcessorFactory extends FieldMutatingUpdatePro
   public static void validateFormatter(DateTimeFormatter formatter) {
     // check it's valid via round-trip
     try {
-      parseInstant(formatter, formatter.format(Instant.now()));
+      parseInstant(formatter, formatter.format(Instant.now()), new ParsePosition(0));
     } catch (Exception e) {
       throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
           "Bad or unsupported pattern: " + formatter.toFormat().toString(), e);
@@ -220,8 +223,23 @@ public class ParseDateFieldUpdateProcessorFactory extends FieldMutatingUpdatePro
   //  the input string contains a timezone/offset that differs from the "override zone"
   //  (which we configure in DEFAULT_TIME_ZONE).  Besides, we need the code below which handles
   //  the optionality of time.  Were it not for that, we truly could do formatter.parse(Instant::from).
-  private static Instant parseInstant(DateTimeFormatter formatter, String dateStr) {
-    final TemporalAccessor temporal = formatter.parse(dateStr);
+  private static Instant parseInstant(DateTimeFormatter formatter, String dateStr, ParsePosition
parsePosition) {
+    // prepare for reuse
+    parsePosition.setIndex(0);
+    parsePosition.setErrorIndex(-1);
+    final TemporalAccessor temporal = formatter.parse(dateStr, parsePosition);
+    // check that all content has been parsed
+    if (parsePosition.getIndex() < dateStr.length()) {
+      final String abbr;
+      if (dateStr.length() > 64) {
+        abbr = dateStr.subSequence(0, 64).toString() + "...";
+      } else {
+        abbr = dateStr;
+      }
+      throw new DateTimeParseException("Text '" + abbr + "' could not be parsed, unparsed
text found at index " +
+          parsePosition.getIndex(), dateStr, parsePosition.getIndex());
+    }
+
     // Get Date; mandatory
     LocalDate date = temporal.query(TemporalQueries.localDate());//mandatory
     if (date == null) {


Mime
View raw message