Skip to content

Instantly share code, notes, and snippets.

@mikhail-antonov
Created March 30, 2017 20:17
Show Gist options
  • Save mikhail-antonov/ef4fd053f8e47947fc59e6cd559aa77d to your computer and use it in GitHub Desktop.
Save mikhail-antonov/ef4fd053f8e47947fc59e6cd559aa77d to your computer and use it in GitHub Desktop.
a test for rocks-java byte comparators semantics
import org.rocksdb.ComparatorOptions;
import org.rocksdb.EnvOptions;
import org.rocksdb.Options;
import org.rocksdb.RocksDBException;
import org.rocksdb.Slice;
import org.rocksdb.SstFileWriter;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import org.junit.Test;
import org.rocksdb.util.BytewiseComparator;
import static com.google.common.base.Preconditions.checkArgument;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
public class TestSstFileWriterByteComparator {
private static final String TMP_SST_FILE = "/tmp/test_sst_file_writer_byte_comparator.sst";
@Test
public void testNativeComparator() throws RocksDBException, IOException {
byte[] key1 = fromHex("29");
byte[] key2 = fromHex("8d");
byte[] v1 = fromHex("00");
EnvOptions envOpts = new EnvOptions();
Options opts = new Options();
SstFileWriter writer = new SstFileWriter(envOpts, opts);
Files.deleteIfExists(FileSystems.getDefault().getPath(TMP_SST_FILE));
// this write should succeed
writer.open(TMP_SST_FILE);
writer.add(new Slice(key1), new Slice(v1));
writer.add(new Slice(key2), new Slice(v1));
writer.finish();
Files.deleteIfExists(FileSystems.getDefault().getPath(TMP_SST_FILE));
// this write should throw an exception, out of order insertion
writer.open(TMP_SST_FILE);
writer.add(new Slice(key2), new Slice(v1));
try {
writer.add(new Slice(key1), new Slice(v1));
fail("This write should have failed, but it didn't!");
} catch (RocksDBException rex) {
// best we can do, better than nothing
assertEquals(rex.getMessage(), "Keys must be added in order");
}
writer.finish();
Files.deleteIfExists(FileSystems.getDefault().getPath(TMP_SST_FILE));
}
@Test
public void testJavaComparator() throws RocksDBException, IOException {
byte[] key1 = fromHex("29");
byte[] key2 = fromHex("8d");
byte[] v1 = fromHex("00");
EnvOptions envOpts = new EnvOptions();
Options opts = new Options();
SstFileWriter writer = new SstFileWriter(envOpts, opts, new BytewiseComparator(new ComparatorOptions()));
Files.deleteIfExists(FileSystems.getDefault().getPath(TMP_SST_FILE));
// this write should succeed
writer.open(TMP_SST_FILE);
writer.add(new Slice(key1), new Slice(v1));
writer.add(new Slice(key2), new Slice(v1));
writer.finish();
Files.deleteIfExists(FileSystems.getDefault().getPath(TMP_SST_FILE));
// this write should throw an exception, out of order insertion
writer.open(TMP_SST_FILE);
writer.add(new Slice(key2), new Slice(v1));
try {
writer.add(new Slice(key1), new Slice(v1));
fail("This write should have failed, but it didn't!");
} catch (RocksDBException rex) {
// best we can do, better than nothing
assertEquals(rex.getMessage(), "Keys must be added in order");
}
writer.finish();
Files.deleteIfExists(FileSystems.getDefault().getPath(TMP_SST_FILE));
}
// below is copied from HBase Bytes class
public static byte[] fromHex(String hex) {
checkArgument(hex.length() % 2 == 0, "length must be a multiple of 2");
int len = hex.length();
byte[] b = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
b[i / 2] = hexCharsToByte(hex.charAt(i),hex.charAt(i+1));
}
return b;
}
private static int hexCharToNibble(char ch) {
if (ch <= '9' && ch >= '0') {
return ch - '0';
} else if (ch >= 'a' && ch <= 'f') {
return ch - 'a' + 10;
} else if (ch >= 'A' && ch <= 'F') {
return ch - 'A' + 10;
}
throw new IllegalArgumentException("Invalid hex char: " + ch);
}
private static byte hexCharsToByte(char c1, char c2) {
return (byte) ((hexCharToNibble(c1) << 4) | hexCharToNibble(c2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment