-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use LRU cache for statement parser (#3492)
- Loading branch information
Showing
9 changed files
with
85 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
apm-agent-core/src/main/java/co/elastic/apm/agent/collections/LRUCacheFactoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package co.elastic.apm.agent.collections; | ||
|
||
import co.elastic.apm.agent.sdk.internal.collections.LRUCacheFactory; | ||
import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap; | ||
|
||
import java.util.Map; | ||
|
||
public class LRUCacheFactoryImpl implements LRUCacheFactory { | ||
@Override | ||
public <K, V> Map<K, V> createCache(int capacity) { | ||
return new ConcurrentLinkedHashMap.Builder<K,V>() | ||
.concurrencyLevel(Runtime.getRuntime().availableProcessors()) | ||
.maximumWeightedCapacity(capacity) | ||
.build(); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...resources/META-INF/services/co.elastic.apm.agent.sdk.internal.collections.LRUCacheFactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
co.elastic.apm.agent.collections.LRUCacheFactoryImpl |
26 changes: 26 additions & 0 deletions
26
...gent-plugin-sdk/src/main/java/co/elastic/apm/agent/sdk/internal/collections/LRUCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package co.elastic.apm.agent.sdk.internal.collections; | ||
|
||
import co.elastic.apm.agent.sdk.internal.InternalUtil; | ||
import co.elastic.apm.agent.sdk.internal.pooling.ObjectPooling; | ||
|
||
import java.util.Map; | ||
|
||
public class LRUCache { | ||
|
||
private static final LRUCacheFactory factory; | ||
|
||
static { | ||
factory = InternalUtil.getServiceProvider(LRUCacheFactory.class); | ||
} | ||
|
||
|
||
/** | ||
* Creates a bounded LRU-cache. Keys and values are strongly referenced. | ||
* The returned map is guaranteed to be thread-safe. | ||
* | ||
* @param capacity the capacity of the cache | ||
*/ | ||
public static <K,V> Map<K,V> createCache(int capacity) { | ||
return factory.createCache(capacity); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...ugin-sdk/src/main/java/co/elastic/apm/agent/sdk/internal/collections/LRUCacheFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package co.elastic.apm.agent.sdk.internal.collections; | ||
|
||
import java.util.Map; | ||
|
||
public interface LRUCacheFactory { | ||
|
||
<K,V> Map<K,V> createCache(int capacity); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
.../src/test/java/co/elastic/apm/agent/sdk/internal/collections/NonEvictingCacheFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package co.elastic.apm.agent.sdk.internal.collections; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
public class NonEvictingCacheFactory implements LRUCacheFactory{ | ||
@Override | ||
public <K, V> Map<K, V> createCache(int capacity) { | ||
return new ConcurrentHashMap<>(); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...resources/META-INF/services/co.elastic.apm.agent.sdk.internal.collections.LRUCacheFactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
co.elastic.apm.agent.sdk.internal.collections.NonEvictingCacheFactory |