[Java] Add static method RocksDB.loadLibrary()

Summary:
Add static method RocksDB.loadLibrary() which loads necessary library
files.

Test Plan:
make rocksdbjava
make jtest
make jdb_bench
java/jdb_bench.sh

Reviewers: haobo, dhruba, sdong, ankgup87, rsumbaly, swapnilghike, zzbennett

Reviewed By: ankgup87

CC: leveldb

Differential Revision: https://reviews.facebook.net/D18375
main
Yueh-Hsuan Chiang 11 years ago
parent 16f1aa7b2d
commit a3ab998ef9
  1. 2
      java/Makefile
  2. 2
      java/RocksDBSample.java
  3. 55
      java/org/rocksdb/RocksDB.java
  4. 2
      java/org/rocksdb/WriteBatchTest.java
  5. 2
      java/org/rocksdb/benchmark/DbBenchmark.java
  6. 2
      java/org/rocksdb/test/BackupableDBTest.java
  7. 3
      java/org/rocksdb/test/OptionsTest.java
  8. 5
      java/org/rocksdb/test/ReadOptionsTest.java
  9. 37
      java/org/rocksdb/util/Environment.java

@ -8,7 +8,7 @@ clean:
rm -f $(ROCKSDB_JAR) rm -f $(ROCKSDB_JAR)
java: java:
javac org/rocksdb/*.java org/rocksdb/util/*.java javac org/rocksdb/util/*.java org/rocksdb/*.java
jar -cf $(ROCKSDB_JAR) org/rocksdb/*.class org/rocksdb/util/*.class jar -cf $(ROCKSDB_JAR) org/rocksdb/*.class org/rocksdb/util/*.class
javah -d $(NATIVE_INCLUDE) -jni $(NATIVE_JAVA_CLASSES) javah -d $(NATIVE_INCLUDE) -jni $(NATIVE_JAVA_CLASSES)

@ -13,7 +13,7 @@ import java.io.IOException;
public class RocksDBSample { public class RocksDBSample {
static { static {
System.loadLibrary("rocksdbjni"); RocksDB.loadLibrary();
} }
public static void main(String[] args) { public static void main(String[] args) {

@ -10,6 +10,7 @@ import java.util.Map;
import java.util.HashMap; import java.util.HashMap;
import java.io.Closeable; import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import org.rocksdb.util.Environment;
/** /**
* A RocksDB is a persistent ordered map from keys to values. It is safe for * A RocksDB is a persistent ordered map from keys to values. It is safe for
@ -19,6 +20,60 @@ import java.io.IOException;
*/ */
public class RocksDB { public class RocksDB {
public static final int NOT_FOUND = -1; public static final int NOT_FOUND = -1;
private static final String[] compressionLibs_ = {
"snappy", "zlib", "bzip2", "lz4", "lz4hc"};
/**
* Loads the necessary library files.
* Calling this method twice will have no effect.
*/
public static synchronized void loadLibrary() {
// loading possibly necessary libraries.
for (String lib : compressionLibs_) {
try {
System.loadLibrary(lib);
} catch (UnsatisfiedLinkError e) {
// since it may be optional, we ignore its loading failure here.
}
}
// However, if any of them is required. We will see error here.
System.loadLibrary("rocksdbjni");
}
/**
* Tries to load the necessary library files from the given list of
* directories.
*
* @param paths a list of strings where each describes a directory
* of a library.
*/
public static synchronized void loadLibrary(List<String> paths) {
for (String lib : compressionLibs_) {
for (String path : paths) {
try {
System.load(path + "/" + Environment.getSharedLibraryName(lib));
break;
} catch (UnsatisfiedLinkError e) {
// since they are optional, we ignore loading fails.
}
}
}
boolean success = false;
UnsatisfiedLinkError err = null;
for (String path : paths) {
try {
System.load(path + "/" + Environment.getJniLibraryName("rocksdbjni"));
success = true;
break;
} catch (UnsatisfiedLinkError e) {
err = e;
}
}
if (success == false) {
throw err;
}
}
/** /**
* The factory constructor of RocksDB that opens a RocksDB instance given * The factory constructor of RocksDB that opens a RocksDB instance given
* the path to the database using the default options w/ createIfMissing * the path to the database using the default options w/ createIfMissing

@ -16,7 +16,7 @@ import java.io.UnsupportedEncodingException;
*/ */
public class WriteBatchTest { public class WriteBatchTest {
static { static {
System.loadLibrary("rocksdbjni"); RocksDB.loadLibrary();
} }
public static void main(String args[]) { public static void main(String args[]) {

@ -172,7 +172,7 @@ public class DbBenchmark {
} }
static { static {
System.loadLibrary("rocksdbjni"); RocksDB.loadLibrary();
} }
abstract class BenchmarkTask implements Callable<Stats> { abstract class BenchmarkTask implements Callable<Stats> {

@ -11,7 +11,7 @@ public class BackupableDBTest {
static final String db_path = "/tmp/backupablejni_db"; static final String db_path = "/tmp/backupablejni_db";
static final String backup_path = "/tmp/backupablejni_db_backup"; static final String backup_path = "/tmp/backupablejni_db_backup";
static { static {
System.loadLibrary("rocksdbjni"); RocksDB.loadLibrary();
} }
public static void main(String[] args) { public static void main(String[] args) {

@ -6,11 +6,12 @@
package org.rocksdb.test; package org.rocksdb.test;
import java.util.Random; import java.util.Random;
import org.rocksdb.RocksDB;
import org.rocksdb.Options; import org.rocksdb.Options;
public class OptionsTest { public class OptionsTest {
static { static {
System.loadLibrary("rocksdbjni"); RocksDB.loadLibrary();
} }
public static void main(String[] args) { public static void main(String[] args) {
Options opt = new Options(); Options opt = new Options();

@ -6,11 +6,12 @@
package org.rocksdb.test; package org.rocksdb.test;
import java.util.Random; import java.util.Random;
import org.rocksdb.*; import org.rocksdb.RocksDB;
import org.rocksdb.ReadOptions;
public class ReadOptionsTest { public class ReadOptionsTest {
static { static {
System.loadLibrary("rocksdbjni"); RocksDB.loadLibrary();
} }
public static void main(String[] args) { public static void main(String[] args) {
ReadOptions opt = new ReadOptions(); ReadOptions opt = new ReadOptions();

@ -0,0 +1,37 @@
package org.rocksdb.util;
public class Environment {
private static String OS = System.getProperty("os.name").toLowerCase();
public static boolean isWindows() {
return (OS.indexOf("win") >= 0);
}
public static boolean isMac() {
return (OS.indexOf("mac") >= 0);
}
public static boolean isUnix() {
return (OS.indexOf("nix") >= 0 ||
OS.indexOf("nux") >= 0 ||
OS.indexOf("aix") >= 0);
}
public static String getSharedLibraryName(String name) {
if (isUnix()) {
return String.format("lib%s.so", name);
} else if (isMac()) {
return String.format("lib%s.dylib", name);
}
throw new UnsupportedOperationException();
}
public static String getJniLibraryName(String name) {
if (isUnix()) {
return String.format("lib%s.so", name);
} else if (isMac()) {
return String.format("lib%s.jnilib", name);
}
throw new UnsupportedOperationException();
}
}
Loading…
Cancel
Save