You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ho...@apache.org on 2016/02/25 02:58:08 UTC
[48/50] [abbrv] lucene-solr git commit: SOLR-445: refactor metadata
key+val parsing/formatting to use a new static inner helper class (KnownErr)
SOLR-445: refactor metadata key+val parsing/formatting to use a new static inner helper class (KnownErr)
Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/08bcb769
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/08bcb769
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/08bcb769
Branch: refs/heads/jira/SOLR-445
Commit: 08bcb769bd1e896e719ebb0b4512208c993d9c38
Parents: 98e8c34
Author: Chris Hostetter <ho...@apache.org>
Authored: Tue Feb 23 16:34:47 2016 -0700
Committer: Chris Hostetter <ho...@apache.org>
Committed: Tue Feb 23 16:34:47 2016 -0700
----------------------------------------------------------------------
.../processor/TolerantUpdateProcessor.java | 96 +++++++++++++++++---
.../processor/TolerantUpdateProcessorTest.java | 37 ++++++++
2 files changed, 122 insertions(+), 11 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/08bcb769/solr/core/src/java/org/apache/solr/update/processor/TolerantUpdateProcessor.java
----------------------------------------------------------------------
diff --git a/solr/core/src/java/org/apache/solr/update/processor/TolerantUpdateProcessor.java b/solr/core/src/java/org/apache/solr/update/processor/TolerantUpdateProcessor.java
index 2fd05bf..60fb387 100644
--- a/solr/core/src/java/org/apache/solr/update/processor/TolerantUpdateProcessor.java
+++ b/solr/core/src/java/org/apache/solr/update/processor/TolerantUpdateProcessor.java
@@ -67,8 +67,6 @@ public class TolerantUpdateProcessor extends UpdateRequestProcessor {
*/
private static final String UNKNOWN_ID = "(unknown)"; // nocommit: fail hard and fast if no uniqueKey
-
- private final static String ERR_META_PREFIX = java.lang.invoke.MethodHandles.lookup().lookupClass().getName() + "--";
/**
* Response Header
*/
@@ -197,17 +195,18 @@ public class TolerantUpdateProcessor extends UpdateRequestProcessor {
}
for (int i = 0; i < remoteErrMetadata.size(); i++) {
- String key = remoteErrMetadata.getName(i);
- if (! key.startsWith(ERR_META_PREFIX) ) {
+ KnownErr err = KnownErr.parseMetadataIfKnownErr(remoteErrMetadata.getName(i),
+ remoteErrMetadata.getVal(i));
+ if (null == err) {
+ // some metadata unrelated to this update processor
continue;
}
- String val = remoteErrMetadata.getVal(i);
- if (key.startsWith("id-", ERR_META_PREFIX.length())) {
- CharSequence id = key.subSequence(ERR_META_PREFIX.length() + 3, key.length());
- processError(id, val);
+
+ if (err.type.equals(CmdType.ADD)) { // nocommit: generalize this to work with any CmdType
+ processError(err.id, err.errorValue);
} else {
- log.error("found remote error metadata using our prefix but not a key we expect: " + key, remoteErr);
- assert false;
+ log.error("found remote error metadata we can't handle key: " + err);
+ assert false : "found remote error metadata we can't handle key: " + err;
}
}
}
@@ -335,7 +334,8 @@ public class TolerantUpdateProcessor extends UpdateRequestProcessor {
}
for (int i = 0; i < errors.size(); i++) {
- errMetadata.add(ERR_META_PREFIX + "id-" + errors.getName(i), errors.getVal(i).get("message"));
+ KnownErr err = new KnownErr(CmdType.ADD, errors.getName(i), errors.getVal(i).get("message"));
+ errMetadata.add(err.getMetadataKey(), err.getMetadataValue());
}
}
@@ -346,5 +346,79 @@ public class TolerantUpdateProcessor extends UpdateRequestProcessor {
}
}
+
+ /**
+ * Helper class for dealing with SolrException metadata (String) keys
+ */
+ public static final class KnownErr {
+ // nocommit: switch metadata key parsing/writting to use this class
+ // nocommit: switch error counting to use instances of this class
+
+ private final static String META_PRE = TolerantUpdateProcessor.class.getName() + "--";
+ private final static int META_PRE_LEN = META_PRE.length();
+
+
+ /** returns a KnownErr instance if this metadataKey is one we care about, else null */
+ public static KnownErr parseMetadataIfKnownErr(String metadataKey, String metadataVal) {
+ if (! metadataKey.startsWith(META_PRE)) {
+ return null; // not a key we care about
+ }
+ final int typeEnd = metadataKey.indexOf(':', META_PRE_LEN);
+ assert 0 < typeEnd; // nocommit: better error handling
+ return new KnownErr(CmdType.valueOf(metadataKey.substring(META_PRE_LEN, typeEnd)),
+ metadataKey.substring(typeEnd+1), metadataVal);
+ }
+
+ public final CmdType type;
+ /** may be null depending on type */
+ public final String id;
+ public final String errorValue;
+
+ public KnownErr(CmdType type, String id, String errorValue) {
+ this.type = type;
+ assert null != type;
+
+ this.id = id;
+ assert null != id;
+
+ this.errorValue = errorValue;
+ assert null != errorValue;
+ }
+
+ public String getMetadataKey() {
+ return META_PRE + type + ":" + id;
+ }
+ public String getMetadataValue() {
+ return errorValue;
+ }
+ public String toString() {
+ return getMetadataKey() + "=>" + getMetadataValue();
+ }
+ public int hashCode() {
+ int h = this.getClass().hashCode();
+ h = h * 31 + type.hashCode();
+ h = h * 31 + id.hashCode();
+ h = h * 31 + errorValue.hashCode();
+ return h;
+ }
+ public boolean equals(Object o) {
+ if (o instanceof KnownErr) {
+ KnownErr that = (KnownErr)o;
+ return that.type.equals(this.type)
+ && that.id.equals(this.id)
+ && that.errorValue.equals(this.errorValue);
+ }
+ return false;
+ }
+ }
+ /**
+ * Helper class for dealing with SolrException metadata (String) keys
+ */
+ public static enum CmdType {
+ ADD, DELID, DELQ; // nocommit: others supported types? (commit?) ..
+
+ // if we add support for things like commit, parsing/toString/hashCode logic
+ // needs to be smarter to account for 'id' being null ... "usesId" should be a prop of enum instances
+ }
}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/08bcb769/solr/core/src/test/org/apache/solr/update/processor/TolerantUpdateProcessorTest.java
----------------------------------------------------------------------
diff --git a/solr/core/src/test/org/apache/solr/update/processor/TolerantUpdateProcessorTest.java b/solr/core/src/test/org/apache/solr/update/processor/TolerantUpdateProcessorTest.java
index 8599df8..9d0b8dc 100644
--- a/solr/core/src/test/org/apache/solr/update/processor/TolerantUpdateProcessorTest.java
+++ b/solr/core/src/test/org/apache/solr/update/processor/TolerantUpdateProcessorTest.java
@@ -38,6 +38,8 @@ import org.apache.solr.request.SolrRequestHandler;
import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.servlet.DirectSolrConnection;
import org.apache.solr.update.AddUpdateCommand;
+import org.apache.solr.update.processor.TolerantUpdateProcessor.KnownErr;
+import org.apache.solr.update.processor.TolerantUpdateProcessor.CmdType;
import org.apache.solr.util.BaseTestHarness;
import org.junit.AfterClass;
import org.junit.BeforeClass;
@@ -316,6 +318,41 @@ public class TolerantUpdateProcessorTest extends UpdateProcessorTestBase {
"//lst[@name='errors']/lst[@name='19']"));
}
+
+ public void testKnownErrClass() {
+
+ assertNull(KnownErr.parseMetadataIfKnownErr("some other key", "some value"));
+
+ for (KnownErr in : new KnownErr[] {
+ new KnownErr(CmdType.ADD, "doc1", "some error"),
+ new KnownErr(CmdType.DELID, "doc1", "some diff error"),
+ new KnownErr(CmdType.DELQ, "-field:yakko other_field:wakko", "some other error"),
+ }) {
+ KnownErr out = KnownErr.parseMetadataIfKnownErr(in.getMetadataKey(), in.getMetadataValue());
+ assertNotNull(out);
+ assertEquals(out.type, in.type);
+ assertEquals(out.id, in.id);
+ assertEquals(out.errorValue, in.errorValue);
+ assertEquals(out.hashCode(), in.hashCode());
+ assertEquals(out.toString(), in.toString());
+
+ assertEquals(out, in);
+ assertEquals(in, out);
+
+ }
+
+ assertFalse((new KnownErr(CmdType.ADD, "doc1", "some error")).equals
+ (new KnownErr(CmdType.ADD, "doc2", "some error")));
+ assertFalse((new KnownErr(CmdType.ADD, "doc1", "some error")).equals
+ (new KnownErr(CmdType.ADD, "doc1", "some errorxx")));
+ assertFalse((new KnownErr(CmdType.ADD, "doc1", "some error")).equals
+ (new KnownErr(CmdType.DELID, "doc1", "some error")));
+
+
+ // nocommit: add randomized testing, particularly with non-trivial 'id' values
+
+ }
+
public String update(String chain, String xml) {
DirectSolrConnection connection = new DirectSolrConnection(h.getCore());