|
| 1 | +package org.rocksdb; |
| 2 | + |
| 3 | +/** |
| 4 | + * Advanced cache configuration options for fine-tuning cache behavior. |
| 5 | + * These options control memory allocation, metadata charging, and |
| 6 | + * secondary cache integration. |
| 7 | + */ |
| 8 | +public class AdvancedCacheOptions { |
| 9 | + private long capacity; |
| 10 | + private int numShardBits; |
| 11 | + private boolean strictCapacityLimit; |
| 12 | + private CacheMetadataChargePolicy metadataChargePolicy; |
| 13 | + private SecondaryCache secondaryCache; |
| 14 | + private MemoryAllocator memoryAllocator; |
| 15 | + |
| 16 | + /** |
| 17 | + * Default constructor with sensible defaults |
| 18 | + */ |
| 19 | + public AdvancedCacheOptions() { |
| 20 | + this.capacity = 8 * 1024 * 1024; // 8MB default |
| 21 | + this.numShardBits = -1; // Auto-determined |
| 22 | + this.strictCapacityLimit = false; |
| 23 | + this.metadataChargePolicy = CacheMetadataChargePolicy.FULL_CHARGE_CACHE_METADATA; |
| 24 | + this.secondaryCache = null; |
| 25 | + this.memoryAllocator = null; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Get the cache capacity in bytes |
| 30 | + * |
| 31 | + * @return the capacity |
| 32 | + */ |
| 33 | + public long getCapacity() { |
| 34 | + return capacity; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Set the cache capacity in bytes |
| 39 | + * |
| 40 | + * @param capacity the capacity to set |
| 41 | + * @return this options instance |
| 42 | + */ |
| 43 | + public AdvancedCacheOptions setCapacity(final long capacity) { |
| 44 | + this.capacity = capacity; |
| 45 | + return this; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Get the number of shard bits. |
| 50 | + * Cache is sharded into 2^numShardBits shards by hash of key. |
| 51 | + * |
| 52 | + * @return the number of shard bits |
| 53 | + */ |
| 54 | + public int getNumShardBits() { |
| 55 | + return numShardBits; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Set the number of shard bits. |
| 60 | + * If < 0, a good default is chosen based on capacity. |
| 61 | + * |
| 62 | + * @param numShardBits the number of shard bits |
| 63 | + * @return this options instance |
| 64 | + */ |
| 65 | + public AdvancedCacheOptions setNumShardBits(final int numShardBits) { |
| 66 | + this.numShardBits = numShardBits; |
| 67 | + return this; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Check if strict capacity limit is enabled |
| 72 | + * |
| 73 | + * @return true if strict capacity limit is enabled |
| 74 | + */ |
| 75 | + public boolean isStrictCapacityLimit() { |
| 76 | + return strictCapacityLimit; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Set strict capacity limit. |
| 81 | + * If true, insert operations fail when capacity is exceeded. |
| 82 | + * If false, insert never fails (unreferenced entries are evicted). |
| 83 | + * |
| 84 | + * @param strictCapacityLimit whether to enforce strict capacity limit |
| 85 | + * @return this options instance |
| 86 | + */ |
| 87 | + public AdvancedCacheOptions setStrictCapacityLimit(final boolean strictCapacityLimit) { |
| 88 | + this.strictCapacityLimit = strictCapacityLimit; |
| 89 | + return this; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Get the metadata charge policy |
| 94 | + * |
| 95 | + * @return the metadata charge policy |
| 96 | + */ |
| 97 | + public CacheMetadataChargePolicy getMetadataChargePolicy() { |
| 98 | + return metadataChargePolicy; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Set the metadata charge policy. |
| 103 | + * Controls whether cache metadata overhead counts against capacity. |
| 104 | + * |
| 105 | + * @param metadataChargePolicy the policy to set |
| 106 | + * @return this options instance |
| 107 | + */ |
| 108 | + public AdvancedCacheOptions setMetadataChargePolicy( |
| 109 | + final CacheMetadataChargePolicy metadataChargePolicy) { |
| 110 | + this.metadataChargePolicy = metadataChargePolicy; |
| 111 | + return this; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Get the secondary cache instance |
| 116 | + * |
| 117 | + * @return the secondary cache, or null if not set |
| 118 | + */ |
| 119 | + public SecondaryCache getSecondaryCache() { |
| 120 | + return secondaryCache; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Set the secondary cache for non-volatile tier caching |
| 125 | + * |
| 126 | + * @param secondaryCache the secondary cache instance |
| 127 | + * @return this options instance |
| 128 | + */ |
| 129 | + public AdvancedCacheOptions setSecondaryCache(final SecondaryCache secondaryCache) { |
| 130 | + this.secondaryCache = secondaryCache; |
| 131 | + return this; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Get the memory allocator |
| 136 | + * |
| 137 | + * @return the memory allocator, or null for system allocator |
| 138 | + */ |
| 139 | + public MemoryAllocator getMemoryAllocator() { |
| 140 | + return memoryAllocator; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Set a custom memory allocator for cache blocks |
| 145 | + * |
| 146 | + * @param memoryAllocator the memory allocator instance |
| 147 | + * @return this options instance |
| 148 | + */ |
| 149 | + public AdvancedCacheOptions setMemoryAllocator(final MemoryAllocator memoryAllocator) { |
| 150 | + this.memoryAllocator = memoryAllocator; |
| 151 | + return this; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Metadata charge policy enum |
| 156 | + */ |
| 157 | + public enum CacheMetadataChargePolicy { |
| 158 | + /** |
| 159 | + * Only the charge of each entry counts against capacity |
| 160 | + */ |
| 161 | + DONT_CHARGE_CACHE_METADATA((byte) 0), |
| 162 | + |
| 163 | + /** |
| 164 | + * Cache metadata overhead also counts against capacity |
| 165 | + */ |
| 166 | + FULL_CHARGE_CACHE_METADATA((byte) 1); |
| 167 | + |
| 168 | + private final byte value; |
| 169 | + |
| 170 | + CacheMetadataChargePolicy(final byte value) { |
| 171 | + this.value = value; |
| 172 | + } |
| 173 | + |
| 174 | + public byte getValue() { |
| 175 | + return value; |
| 176 | + } |
| 177 | + |
| 178 | + public static CacheMetadataChargePolicy fromValue(final byte value) { |
| 179 | + for (final CacheMetadataChargePolicy policy : CacheMetadataChargePolicy.values()) { |
| 180 | + if (policy.getValue() == value) { |
| 181 | + return policy; |
| 182 | + } |
| 183 | + } |
| 184 | + throw new IllegalArgumentException("Unknown CacheMetadataChargePolicy value: " + value); |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments