LUCENE-5099 - QueryNode should have the ability to detach from its node parent

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1560245 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Adriano Crestani Campos 2014-01-22 03:49:44 +00:00
parent 1e5f5795dc
commit 97cf55d3dd
5 changed files with 60 additions and 11 deletions

View File

@ -105,6 +105,10 @@ New Features
using SortField.setMissingValue(SortField.STRING_LAST). (Rob Muir,
Mike McCandless)
* LUCENE-5099: QueryNode should have the ability to detach from its node
parent. Added QueryNode.removeFromParent() that allows nodes to be
detached from its parent node. (Adriano Crestani)
Build

View File

@ -91,4 +91,8 @@ public interface QueryNode {
*/
public Map<String, Object> getTagMap();
/**
* Removes this query node from its parent.
*/
public void removeFromParent();
}

View File

@ -19,6 +19,7 @@ package org.apache.lucene.queryparser.flexible.core.nodes;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@ -102,18 +103,19 @@ public abstract class QueryNodeImpl implements QueryNode, Cloneable {
// reset parent value
for (QueryNode child : children) {
child.removeFromParent();
}
((QueryNodeImpl) child).setParent(null);
ArrayList<QueryNode> existingChildren = new ArrayList<QueryNode>(getChildren());
for (QueryNode existingChild : existingChildren) {
existingChild.removeFromParent();
}
// allocate new children list
allocate();
// add new children and set parent
for (QueryNode child : children) {
add(child);
}
add(children);
}
@Override
@ -154,7 +156,7 @@ public abstract class QueryNodeImpl implements QueryNode, Cloneable {
if (isLeaf() || this.clauses == null) {
return null;
}
return this.clauses;
return new ArrayList<QueryNode>(this.clauses);
}
@Override
@ -181,8 +183,11 @@ public abstract class QueryNodeImpl implements QueryNode, Cloneable {
private QueryNode parent = null;
private void setParent(QueryNode parent) {
if (this.parent != parent) {
this.removeFromParent();
this.parent = parent;
}
}
@Override
public QueryNode getParent() {
@ -242,4 +247,20 @@ public abstract class QueryNodeImpl implements QueryNode, Cloneable {
return (Map<String, Object>) this.tags.clone();
}
@Override
public void removeFromParent() {
if (this.parent != null) {
List<QueryNode> parentChildren = this.parent.getChildren();
Iterator<QueryNode> it = parentChildren.iterator();
while (it.hasNext()) {
if (it.next() == this) {
it.remove();
}
}
this.parent = null;
}
}
} // end class QueryNodeImpl

View File

@ -18,6 +18,7 @@ package org.apache.lucene.queryparser.flexible.core.nodes;
*/
import java.util.Arrays;
import java.util.Collections;
import org.apache.lucene.util.LuceneTestCase;
@ -44,4 +45,23 @@ public class TestQueryNode extends LuceneTestCase {
}
/* LUCENE-5099 - QueryNodeProcessorImpl should set parent to null before returning on processing */
public void testRemoveFromParent() throws Exception {
BooleanQueryNode booleanNode = new BooleanQueryNode(Collections.<QueryNode>emptyList());
FieldQueryNode fieldNode = new FieldQueryNode("foo", "A", 0, 1);
assertNull(fieldNode.getParent());
booleanNode.add(fieldNode);
assertNotNull(fieldNode.getParent());
fieldNode.removeFromParent();
assertNull(fieldNode.getParent());
booleanNode.add(fieldNode);
assertNotNull(fieldNode.getParent());
booleanNode.set(Collections.<QueryNode>emptyList());
assertNull(fieldNode.getParent());
}
}

View File

@ -18,7 +18,6 @@ package org.apache.lucene.queryparser.util;
*/
import java.io.IOException;
import java.io.Reader;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
@ -1066,6 +1065,7 @@ public abstract class QueryParserTestBase extends LuceneTestCase {
assertTrue(bq.getClauses()[1].getQuery() instanceof MatchAllDocsQuery);
}
@SuppressWarnings("unused")
private void assertHits(int expected, String query, IndexSearcher is) throws Exception {
String oldDefaultField = getDefaultField();
setDefaultField("date");