You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ja...@apache.org on 2012/03/29 22:57:16 UTC

svn commit: r1307104 - in /lucene/dev/branches/branch_3x/solr: core/src/java/org/apache/solr/schema/ core/src/test-files/solr/conf/ core/src/test/org/apache/solr/schema/ example/solr/conf/

Author: janhoy
Date: Thu Mar 29 20:57:15 2012
New Revision: 1307104

URL: http://svn.apache.org/viewvc?rev=1307104&view=rev
Log:
SOLR-2202: Stabilize ExchangeRateProvider interface and fix classloader

Modified:
    lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/schema/CurrencyField.java
    lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/schema/ExchangeRateProvider.java
    lucene/dev/branches/branch_3x/solr/core/src/test-files/solr/conf/schema.xml
    lucene/dev/branches/branch_3x/solr/core/src/test/org/apache/solr/schema/CurrencyFieldTest.java
    lucene/dev/branches/branch_3x/solr/core/src/test/org/apache/solr/schema/MockExchangeRateProvider.java
    lucene/dev/branches/branch_3x/solr/example/solr/conf/schema.xml

Modified: lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/schema/CurrencyField.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/schema/CurrencyField.java?rev=1307104&r1=1307103&r2=1307104&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/schema/CurrencyField.java (original)
+++ lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/schema/CurrencyField.java Thu Mar 29 20:57:15 2012
@@ -50,11 +50,11 @@ import javax.xml.xpath.XPathExpressionEx
 import javax.xml.xpath.XPathFactory;
 import java.io.IOException;
 import java.io.InputStream;
-import java.util.ArrayList;
 import java.util.Currency;
 import java.util.HashMap;
-import java.util.List;
+import java.util.HashSet;
 import java.util.Map;
+import java.util.Set;
 
 /**
  * Field type for support of monetary values.
@@ -65,7 +65,7 @@ public class CurrencyField extends Field
   protected static final String PARAM_DEFAULT_CURRENCY      = "defaultCurrency";
   protected static final String PARAM_RATE_PROVIDER_CLASS   = "providerClass";
   protected static final Object PARAM_PRECISION_STEP        = "precisionStep";
-  protected static final String DEFAULT_RATE_PROVIDER_CLASS = "org.apache.solr.schema.FileExchangeRateProvider";
+  protected static final String DEFAULT_RATE_PROVIDER_CLASS = "solr.FileExchangeRateProvider";
   protected static final String DEFAULT_DEFAULT_CURRENCY    = "USD";
   protected static final String DEFAULT_PRECISION_STEP      = "0";
   protected static final String FIELD_SUFFIX_AMOUNT_RAW     = "_amount_raw";
@@ -120,8 +120,7 @@ public class CurrencyField extends Field
     args.remove(PARAM_PRECISION_STEP);
 
     try {
-      // TODO: Are we using correct classloader?
-      Class<?> c = Class.forName(exchangeRateProviderClass);
+      Class<?> c = schema.getResourceLoader().findClass(exchangeRateProviderClass);
       Object clazz = c.newInstance();
       if (clazz instanceof ExchangeRateProvider) {
         provider = (ExchangeRateProvider) clazz;
@@ -511,14 +510,15 @@ class FileExchangeRateProvider implement
     return "["+this.getClass().getName()+" : " + rates.size() + " rates.]";
   }
 
-  public String[] listAvailableCurrencies() {
-    List<String> pairs = new ArrayList<String>();
+  public Set<String> listAvailableCurrencies() {
+    Set<String> currencies = new HashSet<String>();
     for(String from : rates.keySet()) {
+      currencies.add(from);
       for(String to : rates.get(from).keySet()) {
-        pairs.add(from+","+to);
+        currencies.add(to);
       }
     }
-    return pairs.toArray(new String[1]);
+    return currencies;
   }
 
   public boolean reload() throws SolrException {

Modified: lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/schema/ExchangeRateProvider.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/schema/ExchangeRateProvider.java?rev=1307104&r1=1307103&r2=1307104&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/schema/ExchangeRateProvider.java (original)
+++ lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/schema/ExchangeRateProvider.java Thu Mar 29 20:57:15 2012
@@ -17,6 +17,7 @@ package org.apache.solr.schema;
  */
 
 import java.util.Map;
+import java.util.Set;
 
 import org.apache.solr.common.ResourceLoader;
 import org.apache.solr.common.SolrException;
