|
21 | 21 | import org.apache.fluss.exception.FlussRuntimeException; |
22 | 22 | import org.apache.fluss.metrics.SimpleCounter; |
23 | 23 | import org.apache.fluss.metrics.util.TestHistogram; |
| 24 | +import org.apache.fluss.row.BinaryString; |
| 25 | +import org.apache.fluss.row.GenericRow; |
| 26 | +import org.apache.fluss.row.encode.CompactedKeyEncoder; |
24 | 27 | import org.apache.fluss.server.kv.KvCloseMode; |
| 28 | +import org.apache.fluss.types.DataTypes; |
| 29 | +import org.apache.fluss.types.RowType; |
25 | 30 |
|
26 | 31 | import io.github.fluss_contrib.rocksdb.ColumnFamilyOptions; |
27 | 32 | import io.github.fluss_contrib.rocksdb.FlushOptions; |
28 | 33 | import io.github.fluss_contrib.rocksdb.RocksDBException; |
29 | 34 | import org.junit.jupiter.api.Test; |
30 | 35 | import org.junit.jupiter.api.io.TempDir; |
| 36 | +import org.junit.jupiter.params.ParameterizedTest; |
| 37 | +import org.junit.jupiter.params.provider.ValueSource; |
31 | 38 |
|
32 | 39 | import java.io.File; |
33 | 40 | import java.nio.file.Path; |
@@ -68,6 +75,109 @@ void testRocksDbKv(@TempDir Path tempDir) throws Exception { |
68 | 75 | } |
69 | 76 | } |
70 | 77 |
|
| 78 | + @ParameterizedTest |
| 79 | + @ValueSource(booleans = {false, true}) |
| 80 | + void testPrefixLookupByteBoundaries(boolean flush, @TempDir Path tempDir) throws Exception { |
| 81 | + // Already in unsigned bytewise order, including exact prefixes and their upper bounds. |
| 82 | + byte[][] keys = { |
| 83 | + {}, |
| 84 | + {0}, |
| 85 | + {0, 0}, |
| 86 | + {0, (byte) 0xff}, |
| 87 | + {1}, |
| 88 | + {1, 0}, |
| 89 | + {1, (byte) 0xfe}, |
| 90 | + {1, (byte) 0xff}, |
| 91 | + {1, (byte) 0xff, 0}, |
| 92 | + {1, (byte) 0xff, (byte) 0xff}, |
| 93 | + {2}, |
| 94 | + {0x7f}, |
| 95 | + {0x7f, (byte) 0xff}, |
| 96 | + {(byte) 0x80}, |
| 97 | + {(byte) 0x80, 0}, |
| 98 | + {(byte) 0xfe}, |
| 99 | + {(byte) 0xff}, |
| 100 | + {(byte) 0xff, 0}, |
| 101 | + {(byte) 0xff, (byte) 0xff}, |
| 102 | + {(byte) 0xff, (byte) 0xff, (byte) 0xff} |
| 103 | + }; |
| 104 | + byte[][] prefixes = { |
| 105 | + {}, |
| 106 | + {0}, |
| 107 | + {0, (byte) 0xff}, |
| 108 | + {1}, |
| 109 | + {1, (byte) 0xff}, |
| 110 | + {1, (byte) 0xff, (byte) 0xff}, |
| 111 | + {1, (byte) 0xff, (byte) 0xff, 0}, |
| 112 | + {2}, |
| 113 | + {3}, |
| 114 | + {0x7f}, |
| 115 | + {0x7f, (byte) 0xff}, |
| 116 | + {(byte) 0x80}, |
| 117 | + {(byte) 0xff}, |
| 118 | + {(byte) 0xff, (byte) 0xff}, |
| 119 | + {(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff} |
| 120 | + }; |
| 121 | + try (RocksDBKv kv = buildRocksDBKv(tempDir.toFile())) { |
| 122 | + for (byte[] key : keys) { |
| 123 | + kv.put(key, key); |
| 124 | + } |
| 125 | + if (flush) { |
| 126 | + try (FlushOptions options = new FlushOptions().setWaitForFlush(true)) { |
| 127 | + kv.db.flush(options); |
| 128 | + } |
| 129 | + } |
| 130 | + for (byte[] prefix : prefixes) { |
| 131 | + byte[][] expected = |
| 132 | + Arrays.stream(keys) |
| 133 | + .filter( |
| 134 | + key -> |
| 135 | + key.length >= prefix.length |
| 136 | + && Arrays.equals( |
| 137 | + Arrays.copyOf(key, prefix.length), |
| 138 | + prefix)) |
| 139 | + .toArray(byte[][]::new); |
| 140 | + assertThat(kv.prefixLookup(prefix)) |
| 141 | + .as("prefix %s (flushed=%s)", Arrays.toString(prefix), flush) |
| 142 | + .containsExactly(expected); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + void testPrefixLookupWithEncodedKeysAndMixedSstAndMemtable(@TempDir Path tempDir) |
| 149 | + throws Exception { |
| 150 | + RowType keyType = RowType.of(DataTypes.STRING(), DataTypes.INT()); |
| 151 | + CompactedKeyEncoder keyEncoder = new CompactedKeyEncoder(keyType); |
| 152 | + CompactedKeyEncoder prefixEncoder = new CompactedKeyEncoder(keyType, new int[] {0}); |
| 153 | + byte[] prefix = prefixEncoder.encodeKey(GenericRow.of(BinaryString.fromString("ab"), 0)); |
| 154 | + byte[] key1 = keyEncoder.encodeKey(GenericRow.of(BinaryString.fromString("ab"), 1)); |
| 155 | + byte[] key2 = keyEncoder.encodeKey(GenericRow.of(BinaryString.fromString("ab"), 2)); |
| 156 | + byte[] key3 = keyEncoder.encodeKey(GenericRow.of(BinaryString.fromString("ab"), 3)); |
| 157 | + byte[] key4 = keyEncoder.encodeKey(GenericRow.of(BinaryString.fromString("ab"), 4)); |
| 158 | + byte[] other = keyEncoder.encodeKey(GenericRow.of(BinaryString.fromString("ac"), 1)); |
| 159 | + try (RocksDBKv kv = buildRocksDBKv(tempDir.toFile())) { |
| 160 | + kv.put(key1, new byte[] {1}); |
| 161 | + kv.put(key2, new byte[] {2}); |
| 162 | + kv.put(key3, new byte[] {3}); |
| 163 | + kv.put(other, new byte[] {9}); |
| 164 | + try (FlushOptions options = new FlushOptions().setWaitForFlush(true)) { |
| 165 | + kv.db.flush(options); |
| 166 | + kv.put(key1, new byte[] {5}); |
| 167 | + kv.delete(key2); |
| 168 | + kv.put(key4, new byte[] {4}); |
| 169 | + assertThat(kv.prefixLookup(prefix)) |
| 170 | + .containsExactly(new byte[] {5}, new byte[] {3}, new byte[] {4}); |
| 171 | + kv.db.flush(options); |
| 172 | + assertThat(kv.prefixLookup(prefix)) |
| 173 | + .containsExactly(new byte[] {5}, new byte[] {3}, new byte[] {4}); |
| 174 | + kv.db.compactRange(); |
| 175 | + assertThat(kv.prefixLookup(prefix)) |
| 176 | + .containsExactly(new byte[] {5}, new byte[] {3}, new byte[] {4}); |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + |
71 | 181 | @Test |
72 | 182 | void testLiveSstFilesSize(@TempDir Path tempDir) throws Exception { |
73 | 183 | try (RocksDBKv rocksDBKv = buildRocksDBKv(tempDir.toFile())) { |
|
0 commit comments