Skip to content

Commit 75b6e68

Browse files
committed
make the store interface generic
Signed-off-by: Kaushal Kumar <[email protected]>
1 parent f575fbd commit 75b6e68

File tree

3 files changed

+35
-29
lines changed

3 files changed

+35
-29
lines changed

plugins/workload-management/src/main/java/org/opensearch/plugin/wlm/rule/storage/AttributeValueStore.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,26 @@
1313
/**
1414
* This interface provides apis to store Rule attribute values
1515
*/
16-
public interface AttributeValueStore {
16+
public interface AttributeValueStore<K, V> {
1717
/**
1818
* Adds the value to the data structure
1919
* @param key to be added
2020
* @param value to be added
2121
*/
22-
void addValue(String key, String value);
22+
void add(K key, V value);
2323

2424
/**
2525
* removes the key and associated value from the data structure
2626
* @param key to be removed
2727
*/
28-
void removeValue(String key);
28+
void remove(K key);
2929

3030
/**
3131
* Returns the value associated with the key
3232
* @param key in the data structure
3333
* @return
3434
*/
35-
Optional<String> getValue(String key);
35+
Optional<V> get(K key);
3636

3737
/**
3838
* Clears all the keys and their associated values from the structure
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,29 @@
1515

1616
/**
1717
* This is a patricia trie based implementation of AttributeValueStore
18+
* We are choosing patricia trie because it provides very fast search operations on prefix matches as well as range
19+
* lookups. It provides a very efficient storage for strings
20+
* ref: https://commons.apache.org/proper/commons-collections/javadocs/api-4.4/org/apache/commons/collections4/trie/PatriciaTrie.html
1821
*/
19-
public class TrieBasedStore implements AttributeValueStore {
20-
PatriciaTrie<String> trie;
22+
public class DefaultAttributeValueStore<K extends String, V> implements AttributeValueStore<K, V> {
23+
PatriciaTrie<V> trie;
2124

22-
public TrieBasedStore(PatriciaTrie<String> trie) {
25+
public DefaultAttributeValueStore(PatriciaTrie<V> trie) {
2326
this.trie = trie;
2427
}
2528

2629
@Override
27-
public void addValue(String key, String value) {
30+
public void add(K key, V value) {
2831
trie.put(key, value);
2932
}
3033

3134
@Override
32-
public void removeValue(String key) {
35+
public void remove(String key) {
3336
trie.remove(key);
3437
}
3538

3639
@Override
37-
public Optional<String> getValue(String key) {
40+
public Optional<V> get(String key) {
3841
/**
3942
* Since we are inserting prefixes into the trie and searching for larger strings
4043
* It is important to find the largest matching prefix key in the trie efficiently
@@ -47,7 +50,7 @@ public Optional<String> getValue(String key) {
4750
* 1. There is a Rule which has this prefix as one of the attribute values. In this case we should return the
4851
* Rule's label otherwise send empty
4952
*/
50-
for (Map.Entry<String, String> possibleMatch :trie.prefixMap(longestMatchingPrefix).entrySet()) {
53+
for (Map.Entry<String, V> possibleMatch :trie.prefixMap(longestMatchingPrefix).entrySet()) {
5154
if (key.startsWith(possibleMatch.getKey())) {
5255
return Optional.of(possibleMatch.getValue());
5356
}
@@ -64,6 +67,9 @@ private String findLongestMatchingPrefix(String key) {
6467
int mid = low + (high - low + 1) / 2;
6568
String possibleMatchingPrefix = key.substring(0, mid);
6669

70+
/**
71+
* This operation has O(1) complexity because prefixMap returns only the iterator
72+
*/
6773
if (!trie.prefixMap(possibleMatchingPrefix).isEmpty()) {
6874
low = mid;
6975
} else {

plugins/workload-management/src/test/java/org/opensearch/plugin/wlm/rule/storage/AttributeValueStoreTests.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,41 @@
1414

1515
public class AttributeValueStoreTests extends OpenSearchTestCase {
1616

17-
AttributeValueStore subjectUnderTest;
17+
AttributeValueStore<String, String> subjectUnderTest;
1818

1919
public void setUp() throws Exception {
2020
super.setUp();
21-
subjectUnderTest = new TrieBasedStore(new PatriciaTrie<>());
21+
subjectUnderTest = new DefaultAttributeValueStore<>(new PatriciaTrie<>());
2222
}
2323

24-
public void testAddValue() {
25-
subjectUnderTest.addValue("foo", "bar");
26-
assertEquals("bar", subjectUnderTest.getValue("foo").get());
24+
public void testAdd() {
25+
subjectUnderTest.add("foo", "bar");
26+
assertEquals("bar", subjectUnderTest.get("foo").get());
2727
}
2828

29-
public void testRemoveValue() {
30-
subjectUnderTest.addValue("foo", "bar");
31-
subjectUnderTest.removeValue("foo");
29+
public void testRemove() {
30+
subjectUnderTest.add("foo", "bar");
31+
subjectUnderTest.remove("foo");
3232
assertEquals(0, subjectUnderTest.size());
3333
}
3434

35-
public void tesGetValue() {
36-
subjectUnderTest.addValue("foo", "bar");
37-
assertEquals("bar", subjectUnderTest.getValue("foo").get());
35+
public void tesGet() {
36+
subjectUnderTest.add("foo", "bar");
37+
assertEquals("bar", subjectUnderTest.get("foo").get());
3838
}
3939

40-
public void testGetValueWhenNoProperPrefixIsPresent() {
41-
subjectUnderTest.addValue("foo", "bar");
42-
subjectUnderTest.addValue("foodip", "sing");
43-
assertTrue(subjectUnderTest.getValue("foxtail").isEmpty());
44-
subjectUnderTest.addValue("fox", "lucy");
40+
public void testGetWhenNoProperPrefixIsPresent() {
41+
subjectUnderTest.add("foo", "bar");
42+
subjectUnderTest.add("foodip", "sing");
43+
assertTrue(subjectUnderTest.get("foxtail").isEmpty());
44+
subjectUnderTest.add("fox", "lucy");
4545

46-
assertFalse(subjectUnderTest.getValue("foxtail").isEmpty());
46+
assertFalse(subjectUnderTest.get("foxtail").isEmpty());
4747
}
4848

4949

5050
public void testClear() {
51-
subjectUnderTest.addValue("foo", "bar");
51+
subjectUnderTest.add("foo", "bar");
5252
subjectUnderTest.clear();
5353
assertEquals(0, subjectUnderTest.size());
5454
}

0 commit comments

Comments
 (0)