@@ -35,11 +36,10 @@ public interface ExchangeRateProvider {
   public double getExchangeRate(String sourceCurrencyCode, String targetCurrencyCode) throws SolrException;
   
   /**
-   * List all configured currency code pairs
-   * @return a string array of <a href="http://en.wikipedia.org/wiki/ISO_4217">ISO 4217</a> currency codes on the format
-   * ["SRC,DST", "SRC,DST"...]
+   * List all configured currency codes which are valid as source/target for this Provider
+   * @return a Set of <a href="http://en.wikipedia.org/wiki/ISO_4217">ISO 4217</a> currency code strings
    */
-  public String[] listAvailableCurrencies();
+  public Set<String> listAvailableCurrencies();
 
   /**
    * Ask the currency provider to explicitly reload/refresh its configuration.

Modified: lucene/dev/branches/branch_3x/solr/core/src/test-files/solr/conf/schema.xml
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/solr/core/src/test-files/solr/conf/schema.xml?rev=1307104&r1=1307103&r2=1307104&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/solr/core/src/test-files/solr/conf/schema.xml (original)
+++ lucene/dev/branches/branch_3x/solr/core/src/test-files/solr/conf/schema.xml Thu Mar 29 20:57:15 2012
@@ -395,7 +395,7 @@
 
   <!-- Currency type -->
   <fieldType name="currency" class="solr.CurrencyField" currencyConfig="currency.xml"/>
-  <fieldType name="mock_currency" class="solr.CurrencyField" providerClass="org.apache.solr.schema.MockExchangeRateProvider" foo="bar" />
+  <fieldType name="mock_currency" class="solr.CurrencyField" providerClass="solr.MockExchangeRateProvider" foo="bar" />
   
   <!-- omitPositions example -->
   <fieldType name="nopositions" class="solr.TextField" omitPositions="true">

Modified: lucene/dev/branches/branch_3x/solr/core/src/test/org/apache/solr/schema/CurrencyFieldTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/solr/core/src/test/org/apache/solr/schema/CurrencyFieldTest.java?rev=1307104&r1=1307103&r2=1307104&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/solr/core/src/test/org/apache/solr/schema/CurrencyFieldTest.java (original)
+++ lucene/dev/branches/branch_3x/solr/core/src/test/org/apache/solr/schema/CurrencyFieldTest.java Thu Mar 29 20:57:15 2012
@@ -24,6 +24,7 @@ import org.junit.Ignore;
 import org.junit.Test;
 
 import java.util.Random;
+import java.util.Set;
 
 /**
  * Tests currency field type.
@@ -85,13 +86,27 @@ public class CurrencyFieldTest extends S
     
     // A few tests on the provider directly
     ExchangeRateProvider p = ((CurrencyField) tmp).getProvider();
-    String[] available = p.listAvailableCurrencies();
-    assert(available.length == 5);
+    Set<String> availableCurrencies = p.listAvailableCurrencies();
+    assert(availableCurrencies.size() == 4);
     assert(p.reload() == true);
     assert(p.getExchangeRate("USD", "EUR") == 2.5);
   }
 
   @Test
+  public void testMockExchangeRateProvider() throws Exception {
+    SolrCore core = h.getCore();
+    IndexSchema schema = core.getSchema();
+    SchemaField amount = schema.getField("mock_amount");
+
+    // A few tests on the provider directly
+    ExchangeRateProvider p = ((CurrencyField)amount.getType()).getProvider();
+    Set<String> availableCurrencies = p.listAvailableCurrencies();
+    assert(availableCurrencies.size() == 3);
+    assert(p.reload() == true);
+    assert(p.getExchangeRate("USD", "EUR") == 0.8);
+  }
+
+  @Test
   public void testCurrencyRangeSearch() throws Exception {
     for (int i = 1; i <= 10; i++) {
       assertU(adoc("id", "" + i, "amount", i + ",USD"));
@@ -196,7 +211,7 @@ public class CurrencyFieldTest extends S
   }
 
   @Test
-  public void testMockExchangeRateProvider() throws Exception {
+  public void testMockFieldType() throws Exception {
     assertU(adoc("id", "1", "mock_amount", "1.00,USD"));
     assertU(adoc("id", "2", "mock_amount", "1.00,EUR"));
     assertU(adoc("id", "3", "mock_amount", "1.00,NOK"));

Modified: lucene/dev/branches/branch_3x/solr/core/src/test/org/apache/solr/schema/MockExchangeRateProvider.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/solr/core/src/test/org/apache/solr/schema/MockExchangeRateProvider.java?rev=1307104&r1=1307103&r2=1307104&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/solr/core/src/test/org/apache/solr/schema/MockExchangeRateProvider.java (original)
+++ lucene/dev/branches/branch_3x/solr/core/src/test/org/apache/solr/schema/MockExchangeRateProvider.java Thu Mar 29 20:57:15 2012
@@ -17,7 +17,9 @@ package org.apache.solr.schema;
  */
 
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Map;
+import java.util.Set;
 
 import org.apache.solr.common.ResourceLoader;
 import org.apache.solr.common.SolrException;
@@ -51,8 +53,17 @@ public class MockExchangeRateProvider im
     return result;
   }
 
-  public String[] listAvailableCurrencies() {
-    return map.keySet().toArray(new String[1]);
+  public Set<String> listAvailableCurrencies() {
+    Set<String> currenciesPairs = map.keySet();
+    Set<String> returnSet;
+    
+    returnSet = new HashSet<String>();
+    for (String c : currenciesPairs) {
+      String[] pairs = c.split(",");
+      returnSet.add(pairs[0]);
+      returnSet.add(pairs[1]);
+    }
+    return returnSet;
   }
 
   public boolean reload() throws SolrException {

Modified: lucene/dev/branches/branch_3x/solr/example/solr/conf/schema.xml
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/solr/example/solr/conf/schema.xml?rev=1307104&r1=1307103&r2=1307104&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/solr/example/solr/conf/schema.xml (original)
+++ lucene/dev/branches/branch_3x/solr/example/solr/conf/schema.xml Thu Mar 29 20:57:15 2012
@@ -475,12 +475,12 @@
         Parameters:
           defaultCurrency: Specifies the default currency if none specified. Defaults to "USD"
           precisionStep:   Specifies the precisionStep for the TrieLong field used for the amount
-          providerClass:   Lets you plug in other exchange backend. Defaults to FileExchangeRateProvider
-                           The FileExchangeRateProvider takes one parameter:
+          providerClass:   Lets you plug in other exchange provider backend:
+                           solr.FileExchangeRateProvider is the default and takes one parameter:
                              currencyConfig: name of an xml file holding exhange rates
    -->
     <fieldType name="currency" class="solr.CurrencyField" precisionStep="8" defaultCurrency="USD" currencyConfig="currency.xml" />
-
+             
    <!-- some examples for different languages (generally ordered by ISO code) -->
 
     <!-- Arabic -->