Skip to content

Commit

Permalink
[#11503] Remove duplicate code in SimpleCache
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Sep 20, 2024
1 parent c50d182 commit 6fea585
Show file tree
Hide file tree
Showing 18 changed files with 80 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*/
public class MockApiMetaDataService implements ApiMetaDataService {

private final SimpleCache<String> apiCache = new SimpleCache<>();
private final SimpleCache<String, Integer> apiCache = SimpleCache.newIdCache();

private final EnhancedDataSender<MetaDataType> enhancedDataSender;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.navercorp.pinpoint.profiler.cache;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;

public class AtomicIntId<K> implements Function<K, Integer> {

private final AtomicInteger idGen = new AtomicInteger(0);

@Override
public Integer apply(K key) {
return idGen.incrementAndGet();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ public boolean isNewValue() {
public ID getId() {
return id;
}

@Override
public String toString() {
return "Result{" + newValue +", " + id + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,51 +18,61 @@

import com.github.benmanes.caffeine.cache.Caffeine;

import java.util.Objects;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;

/**
* @author emeroad
*/
public class SimpleCache<T> implements Cache<T, Result<Integer>> {
public class SimpleCache<K, V> implements Cache<K, Result<V>> {
// zero means not exist.
private final ConcurrentMap<T, Result<Integer>> cache;
private final AtomicInteger idGen = new AtomicInteger();
private final ConcurrentMap<K, V> cache;
private final Function<K, V> idFunction;

public SimpleCache() {
this(1024);
public static <K> SimpleCache<K, Integer> newIdCache() {
return newIdCache(1024);
}

public SimpleCache(int cacheSize) {
public static <K> SimpleCache<K, Integer> newIdCache(int cacheSize) {
return new SimpleCache<>(cacheSize, new AtomicIntId<>());
}

public SimpleCache(Function<K, V> idFunction) {
this(1024, idFunction);
}

public SimpleCache(int cacheSize, Function<K, V> idFunction) {
this.cache = createCache(cacheSize);
this.idFunction = Objects.requireNonNull(idFunction, "idFunction");
}

private ConcurrentMap<T, Result<Integer>> createCache(int maxCacheSize) {
private ConcurrentMap<K, V> createCache(int maxCacheSize) {
final Caffeine<Object, Object> cacheBuilder = CaffeineBuilder.newBuilder();
cacheBuilder.initialCapacity(maxCacheSize);
cacheBuilder.maximumSize(maxCacheSize);
com.github.benmanes.caffeine.cache.Cache<T, Result<Integer>> localCache = cacheBuilder.build();
com.github.benmanes.caffeine.cache.Cache<K, V> localCache = cacheBuilder.build();
return localCache.asMap();
}

@Override
public Result<Integer> put(T value) {
final Result<Integer> find = this.cache.get(value);
if (find != null) {
return find;
}
public Result<V> put(K value) {
final PutIfAbsent putIfAbsent = new PutIfAbsent();
final V id = this.cache.computeIfAbsent(value, putIfAbsent);
return new Result<>(putIfAbsent.called, id);
}

private class PutIfAbsent implements Function<K, V> {

private boolean called;

// Use negative values too to reduce data size
final int newId = nextId();
final Result<Integer> result = new Result<>(false, newId);
final Result<Integer> before = this.cache.putIfAbsent(value, result);
if (before != null) {
return before;
public PutIfAbsent() {
}
return new Result<>(true, newId);
}

private int nextId() {
return this.idGen.incrementAndGet();
@Override
public V apply(K key) {
this.called = true;
return idFunction.apply(key);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public ApiMetaDataServiceProvider(@MetadataDataSender Provider<EnhancedDataSende
@Override
public ApiMetaDataService get() {
final EnhancedDataSender<MetaDataType> enhancedDataSender = this.enhancedDataSenderProvider.get();
final SimpleCache<String> simpleCache = simpleCacheFactory.newSimpleCache();
final SimpleCache<String, Integer> simpleCache = simpleCacheFactory.newSimpleCache();
return new DefaultApiMetaDataService(enhancedDataSender, simpleCache);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ public class SimpleCacheFactory {
public SimpleCacheFactory() {
}

public <T> SimpleCache<T> newSimpleCache() {
return new SimpleCache<>();
public <T> SimpleCache<T, Integer> newSimpleCache() {
return SimpleCache.newIdCache();
}

public <T> SimpleCache<T> newSimpleCache(int cacheSize) {
return new SimpleCache<>(cacheSize);
public <T> SimpleCache<T, Integer> newSimpleCache(int cacheSize) {
return SimpleCache.newIdCache(cacheSize);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public SqlMetaDataService get() {
SqlCacheService<byte[]> sqlCacheService = new SqlCacheService<>(enhancedDataSender, simpleCachingSqlNormalizer, maxSqlLength);
return new SqlUidMetaDataService(sqlCacheService);
} else {
final SimpleCache<String> stringCache = simpleCacheFactory.newSimpleCache(jdbcSqlCacheSize);
final SimpleCache<String, Integer> stringCache = simpleCacheFactory.newSimpleCache(jdbcSqlCacheSize);
SimpleCachingSqlNormalizer simpleCachingSqlNormalizer = new SimpleCachingSqlNormalizer(stringCache);
SqlCacheService<Integer> sqlCacheService = new SqlCacheService<>(enhancedDataSender, simpleCachingSqlNormalizer, maxSqlLength);
return new DefaultSqlMetaDataService(sqlCacheService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public StringMetadataServiceProvider(@MetadataDataSender EnhancedDataSender<Meta

@Override
public StringMetaDataService get() {
final SimpleCache<String> stringCache = simpleCacheFactory.newSimpleCache();
final SimpleCache<String, Integer> stringCache = simpleCacheFactory.newSimpleCache();
return new DefaultStringMetaDataService(enhancedDataSender, stringCache);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
*/
public class DefaultApiMetaDataService implements ApiMetaDataService {

private final SimpleCache<String> apiCache;
private final SimpleCache<String, Integer> apiCache;

private final EnhancedDataSender<MetaDataType> enhancedDataSender;

public DefaultApiMetaDataService(EnhancedDataSender<MetaDataType> enhancedDataSender, SimpleCache<String> apiCache) {
public DefaultApiMetaDataService(EnhancedDataSender<MetaDataType> enhancedDataSender, SimpleCache<String, Integer> apiCache) {
this.enhancedDataSender = Objects.requireNonNull(enhancedDataSender, "enhancedDataSender");
this.apiCache = Objects.requireNonNull(apiCache, "apiCache");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
*/
public class DefaultStringMetaDataService implements StringMetaDataService {

private final SimpleCache<String> stringCache;
private final SimpleCache<String, Integer> stringCache;

private final EnhancedDataSender<MetaDataType> enhancedDataSender;

public DefaultStringMetaDataService(EnhancedDataSender<MetaDataType> enhancedDataSender, SimpleCache<String> stringCache) {
public DefaultStringMetaDataService(EnhancedDataSender<MetaDataType> enhancedDataSender, SimpleCache<String, Integer> stringCache) {
this.enhancedDataSender = Objects.requireNonNull(enhancedDataSender, "enhancedDataSender");
this.stringCache = Objects.requireNonNull(stringCache, "stringCache");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.navercorp.pinpoint.common.profiler.sql.SqlNormalizer;
import com.navercorp.pinpoint.profiler.cache.Cache;
import com.navercorp.pinpoint.profiler.cache.Result;
import com.navercorp.pinpoint.profiler.cache.UidCache;
import com.navercorp.pinpoint.profiler.cache.SimpleCache;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand All @@ -22,7 +22,7 @@ public class UidCachingSqlNormalizer implements CachingSqlNormalizer<ParsingResu
private final int lengthLimit;

public UidCachingSqlNormalizer(int cacheSize, int lengthLimit) {
this.sqlCache = new UidCache(cacheSize, hashFunction);
this.sqlCache = new SimpleCache<>(cacheSize, hashFunction);
this.sqlNormalizer = new DefaultSqlNormalizer();
this.lengthLimit = lengthLimit;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@ public class SimpleCacheTest {

@Test
public void startKey0() {
SimpleCache<String> cache = new SimpleCache<>();
SimpleCache<String, Integer> cache = SimpleCache.newIdCache();
Result<Integer> test1 = cache.put("test1");
Result<Integer> test2 = cache.put("test2");

Assertions.assertEquals(1, test1.getId());
Assertions.assertEquals(2, test2.getId());
}


@Test
public void put() {
SimpleCache<String> cache = new SimpleCache<>();
SimpleCache<String, Integer> cache = SimpleCache.newIdCache();;
Result<Integer> test = cache.put("test");
Assertions.assertEquals(1, test.getId());
Assertions.assertTrue(test.isNewValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class UidCacheTest {
UidCache sut;
class SimpleCacheUidFunctionTest {
SimpleCache<String, byte[]> sut;

@Mock
Function<String, byte[]> uidFunction;

@BeforeEach
void setUp() {
sut = new UidCache(1024, uidFunction);
sut = new SimpleCache<>(1024, uidFunction);

when(uidFunction.apply(any()))
.thenReturn(new byte[]{});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class DefaultApiMetaDataServiceTest {
@Test
public void cacheApi() {
EnhancedDataSender<MetaDataType> dataSender = mock(EnhancedDataSender.class);
SimpleCache<String> cache = new SimpleCache<>();
SimpleCache<String, Integer> cache = SimpleCache.newIdCache();
ApiMetaDataService apiMetaDataService = new DefaultApiMetaDataService(dataSender, cache);

MethodDescriptor methodDescriptor = new DefaultMethodDescriptor("clazz", "method",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class DefaultStringMetaDataServiceTest {
@Test
public void cacheString() {
EnhancedDataSender<MetaDataType> dataSender = mock(EnhancedDataSender.class);
SimpleCache<String> stringCache = new SimpleCache<>();
SimpleCache<String, Integer> stringCache = SimpleCache.newIdCache();
StringMetaDataService stringMetaDataService = new DefaultStringMetaDataService(dataSender, stringCache);

String str = "test";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class SimpleCachingSqlNormalizerTest {

@Test
public void testNormalizedSql() {
SimpleCache<String> cache = newCache(1);
SimpleCache<String, Integer> cache = newCache(1);
SimpleCachingSqlNormalizer normalizer = new SimpleCachingSqlNormalizer(cache);
ParsingResultInternal<Integer> parsingResult = new DefaultParsingResult("select * from dual");

Expand All @@ -45,7 +45,7 @@ public void testNormalizedSql() {

@Test
public void testNormalizedSql_cache_expire() {
SimpleCache<String> cache = newCache(1);
SimpleCache<String, Integer> cache = newCache(1);
SimpleCachingSqlNormalizer normalizer = new SimpleCachingSqlNormalizer(cache);
ParsingResultInternal<Integer> parsingResult = new DefaultParsingResult("select * from table1");
boolean newCache = normalizer.normalizedSql(parsingResult);
Expand All @@ -61,7 +61,7 @@ public void testNormalizedSql_cache_expire() {
Assertions.assertTrue(newCache_parsingResult1_recached);
}

private SimpleCache<String> newCache(int size) {
return new SimpleCache<>(size);
private SimpleCache<String, Integer> newCache(int cacheSize) {
return SimpleCache.newIdCache(cacheSize);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class SqlCacheServiceTest {

@BeforeEach
public void setUp() {
SimpleCache<String> sqlCache = new SimpleCache<>(100);
SimpleCache<String, Integer> sqlCache = SimpleCache.newIdCache(100);
SimpleCachingSqlNormalizer cachingSqlNormalizer = new SimpleCachingSqlNormalizer(sqlCache);
sut = new SqlCacheService<>(dataSender, cachingSqlNormalizer, MAX_LENGTH);
}
Expand Down

0 comments on commit 6fea585

Please sign in to comment.