Skip to content

Commit

Permalink
Merge pull request #16 from aaronriekenberg/main
Browse files Browse the repository at this point in the history
Add NativeMemoryMap.hottestKeys, NativeMemoryMap.coldestKeys.
  • Loading branch information
aaronriekenberg authored Mar 11, 2024
2 parents 1e5a438 + 6dde47d commit 9244efc
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 9244efc

Please sign in to comment.