您好,登錄后才能下訂單哦!
這篇文章給大家介紹zk中ReferenceCountedACLCache的作用是什么,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
作用:完成LIst<ACL>與Long互相轉換,DataNode中acl是一個Long值,并不是ACL列表
空間復雜:內部類AtomicLongWithEquals
屬性:
//日志信息 private static final Logger LOG = LoggerFactory.getLogger(ReferenceCountedACLCache.class); // long ACL 列表對應關系 final Map<Long, List<ACL>> longKeyMap = new HashMap<Long, List<ACL>>(); // ACL 列表 long對應關系 final Map<List<ACL>, Long> aclKeyMap = new HashMap<List<ACL>, Long>(); final Map<Long, AtomicLongWithEquals> referenceCounter = new HashMap<Long, AtomicLongWithEquals>(); private static final long OPEN_UNSAFE_ACL_ID = -1L; /** * these are the number of acls that we have in the datatree */ long aclIndex = 0;
方法:
記錄引用次數
private static class AtomicLongWithEquals extends AtomicLong { private static final long serialVersionUID = 3355155896813725462L; public AtomicLongWithEquals(long i) { super(i); } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } return equals((AtomicLongWithEquals) o); } public boolean equals(AtomicLongWithEquals that) { return get() == that.get(); } @Override public int hashCode() { return 31 * Long.valueOf(get()).hashCode(); } } 添加使用 public synchronized void addUsage(Long acl) { if (acl == OPEN_UNSAFE_ACL_ID) { return; } if (!longKeyMap.containsKey(acl)) { LOG.info("Ignoring acl " + acl + " as it does not exist in the cache"); return; } AtomicLong count = referenceCounter.get(acl); if (count == null) { referenceCounter.put(acl, new AtomicLongWithEquals(1)); } else { count.incrementAndGet(); } } //移除引用 public synchronized void removeUsage(Long acl) { if (acl == OPEN_UNSAFE_ACL_ID) { return; } if (!longKeyMap.containsKey(acl)) { LOG.info("Ignoring acl " + acl + " as it does not exist in the cache"); return; } long newCount = referenceCounter.get(acl).decrementAndGet(); if (newCount <= 0) { referenceCounter.remove(acl); aclKeyMap.remove(longKeyMap.get(acl)); longKeyMap.remove(acl); } } //如果引用計數值小于0,則移除相關信息 public synchronized void purgeUnused() { Iterator<Map.Entry<Long, AtomicLongWithEquals>> refCountIter = referenceCounter.entrySet().iterator(); while (refCountIter.hasNext()) { Map.Entry<Long, AtomicLongWithEquals> entry = refCountIter.next(); if (entry.getValue().get() <= 0) { Long acl = entry.getKey(); aclKeyMap.remove(longKeyMap.get(acl)); longKeyMap.remove(acl); refCountIter.remove(); } } }
關于zk中ReferenceCountedACLCache的作用是什么就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。