Skip to content

Commit 9d34c61

Browse files
committed
tests
1 parent ba6602a commit 9d34c61

1 file changed

Lines changed: 130 additions & 0 deletions

File tree

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2+
// This source code is licensed under both the GPLv2 (found in the
3+
// COPYING file in the root directory) and Apache 2.0 License
4+
// (found in the LICENSE.Apache file in the root directory).
5+
6+
package org.rocksdb;
7+
8+
import org.junit.ClassRule;
9+
import org.junit.Rule;
10+
import org.junit.Test;
11+
import org.junit.rules.TemporaryFolder;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
15+
public class SecondaryCacheTest {
16+
17+
@ClassRule
18+
public static final RocksNativeLibraryResource ROCKS_NATIVE_LIBRARY_RESOURCE =
19+
new RocksNativeLibraryResource();
20+
21+
@Rule
22+
public TemporaryFolder dbFolder = new TemporaryFolder();
23+
24+
@Test
25+
public void newCompressedSecondaryCache() {
26+
try (final SecondaryCache secondaryCache =
27+
SecondaryCache.newCompressedSecondaryCache(1024 * 1024)) {
28+
assertThat(secondaryCache).isNotNull();
29+
}
30+
}
31+
32+
@Test
33+
public void newCompressedSecondaryCacheWithOptions() {
34+
try (final SecondaryCache secondaryCache =
35+
SecondaryCache.newCompressedSecondaryCache(
36+
1024 * 1024, // 1MB capacity
37+
0, // num_shard_bits (single shard for testing)
38+
false, // strict_capacity_limit
39+
0.5)) { // high_pri_pool_ratio
40+
assertThat(secondaryCache).isNotNull();
41+
}
42+
}
43+
44+
@Test
45+
public void testLRUCacheWithSecondaryCache() throws RocksDBException {
46+
// Create a secondary cache
47+
try (final SecondaryCache secondaryCache =
48+
SecondaryCache.newCompressedSecondaryCache(8 * 1024 * 1024);
49+
// Create an LRU cache with the secondary cache
50+
final Cache primaryCache =
51+
new LRUCache(
52+
1024 * 1024, // 1MB primary cache
53+
0, // num_shard_bits
54+
false, // strict_capacity_limit
55+
0.5, // high_pri_pool_ratio
56+
0.0, // low_pri_pool_ratio
57+
secondaryCache)) {
58+
assertThat(primaryCache).isNotNull();
59+
assertThat(primaryCache.getUsage()).isGreaterThanOrEqualTo(0);
60+
assertThat(primaryCache.getPinnedUsage()).isGreaterThanOrEqualTo(0);
61+
}
62+
}
63+
64+
@Test
65+
public void testWithBlockBasedTableConfig() throws RocksDBException {
66+
try (final SecondaryCache secondaryCache =
67+
SecondaryCache.newCompressedSecondaryCache(16 * 1024 * 1024);
68+
final Cache primaryCache =
69+
new LRUCache(4 * 1024 * 1024, -1, false, 0.5, 0.0, secondaryCache);
70+
final Options options = new Options().setCreateIfMissing(true)) {
71+
72+
final BlockBasedTableConfig tableConfig = new BlockBasedTableConfig();
73+
tableConfig.setBlockCache(primaryCache);
74+
tableConfig.setBlockSize(4 * 1024);
75+
options.setTableFormatConfig(tableConfig);
76+
77+
try (final RocksDB db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath())) {
78+
assertThat(db).isNotNull();
79+
80+
// Write some data
81+
for (int i = 0; i < 100; i++) {
82+
db.put(("key" + i).getBytes(), ("value" + i).getBytes());
83+
}
84+
85+
// Read some data
86+
for (int i = 0; i < 100; i++) {
87+
final byte[] value = db.get(("key" + i).getBytes());
88+
assertThat(value).isEqualTo(("value" + i).getBytes());
89+
}
90+
91+
// Check that the cache is being used
92+
assertThat(primaryCache.getUsage()).isGreaterThan(0);
93+
}
94+
}
95+
}
96+
97+
@Test
98+
public void testSecondaryCacheWithLargeDataset() throws RocksDBException {
99+
// Create a small primary cache with larger secondary cache
100+
// This simulates a real-world tiered caching scenario
101+
try (final SecondaryCache secondaryCache =
102+
SecondaryCache.newCompressedSecondaryCache(32 * 1024 * 1024);
103+
final Cache primaryCache =
104+
new LRUCache(2 * 1024 * 1024, -1, false, 0.5, 0.0, secondaryCache);
105+
final Options options = new Options().setCreateIfMissing(true)) {
106+
107+
final BlockBasedTableConfig tableConfig = new BlockBasedTableConfig();
108+
tableConfig.setBlockCache(primaryCache);
109+
tableConfig.setBlockSize(4 * 1024);
110+
options.setTableFormatConfig(tableConfig);
111+
112+
try (final RocksDB db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath())) {
113+
// Write enough data to overflow the primary cache
114+
final byte[] largeValue = new byte[1024];
115+
for (int i = 0; i < 1000; i++) {
116+
db.put(("largekey" + i).getBytes(), largeValue);
117+
}
118+
119+
db.flush(new FlushOptions());
120+
121+
// Read back the data - some should come from secondary cache
122+
for (int i = 0; i < 1000; i++) {
123+
final byte[] value = db.get(("largekey" + i).getBytes());
124+
assertThat(value).isNotNull();
125+
assertThat(value.length).isEqualTo(1024);
126+
}
127+
}
128+
}
129+
}
130+
}

0 commit comments

Comments
 (0)