|
| 1 | +package org.rocksdb; |
| 2 | + |
| 3 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 4 | +import static org.assertj.core.api.Assertions.assertThat; |
| 5 | + |
| 6 | +import java.io.*; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | +import org.junit.ClassRule; |
| 10 | +import org.junit.Rule; |
| 11 | +import org.junit.Test; |
| 12 | +import org.junit.rules.ExpectedException; |
| 13 | +import org.junit.rules.TemporaryFolder; |
| 14 | + |
| 15 | +public class MultiGetCorruptionTest { |
| 16 | + private static final byte[] KEY = "key".getBytes(UTF_8); |
| 17 | + private static final byte[] VALUE; |
| 18 | + |
| 19 | + static { |
| 20 | + StringBuilder sb = new StringBuilder(); |
| 21 | + for (int i = 0; i < 100; i++) { |
| 22 | + sb.append("value" + i + "\n"); |
| 23 | + } |
| 24 | + VALUE = sb.toString().getBytes(UTF_8); |
| 25 | + } |
| 26 | + |
| 27 | + @ClassRule |
| 28 | + public static final RocksNativeLibraryResource ROCKS_NATIVE_LIBRARY_RESOURCE = |
| 29 | + new RocksNativeLibraryResource(); |
| 30 | + |
| 31 | + @Rule public TemporaryFolder dbFolder = new TemporaryFolder(); |
| 32 | + |
| 33 | + @Rule public ExpectedException exception = ExpectedException.none(); |
| 34 | + |
| 35 | + @Test |
| 36 | + public void getKeyException() throws RocksDBException, IOException { |
| 37 | + createCorruptedDatabase(); |
| 38 | + try (Options options = new Options().setCreateIfMissing(true).setParanoidChecks(true); |
| 39 | + RocksDB db = RocksDB.openReadOnly(options, dbFolder.getRoot().getAbsolutePath())) { |
| 40 | + exception.expect(RocksDBException.class); // We need to be sure, exception is thrown only |
| 41 | + db.get(KEY); // on GET operation. Require careful data corruption. |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void multiGetKeyException() throws RocksDBException, IOException { |
| 47 | + createCorruptedDatabase(); |
| 48 | + try (Options options = new Options().setCreateIfMissing(true).setParanoidChecks(true); |
| 49 | + RocksDB db = RocksDB.openReadOnly(options, dbFolder.getRoot().getAbsolutePath())) { |
| 50 | + // exception.expect(RocksDBException.class); |
| 51 | + List<byte[]> keys = new ArrayList<>(); |
| 52 | + keys.add(KEY); |
| 53 | + List<byte[]> result = db.multiGetAsList(keys); |
| 54 | + assertThat(result).isNotNull(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private void createCorruptedDatabase() throws RocksDBException, IOException { |
| 59 | + try (Options options = new Options().setCreateIfMissing(true).setParanoidChecks(true); |
| 60 | + RocksDB db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath())) { |
| 61 | + db.put(KEY, VALUE); |
| 62 | + db.flush(new FlushOptions().setWaitForFlush(true)); |
| 63 | + } |
| 64 | + |
| 65 | + File[] files = dbFolder.getRoot().listFiles((dir, name) -> name.endsWith("sst")); |
| 66 | + assertThat(files).hasSize(1); |
| 67 | + |
| 68 | + try (RandomAccessFile file = new RandomAccessFile(files[0], "rw")) { |
| 69 | + file.seek(30); |
| 70 | + file.write("corrupted".getBytes(UTF_8)); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments