Created
June 14, 2016 10:06
-
-
Save adamretter/f9b78728121203920c9a0bab09994e69 to your computer and use it in GitHub Desktop.
RocksDB 4.5.1 JNI Comparator Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package comptest; | |
import org.rocksdb.*; | |
import static java.nio.charset.StandardCharsets.UTF_8; | |
/** | |
* Created by aretter on 14/06/2016. | |
*/ | |
public class CompTest { | |
public static void main(final String args[]) throws RocksDBException { | |
RocksDB.loadLibrary(); | |
final ComparatorOptions comparatorOptions = new ComparatorOptions(); | |
final Comparator myComparator = new Comparator(comparatorOptions) { | |
public String name() { | |
return "my comparator"; | |
} | |
public int compare(final Slice s1, final Slice s2) { | |
return s1.data().length - s2.data().length; | |
} | |
}; | |
final byte[] key1 = "key1".getBytes(UTF_8); | |
final byte[] value1 = "value1".getBytes(UTF_8); | |
RocksDB db = null; | |
Options options = null; | |
try { | |
options = new Options() | |
.setCreateIfMissing(true) | |
.setComparator(myComparator); | |
db = RocksDB.open(options, "/tmp/comptest"); | |
db.put(key1, value1); | |
System.out.println(new String(db.get(key1), UTF_8)); | |
} finally { | |
if(options != null) { | |
options.dispose(); | |
} | |
if(db != null) { | |
db.close(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The maven
pom.xml
looks like this: