Apache Solr open-source search software
LUCENE-5099 - QueryNode should have the ability to detach from its node parent
Adriano Crestani Campos97cf55d3dd50
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 128fdfc3ce8..577e16ca24f 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -104,6 +104,10 @@ New Featuresusing SortField.setMissingValue(SortField.STRING_FIRST), or last,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)Builddiff --git a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/QueryNode.java b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/QueryNode.javaindex fc539006c22..42ec586b3fa 100644--- a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/QueryNode.java+++ b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/QueryNode.java@@ -91,4 +91,8 @@ public interface QueryNode {*/public Map<String, Object> getTagMap();+ /**+ * Removes this query node from its parent.+ */+ public void removeFromParent();}diff --git a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/QueryNodeImpl.java b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/QueryNodeImpl.javaindex cc5d7507568..81fd9bb64d2 100644--- a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/QueryNodeImpl.java+++ b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/QueryNodeImpl.java@@ -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 valuefor (QueryNode child : children) {-- ((QueryNodeImpl) child).setParent(null);-+ child.removeFromParent();}-++ ArrayList<QueryNode> existingChildren = new ArrayList<QueryNode>(getChildren());+ for (QueryNode existingChild : existingChildren) {+ existingChild.removeFromParent();+ }+// allocate new children listallocate();-+// 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,7 +183,10 @@ public abstract class QueryNodeImpl implements QueryNode, Cloneable {private QueryNode parent = null;private void setParent(QueryNode parent) {- this.parent = parent;+ if (this.parent != parent) {+ this.removeFromParent();+ this.parent = parent;+ }}@Override@@ -241,5 +246,21 @@ public abstract class QueryNodeImpl implements QueryNode, Cloneable {public Map<String, Object> getTagMap() {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 QueryNodeImpldiff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/core/nodes/TestQueryNode.java b/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/core/nodes/TestQueryNode.javaindex f3b2e8da81b..5b60b6930c3 100644--- a/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/core/nodes/TestQueryNode.java+++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/core/nodes/TestQueryNode.java@@ -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());+ }+}diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/util/QueryParserTestBase.java b/lucene/queryparser/src/test/org/apache/lucene/queryparser/util/QueryParserTestBase.javaindex 0c39939388f..a1890a14f1d 100644--- a/lucene/queryparser/src/test/org/apache/lucene/queryparser/util/QueryParserTestBase.java+++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/util/QueryParserTestBase.java@@ -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");