|
| 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 static org.assertj.core.api.Assertions.assertThat; |
| 9 | + |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | +import org.junit.ClassRule; |
| 13 | +import org.junit.Rule; |
| 14 | +import org.junit.Test; |
| 15 | +import org.junit.rules.TemporaryFolder; |
| 16 | + |
| 17 | +public class FlushWALOptionsTest { |
| 18 | + |
| 19 | + @ClassRule |
| 20 | + public static final RocksNativeLibraryResource ROCKS_NATIVE_LIBRARY_RESOURCE = |
| 21 | + new RocksNativeLibraryResource(); |
| 22 | + |
| 23 | + @Rule public TemporaryFolder dbFolder = new TemporaryFolder(); |
| 24 | + |
| 25 | + @Test |
| 26 | + public void newFlushWALOptions() { |
| 27 | + try (final FlushWALOptions flushWALOptions = new FlushWALOptions()) { |
| 28 | + assertThat(flushWALOptions).isNotNull(); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void sync() { |
| 34 | + try (final FlushWALOptions flushWALOptions = new FlushWALOptions()) { |
| 35 | + // Default value should be false |
| 36 | + assertThat(flushWALOptions.sync()).isFalse(); |
| 37 | + |
| 38 | + // Test setting to true |
| 39 | + flushWALOptions.setSync(true); |
| 40 | + assertThat(flushWALOptions.sync()).isTrue(); |
| 41 | + |
| 42 | + // Test setting to false |
| 43 | + flushWALOptions.setSync(false); |
| 44 | + assertThat(flushWALOptions.sync()).isFalse(); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void rateLimiterPriority() { |
| 50 | + try (final FlushWALOptions flushWALOptions = new FlushWALOptions()) { |
| 51 | + // Default value should be IO_TOTAL |
| 52 | + assertThat(flushWALOptions.rateLimiterPriority()).isEqualTo(IOPriority.IO_TOTAL); |
| 53 | + |
| 54 | + // Test setting to IO_HIGH |
| 55 | + flushWALOptions.setRateLimiterPriority(IOPriority.IO_HIGH); |
| 56 | + assertThat(flushWALOptions.rateLimiterPriority()).isEqualTo(IOPriority.IO_HIGH); |
| 57 | + |
| 58 | + // Test setting to IO_LOW |
| 59 | + flushWALOptions.setRateLimiterPriority(IOPriority.IO_LOW); |
| 60 | + assertThat(flushWALOptions.rateLimiterPriority()).isEqualTo(IOPriority.IO_LOW); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void flushWalWithOptions() throws RocksDBException { |
| 66 | + try (final Options options = new Options() |
| 67 | + .setCreateIfMissing(true) |
| 68 | + .setManualWalFlush(true); |
| 69 | + final RocksDB db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath()); |
| 70 | + final FlushWALOptions flushWALOptions = new FlushWALOptions()) { |
| 71 | + |
| 72 | + // Put some data |
| 73 | + db.put("key1".getBytes(), "value1".getBytes()); |
| 74 | + |
| 75 | + // Flush WAL without sync |
| 76 | + flushWALOptions.setSync(false); |
| 77 | + db.flushWal(flushWALOptions); |
| 78 | + |
| 79 | + // Put more data |
| 80 | + db.put("key2".getBytes(), "value2".getBytes()); |
| 81 | + |
| 82 | + // Flush WAL with sync |
| 83 | + flushWALOptions.setSync(true); |
| 84 | + db.flushWal(flushWALOptions); |
| 85 | + |
| 86 | + // Verify data is accessible |
| 87 | + assertThat(new String(db.get("key1".getBytes()))).isEqualTo("value1"); |
| 88 | + assertThat(new String(db.get("key2".getBytes()))).isEqualTo("value2"); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void flushWalWithRateLimiter() throws RocksDBException { |
| 94 | + try (final RateLimiter rateLimiter = new RateLimiter(10000000); // 10 MB/s |
| 95 | + final Options options = new Options() |
| 96 | + .setCreateIfMissing(true) |
| 97 | + .setManualWalFlush(true) |
| 98 | + .setRateLimiter(rateLimiter); |
| 99 | + final RocksDB db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath()); |
| 100 | + final FlushWALOptions flushWALOptions = new FlushWALOptions()) { |
| 101 | + |
| 102 | + // Put some data |
| 103 | + db.put("key1".getBytes(), "value1".getBytes()); |
| 104 | + |
| 105 | + // Flush WAL with high priority |
| 106 | + flushWALOptions.setSync(true); |
| 107 | + flushWALOptions.setRateLimiterPriority(IOPriority.IO_HIGH); |
| 108 | + db.flushWal(flushWALOptions); |
| 109 | + |
| 110 | + // Verify data is accessible |
| 111 | + assertThat(new String(db.get("key1".getBytes()))).isEqualTo("value1"); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments