SOLR-3215: Clone SolrInputDocument when distrib indexing so that update processors after the distrib update process do not process the document twice.

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1363013 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark Robert Miller 2012-07-18 16:52:42 +00:00
parent 843d2d729e
commit 83e07fec09
5 changed files with 106 additions and 3 deletions

View File

@ -117,6 +117,9 @@ Other Changes
* SOLR-3600: Raise the default zkClientTimeout from 10 seconds to 15 seconds. (Mark Miller)
* SOLR-3215: Clone SolrInputDocument when distrib indexing so that update processors after
the distrib update process do not process the document twice. (Mark Miller)
================== 4.0.0-ALPHA ==================
More information about this release, including any errata related to the
release notes, upgrade instructions, or other changes may be found online at:

View File

@ -285,7 +285,10 @@ public class DistributedUpdateProcessor extends UpdateRequestProcessor {
boolean dropCmd = false;
if (!forwardToLeader) {
dropCmd = versionAdd(cmd);
// clone the original doc
SolrInputDocument clonedDoc = cmd.solrDoc.deepCopy();
dropCmd = versionAdd(cmd, clonedDoc);
cmd.solrDoc = clonedDoc;
}
if (dropCmd) {
@ -393,10 +396,11 @@ public class DistributedUpdateProcessor extends UpdateRequestProcessor {
/**
* @param cmd
* @param cloneDoc needs the version if it's assigned
* @return whether or not to drop this cmd
* @throws IOException
*/
private boolean versionAdd(AddUpdateCommand cmd) throws IOException {
private boolean versionAdd(AddUpdateCommand cmd, SolrInputDocument cloneDoc) throws IOException {
BytesRef idBytes = cmd.getIndexedId();
if (vinfo == null || idBytes == null) {
@ -469,6 +473,7 @@ public class DistributedUpdateProcessor extends UpdateRequestProcessor {
long version = vinfo.getNewClock();
cmd.setVersion(version);
cmd.getSolrInputDocument().setField(VersionInfo.VERSION_FIELD, version);
cloneDoc.setField(VersionInfo.VERSION_FIELD, version);
bucket.updateHighest(version);
} else {
// The leader forwarded us this update.

View File

@ -0,0 +1,71 @@
package org.apache.solr;
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;
public class TestDocumentBuilder extends LuceneTestCase {
@Test
public void testDeepCopy() throws IOException {
SolrInputDocument doc = new SolrInputDocument();
doc.addField("field1", "value1");
doc.addField("field2", "value1");
doc.addField("field3", "value2");
doc.addField("field4", 15);
List<Integer> list = new ArrayList<Integer>();
list.add(45);
list.add(33);
list.add(20);
doc.addField("field5", list);
doc.setDocumentBoost(5f);
SolrInputDocument clone = doc.deepCopy();
System.out.println("doc1: "+ doc);
System.out.println("clone: "+ clone);
assertNotSame(doc, clone);
Collection<String> fieldNames = doc.getFieldNames();
for (String name : fieldNames) {
Collection<Object> values = doc.getFieldValues(name);
Collection<Object> cloneValues = clone.getFieldValues(name);
assertEquals(values.size(), cloneValues.size());
assertNotSame(values, cloneValues);
Iterator<Object> cloneIt = cloneValues.iterator();
for (Object value : values) {
Object cloneValue = cloneIt.next();
assertSame(value, cloneValue);
}
}
}
}

View File

@ -18,10 +18,10 @@
package org.apache.solr.common;
import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Collection;
import java.util.Set;
/**
@ -182,6 +182,15 @@ public class SolrInputDocument implements Map<String,SolrInputField>, Iterable<S
return "SolrInputDocument" + _fields.values();
}
public SolrInputDocument deepCopy() {
SolrInputDocument clone = new SolrInputDocument();
Set<Entry<String,SolrInputField>> entries = _fields.entrySet();
for (Map.Entry<String,SolrInputField> fieldEntry : entries) {
clone._fields.put(fieldEntry.getKey(), fieldEntry.getValue().deepCopy());
}
clone._documentBoost = _documentBoost;
return clone;
}
//---------------------------------------------------
// MAP interface

View File

@ -199,4 +199,19 @@ public class SolrInputField implements Iterable<Object>, Serializable
{
return name + ((boost == 1.0) ? "=" : ("("+boost+")=")) + value;
}
public SolrInputField deepCopy() {
SolrInputField clone = new SolrInputField(name);
clone.boost = boost;
// We can't clone here, so we rely on simple primitives
if (value instanceof Collection) {
Collection<Object> values = (Collection<Object>) value;
Collection<Object> cloneValues = new ArrayList<Object>(values.size());
cloneValues.addAll(values);
clone.value = cloneValues;
} else {
clone.value = value;
}
return clone;
}
}