Skip to content

Commit 9244efc

Browse files
Merge pull request #16 from aaronriekenberg/main
Add NativeMemoryMap.hottestKeys, NativeMemoryMap.coldestKeys.
2 parents 1e5a438 + 6dde47d commit 9244efc

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

examples/map/offheap-eviction/src/main/kotlin/com/target/nativememoryallocator/examples/map/offheap.eviction/OffHeapEviction.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ private class OffHeapEviction {
6868

6969
logger.info { "nativeMemoryMap.size = ${nativeMemoryMap.size}" }
7070
logger.info { "nativeMemoryAllocator.nativeMemoryAllocatorMetadata = ${nativeMemoryAllocator.nativeMemoryAllocatorMetadata}" }
71+
logger.info { "nativeMemoryMap.hottestKeys(10) = ${nativeMemoryMap.hottestKeys(numKeys = 10)}" }
72+
logger.info { "nativeMemoryMap.coldestKeys(10) = ${nativeMemoryMap.coldestKeys(numKeys = 10)}" }
7173

7274
val randomIndexValue = nativeMemoryMap.get(key = randomIndex)
7375
randomIndexValue?.let {

src/main/kotlin/com/target/nativememoryallocator/map/NativeMemoryMap.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,28 @@ interface NativeMemoryMap<KEY_TYPE : Any, VALUE_TYPE : Any> : BaseNativeMemoryMa
9999
* @return [Set] of [Map.Entry] for the map
100100
*/
101101
val entries: Set<Map.Entry<KEY_TYPE, NativeMemoryBufferMetadata>>
102+
103+
/**
104+
* [Set] of most-used keys in the map.
105+
*
106+
* This will only work if [NativeMemoryMapBackend.CAFFEINE] is used,
107+
* and caffeine is configured with maximumSize eviction policy.
108+
*
109+
* @param numKeys number of keys to return
110+
* @return [Set] of hottest [KEY_TYPE] keys
111+
*/
112+
fun hottestKeys(numKeys: Int): Set<KEY_TYPE>
113+
114+
/**
115+
* [Set] of least-used keys in the map.
116+
*
117+
* This will only work if [NativeMemoryMapBackend.CAFFEINE] is used,
118+
* and caffeine is configured with maximumSize eviction policy.
119+
*
120+
* @param numKeys number of keys to return
121+
* @return [Set] of coldest [KEY_TYPE] keys
122+
*/
123+
fun coldestKeys(numKeys: Int): Set<KEY_TYPE>
102124
}
103125

104126
/**

src/main/kotlin/com/target/nativememoryallocator/map/impl/CaffeineNativeMemoryMapImpl.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,18 @@ internal class CaffeineNativeMemoryMapImpl<KEY_TYPE : Any, VALUE_TYPE : Any>(
115115
caffeineStats = caffeineCache.stats(),
116116
)
117117

118+
/**
119+
* Override hottestKeys to use caffeine eviction policy.
120+
*/
121+
override fun hottestKeys(numKeys: Int): Set<KEY_TYPE> {
122+
return caffeineCache.policy().eviction().map { it.hottest(numKeys).keys }.orElse(mutableSetOf())
123+
}
124+
125+
/**
126+
* Override coldestKeys to use caffeine eviction policy.
127+
*/
128+
override fun coldestKeys(numKeys: Int): Set<KEY_TYPE> {
129+
return caffeineCache.policy().eviction().map { it.coldest(numKeys).keys }.orElse(mutableSetOf())
130+
}
131+
118132
}

src/main/kotlin/com/target/nativememoryallocator/map/impl/NativeMemoryMapImpl.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,8 @@ internal class NativeMemoryMapImpl<KEY_TYPE : Any, VALUE_TYPE : Any>(
140140

141141
override val operationCounters: NativeMemoryMapOperationCounters? = null
142142

143+
override fun hottestKeys(numKeys: Int): Set<KEY_TYPE> = emptySet()
144+
145+
override fun coldestKeys(numKeys: Int): Set<KEY_TYPE> = emptySet()
146+
143147
}

0 commit comments

Comments
 (0)