SOLR-5148: optimization - lazy create child doc list

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1522680 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2013-09-12 17:44:09 +00:00
parent 534a9f3080
commit f2b3d010b3
5 changed files with 37 additions and 22 deletions

View File

@ -197,8 +197,11 @@ public class AddUpdateCommand extends UpdateCommand implements Iterable<IndexDoc
} }
private void recUnwrapp(List<SolrInputDocument> unwrappedDocs, SolrInputDocument currentDoc) { private void recUnwrapp(List<SolrInputDocument> unwrappedDocs, SolrInputDocument currentDoc) {
for (SolrInputDocument child : currentDoc.getChildDocuments()) { List<SolrInputDocument> children = currentDoc.getChildDocuments();
recUnwrapp(unwrappedDocs, child); if (children != null) {
for (SolrInputDocument child : children) {
recUnwrapp(unwrappedDocs, child);
}
} }
unwrappedDocs.add(currentDoc); unwrappedDocs.add(currentDoc);
} }

View File

@ -424,7 +424,8 @@ public class AddBlockUpdateTest extends SolrTestCaseJ4 {
assertEquals("v2", result.getFieldValue("parent_f2")); assertEquals("v2", result.getFieldValue("parent_f2"));
List<SolrInputDocument> resultChilds = result.getChildDocuments(); 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) { for (int childIndex = 0; childIndex < childsNum; ++childIndex) {
SolrInputDocument child = resultChilds.get(childIndex); SolrInputDocument child = resultChilds.get(childIndex);
@ -433,7 +434,9 @@ public class AddBlockUpdateTest extends SolrTestCaseJ4 {
} }
List<SolrInputDocument> grandChilds = child.getChildDocuments(); 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) { for (int grandIndex = 0; grandIndex < childIndex * 2; ++grandIndex) {
SolrInputDocument grandChild = grandChilds.get(grandIndex); SolrInputDocument grandChild = grandChilds.get(grandIndex);
assertFalse(grandChild.hasChildDocuments()); assertFalse(grandChild.hasChildDocuments());

View File

@ -133,9 +133,11 @@ public class ClientUtils
} }
} }
} }
for (SolrInputDocument childDocument : doc.getChildDocuments()) { if (doc.hasChildDocuments()) {
writeXML(childDocument, writer); for (SolrInputDocument childDocument : doc.getChildDocuments()) {
writeXML(childDocument, writer);
}
} }
writer.write("</doc>"); writer.write("</doc>");

View File

@ -43,12 +43,10 @@ public class SolrInputDocument implements Map<String,SolrInputField>, Iterable<S
public SolrInputDocument() { public SolrInputDocument() {
_fields = new LinkedHashMap<String,SolrInputField>(); _fields = new LinkedHashMap<String,SolrInputField>();
_childDocuments = new ArrayList<SolrInputDocument>();
} }
public SolrInputDocument(Map<String,SolrInputField> fields) { public SolrInputDocument(Map<String,SolrInputField> fields) {
_fields = fields; _fields = fields;
_childDocuments = new ArrayList<SolrInputDocument>();
} }
/** /**
@ -60,9 +58,7 @@ public class SolrInputDocument implements Map<String,SolrInputField>, Iterable<S
if( _fields != null ) { if( _fields != null ) {
_fields.clear(); _fields.clear();
} }
if (_childDocuments != null) { _childDocuments = null;
_childDocuments.clear();
}
} }
/////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////
@ -198,7 +194,9 @@ public class SolrInputDocument implements Map<String,SolrInputField>, Iterable<S
@Override @Override
public String toString() public String toString()
{ {
return "SolrInputDocument(fields: " + _fields.values() + ", childs: " + _childDocuments + ")"; return "SolrInputDocument(fields: " + _fields.values()
+ ( _childDocuments == null ? "" : (", children: " + _childDocuments) )
+ ")";
} }
public SolrInputDocument deepCopy() { 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._fields.put(fieldEntry.getKey(), fieldEntry.getValue().deepCopy());
} }
clone._documentBoost = _documentBoost; clone._documentBoost = _documentBoost;
clone._childDocuments = new ArrayList<SolrInputDocument>(_childDocuments.size()); if (_childDocuments != null) {
for (SolrInputDocument child : _childDocuments) { clone._childDocuments = new ArrayList<SolrInputDocument>(_childDocuments.size());
clone._childDocuments.add(child.deepCopy()); for (SolrInputDocument child : _childDocuments) {
} clone._childDocuments.add(child.deepCopy());
}
}
return clone; return clone;
} }
@ -277,6 +277,9 @@ public class SolrInputDocument implements Map<String,SolrInputField>, Iterable<S
} }
public void addChildDocument(SolrInputDocument child) { public void addChildDocument(SolrInputDocument child) {
if (_childDocuments == null) {
_childDocuments = new ArrayList<SolrInputDocument>();
}
_childDocuments.add(child); _childDocuments.add(child);
} }
@ -285,7 +288,8 @@ public class SolrInputDocument implements Map<String,SolrInputField>, Iterable<S
addChildDocument(child); addChildDocument(child);
} }
} }
/** Returns the list of child documents, or null if none. */
public List<SolrInputDocument> getChildDocuments() { public List<SolrInputDocument> getChildDocuments() {
return _childDocuments; return _childDocuments;
} }

View File

@ -387,7 +387,8 @@ public class JavaBinCodec {
public void writeSolrInputDocument(SolrInputDocument sdoc) throws IOException { public void writeSolrInputDocument(SolrInputDocument sdoc) throws IOException {
writeTag(SOLRINPUTDOC, sdoc.size()); 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()); writeFloat(sdoc.getDocumentBoost());
for (SolrInputField inputField : sdoc.values()) { for (SolrInputField inputField : sdoc.values()) {
if (inputField.getBoost() != 1.0f) { if (inputField.getBoost() != 1.0f) {
@ -396,8 +397,10 @@ public class JavaBinCodec {
writeExternString(inputField.getName()); writeExternString(inputField.getName());
writeVal(inputField.getValue()); writeVal(inputField.getValue());
} }
for (SolrInputDocument child : sdoc.getChildDocuments()) { if (children != null) {
writeSolrInputDocument(child); for (SolrInputDocument child : sdoc.getChildDocuments()) {
writeSolrInputDocument(child);
}
} }
} }