Skip to content

Commit ac94531

Browse files
committed
SecondaryCache and MemoryAllocator
1 parent 64eebd7 commit ac94531

2 files changed

Lines changed: 160 additions & 0 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package org.rocksdb;
2+
3+
/**
4+
* MemoryAllocator is a custom memory allocator that can be used by RocksDB
5+
* for cache blocks and other memory allocations. This allows using custom
6+
* allocators like JEMalloc for better memory management and reduced fragmentation.
7+
* <p>
8+
* As a descendent of {@link RocksObject}, this class is {@link AutoCloseable}
9+
* and will be automatically released if opened in the preamble of a try with
10+
* resources block.
11+
*/
12+
public abstract class MemoryAllocator extends RocksObject {
13+
14+
/**
15+
* Constructor for MemoryAllocator
16+
*
17+
* @param nativeHandle the native handle to the C++ MemoryAllocator object
18+
*/
19+
protected MemoryAllocator(final long nativeHandle) {
20+
super(nativeHandle);
21+
}
22+
23+
@Override
24+
protected final void disposeInternal(final long handle) {
25+
disposeInternalJni(handle);
26+
}
27+
28+
/**
29+
* Creates a default system memory allocator
30+
*
31+
* @return a new DefaultMemoryAllocator instance
32+
*/
33+
public static MemoryAllocator createDefault() {
34+
return new DefaultMemoryAllocator();
35+
}
36+
37+
/**
38+
* Creates a JEMalloc-based memory allocator.
39+
* This allocator can help reduce memory fragmentation.
40+
* Note: Requires JEMalloc to be available on the system.
41+
*
42+
* @return a new JEMallocMemoryAllocator instance
43+
* @throws UnsupportedOperationException if JEMalloc is not available
44+
*/
45+
public static MemoryAllocator createJEMalloc() {
46+
return new JEMallocMemoryAllocator();
47+
}
48+
49+
/**
50+
* Default memory allocator using system malloc/free
51+
*/
52+
private static class DefaultMemoryAllocator extends MemoryAllocator {
53+
private DefaultMemoryAllocator() {
54+
super(newDefaultMemoryAllocatorInstance());
55+
}
56+
57+
private static native long newDefaultMemoryAllocatorInstance();
58+
}
59+
60+
/**
61+
* JEMalloc-based memory allocator for reduced fragmentation
62+
*/
63+
private static class JEMallocMemoryAllocator extends MemoryAllocator {
64+
private JEMallocMemoryAllocator() {
65+
super(newJEMallocMemoryAllocatorInstance());
66+
}
67+
68+
private static native long newJEMallocMemoryAllocatorInstance();
69+
}
70+
71+
private static native void disposeInternalJni(final long handle);
72+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package org.rocksdb;
2+
3+
/**
4+
* SecondaryCache is a Java wrapper class for the C++ SecondaryCache.
5+
* It provides a persistent cache layer that can be used as a second-tier
6+
* cache below the block cache.
7+
* <p>
8+
* SecondaryCache allows for using non-volatile storage (like SSDs) or
9+
* compressed RAM as a cache tier, providing more capacity at lower cost
10+
* than keeping everything in the primary block cache.
11+
* <p>
12+
* As a descendent of {@link RocksObject}, this class is {@link AutoCloseable}
13+
* and will be automatically released if opened in the preamble of a try with
14+
* resources block.
15+
*/
16+
public abstract class SecondaryCache extends RocksObject {
17+
18+
/**
19+
* Constructor for SecondaryCache
20+
*
21+
* @param nativeHandle the native handle to the C++ SecondaryCache object
22+
*/
23+
protected SecondaryCache(final long nativeHandle) {
24+
super(nativeHandle);
25+
}
26+
27+
@Override
28+
protected final void disposeInternal(final long handle) {
29+
disposeInternalJni(handle);
30+
}
31+
32+
/**
33+
* Creates a new CompressedSecondaryCache with the specified capacity.
34+
* CompressedSecondaryCache stores cache items in compressed form,
35+
* allowing more data to fit in memory.
36+
*
37+
* @param capacity the capacity of the secondary cache in bytes
38+
* @return a new CompressedSecondaryCache instance
39+
*/
40+
public static SecondaryCache newCompressedSecondaryCache(final long capacity) {
41+
return new CompressedSecondaryCache(capacity);
42+
}
43+
44+
/**
45+
* Creates a new CompressedSecondaryCache with detailed options.
46+
*
47+
* @param capacity the capacity of the secondary cache in bytes
48+
* @param numShardBits the number of bits used for sharding (controls concurrency)
49+
* @param strictCapacityLimit if true, cache operations fail when capacity is exceeded
50+
* @param highPriPoolRatio ratio of cache reserved for high priority entries (0.0 to 1.0)
51+
* @return a new CompressedSecondaryCache instance
52+
*/
53+
public static SecondaryCache newCompressedSecondaryCache(
54+
final long capacity,
55+
final int numShardBits,
56+
final boolean strictCapacityLimit,
57+
final double highPriPoolRatio) {
58+
return new CompressedSecondaryCache(
59+
capacity, numShardBits, strictCapacityLimit, highPriPoolRatio);
60+
}
61+
62+
/**
63+
* CompressedSecondaryCache implementation that stores cached items
64+
* in compressed format to maximize capacity.
65+
*/
66+
private static class CompressedSecondaryCache extends SecondaryCache {
67+
68+
private CompressedSecondaryCache(final long capacity) {
69+
super(newCompressedSecondaryCacheInstance(capacity));
70+
}
71+
72+
private CompressedSecondaryCache(
73+
final long capacity,
74+
final int numShardBits,
75+
final boolean strictCapacityLimit,
76+
final double highPriPoolRatio) {
77+
super(newCompressedSecondaryCacheInstance(
78+
capacity, numShardBits, strictCapacityLimit, highPriPoolRatio));
79+
}
80+
81+
private static native long newCompressedSecondaryCacheInstance(long capacity);
82+
83+
private static native long newCompressedSecondaryCacheInstance(
84+
long capacity, int numShardBits, boolean strictCapacityLimit, double highPriPoolRatio);
85+
}
86+
87+
private static native void disposeInternalJni(final long handle);
88+
}

0 commit comments

Comments
 (0)