|
| 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