You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-commits@lucene.apache.org by ho...@apache.org on 2009/02/19 23:20:32 UTC

svn commit: r746031 - in /lucene/solr/trunk: ./ src/common/org/apache/solr/common/util/ src/java/org/apache/solr/handler/component/ src/java/org/apache/solr/util/

Author: hossman
Date: Thu Feb 19 22:20:32 2009
New Revision: 746031

URL: http://svn.apache.org/viewvc?rev=746031&view=rev
Log:
SOLR-967: New type-safe constructor for NamedList

Modified:
    lucene/solr/trunk/CHANGES.txt
    lucene/solr/trunk/src/common/org/apache/solr/common/util/NamedList.java
    lucene/solr/trunk/src/common/org/apache/solr/common/util/SimpleOrderedMap.java
    lucene/solr/trunk/src/java/org/apache/solr/handler/component/DebugComponent.java
    lucene/solr/trunk/src/java/org/apache/solr/handler/component/HighlightComponent.java
    lucene/solr/trunk/src/java/org/apache/solr/util/NamedList.java
    lucene/solr/trunk/src/java/org/apache/solr/util/SimpleOrderedMap.java
    lucene/solr/trunk/src/java/org/apache/solr/util/TestHarness.java

Modified: lucene/solr/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/CHANGES.txt?rev=746031&r1=746030&r2=746031&view=diff
==============================================================================
--- lucene/solr/trunk/CHANGES.txt (original)
+++ lucene/solr/trunk/CHANGES.txt Thu Feb 19 22:20:32 2009
@@ -312,8 +312,10 @@
 
 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)
 

Modified: lucene/solr/trunk/src/common/org/apache/solr/common/util/NamedList.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/common/org/apache/solr/common/util/NamedList.java?rev=746031&r1=746030&r2=746031&view=diff
==============================================================================
--- lucene/solr/trunk/src/common/org/apache/solr/common/util/NamedList.java (original)
+++ lucene/solr/trunk/src/common/org/apache/solr/common/util/NamedList.java Thu Feb 19 22:20:32 2009
@@ -57,15 +57,61 @@
     nvPairs = new ArrayList();
   }
 
+
+  /**
+   * Creates a NamedList instance containing the "name,value" pairs contained in the
+   * Entry[].
+   * 
+   * <p>
+   * 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)}.
+   * </p>
+   *
+   * @param nameValuePairs the name value pairs
+   */
+  public NamedList(Map.Entry<String, ? extends T>[] 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.
+   * <p>
+   * When using this constructor, runtime typesafety is only garunteed if the all
+   * even numbered elements of the input list are of type "T".
+   * </p>
+   *
+   * @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&lt;String, ?&gt; 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&lt;String, ?&gt;.
+   * @see https://issues.apache.org/jira/browse/SOLR-912
+   */
+  @Deprecated
+  private List  nameValueMapToList(Map.Entry<String, ? extends T>[] nameValuePairs) {
+    List result = new ArrayList();
+    for (Map.Entry<String, ?> 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 @@
 
     return sb.toString();
   }
+  
+  /**
+   * 
+   * Helper class implementing Map.Entry<String, T> to store the key-value
+   * relationship in NamedList (the keys of which are String-s) 
+   * 
+   * @param <T>
+   */
+  public static final class NamedListEntry<T> implements Map.Entry<String, T> {
+
+    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

Modified: lucene/solr/trunk/src/common/org/apache/solr/common/util/SimpleOrderedMap.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/common/org/apache/solr/common/util/SimpleOrderedMap.java?rev=746031&r1=746030&r2=746031&view=diff
==============================================================================
--- lucene/solr/trunk/src/common/org/apache/solr/common/util/SimpleOrderedMap.java (original)
+++ lucene/solr/trunk/src/common/org/apache/solr/common/util/SimpleOrderedMap.java Thu Feb 19 22:20:32 2009
@@ -49,9 +49,14 @@
    *
    * @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<String, T>[] nameValuePairs) { 
+    super(nameValuePairs);
+  }
 
   @Override
   public SimpleOrderedMap<T> clone() {

Modified: lucene/solr/trunk/src/java/org/apache/solr/handler/component/DebugComponent.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/java/org/apache/solr/handler/component/DebugComponent.java?rev=746031&r1=746030&r2=746031&view=diff
==============================================================================
--- lucene/solr/trunk/src/java/org/apache/solr/handler/component/DebugComponent.java (original)
+++ lucene/solr/trunk/src/java/org/apache/solr/handler/component/DebugComponent.java Thu Feb 19 22:20:32 2009
@@ -108,7 +108,7 @@
       NamedList info = null;
       NamedList explain = new SimpleOrderedMap();
 
-      Object[] arr = new Object[rb.resultIds.size() * 2];
+      Map.Entry<String, Object>[]  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 @@
             // 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<Object>( id, sexplain.getVal(i)); 
           }
         }
       }
 
