Skip to content

Commit

Permalink
Add NativeMemoryMap.hottestKeys, NativeMemoryMap.coldestKeys.
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronriekenberg committed Mar 8, 2024
1 parent 1e5a438 commit 6dde47d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ private class OffHeapEviction {

logger.info { "nativeMemoryMap.size = ${nativeMemoryMap.size}" }
logger.info { "nativeMemoryAllocator.nativeMemoryAllocatorMetadata = ${nativeMemoryAllocator.nativeMemoryAllocatorMetadata}" }
logger.info { "nativeMemoryMap.hottestKeys(10) = ${nativeMemoryMap.hottestKeys(numKeys = 10)}" }
logger.info { "nativeMemoryMap.coldestKeys(10) = ${nativeMemoryMap.coldestKeys(numKeys = 10)}" }

val randomIndexValue = nativeMemoryMap.get(key = randomIndex)
randomIndexValue?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,28 @@ interface NativeMemoryMap<KEY_TYPE : Any, VALUE_TYPE : Any> : BaseNativeMemoryMa
* @return [Set] of [Map.Entry] for the map
*/
val entries: Set<Map.Entry<KEY_TYPE, NativeMemoryBufferMetadata>>

/**
* [Set] of most-used keys in the map.
*
* This will only work if [NativeMemoryMapBackend.CAFFEINE] is used,
* and caffeine is configured with maximumSize eviction policy.
*
* @param numKeys number of keys to return
* @return [Set] of hottest [KEY_TYPE] keys
*/
fun hottestKeys(numKeys: Int): Set<KEY_TYPE>

/**
* [Set] of least-used keys in the map.
*
* This will only work if [NativeMemoryMapBackend.CAFFEINE] is used,
* and caffeine is configured with maximumSize eviction policy.
*
* @param numKeys number of keys to return
* @return [Set] of coldest [KEY_TYPE] keys
*/
fun coldestKeys(numKeys: Int): Set<KEY_TYPE>
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,18 @@ internal class CaffeineNativeMemoryMapImpl<KEY_TYPE : Any, VALUE_TYPE : Any>(
caffeineStats = caffeineCache.stats(),
)

/**
* Override hottestKeys to use caffeine eviction policy.
*/
override fun hottestKeys(numKeys: Int): Set<KEY_TYPE> {
return caffeineCache.policy().eviction().map { it.hottest(numKeys).keys }.orElse(mutableSetOf())
}

/**
* Override coldestKeys to use caffeine eviction policy.
*/
override fun coldestKeys(numKeys: Int): Set<KEY_TYPE> {
return caffeineCache.policy().eviction().map { it.coldest(numKeys).keys }.orElse(mutableSetOf())
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,8 @@ internal class NativeMemoryMapImpl<KEY_TYPE : Any, VALUE_TYPE : Any>(

override val operationCounters: NativeMemoryMapOperationCounters? = null

override fun hottestKeys(numKeys: Int): Set<KEY_TYPE> = emptySet()

override fun coldestKeys(numKeys: Int): Set<KEY_TYPE> = emptySet()

}

0 comments on commit 6dde47d

Please sign in to comment.