hsf筆記-AttributeMap

1.AttributeMap

AttributeMap有2個(gè)實(shí)現(xiàn)

  1. DefaultAttributeMap 使用數(shù)組實(shí)現(xiàn)
  2. ConcurrentAttributeMap 使用ConcurrentHashMap實(shí)現(xiàn)
image.png

其中key為AttributeKey

public interface AttributeMap {
    Object put(AttributeKey key, Object value);

    Object get(AttributeKey key);

    Object putIfAbsent(AttributeKey key, Object value);

    Object remove(AttributeKey key);
}

2.AttributeKey

AttributeKey中比較關(guān)鍵是AttributeNamespace,其中id是由namespace的IdGen遞增添加的

public class AttributeKey {
    private final AttributeNamespace namespace;
    private final String name;
    private final int id;

    protected AttributeKey(AttributeNamespace namespace, String name) {
        this.namespace = namespace;
        this.name = name;
        this.id = namespace.getIdGen().getAndIncrement();
    }

    public int getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

    public AttributeNamespace getNamespace() {
        return this.namespace;
    }
}

3. AttributeNamespace

AttributeNamespace由2個(gè)Map來(lái)維護(hù)索引,name和id,每當(dāng)創(chuàng)建一個(gè)新的AttributeKey時(shí),id就會(huì)遞增1

public class AttributeNamespace {
    private static final ConcurrentHashMap<String, AttributeNamespace> name2Instance = new ConcurrentHashMap();
    private final String namespace;
    private final AtomicInteger idGen = new AtomicInteger();
    private final ConcurrentHashMap<String, AttributeKey> name2AttributeKey = new ConcurrentHashMap();
    private final ConcurrentHashMap<Integer, AttributeKey> id2AttributeKey = new ConcurrentHashMap();

    public static AttributeNamespace getOrCreateNamespace(String namespace) {
        AttributeNamespace instance = (AttributeNamespace)name2Instance.get(namespace);
        if (instance == null) {
            instance = new AttributeNamespace(namespace);
            AttributeNamespace old = (AttributeNamespace)name2Instance.putIfAbsent(namespace, instance);
            if (old != null) {
                instance = old;
            }
        }

        return instance;
    }

    public static AttributeNamespace createNamespace(String namespace) {
        AttributeNamespace key = new AttributeNamespace(namespace);
        AttributeNamespace old = (AttributeNamespace)name2Instance.putIfAbsent(namespace, key);
        if (old != null) {
            throw new IllegalArgumentException(String.format("namespace '%s' is already in use", namespace));
        } else {
            return key;
        }
    }

    private AttributeNamespace(String namespace) {
        this.namespace = namespace;
    }

    public String getNamespace() {
        return this.namespace;
    }

    public AtomicInteger getIdGen() {
        return this.idGen;
    }

    public AttributeKey getOrCreate(String name) {
        AttributeKey key = (AttributeKey)this.name2AttributeKey.get(name);
        if (key == null) {
            key = new AttributeKey(this, name);
            AttributeKey old = (AttributeKey)this.name2AttributeKey.putIfAbsent(name, key);
            if (old == null) {
                this.cacheForId(key);
            } else {
                key = old;
            }
        }

        return key;
    }

    public AttributeKey create(String name) {
        AttributeKey key = new AttributeKey(this, name);
        AttributeKey old = (AttributeKey)this.name2AttributeKey.putIfAbsent(name, key);
        if (old != null) {
            throw new IllegalArgumentException(String.format("name '%s' in namespace '%s' is already in use", name, this.namespace));
        } else {
            this.cacheForId(key);
            return key;
        }
    }

    private void cacheForId(AttributeKey key) {
        this.id2AttributeKey.put(key.getId(), key);
    }

    public AttributeKey get(int id) {
        return (AttributeKey)this.id2AttributeKey.get(id);
    }
}

3.1 AttributeNamespace操作示例

   @Test
   public void test1()
   {
       AttributeNamespace space=AttributeNamespace.createNamespace("test");
       space.create("test");
       space.getOrCreate("test");
       space.create("test1");
       space.create("test2");
   }

4.AttributeMap和AttributeNamespace

首先當(dāng)使用AttributeNamespace創(chuàng)建AttributeKey時(shí),其內(nèi)部容器已經(jīng)有一個(gè)備份,
所以不管在AttributeMap添加還是刪除,相同的AttributeKey則不會(huì)再重新創(chuàng)建,這樣的好處可以讓相同AttributeNamespace的AttributeKey可以在不同的AttributeMap中共享,最終實(shí)現(xiàn)了相同的AttributeKey,不同的value

public class AttributeMapTest {
    @Test
    public void test2() {
        AttributeNamespace space = AttributeNamespace.createNamespace("test");

        AttributeMap map = new DefaultAttributeMap(space, 12);

        for (int i = 0; i < 20; i++) {
            map.put(space.getOrCreate("key" + i), "value" + i);
        }

        System.out.println(map.get(space.getOrCreate("key2")));

        for (int i = 0; i < 20; i++) {
            map.remove(space.getOrCreate("key" + i));
        }

        map.put(space.getOrCreate("key" + 80), "value" + 88);

        for (int i = 0; i < 20; i++) {
            map.put(space.getOrCreate("key" + i), "value" + i);
        }
        AttributeMap map2 = new ConcurrentAttributeMap(space, 5);
        for (int i = 0; i < 20; i++) {
            map2.put(space.getOrCreate("key" + i), "value2" + i);
        }
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,602評(píng)論 19 139
  • 關(guān)于Mongodb的全面總結(jié) MongoDB的內(nèi)部構(gòu)造《MongoDB The Definitive Guide》...
    中v中閱讀 32,309評(píng)論 2 89
  • 1.ios高性能編程 (1).內(nèi)層 最小的內(nèi)層平均值和峰值(2).耗電量 高效的算法和數(shù)據(jù)結(jié)構(gòu)(3).初始化時(shí)...
    歐辰_OSR閱讀 30,262評(píng)論 8 265
  • 秋光半盞,夜夢(mèng)難離,青山往事猶在,兩堆黃土心悲。淚眸情深孫兒懂,不是無(wú)心、卻也無(wú)心,而今恩情難還,腸斷天涯...
    子山上人閱讀 323評(píng)論 0 0
  • 各式蛋糕,預(yù)定來(lái)私我
    oktracy閱讀 173評(píng)論 0 0

友情鏈接更多精彩內(nèi)容