Skip to content

Instantly share code, notes, and snippets.

@adamretter
Created June 14, 2016 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamretter/f9b78728121203920c9a0bab09994e69 to your computer and use it in GitHub Desktop.
Save adamretter/f9b78728121203920c9a0bab09994e69 to your computer and use it in GitHub Desktop.
RocksDB 4.5.1 JNI Comparator Example
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();
}
}
}
}
@adamretter
Copy link
Author

The maven pom.xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>rocks-comp-test</groupId>
    <artifactId>rocks-comp-test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.rocksdb</groupId>
            <artifactId>rocksdbjni</artifactId>
            <version>4.5.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>

        </plugins>
    </build>

</project>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment