Prechádzať zdrojové kódy

improve getting setting as array, automatically support comma delimited values under the exact setting

kimchy 15 rokov pred
rodič
commit
ff0eeb3d1a

+ 13 - 7
modules/elasticsearch/src/main/java/org/elasticsearch/common/settings/ImmutableSettings.java

@@ -22,6 +22,8 @@ package org.elasticsearch.common.settings;
 import org.elasticsearch.common.Booleans;
 import org.elasticsearch.common.Classes;
 import org.elasticsearch.common.Strings;
+import org.elasticsearch.common.collect.ImmutableMap;
+import org.elasticsearch.common.collect.Lists;
 import org.elasticsearch.common.io.Streams;
 import org.elasticsearch.common.io.stream.StreamInput;
 import org.elasticsearch.common.io.stream.StreamOutput;
@@ -43,7 +45,6 @@ import java.util.*;
 import java.util.concurrent.TimeUnit;
 
 import static org.elasticsearch.common.Strings.*;
-import static org.elasticsearch.common.collect.Lists.*;
 import static org.elasticsearch.common.unit.ByteSizeValue.*;
 import static org.elasticsearch.common.unit.SizeValue.*;
 import static org.elasticsearch.common.unit.TimeValue.*;
@@ -51,18 +52,18 @@ import static org.elasticsearch.common.unit.TimeValue.*;
 /**
  * An immutable implementation of {@link Settings}.
  *
- * @author kimchy (Shay Banon)
+ * @author kimchy (shay.banon)
  */
 @ThreadSafe
 @Immutable
 public class ImmutableSettings implements Settings {
 
-    private Map<String, String> settings;
+    private ImmutableMap<String, String> settings;
 
     private transient ClassLoader classLoader;
 
     private ImmutableSettings(Map<String, String> settings, ClassLoader classLoader) {
-        this.settings = settings;
+        this.settings = ImmutableMap.copyOf(settings);
         this.classLoader = classLoader == null ? buildClassLoader() : classLoader;
     }
 
@@ -70,8 +71,8 @@ public class ImmutableSettings implements Settings {
         return this.classLoader;
     }
 
-    @Override public Map<String, String> getAsMap() {
-        return Collections.unmodifiableMap(this.settings);
+    @Override public ImmutableMap<String, String> getAsMap() {
+        return this.settings;
     }
 
     @Override public Settings getComponentSettings(Class component) {
@@ -228,7 +229,12 @@ public class ImmutableSettings implements Settings {
     }
 
     @Override public String[] getAsArray(String settingPrefix, String[] defaultArray) throws SettingsException {
-        List<String> result = newArrayList();
+        List<String> result = Lists.newArrayList();
+
+        if (get(settingPrefix) != null) {
+            Collections.addAll(result, Strings.commaDelimitedListToStringArray(get(settingPrefix)));
+        }
+
         int counter = 0;
         while (true) {
             String value = get(settingPrefix + '.' + (counter++));

+ 8 - 1
modules/elasticsearch/src/main/java/org/elasticsearch/common/settings/Settings.java

@@ -19,6 +19,7 @@
 
 package org.elasticsearch.common.settings;
 
+import org.elasticsearch.common.collect.ImmutableMap;
 import org.elasticsearch.common.unit.ByteSizeValue;
 import org.elasticsearch.common.unit.SizeValue;
 import org.elasticsearch.common.unit.TimeValue;
@@ -63,7 +64,7 @@ public interface Settings {
     /**
      * The settings as a {@link java.util.Map}.
      */
-    Map<String, String> getAsMap();
+    ImmutableMap<String, String> getAsMap();
 
     /**
      * Returns the setting value associated with the setting key.
@@ -210,6 +211,9 @@ public interface Settings {
      * The values associated with a setting prefix as an array. The settings array is in the format of:
      * <tt>settingPrefix.[index]</tt>.
      *
+     * <p>It will also automatically load a comma separated list under the settingPrefix and merge with
+     * the numbered format.
+     *
      * @param settingPrefix The setting prefix to load the array by
      * @return The setting array values
      * @throws SettingsException
@@ -220,6 +224,9 @@ public interface Settings {
      * The values associated with a setting prefix as an array. The settings array is in the format of:
      * <tt>settingPrefix.[index]</tt>.
      *
+     * <p>It will also automatically load a comma separated list under the settingPrefix and merge with
+     * the numbered format.
+     *
      * @param settingPrefix The setting prefix to load the array by
      * @return The setting array values
      * @throws SettingsException

+ 0 - 5
modules/elasticsearch/src/main/java/org/elasticsearch/discovery/zen/ping/unicast/UnicastZenPing.java

@@ -24,7 +24,6 @@ import org.elasticsearch.ElasticSearchIllegalArgumentException;
 import org.elasticsearch.cluster.ClusterName;
 import org.elasticsearch.cluster.node.DiscoveryNode;
 import org.elasticsearch.cluster.node.DiscoveryNodes;
-import org.elasticsearch.common.Strings;
 import org.elasticsearch.common.collect.Lists;
 import org.elasticsearch.common.component.AbstractLifecycleComponent;
 import org.elasticsearch.common.io.stream.StreamInput;
@@ -89,10 +88,6 @@ public class UnicastZenPing extends AbstractLifecycleComponent<ZenPing> implemen
         this.clusterName = clusterName;
 
         List<String> hosts = Lists.newArrayList(componentSettings.getAsArray("hosts"));
-        if (componentSettings.get("hosts") != null) {
-            hosts.addAll(Strings.commaDelimitedListToSet(componentSettings.get("hosts")));
-        }
-
         logger.debug("using initial hosts {}", hosts);
 
         List<DiscoveryNode> nodes = Lists.newArrayList();

+ 1 - 5
plugins/cloud/aws/src/main/java/org/elasticsearch/discovery/ec2/AwsEc2UnicastHostsProvider.java

@@ -69,11 +69,7 @@ public class AwsEc2UnicastHostsProvider extends AbstractComponent implements Uni
         this.ports = componentSettings.get("ports", "9300-9302");
 
         this.bindAnyGroup = componentSettings.getAsBoolean("any_group", true);
-        Set<String> groups = Sets.newHashSet(componentSettings.getAsArray("groups"));
-        if (componentSettings.get("groups") != null) {
-            groups.addAll(Strings.commaDelimitedListToSet(componentSettings.get("groups")));
-        }
-        this.groups = ImmutableSet.copyOf(groups);
+        this.groups = ImmutableSet.copyOf(componentSettings.getAsArray("groups"));
 
         Set<String> availabilityZones = Sets.newHashSet(componentSettings.getAsArray("availability_zones"));
         if (componentSettings.get("availability_zones") != null) {