]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
11fdf7f2 TL |
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). | |
7c673cae FG |
5 | |
6 | package org.rocksdb; | |
7 | ||
20effc67 TL |
8 | import static org.assertj.core.api.Assertions.assertThat; |
9 | import static org.junit.Assert.fail; | |
10 | ||
11 | import java.nio.charset.StandardCharsets; | |
7c673cae | 12 | import org.junit.ClassRule; |
494da23a | 13 | import org.junit.Ignore; |
11fdf7f2 | 14 | import org.junit.Rule; |
7c673cae | 15 | import org.junit.Test; |
11fdf7f2 TL |
16 | import org.junit.rules.TemporaryFolder; |
17 | ||
7c673cae FG |
18 | public class BlockBasedTableConfigTest { |
19 | ||
20 | @ClassRule | |
f67539c2 TL |
21 | public static final RocksNativeLibraryResource ROCKS_NATIVE_LIBRARY_RESOURCE = |
22 | new RocksNativeLibraryResource(); | |
7c673cae | 23 | |
11fdf7f2 TL |
24 | @Rule public TemporaryFolder dbFolder = new TemporaryFolder(); |
25 | ||
494da23a TL |
26 | @Test |
27 | public void cacheIndexAndFilterBlocks() { | |
28 | final BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); | |
29 | blockBasedTableConfig.setCacheIndexAndFilterBlocks(true); | |
30 | assertThat(blockBasedTableConfig.cacheIndexAndFilterBlocks()). | |
31 | isTrue(); | |
32 | ||
33 | } | |
34 | ||
35 | @Test | |
36 | public void cacheIndexAndFilterBlocksWithHighPriority() { | |
37 | final BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); | |
494da23a TL |
38 | assertThat(blockBasedTableConfig.cacheIndexAndFilterBlocksWithHighPriority()). |
39 | isTrue(); | |
20effc67 TL |
40 | blockBasedTableConfig.setCacheIndexAndFilterBlocksWithHighPriority(false); |
41 | assertThat(blockBasedTableConfig.cacheIndexAndFilterBlocksWithHighPriority()).isFalse(); | |
494da23a TL |
42 | } |
43 | ||
44 | @Test | |
45 | public void pinL0FilterAndIndexBlocksInCache() { | |
46 | final BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); | |
47 | blockBasedTableConfig.setPinL0FilterAndIndexBlocksInCache(true); | |
48 | assertThat(blockBasedTableConfig.pinL0FilterAndIndexBlocksInCache()). | |
49 | isTrue(); | |
50 | } | |
51 | ||
52 | @Test | |
53 | public void pinTopLevelIndexAndFilter() { | |
54 | final BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); | |
55 | blockBasedTableConfig.setPinTopLevelIndexAndFilter(false); | |
56 | assertThat(blockBasedTableConfig.pinTopLevelIndexAndFilter()). | |
57 | isFalse(); | |
58 | } | |
59 | ||
60 | @Test | |
61 | public void indexType() { | |
62 | final BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); | |
20effc67 | 63 | assertThat(IndexType.values().length).isEqualTo(4); |
494da23a TL |
64 | blockBasedTableConfig.setIndexType(IndexType.kHashSearch); |
65 | assertThat(blockBasedTableConfig.indexType().equals( | |
66 | IndexType.kHashSearch)); | |
67 | assertThat(IndexType.valueOf("kBinarySearch")).isNotNull(); | |
68 | blockBasedTableConfig.setIndexType(IndexType.valueOf("kBinarySearch")); | |
69 | assertThat(blockBasedTableConfig.indexType().equals( | |
70 | IndexType.kBinarySearch)); | |
71 | } | |
72 | ||
73 | @Test | |
74 | public void dataBlockIndexType() { | |
75 | final BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); | |
76 | blockBasedTableConfig.setDataBlockIndexType(DataBlockIndexType.kDataBlockBinaryAndHash); | |
77 | assertThat(blockBasedTableConfig.dataBlockIndexType().equals( | |
78 | DataBlockIndexType.kDataBlockBinaryAndHash)); | |
79 | blockBasedTableConfig.setDataBlockIndexType(DataBlockIndexType.kDataBlockBinarySearch); | |
80 | assertThat(blockBasedTableConfig.dataBlockIndexType().equals( | |
81 | DataBlockIndexType.kDataBlockBinarySearch)); | |
82 | } | |
83 | ||
84 | @Test | |
85 | public void checksumType() { | |
86 | final BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); | |
20effc67 | 87 | assertThat(ChecksumType.values().length).isEqualTo(4); |
494da23a TL |
88 | assertThat(ChecksumType.valueOf("kxxHash")). |
89 | isEqualTo(ChecksumType.kxxHash); | |
90 | blockBasedTableConfig.setChecksumType(ChecksumType.kNoChecksum); | |
91 | blockBasedTableConfig.setChecksumType(ChecksumType.kxxHash); | |
92 | assertThat(blockBasedTableConfig.checksumType().equals( | |
93 | ChecksumType.kxxHash)); | |
94 | } | |
95 | ||
7c673cae FG |
96 | @Test |
97 | public void noBlockCache() { | |
494da23a | 98 | final BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); |
7c673cae FG |
99 | blockBasedTableConfig.setNoBlockCache(true); |
100 | assertThat(blockBasedTableConfig.noBlockCache()).isTrue(); | |
101 | } | |
102 | ||
103 | @Test | |
494da23a TL |
104 | public void blockCache() { |
105 | try ( | |
106 | final Cache cache = new LRUCache(17 * 1024 * 1024); | |
107 | final Options options = new Options().setTableFormatConfig( | |
108 | new BlockBasedTableConfig().setBlockCache(cache))) { | |
109 | assertThat(options.tableFactoryName()).isEqualTo("BlockBasedTable"); | |
110 | } | |
7c673cae FG |
111 | } |
112 | ||
11fdf7f2 | 113 | @Test |
494da23a | 114 | public void blockCacheIntegration() throws RocksDBException { |
11fdf7f2 TL |
115 | try (final Cache cache = new LRUCache(8 * 1024 * 1024); |
116 | final Statistics statistics = new Statistics()) { | |
117 | for (int shard = 0; shard < 8; shard++) { | |
118 | try (final Options options = | |
119 | new Options() | |
120 | .setCreateIfMissing(true) | |
121 | .setStatistics(statistics) | |
122 | .setTableFormatConfig(new BlockBasedTableConfig().setBlockCache(cache)); | |
123 | final RocksDB db = | |
124 | RocksDB.open(options, dbFolder.getRoot().getAbsolutePath() + "/" + shard)) { | |
125 | final byte[] key = "some-key".getBytes(StandardCharsets.UTF_8); | |
126 | final byte[] value = "some-value".getBytes(StandardCharsets.UTF_8); | |
127 | ||
128 | db.put(key, value); | |
129 | db.flush(new FlushOptions()); | |
130 | db.get(key); | |
131 | ||
132 | assertThat(statistics.getTickerCount(TickerType.BLOCK_CACHE_ADD)).isEqualTo(shard + 1); | |
133 | } | |
134 | } | |
135 | } | |
136 | } | |
137 | ||
7c673cae | 138 | @Test |
494da23a TL |
139 | public void persistentCache() throws RocksDBException { |
140 | try (final DBOptions dbOptions = new DBOptions(). | |
141 | setInfoLogLevel(InfoLogLevel.INFO_LEVEL). | |
142 | setCreateIfMissing(true); | |
143 | final Logger logger = new Logger(dbOptions) { | |
144 | @Override | |
145 | protected void log(final InfoLogLevel infoLogLevel, final String logMsg) { | |
146 | System.out.println(infoLogLevel.name() + ": " + logMsg); | |
147 | } | |
148 | }) { | |
149 | try (final PersistentCache persistentCache = | |
150 | new PersistentCache(Env.getDefault(), dbFolder.getRoot().getPath(), 1024 * 1024 * 100, logger, false); | |
151 | final Options options = new Options().setTableFormatConfig( | |
152 | new BlockBasedTableConfig().setPersistentCache(persistentCache))) { | |
153 | assertThat(options.tableFactoryName()).isEqualTo("BlockBasedTable"); | |
154 | } | |
155 | } | |
7c673cae FG |
156 | } |
157 | ||
158 | @Test | |
494da23a TL |
159 | public void blockCacheCompressed() { |
160 | try (final Cache cache = new LRUCache(17 * 1024 * 1024); | |
161 | final Options options = new Options().setTableFormatConfig( | |
162 | new BlockBasedTableConfig().setBlockCacheCompressed(cache))) { | |
163 | assertThat(options.tableFactoryName()).isEqualTo("BlockBasedTable"); | |
164 | } | |
7c673cae FG |
165 | } |
166 | ||
494da23a | 167 | @Ignore("See issue: https://github.com/facebook/rocksdb/issues/4822") |
7c673cae | 168 | @Test |
494da23a TL |
169 | public void blockCacheCompressedIntegration() throws RocksDBException { |
170 | final byte[] key1 = "some-key1".getBytes(StandardCharsets.UTF_8); | |
171 | final byte[] key2 = "some-key1".getBytes(StandardCharsets.UTF_8); | |
172 | final byte[] key3 = "some-key1".getBytes(StandardCharsets.UTF_8); | |
173 | final byte[] key4 = "some-key1".getBytes(StandardCharsets.UTF_8); | |
174 | final byte[] value = "some-value".getBytes(StandardCharsets.UTF_8); | |
7c673cae | 175 | |
494da23a TL |
176 | try (final Cache compressedCache = new LRUCache(8 * 1024 * 1024); |
177 | final Statistics statistics = new Statistics()) { | |
178 | ||
179 | final BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig() | |
180 | .setNoBlockCache(true) | |
181 | .setBlockCache(null) | |
182 | .setBlockCacheCompressed(compressedCache) | |
183 | .setFormatVersion(4); | |
7c673cae | 184 | |
494da23a TL |
185 | try (final Options options = new Options() |
186 | .setCreateIfMissing(true) | |
187 | .setStatistics(statistics) | |
188 | .setTableFormatConfig(blockBasedTableConfig)) { | |
189 | ||
190 | for (int shard = 0; shard < 8; shard++) { | |
191 | try (final FlushOptions flushOptions = new FlushOptions(); | |
192 | final WriteOptions writeOptions = new WriteOptions(); | |
193 | final ReadOptions readOptions = new ReadOptions(); | |
194 | final RocksDB db = | |
195 | RocksDB.open(options, dbFolder.getRoot().getAbsolutePath() + "/" + shard)) { | |
196 | ||
197 | db.put(writeOptions, key1, value); | |
198 | db.put(writeOptions, key2, value); | |
199 | db.put(writeOptions, key3, value); | |
200 | db.put(writeOptions, key4, value); | |
201 | db.flush(flushOptions); | |
202 | ||
203 | db.get(readOptions, key1); | |
204 | db.get(readOptions, key2); | |
205 | db.get(readOptions, key3); | |
206 | db.get(readOptions, key4); | |
207 | ||
208 | assertThat(statistics.getTickerCount(TickerType.BLOCK_CACHE_COMPRESSED_ADD)).isEqualTo(shard + 1); | |
209 | } | |
210 | } | |
211 | } | |
212 | } | |
7c673cae FG |
213 | } |
214 | ||
215 | @Test | |
494da23a TL |
216 | public void blockSize() { |
217 | final BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); | |
218 | blockBasedTableConfig.setBlockSize(10); | |
219 | assertThat(blockBasedTableConfig.blockSize()).isEqualTo(10); | |
7c673cae FG |
220 | } |
221 | ||
222 | @Test | |
494da23a TL |
223 | public void blockSizeDeviation() { |
224 | final BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); | |
225 | blockBasedTableConfig.setBlockSizeDeviation(12); | |
226 | assertThat(blockBasedTableConfig.blockSizeDeviation()). | |
227 | isEqualTo(12); | |
7c673cae FG |
228 | } |
229 | ||
230 | @Test | |
494da23a TL |
231 | public void blockRestartInterval() { |
232 | final BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); | |
233 | blockBasedTableConfig.setBlockRestartInterval(15); | |
234 | assertThat(blockBasedTableConfig.blockRestartInterval()). | |
235 | isEqualTo(15); | |
7c673cae FG |
236 | } |
237 | ||
238 | @Test | |
494da23a TL |
239 | public void indexBlockRestartInterval() { |
240 | final BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); | |
241 | blockBasedTableConfig.setIndexBlockRestartInterval(15); | |
242 | assertThat(blockBasedTableConfig.indexBlockRestartInterval()). | |
243 | isEqualTo(15); | |
7c673cae FG |
244 | } |
245 | ||
246 | @Test | |
494da23a TL |
247 | public void metadataBlockSize() { |
248 | final BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); | |
249 | blockBasedTableConfig.setMetadataBlockSize(1024); | |
250 | assertThat(blockBasedTableConfig.metadataBlockSize()). | |
251 | isEqualTo(1024); | |
7c673cae FG |
252 | } |
253 | ||
254 | @Test | |
494da23a TL |
255 | public void partitionFilters() { |
256 | final BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); | |
257 | blockBasedTableConfig.setPartitionFilters(true); | |
258 | assertThat(blockBasedTableConfig.partitionFilters()). | |
259 | isTrue(); | |
7c673cae FG |
260 | } |
261 | ||
20effc67 TL |
262 | @Test |
263 | public void optimizeFiltersForMemory() { | |
264 | final BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); | |
265 | blockBasedTableConfig.setOptimizeFiltersForMemory(true); | |
266 | assertThat(blockBasedTableConfig.optimizeFiltersForMemory()).isTrue(); | |
267 | } | |
268 | ||
7c673cae | 269 | @Test |
494da23a TL |
270 | public void useDeltaEncoding() { |
271 | final BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); | |
272 | blockBasedTableConfig.setUseDeltaEncoding(false); | |
273 | assertThat(blockBasedTableConfig.useDeltaEncoding()). | |
274 | isFalse(); | |
7c673cae FG |
275 | } |
276 | ||
7c673cae | 277 | @Test |
494da23a | 278 | public void blockBasedTableWithFilterPolicy() { |
7c673cae FG |
279 | try(final Options options = new Options() |
280 | .setTableFormatConfig(new BlockBasedTableConfig() | |
494da23a | 281 | .setFilterPolicy(new BloomFilter(10)))) { |
7c673cae FG |
282 | assertThat(options.tableFactoryName()). |
283 | isEqualTo("BlockBasedTable"); | |
284 | } | |
285 | } | |
286 | ||
287 | @Test | |
494da23a | 288 | public void blockBasedTableWithoutFilterPolicy() { |
7c673cae | 289 | try(final Options options = new Options().setTableFormatConfig( |
494da23a | 290 | new BlockBasedTableConfig().setFilterPolicy(null))) { |
7c673cae FG |
291 | assertThat(options.tableFactoryName()). |
292 | isEqualTo("BlockBasedTable"); | |
293 | } | |
294 | } | |
295 | ||
11fdf7f2 | 296 | @Test |
494da23a TL |
297 | public void wholeKeyFiltering() { |
298 | final BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); | |
299 | blockBasedTableConfig.setWholeKeyFiltering(false); | |
300 | assertThat(blockBasedTableConfig.wholeKeyFiltering()). | |
301 | isFalse(); | |
302 | } | |
303 | ||
304 | @Test | |
305 | public void verifyCompression() { | |
306 | final BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); | |
20effc67 | 307 | assertThat(blockBasedTableConfig.verifyCompression()).isFalse(); |
494da23a TL |
308 | blockBasedTableConfig.setVerifyCompression(true); |
309 | assertThat(blockBasedTableConfig.verifyCompression()). | |
310 | isTrue(); | |
311 | } | |
312 | ||
313 | @Test | |
314 | public void readAmpBytesPerBit() { | |
315 | final BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); | |
316 | blockBasedTableConfig.setReadAmpBytesPerBit(2); | |
317 | assertThat(blockBasedTableConfig.readAmpBytesPerBit()). | |
318 | isEqualTo(2); | |
11fdf7f2 TL |
319 | } |
320 | ||
7c673cae | 321 | @Test |
494da23a TL |
322 | public void formatVersion() { |
323 | final BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); | |
20effc67 | 324 | for (int version = 0; version <= 5; version++) { |
494da23a TL |
325 | blockBasedTableConfig.setFormatVersion(version); |
326 | assertThat(blockBasedTableConfig.formatVersion()).isEqualTo(version); | |
7c673cae FG |
327 | } |
328 | } | |
329 | ||
330 | @Test(expected = AssertionError.class) | |
494da23a TL |
331 | public void formatVersionFailNegative() { |
332 | final BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); | |
333 | blockBasedTableConfig.setFormatVersion(-1); | |
7c673cae FG |
334 | } |
335 | ||
20effc67 TL |
336 | @Test(expected = RocksDBException.class) |
337 | public void invalidFormatVersion() throws RocksDBException { | |
338 | final BlockBasedTableConfig blockBasedTableConfig = | |
339 | new BlockBasedTableConfig().setFormatVersion(99999); | |
340 | ||
341 | try (final Options options = new Options().setTableFormatConfig(blockBasedTableConfig); | |
342 | final RocksDB db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath())) { | |
343 | fail("Opening the database with an invalid format_version should have raised an exception"); | |
344 | } | |
494da23a TL |
345 | } |
346 | ||
347 | @Test | |
348 | public void enableIndexCompression() { | |
349 | final BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); | |
350 | blockBasedTableConfig.setEnableIndexCompression(false); | |
351 | assertThat(blockBasedTableConfig.enableIndexCompression()). | |
352 | isFalse(); | |
353 | } | |
354 | ||
355 | @Test | |
356 | public void blockAlign() { | |
357 | final BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); | |
358 | blockBasedTableConfig.setBlockAlign(true); | |
359 | assertThat(blockBasedTableConfig.blockAlign()). | |
360 | isTrue(); | |
361 | } | |
362 | ||
20effc67 TL |
363 | @Test |
364 | public void indexShortening() { | |
365 | final BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); | |
366 | blockBasedTableConfig.setIndexShortening(IndexShorteningMode.kShortenSeparatorsAndSuccessor); | |
367 | assertThat(blockBasedTableConfig.indexShortening()) | |
368 | .isEqualTo(IndexShorteningMode.kShortenSeparatorsAndSuccessor); | |
369 | } | |
370 | ||
494da23a TL |
371 | @Deprecated |
372 | @Test | |
373 | public void hashIndexAllowCollision() { | |
374 | final BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); | |
375 | blockBasedTableConfig.setHashIndexAllowCollision(false); | |
376 | assertThat(blockBasedTableConfig.hashIndexAllowCollision()). | |
377 | isTrue(); // NOTE: setHashIndexAllowCollision should do nothing! | |
378 | } | |
379 | ||
380 | @Deprecated | |
381 | @Test | |
382 | public void blockCacheSize() { | |
383 | final BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); | |
384 | blockBasedTableConfig.setBlockCacheSize(8 * 1024); | |
385 | assertThat(blockBasedTableConfig.blockCacheSize()). | |
386 | isEqualTo(8 * 1024); | |
387 | } | |
388 | ||
389 | @Deprecated | |
390 | @Test | |
391 | public void blockCacheNumShardBits() { | |
392 | final BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); | |
393 | blockBasedTableConfig.setCacheNumShardBits(5); | |
394 | assertThat(blockBasedTableConfig.cacheNumShardBits()). | |
395 | isEqualTo(5); | |
396 | } | |
397 | ||
398 | @Deprecated | |
399 | @Test | |
400 | public void blockCacheCompressedSize() { | |
401 | final BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); | |
402 | blockBasedTableConfig.setBlockCacheCompressedSize(40); | |
403 | assertThat(blockBasedTableConfig.blockCacheCompressedSize()). | |
404 | isEqualTo(40); | |
405 | } | |
406 | ||
407 | @Deprecated | |
408 | @Test | |
409 | public void blockCacheCompressedNumShardBits() { | |
410 | final BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig(); | |
411 | blockBasedTableConfig.setBlockCacheCompressedNumShardBits(4); | |
412 | assertThat(blockBasedTableConfig.blockCacheCompressedNumShardBits()). | |
413 | isEqualTo(4); | |
7c673cae FG |
414 | } |
415 | } |