Skip to content

Commit 420d8fc

Browse files
committed
Simplifying configuration and cleaning up instantiation
1 parent dad3d58 commit 420d8fc

File tree

3 files changed

+54
-44
lines changed

3 files changed

+54
-44
lines changed

src/main/java/com/yahoo/bullet/BulletConfig.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,15 @@ public class BulletConfig extends Config {
4646

4747
public static final String RESULT_METADATA_METRICS_MAPPING = "bullet.result.metadata.metrics.mapping";
4848

49+
public static final String DEFAULT_CONFIGURATION_NAME = "bullet_defaults.yaml";
50+
4951
/**
5052
* Constructor that loads specific file augmented with defaults.
5153
*
5254
* @param file YAML file to load.
5355
* @throws IOException if an error occurred with the file loading.
5456
*/
5557
public BulletConfig(String file) throws IOException {
56-
super(file);
57-
}
58-
59-
/**
60-
* Default constructor.
61-
*
62-
* @throws IOException if an error occurred with loading the default config.
63-
*/
64-
public BulletConfig() throws IOException {
65-
super();
58+
super(file, DEFAULT_CONFIGURATION_NAME);
6659
}
6760
}

src/main/java/com/yahoo/bullet/Config.java

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,42 +21,33 @@
2121
import java.util.Set;
2222

2323
@Slf4j
24-
public abstract class Config implements Serializable {
24+
public class Config implements Serializable {
2525
private Map<String, Object> data;
2626

27-
public static final String DEFAULT_CONFIGURATION_NAME = "bullet_defaults.yaml";
28-
private String defaultConfiguration;
29-
3027
/**
31-
* Constructor that loads specific file augmented with defaults and the name of the default configuration file.
32-
*
28+
* Constructor that loads a specific file and loads the settings in that file.
3329
*
3430
* @param file YAML file to load.
35-
* @param defaultConfigurationFile Default YAML file to load.
3631
* @throws IOException if an error occurred with the file loading.
3732
*/
38-
public Config(String file, String defaultConfigurationFile) throws IOException {
39-
this.defaultConfiguration = defaultConfigurationFile;
40-
data = loadConfigResource(file);
33+
public Config(String file) throws IOException {
34+
data = readYAML(file);
35+
log.info("Configuration: {} ", data);
4136
}
4237

4338
/**
44-
* Constructor that loads specific file augmented with defaults.
39+
* Constructor that loads specific file augmented with defaults and the name of the default configuration file.
4540
*
4641
* @param file YAML file to load.
42+
* @param defaultConfigurationFile Default YAML file to load.
4743
* @throws IOException if an error occurred with the file loading.
4844
*/
49-
public Config(String file) throws IOException {
50-
this(file, DEFAULT_CONFIGURATION_NAME);
51-
}
52-
53-
/**
54-
* Default constructor.
55-
*
56-
* @throws IOException if an error occurred with loading the default config.
57-
*/
58-
public Config() throws IOException {
59-
this(null);
45+
public Config(String file, String defaultConfigurationFile) throws IOException {
46+
this(defaultConfigurationFile);
47+
// Override
48+
Map<String, Object> specificConf = readYAML(file);
49+
data.putAll(specificConf);
50+
log.info("Final configuration: {} ", data);
6051
}
6152

6253
/**
@@ -119,22 +110,24 @@ public void set(String key, Object value) {
119110
data.put(key, value);
120111
}
121112

113+
/**
114+
* Merges another Config into this one.
115+
*
116+
* @param other The other {@link Config} to merge into this one.
117+
*/
118+
public void merge(Config other) {
119+
if (other != null) {
120+
data.putAll(other.data);
121+
}
122+
}
123+
122124
/**
123125
* Clears out the configuration.
124126
*/
125127
public void clear() {
126128
data.clear();
127129
}
128130

129-
private Map<String, Object> loadConfigResource(String yamlFile) throws IOException {
130-
Map<String, Object> defaultconf = readYAML(defaultConfiguration);
131-
Map<String, Object> specificConf = readYAML(yamlFile);
132-
// Override
133-
defaultconf.putAll(specificConf);
134-
log.info("Final configuration: {} ", defaultconf);
135-
return defaultconf;
136-
}
137-
138131
/**
139132
* Reads a YAML file containing mappings and returns them as a {@link Map}.
140133
*

src/test/java/com/yahoo/bullet/BulletConfigTest.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void testCustomConfig() throws IOException {
3535

3636
@Test
3737
public void testCustomProperties() throws IOException {
38-
BulletConfig config = new BulletConfig();
38+
BulletConfig config = new BulletConfig(null);
3939
Assert.assertNull(config.get("foo"));
4040
config.set("foo", "bar");
4141
Assert.assertEquals(config.get("foo"), "bar");
@@ -51,7 +51,7 @@ public void testGettingWithDefault() throws IOException {
5151

5252
@Test
5353
public void testGettingMultipleProperties() throws IOException {
54-
BulletConfig config = new BulletConfig();
54+
BulletConfig config = new BulletConfig(null);
5555
config.clear();
5656
config.set("1", 1);
5757
config.set("pi", 3.14);
@@ -74,7 +74,7 @@ public void testGettingMultipleProperties() throws IOException {
7474

7575
@Test
7676
public void testGettingMaskedProperties() throws IOException {
77-
BulletConfig config = new BulletConfig();
77+
BulletConfig config = new BulletConfig(null);
7878
config.clear();
7979
config.set("1", 1);
8080
config.set("pi", 3.14);
@@ -94,4 +94,28 @@ public void testGettingMaskedProperties() throws IOException {
9494
Assert.assertEquals(mappings.get("foo"), "bar");
9595
Assert.assertEquals(mappings.get("true"), true);
9696
}
97+
98+
@Test
99+
public void testMerging() throws IOException {
100+
BulletConfig config = new BulletConfig("src/test/resources/test_config.yaml");
101+
102+
int configSize = config.getAll(Optional.empty()).size();
103+
Assert.assertEquals(config.get(BulletConfig.SPECIFICATION_MAX_DURATION), 10000L);
104+
Assert.assertEquals(config.get(BulletConfig.AGGREGATION_MAX_SIZE), 100L);
105+
106+
Config another = new BulletConfig(null);
107+
another.clear();
108+
another.set(BulletConfig.SPECIFICATION_MAX_DURATION, 42L);
109+
config.set("pi", 3.14);
110+
111+
config.merge(another);
112+
113+
// Test null
114+
config.merge(null);
115+
116+
Assert.assertEquals(config.getAll(Optional.empty()).size(), configSize + 1);
117+
Assert.assertEquals(config.get(BulletConfig.SPECIFICATION_MAX_DURATION), 42L);
118+
Assert.assertEquals(config.get(BulletConfig.AGGREGATION_MAX_SIZE), 100L);
119+
Assert.assertEquals(config.get("pi"), 3.14);
120+
}
97121
}

0 commit comments

Comments
 (0)