diff --git a/CHANGES.txt b/CHANGES.txt index f6588256d6b..5005b831620 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -312,8 +312,10 @@ Other Changes 18. SOLR-1022: Better "ignored" field in example schema.xml (Peter Wolanin via hossman) -Build +19. SOLR-967: New type-safe constructor for NamedList (Kay Kay via hossman) + +Build ---------------------- 1. SOLR-776: Added in ability to sign artifacts via Ant for releases (gsingers) diff --git a/src/common/org/apache/solr/common/util/NamedList.java b/src/common/org/apache/solr/common/util/NamedList.java index e1d10254636..cb0e7ea5944 100644 --- a/src/common/org/apache/solr/common/util/NamedList.java +++ b/src/common/org/apache/solr/common/util/NamedList.java @@ -57,15 +57,61 @@ public class NamedList implements Cloneable, Serializable, Iterable + * Modifying the contents of the Entry[] after calling this constructor may change + * the NamedList (in future versions of Solr), but this is not garunteed and should + * not be relied upon. To modify the NamedList, refer to {@link #add(String, Object)} + * or {@link #remove(String)}. + *

+ * + * @param nameValuePairs the name value pairs + */ + public NamedList(Map.Entry[] nameValuePairs) { + nvPairs = nameValueMapToList(nameValuePairs); + } + /** * Creates an instance backed by an explicitly specified list of * pairwise names/values. * - * @param nameValuePairs underlying List which should be used to implement a NamedList; modifying this List will affect the NamedList. + *

+ * When using this constructor, runtime typesafety is only garunteed if the all + * even numbered elements of the input list are of type "T". + *

+ * + * @param nameValuePairs underlying List which should be used to implement a NamedList + * @deprecated Use {@link #NamedList(java.util.Map.Entry[])} for the NamedList instantiation */ + @Deprecated public NamedList(List nameValuePairs) { nvPairs=nameValuePairs; } + + /** + * Method to serialize Map.Entry<String, ?> to a List in which the even + * indexed elements (0,2,4. ..etc) are Strings and odd elements (1,3,5,) are of + * the type "T". + * + * @param nameValuePairs + * @return Modified List as per the above description + * @deprecated This a temporary placeholder method until the guts of the class + * are actually replaced by List<String, ?>. + * @see https://issues.apache.org/jira/browse/SOLR-912 + */ + @Deprecated + private List nameValueMapToList(Map.Entry[] nameValuePairs) { + List result = new ArrayList(); + for (Map.Entry ent : nameValuePairs) { + result.add(ent.getKey()); + result.add(ent.getValue()); + } + return result; + } /** The total number of name/value pairs */ public int size() { @@ -211,6 +257,43 @@ public class NamedList implements Cloneable, Serializable, Iterable to store the key-value + * relationship in NamedList (the keys of which are String-s) + * + * @param + */ + public static final class NamedListEntry implements Map.Entry { + + public NamedListEntry() { + + } + + public NamedListEntry(String _key, T _value) { + key = _key; + value = _value; + } + + public String getKey() { + return key; + } + + public T getValue() { + return value; + } + + public T setValue(T _value) { + T oldValue = value; + value = _value; + return oldValue; + } + + private String key; + + private T value; + } /** * Iterates over the Map and sequentially adds it's key/value pairs diff --git a/src/common/org/apache/solr/common/util/SimpleOrderedMap.java b/src/common/org/apache/solr/common/util/SimpleOrderedMap.java index 0378753e8cc..e00938b8e86 100755 --- a/src/common/org/apache/solr/common/util/SimpleOrderedMap.java +++ b/src/common/org/apache/solr/common/util/SimpleOrderedMap.java @@ -49,9 +49,14 @@ public class SimpleOrderedMap extends NamedList { * * @param nameValuePairs underlying List which should be used to implement a SimpleOrderedMap; modifying this List will affect the SimpleOrderedMap. */ + @Deprecated public SimpleOrderedMap(List nameValuePairs) { super(nameValuePairs); } + + public SimpleOrderedMap(Map.Entry[] nameValuePairs) { + super(nameValuePairs); + } @Override public SimpleOrderedMap clone() { diff --git a/src/java/org/apache/solr/handler/component/DebugComponent.java b/src/java/org/apache/solr/handler/component/DebugComponent.java index 99ef332e1c2..2d958f18692 100644 --- a/src/java/org/apache/solr/handler/component/DebugComponent.java +++ b/src/java/org/apache/solr/handler/component/DebugComponent.java @@ -108,7 +108,7 @@ public class DebugComponent extends SearchComponent NamedList info = null; NamedList explain = new SimpleOrderedMap(); - Object[] arr = new Object[rb.resultIds.size() * 2]; + Map.Entry[] arr = new NamedList.NamedListEntry[rb.resultIds.size()]; for (ShardRequest sreq : rb.finished) { if ((sreq.purpose & ShardRequest.PURPOSE_GET_DEBUG) == 0) continue; @@ -123,13 +123,12 @@ public class DebugComponent extends SearchComponent // TODO: lookup won't work for non-string ids... String vs Float ShardDoc sdoc = rb.resultIds.get(id); int idx = sdoc.positionInResponse; - arr[idx<<1] = id; - arr[(idx<<1)+1] = sexplain.getVal(i); + arr[idx] = new NamedList.NamedListEntry( id, sexplain.getVal(i)); } } } - explain = HighlightComponent.removeNulls(new SimpleOrderedMap(Arrays.asList(arr))); + explain = HighlightComponent.removeNulls(new SimpleOrderedMap(arr)); if (info == null) { info = new SimpleOrderedMap(); diff --git a/src/java/org/apache/solr/handler/component/HighlightComponent.java b/src/java/org/apache/solr/handler/component/HighlightComponent.java index c04e1168630..c2e206f39dd 100644 --- a/src/java/org/apache/solr/handler/component/HighlightComponent.java +++ b/src/java/org/apache/solr/handler/component/HighlightComponent.java @@ -30,6 +30,7 @@ import org.apache.solr.request.SolrQueryRequest; import java.io.IOException; import java.net.URL; import java.util.Arrays; +import java.util.Map; /** * TODO! @@ -115,7 +116,7 @@ public class HighlightComponent extends SearchComponent if (rb.doHighlights && rb.stage == ResponseBuilder.STAGE_GET_FIELDS) { NamedList hlResult = new SimpleOrderedMap(); - Object[] arr = new Object[rb.resultIds.size() * 2]; + Map.Entry[] arr = new NamedList.NamedListEntry[rb.resultIds.size()]; // TODO: make a generic routine to do automatic merging of id keyed data for (ShardRequest sreq : rb.finished) { @@ -126,14 +127,13 @@ public class HighlightComponent extends SearchComponent String id = hl.getName(i); ShardDoc sdoc = rb.resultIds.get(id); int idx = sdoc.positionInResponse; - arr[idx<<1] = id; - arr[(idx<<1)+1] = hl.getVal(i); + arr[idx] = new NamedList.NamedListEntry(id, hl.getVal(i)); } } } // remove nulls in case not all docs were able to be retrieved - rb.rsp.add("highlighting", removeNulls(new SimpleOrderedMap(Arrays.asList(arr)))); + rb.rsp.add("highlighting", removeNulls(new SimpleOrderedMap(arr))); } } diff --git a/src/java/org/apache/solr/util/NamedList.java b/src/java/org/apache/solr/util/NamedList.java index 9b24ff9c085..3954c2b1c57 100644 --- a/src/java/org/apache/solr/util/NamedList.java +++ b/src/java/org/apache/solr/util/NamedList.java @@ -18,6 +18,7 @@ package org.apache.solr.util; import java.util.List; +import java.util.Map; /** * This class is scheduled for deletion. Please update your code to the moved package. @@ -30,7 +31,12 @@ public class NamedList extends org.apache.solr.common.util.NamedList { super(); } + @Deprecated public NamedList(List nameValuePairs) { super(nameValuePairs); } + + public NamedList(Map.Entry[] nameValuePairs) { + super(nameValuePairs); + } } diff --git a/src/java/org/apache/solr/util/SimpleOrderedMap.java b/src/java/org/apache/solr/util/SimpleOrderedMap.java index bb4c206f021..85dbecf89cd 100644 --- a/src/java/org/apache/solr/util/SimpleOrderedMap.java +++ b/src/java/org/apache/solr/util/SimpleOrderedMap.java @@ -17,6 +17,7 @@ package org.apache.solr.util; import java.util.List; +import java.util.Map; /** * This class is scheduled for deletion. Please update your code to the moved package. @@ -30,7 +31,12 @@ public class SimpleOrderedMap extends org.apache.solr.common.util.SimpleOrder super(); } + @Deprecated public SimpleOrderedMap(List nameValuePairs) { super(nameValuePairs); } + + public SimpleOrderedMap(Map.Entry [] nameValuePairs) { + super(nameValuePairs); + } } diff --git a/src/java/org/apache/solr/util/TestHarness.java b/src/java/org/apache/solr/util/TestHarness.java index ade2511520d..ba86f151c32 100644 --- a/src/java/org/apache/solr/util/TestHarness.java +++ b/src/java/org/apache/solr/util/TestHarness.java @@ -33,6 +33,7 @@ import org.apache.solr.request.SolrQueryResponse; import org.apache.solr.schema.IndexSchema; import org.w3c.dom.Document; import org.xml.sax.SAXException; +import org.apache.solr.common.util.NamedList.NamedListEntry; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -47,8 +48,10 @@ import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; import java.io.UnsupportedEncodingException; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; @@ -554,8 +557,14 @@ public class TestHarness { return new LocalSolrQueryRequest(TestHarness.this.getCore(), q[0], qtype, start, limit, args); } - - return new LocalSolrQueryRequest(TestHarness.this.getCore(),new NamedList(Arrays.asList(q))); + if (q.length%2 != 0) { + throw new RuntimeException("The length of the string array (query arguments) needs to be even"); + } + Map.Entry [] entries = new NamedListEntry[q.length / 2]; + for (int i = 0; i < q.length; i += 2) { + entries[i/2] = new NamedListEntry(q[i], q[i+1]); + } + return new LocalSolrQueryRequest(TestHarness.this.getCore(), new NamedList(entries)); } } }