-
Notifications
You must be signed in to change notification settings - Fork 870
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Drop instrumentation-api-caching module and move weak cache implementation to instrumentation-api #4667
Drop instrumentation-api-caching module and move weak cache implementation to instrumentation-api #4667
Changes from 11 commits
99b42e1
f08d4f5
e651e80
c63d3fe
f61ea3a
830e1d2
4abddb3
7bdf3e0
cde78fb
437da21
fdc64bb
f7385d4
5e595e0
0e04a04
257a6ec
2f57a21
dac1522
6d63815
7448cde
fad0967
9ab70f4
3f36678
6ebb98b
37569ca
fe8ff0b
d4d02da
55f8292
ce93d40
03824a4
a8805fb
7ad1a12
7ae3ab2
11bee8a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.api.cache; | ||
|
||
import java.util.concurrent.ConcurrentMap; | ||
import java.util.function.Function; | ||
import javax.annotation.Nullable; | ||
|
||
final class BoundedCache<K, V> implements Cache<K, V> { | ||
trask marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private final ConcurrentMap<K, V> delegate; | ||
|
||
BoundedCache(ConcurrentMap<K, V> delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) { | ||
return delegate.computeIfAbsent(key, mappingFunction); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public V get(K key) { | ||
return delegate.get(key); | ||
} | ||
|
||
@Override | ||
public void put(K key, V value) { | ||
delegate.put(key, value); | ||
} | ||
|
||
@Override | ||
public void remove(K key) { | ||
delegate.remove(key); | ||
} | ||
|
||
// Visible for tests | ||
int size() { | ||
return delegate.size(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.api.cache; | ||
|
||
import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap; | ||
import java.util.function.Function; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* A cache from keys to values. | ||
* | ||
* <p>Keys are always referenced weakly and are compared using identity comparison, not {@link | ||
trask marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* Object#equals(Object)}. | ||
*/ | ||
public interface Cache<K, V> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to keep the name There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did not want to rename in this PR, to avoid too many changes at once. We can rename later, if we want There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now there are two different implementations, so we better keep this generic name |
||
|
||
/** | ||
* Returns new unbounded cache | ||
* | ||
* <p>Keys are referenced weakly and compared using identity comparison, not {@link | ||
* Object#equals(Object)}. | ||
*/ | ||
static <K, V> Cache<K, V> weak() { | ||
return new WeakLockFreeCache<>(); | ||
} | ||
|
||
/** | ||
* Returns new bounded cache. | ||
* | ||
* <p>Both keys and values are strongly referenced. | ||
*/ | ||
static <K, V> Cache<K, V> bounded(int capacity) { | ||
ConcurrentLinkedHashMap<K, V> map = | ||
new ConcurrentLinkedHashMap.Builder<K, V>().maximumWeightedCapacity(capacity).build(); | ||
return new BoundedCache<>(map); | ||
} | ||
|
||
/** | ||
* Returns the cached value associated with the provided {@code key}. If no value is cached yet, | ||
* computes the value using {@code mappingFunction}, stores the result, and returns it. | ||
*/ | ||
V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction); | ||
|
||
/** | ||
* Returns the cached value associated with the provided {@code key} if present, or {@code null} | ||
* otherwise. | ||
*/ | ||
@Nullable | ||
V get(K key); | ||
|
||
/** Puts the {@code value} into the cache for the {@code key}. */ | ||
void put(K key, V value); | ||
|
||
/** Removes a value for {@code key} if present. */ | ||
void remove(K key); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's shade these in. less transitive dependencies will make
instrumentation-api
more palatable for libraries to embed directlyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we copied in CLHM in order to update code to use default CHM
I copied in weak-lock-free also (instead of shading), just to avoid gradle shadow problems (only 4 files)