protected override Tuple <object, IConfigurationSource> DoGetPropertyValue <K, V>(PropertyConfig <K, V> propertyConfig) { if (propertyConfig == null) { throw new ArgumentNullException("propertyConfig is null"); } if (!(propertyConfig.Key is ILabeledKey)) { return(base.DoGetPropertyValue(propertyConfig)); } IPropertyConfig noLabelPropertyConfig = AbstractLabeledConfigurationSource.MakeNoLabelConfig(propertyConfig); for (PropertyLabels propertyLabels = ((ILabeledKey)propertyConfig.Key).Labels; propertyLabels != null; propertyLabels = propertyLabels.Alternative) { foreach (IConfigurationSource source in SortedSources.Values) { if (!(source is ILabeledConfigurationSource) && propertyLabels.Labels.Count != 0) { continue; } V value = default(V); if (propertyLabels.Labels.Count == 0) { value = (V)GetPropertyValue(source, noLabelPropertyConfig); } else { value = (V)GetPropertyValue(source, noLabelPropertyConfig, propertyLabels.Labels); } value = ApplyValueFilter(source, propertyConfig, value); if (!object.Equals(value, default(V))) { return(Tuple.Create <object, IConfigurationSource>(value, source)); } } } return(Tuple.Create <object, IConfigurationSource>(propertyConfig.DefaultValue, null)); }
public virtual void TestGetLabeledPropertyValuePerf() { ConfigurationSourceConfig config = ConfigurationSources.NewConfig("labeled-source"); TestLabeledConfigurationSource labeledSource = CreateLabeledSource(config); config = ConfigurationSources.NewConfig("dynamic-labeled-source"); TestDynamicLabeledConfigurationSource dynamicLabeledSource = CreateDynamicLabeledSource(config); ILabeledConfigurationManager manager = CreateLabeledManager( new Dictionary <int, IConfigurationSource>() { { 1, labeledSource }, { 2, dynamicLabeledSource } }); List <IPropertyLabel> labels = new List <IPropertyLabel>(); labels.Add(LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.DC_KEY, "sh-1")); labels.Add(LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.APP_KEY, "app-1")); PropertyLabels propertyLabels = LabeledConfigurationProperties.NewLabels(labels); LabeledKey <string> key = LabeledConfigurationProperties.NewKeyBuilder <string>().SetKey("labeled-key-1") .SetPropertyLabels(propertyLabels).Build(); PropertyConfig <LabeledKey <string>, string> propertyConfig = ConfigurationProperties .NewConfigBuilder <LabeledKey <string>, string>().SetKey(key).SetDefaultValue("default-value-1").Build(); String propertyValue = manager.GetPropertyValue(propertyConfig); Assert.Equal("v-1-2", propertyValue); int times = 100 * 1000; DateTime start = DateTime.Now; for (int i = 0; i < times; i++) { propertyValue = manager.GetPropertyValue(propertyConfig); } DateTime end = DateTime.Now; TimeSpan elipsed = end - start; Console.WriteLine("{0} times GetPropertyValue, total time: {1}, avg time: {2}", times, elipsed.TotalMilliseconds, (double)elipsed.TotalMilliseconds / times); }
public DefaultPropertyLabels(ICollection <IPropertyLabel> labels, PropertyLabels alternative) { if (labels == null || labels.Count == 0) { throw new ArgumentNullException("labels is null or empty"); } _labelMap = new Dictionary <object, IPropertyLabel>(); labels.ToList().ForEach(l => { if (l != null) { _labelMap[l.Key] = l; } }); if (_labelMap.Count == 0) { throw new ArgumentNullException("all elements of labels are null"); } _labels = ImmutableList.CreateRange(_labelMap.Values); _alternative = alternative; }
public virtual void TestGetLabeledProperty() { ConfigurationSourceConfig config = ConfigurationSources.NewConfig("labeled-source"); TestLabeledConfigurationSource labeledSource = CreateLabeledSource(config); config = ConfigurationSources.NewConfig("dynamic-labeled-source"); TestDynamicLabeledConfigurationSource dynamicLabeledSource = CreateDynamicLabeledSource(config); ILabeledConfigurationManager manager = CreateLabeledManager( new Dictionary <int, IConfigurationSource>() { { 1, labeledSource }, { 2, dynamicLabeledSource } }); List <IPropertyLabel> labels = new List <IPropertyLabel>(); labels.Add(LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.DC_KEY, "sh-1")); labels.Add(LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.APP_KEY, "app-1")); PropertyLabels propertyLabels = LabeledConfigurationProperties.NewLabels(labels); LabeledKey <string> key = LabeledConfigurationProperties.NewKeyBuilder <string>().SetKey("labeled-key-1") .SetPropertyLabels(propertyLabels).Build(); PropertyConfig <LabeledKey <string>, string> propertyConfig = ConfigurationProperties .NewConfigBuilder <LabeledKey <string>, string>().SetKey(key).SetDefaultValue("default-value-1").Build(); IProperty <LabeledKey <string>, string> property = manager.GetProperty(propertyConfig); Console.WriteLine(property); Assert.Equal("v-1-2", property.Value); labels = new List <IPropertyLabel>(); labels.Add(LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.DC_KEY, "sh-1-not-exist")); labels.Add(LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.APP_KEY, "app-1")); propertyLabels = LabeledConfigurationProperties.NewLabels(labels); key = LabeledConfigurationProperties.NewKeyBuilder <String>().SetKey("labeled-key-1") .SetPropertyLabels(propertyLabels).Build(); propertyConfig = ConfigurationProperties.NewConfigBuilder <LabeledKey <string>, String>().SetKey(key) .SetDefaultValue("default-value-1").Build(); property = manager.GetProperty(propertyConfig); Console.WriteLine(property); Assert.Equal("default-value-1", property.Value); propertyLabels = LabeledConfigurationProperties.NewLabels( LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.DC_KEY, "sh-1"), LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.APP_KEY, "app-1")); labels = new List <IPropertyLabel>(); labels.Add(LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.DC_KEY, "sh-1-not-exist")); labels.Add(LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.APP_KEY, "app-1")); propertyLabels = LabeledConfigurationProperties.NewLabels(labels, propertyLabels); key = LabeledConfigurationProperties.NewKeyBuilder <string>().SetKey("labeled-key-1") .SetPropertyLabels(propertyLabels).Build(); propertyConfig = ConfigurationProperties.NewConfigBuilder <LabeledKey <string>, string>().SetKey(key) .SetDefaultValue("default-value-1").Build(); property = manager.GetProperty(propertyConfig); Console.WriteLine(property); Assert.Equal("v-1-2", property.Value); TestDataCenterSetting Setting = new TestDataCenterSetting("labeled-key-1", "v-4-2", "sh-1-not-exist", "app-1"); dynamicLabeledSource.updateSetting(Setting); Thread.Sleep(10); Console.WriteLine(property); Assert.Equal("v-4-2", property.Value); dynamicLabeledSource.removeSetting(Setting); Thread.Sleep(10); Console.WriteLine(property); Assert.Equal("v-1-2", property.Value); labels = new List <IPropertyLabel>(); labels.Add(LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.DC_KEY, "sh-1-not-exist")); labels.Add(LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.APP_KEY, "app-1")); propertyLabels = LabeledConfigurationProperties.NewLabels(labels, PropertyLabels.Empty); key = LabeledConfigurationProperties.NewKeyBuilder <string>().SetKey("labeled-key-1") .SetPropertyLabels(propertyLabels).Build(); propertyConfig = ConfigurationProperties.NewConfigBuilder <LabeledKey <string>, string>().SetKey(key) .SetDefaultValue("default-value-1").Build(); property = manager.GetProperty(propertyConfig); Console.WriteLine(property); Assert.Equal("v-0-2", property.Value); }
public virtual B SetPropertyLabels(PropertyLabels labels) { GetLabeledKey()._labels = labels; return((B)(object)this); }