-      explain = HighlightComponent.removeNulls(new SimpleOrderedMap(Arrays.asList(arr)));
+      explain = HighlightComponent.removeNulls(new SimpleOrderedMap(arr));
 
       if (info == null) {
         info = new SimpleOrderedMap();

Modified: lucene/solr/trunk/src/java/org/apache/solr/handler/component/HighlightComponent.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/java/org/apache/solr/handler/component/HighlightComponent.java?rev=746031&r1=746030&r2=746031&view=diff
==============================================================================
--- lucene/solr/trunk/src/java/org/apache/solr/handler/component/HighlightComponent.java (original)
+++ lucene/solr/trunk/src/java/org/apache/solr/handler/component/HighlightComponent.java Thu Feb 19 22:20:32 2009
@@ -30,6 +30,7 @@
 import java.io.IOException;
 import java.net.URL;
 import java.util.Arrays;
+import java.util.Map;
 
 /**
  * TODO!
@@ -115,7 +116,7 @@
     if (rb.doHighlights && rb.stage == ResponseBuilder.STAGE_GET_FIELDS) {
       NamedList hlResult = new SimpleOrderedMap();
 
-      Object[] arr = new Object[rb.resultIds.size() * 2];
+      Map.Entry<String, Object>[] 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 @@
             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<Object>(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)));      
     }
   }
 

Modified: lucene/solr/trunk/src/java/org/apache/solr/util/NamedList.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/java/org/apache/solr/util/NamedList.java?rev=746031&r1=746030&r2=746031&view=diff
==============================================================================
--- lucene/solr/trunk/src/java/org/apache/solr/util/NamedList.java (original)
+++ lucene/solr/trunk/src/java/org/apache/solr/util/NamedList.java Thu Feb 19 22:20:32 2009
@@ -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 @@
     super();
   }
 
+  @Deprecated
   public NamedList(List nameValuePairs) {
     super(nameValuePairs);
   }
+  
+  public NamedList(Map.Entry<String, T>[] nameValuePairs) { 
+    super(nameValuePairs);
+  }
 }

Modified: lucene/solr/trunk/src/java/org/apache/solr/util/SimpleOrderedMap.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/java/org/apache/solr/util/SimpleOrderedMap.java?rev=746031&r1=746030&r2=746031&view=diff
==============================================================================
--- lucene/solr/trunk/src/java/org/apache/solr/util/SimpleOrderedMap.java (original)
+++ lucene/solr/trunk/src/java/org/apache/solr/util/SimpleOrderedMap.java Thu Feb 19 22:20:32 2009
@@ -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 @@
     super();
   }
 
+  @Deprecated
   public SimpleOrderedMap(List nameValuePairs) {
     super(nameValuePairs);
   }
+
+  public SimpleOrderedMap(Map.Entry<String, T> [] nameValuePairs) {
+    super(nameValuePairs);
+  }
 }

Modified: lucene/solr/trunk/src/java/org/apache/solr/util/TestHarness.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/java/org/apache/solr/util/TestHarness.java?rev=746031&r1=746030&r2=746031&view=diff
==============================================================================
--- lucene/solr/trunk/src/java/org/apache/solr/util/TestHarness.java (original)
+++ lucene/solr/trunk/src/java/org/apache/solr/util/TestHarness.java Thu Feb 19 22:20:32 2009
@@ -33,6 +33,7 @@
 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.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 @@
         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<String, String> [] entries = new NamedListEntry[q.length / 2];
+      for (int i = 0; i < q.length; i += 2) {
+        entries[i/2] = new NamedListEntry<String>(q[i], q[i+1]);
+      }
+      return new LocalSolrQueryRequest(TestHarness.this.getCore(), new NamedList(entries));
     }
   }
 }