Skip to content

Apache Lucene open-source search software

SOLR-5148: optimization - lazy create child doc list

Yonik Seeleyf2b3d010b37c
diff --git a/solr/core/src/java/org/apache/solr/update/AddUpdateCommand.java b/solr/core/src/java/org/apache/solr/update/AddUpdateCommand.java
index 56561750fed..3facac9f554 100644
--- a/solr/core/src/java/org/apache/solr/update/AddUpdateCommand.java
+++ b/solr/core/src/java/org/apache/solr/update/AddUpdateCommand.java
@@ -197,8 +197,11 @@ public class AddUpdateCommand extends UpdateCommand implements Iterable<IndexDoc
}
private void recUnwrapp(List<SolrInputDocument> unwrappedDocs, SolrInputDocument currentDoc) {
- for (SolrInputDocument child : currentDoc.getChildDocuments()) {
- recUnwrapp(unwrappedDocs, child);
+ List<SolrInputDocument> children = currentDoc.getChildDocuments();
+ if (children != null) {
+ for (SolrInputDocument child : children) {
+ recUnwrapp(unwrappedDocs, child);
+ }
}
unwrappedDocs.add(currentDoc);
}
diff --git a/solr/core/src/test/org/apache/solr/update/AddBlockUpdateTest.java b/solr/core/src/test/org/apache/solr/update/AddBlockUpdateTest.java
index b4929bfe542..f0404453ec3 100644
--- a/solr/core/src/test/org/apache/solr/update/AddBlockUpdateTest.java
+++ b/solr/core/src/test/org/apache/solr/update/AddBlockUpdateTest.java
@@ -424,7 +424,8 @@ public class AddBlockUpdateTest extends SolrTestCaseJ4 {
assertEquals("v2", result.getFieldValue("parent_f2"));
List<SolrInputDocument> resultChilds = result.getChildDocuments();
- assertEquals(childsNum, resultChilds.size());
+ int resultChildsSize = resultChilds == null ? 0 : resultChilds.size();
+ assertEquals(childsNum, resultChildsSize);
for (int childIndex = 0; childIndex < childsNum; ++childIndex) {
SolrInputDocument child = resultChilds.get(childIndex);
@@ -433,7 +434,9 @@ public class AddBlockUpdateTest extends SolrTestCaseJ4 {
}
List<SolrInputDocument> grandChilds = child.getChildDocuments();
- assertEquals(childIndex * 2, grandChilds.size());
+ int grandChildsSize = grandChilds == null ? 0 : grandChilds.size();
+
+ assertEquals(childIndex * 2, grandChildsSize);
for (int grandIndex = 0; grandIndex < childIndex * 2; ++grandIndex) {
SolrInputDocument grandChild = grandChilds.get(grandIndex);
assertFalse(grandChild.hasChildDocuments());
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/util/ClientUtils.java b/solr/solrj/src/java/org/apache/solr/client/solrj/util/ClientUtils.java
index 2385512a1a0..cb7660594d8 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/util/ClientUtils.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/util/ClientUtils.java
@@ -133,9 +133,11 @@ public class ClientUtils
}
}
}
-
- for (SolrInputDocument childDocument : doc.getChildDocuments()) {
- writeXML(childDocument, writer);
+
+ if (doc.hasChildDocuments()) {
+ for (SolrInputDocument childDocument : doc.getChildDocuments()) {
+ writeXML(childDocument, writer);
+ }
}
writer.write("</doc>");
diff --git a/solr/solrj/src/java/org/apache/solr/common/SolrInputDocument.java b/solr/solrj/src/java/org/apache/solr/common/SolrInputDocument.java
index 0fdf78b7a39..f731910f196 100644
--- a/solr/solrj/src/java/org/apache/solr/common/SolrInputDocument.java
+++ b/solr/solrj/src/java/org/apache/solr/common/SolrInputDocument.java
@@ -43,12 +43,10 @@ public class SolrInputDocument implements Map<String,SolrInputField>, Iterable<S
public SolrInputDocument() {
_fields = new LinkedHashMap<String,SolrInputField>();
- _childDocuments = new ArrayList<SolrInputDocument>();
}
public SolrInputDocument(Map<String,SolrInputField> fields) {
_fields = fields;
- _childDocuments = new ArrayList<SolrInputDocument>();
}
/**
@@ -60,9 +58,7 @@ public class SolrInputDocument implements Map<String,SolrInputField>, Iterable<S
if( _fields != null ) {
_fields.clear();
}
- if (_childDocuments != null) {
- _childDocuments.clear();
- }
+ _childDocuments = null;
}
///////////////////////////////////////////////////////////////////
@@ -198,7 +194,9 @@ public class SolrInputDocument implements Map<String,SolrInputField>, Iterable<S
@Override
public String toString()
{
- return "SolrInputDocument(fields: " + _fields.values() + ", childs: " + _childDocuments + ")";
+ return "SolrInputDocument(fields: " + _fields.values()
+ + ( _childDocuments == null ? "" : (", children: " + _childDocuments) )
+ + ")";
}
public SolrInputDocument deepCopy() {
@@ -208,11 +206,13 @@ public class SolrInputDocument implements Map<String,SolrInputField>, Iterable<S
clone._fields.put(fieldEntry.getKey(), fieldEntry.getValue().deepCopy());
}
clone._documentBoost = _documentBoost;
-
- clone._childDocuments = new ArrayList<SolrInputDocument>(_childDocuments.size());
- for (SolrInputDocument child : _childDocuments) {
- clone._childDocuments.add(child.deepCopy());
- }
+
+ if (_childDocuments != null) {
+ clone._childDocuments = new ArrayList<SolrInputDocument>(_childDocuments.size());
+ for (SolrInputDocument child : _childDocuments) {
+ clone._childDocuments.add(child.deepCopy());
+ }
+ }
return clone;
}
@@ -277,6 +277,9 @@ public class SolrInputDocument implements Map<String,SolrInputField>, Iterable<S
}
public void addChildDocument(SolrInputDocument child) {
+ if (_childDocuments == null) {
+ _childDocuments = new ArrayList<SolrInputDocument>();
+ }
_childDocuments.add(child);
}
@@ -285,7 +288,8 @@ public class SolrInputDocument implements Map<String,SolrInputField>, Iterable<S
addChildDocument(child);
}
}
-
+
+ /** Returns the list of child documents, or null if none. */
public List<SolrInputDocument> getChildDocuments() {
return _childDocuments;
}
diff --git a/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java b/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java
index 583c33eb35b..cd5ae826ec6 100644
--- a/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java
+++ b/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java
@@ -387,7 +387,8 @@ public class JavaBinCodec {
public void writeSolrInputDocument(SolrInputDocument sdoc) throws IOException {
writeTag(SOLRINPUTDOC, sdoc.size());
- writeTag(SOLRINPUTDOC_CHILDS, sdoc.getChildDocuments().size());
+ List<SolrInputDocument> children = sdoc.getChildDocuments();
+ writeTag(SOLRINPUTDOC_CHILDS, children==null ? 0 : children.size());
writeFloat(sdoc.getDocumentBoost());
for (SolrInputField inputField : sdoc.values()) {
if (inputField.getBoost() != 1.0f) {
@@ -396,8 +397,10 @@ public class JavaBinCodec {
writeExternString(inputField.getName());
writeVal(inputField.getValue());
}
- for (SolrInputDocument child : sdoc.getChildDocuments()) {
- writeSolrInputDocument(child);
+ if (children != null) {
+ for (SolrInputDocument child : sdoc.getChildDocuments()) {
+ writeSolrInputDocument(child);
+ }
}
}