LUCENE-5805: QueryNodeImpl.removeFromParent was doing nothing, in a costly way

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1683839 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2015-06-05 18:47:16 +00:00
parent f2291c1356
commit 2cb14e923c
4 changed files with 42 additions and 13 deletions

View File

@ -74,6 +74,9 @@ Bug fixes
* LUCENE-6520: Geo3D GeoPath.done() would throw an NPE if adjacent path * LUCENE-6520: Geo3D GeoPath.done() would throw an NPE if adjacent path
segments were co-linear. (Karl Wright via David Smiley) segments were co-linear. (Karl Wright via David Smiley)
* LUCENE-5805: QueryNodeImpl.removeFromParent was doing nothing in a
costly manner (Christoph Kaser, Cao Manh Dat via Mike McCAndless)
Changes in Runtime Behavior Changes in Runtime Behavior
* LUCENE-6501: The subreader structure in ParallelCompositeReader * LUCENE-6501: The subreader structure in ParallelCompositeReader

View File

@ -95,4 +95,11 @@ public interface QueryNode {
* Removes this query node from its parent. * Removes this query node from its parent.
*/ */
public void removeFromParent(); public void removeFromParent();
/**
* Remove a child node
* @param childNode Which child to remove
*/
public void removeChildren(QueryNode childNode);
} }

View File

@ -248,18 +248,22 @@ public abstract class QueryNodeImpl implements QueryNode, Cloneable {
} }
@Override @Override
public void removeFromParent() { public void removeChildren(QueryNode childNode){
if (this.parent != null) { Iterator<QueryNode> it = this.clauses.iterator();
List<QueryNode> parentChildren = this.parent.getChildren();
Iterator<QueryNode> it = parentChildren.iterator();
while(it.hasNext()){ while(it.hasNext()){
if (it.next() == this) { if(it.next() == childNode){
it.remove(); it.remove();
} }
} }
childNode.removeFromParent();
}
@Override
public void removeFromParent() {
if (this.parent != null) {
QueryNode parent = this.parent;
this.parent = null; this.parent = null;
parent.removeChildren(this);
} }
} }

View File

@ -45,6 +45,7 @@ public class TestQueryNode extends LuceneTestCase {
} }
/* LUCENE-5099 - QueryNodeProcessorImpl should set parent to null before returning on processing */ /* LUCENE-5099 - QueryNodeProcessorImpl should set parent to null before returning on processing */
public void testRemoveFromParent() throws Exception { public void testRemoveFromParent() throws Exception {
BooleanQueryNode booleanNode = new BooleanQueryNode(Collections.<QueryNode>emptyList()); BooleanQueryNode booleanNode = new BooleanQueryNode(Collections.<QueryNode>emptyList());
@ -56,6 +57,8 @@ public class TestQueryNode extends LuceneTestCase {
fieldNode.removeFromParent(); fieldNode.removeFromParent();
assertNull(fieldNode.getParent()); assertNull(fieldNode.getParent());
/* LUCENE-5805 - QueryNodeImpl.removeFromParent does a lot of work without any effect */
assertFalse(booleanNode.getChildren().contains(fieldNode));
booleanNode.add(fieldNode); booleanNode.add(fieldNode);
assertNotNull(fieldNode.getParent()); assertNotNull(fieldNode.getParent());
@ -64,4 +67,16 @@ public class TestQueryNode extends LuceneTestCase {
assertNull(fieldNode.getParent()); assertNull(fieldNode.getParent());
} }
public void testRemoveChildren() throws Exception{
BooleanQueryNode booleanNode = new BooleanQueryNode(Collections.<QueryNode>emptyList());
FieldQueryNode fieldNode = new FieldQueryNode("foo", "A", 0, 1);
booleanNode.add(fieldNode);
assertTrue(booleanNode.getChildren().size() == 1);
booleanNode.removeChildren(fieldNode);
assertTrue(booleanNode.getChildren().size()==0);
assertNull(fieldNode.getParent());
}
} }