From 5c958cb3e568975cb53a4c7ea398b993c8ae2730 Mon Sep 17 00:00:00 2001 From: Gabriel Reid Date: Tue, 30 Oct 2018 13:14:42 +0100 Subject: [PATCH 1/2] Update project pom --- pom.xml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pom.xml b/pom.xml index 7dac5c194..bfe869874 100644 --- a/pom.xml +++ b/pom.xml @@ -68,6 +68,12 @@ This file is read only. Generated by the Tablesaw Ivy plugin. 2.13.0 test + + org.assertj + assertj-core + 3.11.1 + test + io.netty netty From 2892e2fda51e786c1d22ba95d2de76d816b87583 Mon Sep 17 00:00:00 2001 From: Gabriel Reid Date: Tue, 30 Oct 2018 13:32:32 +0100 Subject: [PATCH 2/2] Use JDK classes for DataCache Minor change to use built-in JDK functionality for the DataCache class. This is just an opportunity to remove some code, as well as a minor speed increase because it only requires calculating the hashCode for stored elements once (instead of once during a store operation and once during eviction of the oldest element). --- .../datastore/cassandra/DataCache.java | 105 ++++-------------- 1 file changed, 21 insertions(+), 84 deletions(-) diff --git a/src/main/java/org/kairosdb/datastore/cassandra/DataCache.java b/src/main/java/org/kairosdb/datastore/cassandra/DataCache.java index e7cd08682..ab80f26b0 100755 --- a/src/main/java/org/kairosdb/datastore/cassandra/DataCache.java +++ b/src/main/java/org/kairosdb/datastore/cassandra/DataCache.java @@ -16,8 +16,12 @@ package org.kairosdb.datastore.cassandra; +import com.google.common.collect.ImmutableSet; + +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; /** This cache serves two purposes. @@ -33,37 +37,17 @@ */ public class DataCache { - private final Object m_lock = new Object(); - - private final LinkItem m_front = new LinkItem(null); - private final LinkItem m_back = new LinkItem(null); - - private int m_maxSize; + private Map m_internalMap; - - private class LinkItem + public DataCache(final int cacheSize) { - private LinkItem m_prev; - private LinkItem m_next; - - private final T m_data; - - public LinkItem(T data) - { - m_data = data; - } - } - - //Using a ConcurrentHashMap so we can use the putIfAbsent method. - private ConcurrentHashMap> m_hashMap; - - public DataCache(int cacheSize) - { - m_hashMap = new ConcurrentHashMap<>(); - m_maxSize = cacheSize; - - m_front.m_next = m_back; - m_back.m_prev = m_front; + m_internalMap = Collections.synchronizedMap(new LinkedHashMap(cacheSize, 1f, true) { + @Override + protected boolean removeEldestEntry(Map.Entry eldest) + { + return size() > cacheSize; + } + }); } /** @@ -81,72 +65,25 @@ public DataCache(int cacheSize) */ public T cacheItem(T cacheData) { - LinkItem mappedItem = null; - - synchronized (m_lock) - { - LinkItem li = new LinkItem(cacheData); - mappedItem = m_hashMap.putIfAbsent(cacheData, li); - - if (mappedItem != null) - { - //moves item to top of list - remove(mappedItem); - addItem(mappedItem); - } - else - addItem(li); - - if (m_hashMap.size() > m_maxSize) - { - LinkItem last = m_back.m_prev; - remove(last); - - m_hashMap.remove(last.m_data); - } - } - - return (mappedItem == null ? null : mappedItem.m_data); - } - - private void remove(LinkItem li) - { - li.m_prev.m_next = li.m_next; - li.m_next.m_prev = li.m_prev; + return m_internalMap.putIfAbsent(cacheData, cacheData); } - private void addItem(LinkItem li) - { - li.m_prev = m_front; - li.m_next = m_front.m_next; - - m_front.m_next = li; - li.m_next.m_prev = li; - } public Set getCachedKeys() { - return (m_hashMap.keySet()); + synchronized (m_internalMap) + { + return ImmutableSet.copyOf(m_internalMap.keySet()); + } } public void removeKey(T key) { - synchronized (m_lock) - { - LinkItem li = m_hashMap.remove(key); - if (li != null) - remove(li); - } + m_internalMap.remove(key); } public void clear() { - synchronized (m_lock) - { - m_front.m_next = m_back; - m_back.m_prev = m_front; - - m_hashMap.clear(); - } + m_internalMap.clear(); } }