本文主要研究一下dubbo的DataStore

DataStore

dubbo-2.7.2/dubbo-common/src/main/java/org/apache/dubbo/common/store/DataStore.java

@SPI("simple")
public interface DataStore {

    /**
     * return a snapshot value of componentName
     */
    Map<String, Object> get(String componentName);

    Object get(String componentName, String key);

    void put(String componentName, String key, Object value);

    void remove(String componentName, String key);

}
  • DataStore定义了get、put、remove方法,它有一个实现类为SimpleDataStore

SimpleDataStore

dubbo-2.7.2/dubbo-common/src/main/java/org/apache/dubbo/common/store/support/SimpleDataStore.java

public class SimpleDataStore implements DataStore {

    // <component name or id, <data-name, data-value>>
    private ConcurrentMap<String, ConcurrentMap<String, Object>> data =
            new ConcurrentHashMap<String, ConcurrentMap<String, Object>>();

    @Override
    public Map<String, Object> get(String componentName) {
        ConcurrentMap<String, Object> value = data.get(componentName);
        if (value == null) {
            return new HashMap<String, Object>();
        }

        return new HashMap<String, Object>(value);
    }

    @Override
    public Object get(String componentName, String key) {
        if (!data.containsKey(componentName)) {
            return null;
        }
        return data.get(componentName).get(key);
    }

    @Override
    public void put(String componentName, String key, Object value) {
        Map<String, Object> componentData = data.get(componentName);
        if (null == componentData) {
            data.putIfAbsent(componentName, new ConcurrentHashMap<String, Object>());
            componentData = data.get(componentName);
        }
        componentData.put(key, value);
    }

    @Override
    public void remove(String componentName, String key) {
        if (!data.containsKey(componentName)) {
            return;
        }
        data.get(componentName).remove(key);
    }

}
  • SimpleDataStore使用ConcurrentHashMap来存储数据,每个key存储的是ConcurrentMap

实例

dubbo-2.7.2/dubbo-common/src/test/java/org/apache/dubbo/common/store/support/SimpleDataStoreTest.java

public class SimpleDataStoreTest {
    private SimpleDataStore dataStore = new SimpleDataStore();

    @Test
    public void testPutGet() throws Exception {
        assertNull(dataStore.get("xxx", "yyy"));

        dataStore.put("name", "key", "1");
        assertEquals("1", dataStore.get("name", "key"));

        assertNull(dataStore.get("xxx", "yyy"));
    }

    @Test
    public void testRemove() throws Exception {
        dataStore.remove("xxx", "yyy");

        dataStore.put("name", "key", "1");
        dataStore.remove("name", "key");
        assertNull(dataStore.get("name", "key"));
    }

    @Test
    public void testGetComponent() throws Exception {
        Map<String, Object> map = dataStore.get("component");
        assertTrue(map != null && map.isEmpty());
        dataStore.put("component", "key", "value");
        map = dataStore.get("component");
        assertTrue(map != null && map.size() == 1);
        dataStore.remove("component", "key");
        assertNotEquals(map, dataStore.get("component"));
    }
}
  • 这里使用testPutGet、testRemove、testGetComponent进行了验证

小结

DataStore定义了get、put、remove方法,它有一个实现类为SimpleDataStore;SimpleDataStore使用ConcurrentHashMap来存储数据,每个key存储的是ConcurrentMap

doc


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...


引用和评论

0 条评论