Skip to content

Commit ba6602a

Browse files
committed
LRU cache integration
1 parent 8f81709 commit ba6602a

2 files changed

Lines changed: 50 additions & 15 deletions

File tree

java/rocksjni/lru_cache.cc

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,33 @@
1616
/*
1717
* Class: org_rocksdb_LRUCache
1818
* Method: newLRUCache
19-
* Signature: (JIZD)J
19+
* Signature: (JIZDDJ)J
2020
*/
2121
jlong Java_org_rocksdb_LRUCache_newLRUCache(JNIEnv* /*env*/, jclass /*jcls*/,
2222
jlong jcapacity,
2323
jint jnum_shard_bits,
2424
jboolean jstrict_capacity_limit,
2525
jdouble jhigh_pri_pool_ratio,
26-
jdouble jlow_pri_pool_ratio) {
27-
auto* sptr_lru_cache = new std::shared_ptr<ROCKSDB_NAMESPACE::Cache>(
28-
ROCKSDB_NAMESPACE::NewLRUCache(
29-
static_cast<size_t>(jcapacity), static_cast<int>(jnum_shard_bits),
30-
static_cast<bool>(jstrict_capacity_limit),
31-
static_cast<double>(jhigh_pri_pool_ratio),
32-
nullptr /* memory_allocator */, rocksdb::kDefaultToAdaptiveMutex,
33-
rocksdb::kDefaultCacheMetadataChargePolicy,
34-
static_cast<double>(jlow_pri_pool_ratio)));
26+
jdouble jlow_pri_pool_ratio,
27+
jlong jsecondary_cache_handle) {
28+
std::shared_ptr<ROCKSDB_NAMESPACE::SecondaryCache> secondary_cache;
29+
if (jsecondary_cache_handle != 0) {
30+
auto* secondary_cache_ptr =
31+
reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::SecondaryCache>*>(
32+
jsecondary_cache_handle);
33+
secondary_cache = *secondary_cache_ptr;
34+
}
35+
36+
auto opts = ROCKSDB_NAMESPACE::LRUCacheOptions();
37+
opts.capacity = static_cast<size_t>(jcapacity);
38+
opts.num_shard_bits = static_cast<int>(jnum_shard_bits);
39+
opts.strict_capacity_limit = static_cast<bool>(jstrict_capacity_limit);
40+
opts.high_pri_pool_ratio = static_cast<double>(jhigh_pri_pool_ratio);
41+
opts.low_pri_pool_ratio = static_cast<double>(jlow_pri_pool_ratio);
42+
opts.secondary_cache = secondary_cache;
43+
44+
auto* sptr_lru_cache =
45+
new std::shared_ptr<ROCKSDB_NAMESPACE::Cache>(opts.MakeSharedCache());
3546
return GET_CPLUSPLUS_POINTER(sptr_lru_cache);
3647
}
3748

java/src/main/java/org/rocksdb/LRUCache.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public LRUCache(final long capacity) {
3131
* by hash of the key
3232
*/
3333
public LRUCache(final long capacity, final int numShardBits) {
34-
super(newLRUCache(capacity, numShardBits, false, 0.0, 0.0));
34+
super(newLRUCache(capacity, numShardBits, false, 0.0, 0.0, 0));
3535
}
3636

3737
/**
@@ -49,7 +49,7 @@ public LRUCache(final long capacity, final int numShardBits) {
4949
*/
5050
public LRUCache(final long capacity, final int numShardBits,
5151
final boolean strictCapacityLimit) {
52-
super(newLRUCache(capacity, numShardBits, strictCapacityLimit, 0.0, 0.0));
52+
super(newLRUCache(capacity, numShardBits, strictCapacityLimit, 0.0, 0.0, 0));
5353
}
5454

5555
/**
@@ -71,7 +71,7 @@ public LRUCache(final long capacity, final int numShardBits,
7171
*/
7272
public LRUCache(final long capacity, final int numShardBits, final boolean strictCapacityLimit,
7373
final double highPriPoolRatio) {
74-
super(newLRUCache(capacity, numShardBits, strictCapacityLimit, highPriPoolRatio, 0.0));
74+
super(newLRUCache(capacity, numShardBits, strictCapacityLimit, highPriPoolRatio, 0.0, 0));
7575
}
7676

7777
/**
@@ -96,12 +96,36 @@ public LRUCache(final long capacity, final int numShardBits, final boolean stric
9696
public LRUCache(final long capacity, final int numShardBits, final boolean strictCapacityLimit,
9797
final double highPriPoolRatio, final double lowPriPoolRatio) {
9898
super(newLRUCache(
99-
capacity, numShardBits, strictCapacityLimit, highPriPoolRatio, lowPriPoolRatio));
99+
capacity, numShardBits, strictCapacityLimit, highPriPoolRatio, lowPriPoolRatio, 0));
100+
}
101+
102+
/**
103+
* Create a new cache with a secondary cache tier.
104+
* Evicted entries go to secondary cache, secondary hits promote back to primary.
105+
* numShardBits = -1 means it is automatically determined: every shard
106+
* will be at least 512KB and number of shard bits will not exceed 6.
107+
*
108+
* @param capacity The fixed size capacity of the primary cache
109+
* @param numShardBits The cache is sharded to 2^numShardBits shards,
110+
* by hash of the key
111+
* @param strictCapacityLimit insert to the cache will fail when cache is full
112+
* @param highPriPoolRatio percentage of the cache reserves for high priority
113+
* entries
114+
* @param lowPriPoolRatio percentage of the cache reserves for low priority
115+
* entries
116+
* @param secondaryCache the secondary cache instance, or null for no secondary cache
117+
*/
118+
public LRUCache(final long capacity, final int numShardBits, final boolean strictCapacityLimit,
119+
final double highPriPoolRatio, final double lowPriPoolRatio,
120+
final SecondaryCache secondaryCache) {
121+
super(newLRUCache(
122+
capacity, numShardBits, strictCapacityLimit, highPriPoolRatio, lowPriPoolRatio,
123+
secondaryCache == null ? 0 : secondaryCache.nativeHandle_));
100124
}
101125

102126
private static native long newLRUCache(final long capacity, final int numShardBits,
103127
final boolean strictCapacityLimit, final double highPriPoolRatio,
104-
final double lowPriPoolRatio);
128+
final double lowPriPoolRatio, final long secondaryCacheHandle);
105129
@Override
106130
protected final void disposeInternal(final long handle) {
107131
disposeInternalJni(handle);

0 commit comments

Comments
 (0)