Skip to content

Commit 64eebd7

Browse files
committed
Add FileTemperatureAge and AdvancedCacheOptions
1 parent 65e5866 commit 64eebd7

2 files changed

Lines changed: 276 additions & 0 deletions

File tree

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package org.rocksdb;
2+
3+
/**
4+
* EXPERIMENTAL
5+
* Age (in seconds) threshold for different file temperatures.
6+
* When all the data in a file is older than the specified age,
7+
* RocksDB will compact the file to the specified temperature.
8+
*/
9+
public class FileTemperatureAge {
10+
private Temperature temperature;
11+
private long age;
12+
13+
/**
14+
* Default constructor with unknown temperature and zero age
15+
*/
16+
public FileTemperatureAge() {
17+
this.temperature = Temperature.UNKNOWN;
18+
this.age = 0;
19+
}
20+
21+
/**
22+
* Constructor with specified temperature and age
23+
*
24+
* @param temperature the target temperature for old files
25+
* @param age the age threshold in seconds
26+
*/
27+
public FileTemperatureAge(final Temperature temperature, final long age) {
28+
this.temperature = temperature;
29+
this.age = age;
30+
}
31+
32+
/**
33+
* Get the temperature
34+
*
35+
* @return the temperature
36+
*/
37+
public Temperature getTemperature() {
38+
return temperature;
39+
}
40+
41+
/**
42+
* Set the temperature
43+
*
44+
* @param temperature the temperature to set
45+
* @return this FileTemperatureAge instance
46+
*/
47+
public FileTemperatureAge setTemperature(final Temperature temperature) {
48+
this.temperature = temperature;
49+
return this;
50+
}
51+
52+
/**
53+
* Get the age threshold in seconds
54+
*
55+
* @return the age in seconds
56+
*/
57+
public long getAge() {
58+
return age;
59+
}
60+
61+
/**
62+
* Set the age threshold in seconds
63+
*
64+
* @param age the age in seconds
65+
* @return this FileTemperatureAge instance
66+
*/
67+
public FileTemperatureAge setAge(final long age) {
68+
this.age = age;
69+
return this;
70+
}
71+
72+
@Override
73+
public boolean equals(Object o) {
74+
if (this == o) return true;
75+
if (o == null || getClass() != o.getClass()) return false;
76+
FileTemperatureAge that = (FileTemperatureAge) o;
77+
return age == that.age && temperature == that.temperature;
78+
}
79+
80+
@Override
81+
public int hashCode() {
82+
return 31 * temperature.hashCode() + Long.hashCode(age);
83+
}
84+
85+
@Override
86+
public String toString() {
87+
return "FileTemperatureAge{temperature=" + temperature + ", age=" + age + "s}";
88+
}
89+
}

0 commit comments

Comments
 (0)