Addressing review comments (adding a env variable to override temp directory)

main
Naveen 10 years ago
parent cf7ace8865
commit fd7d3fe604
  1. 19
      java/org/rocksdb/NativeLibraryLoader.java
  2. 7
      java/org/rocksdb/RocksDB.java

@ -8,15 +8,18 @@ import java.io.*;
* The shared library is extracted to a temp folder and loaded from there. * The shared library is extracted to a temp folder and loaded from there.
*/ */
public class NativeLibraryLoader { public class NativeLibraryLoader {
private static String sharedLibraryName = "librocksdbjni.so"; private static String sharedLibraryName = "librocksdbjni.so";
private static String tempFilePrefix = "librocksdbjni"; private static String tempFilePrefix = "librocksdbjni";
private static String tempFileSuffix = ".so"; private static String tempFileSuffix = ".so";
public static void loadLibraryFromJar() public static void loadLibraryFromJar(String tmpDir)
throws IOException { throws IOException {
File temp;
if(tmpDir == null || tmpDir.equals(""))
temp = File.createTempFile(tempFilePrefix, tempFileSuffix);
else
temp = new File(tmpDir+"/"+sharedLibraryName);
File temp = File.createTempFile(tempFilePrefix, tempFileSuffix);
temp.deleteOnExit(); temp.deleteOnExit();
if (!temp.exists()) { if (!temp.exists()) {
@ -31,14 +34,18 @@ public class NativeLibraryLoader {
throw new RuntimeException(sharedLibraryName + " was not found inside JAR."); throw new RuntimeException(sharedLibraryName + " was not found inside JAR.");
} }
OutputStream os = null;
try { try {
OutputStream os = new FileOutputStream(temp); os = new FileOutputStream(temp);
while ((readBytes = is.read(buffer)) != -1) { while ((readBytes = is.read(buffer)) != -1) {
os.write(buffer, 0, readBytes); os.write(buffer, 0, readBytes);
} }
} finally { } finally {
os.close(); if(os != null)
is.close(); os.close();
if(is != null)
is.close();
} }
System.load(temp.getAbsolutePath()); System.load(temp.getAbsolutePath());

@ -20,21 +20,20 @@ import org.rocksdb.NativeLibraryLoader;
* indicates sth wrong at the rocksdb library side and the call failed. * indicates sth wrong at the rocksdb library side and the call failed.
*/ */
public class RocksDB extends RocksObject { public class RocksDB extends RocksObject {
public static final int NOT_FOUND = -1; public static final int NOT_FOUND = -1;
private static final String[] compressionLibs_ = { private static final String[] compressionLibs_ = {
"snappy", "z", "bzip2", "lz4", "lz4hc"}; "snappy", "z", "bzip2", "lz4", "lz4hc"};
static { static {
RocksDB.loadLibrary(); RocksDB.loadLibrary();
} }
/** /**
* Loads the necessary library files. * Loads the necessary library files.
* Calling this method twice will have no effect. * Calling this method twice will have no effect.
*/ */
public static synchronized void loadLibrary() { public static synchronized void loadLibrary() {
String tmpDir = System.getenv("ROCKSDB_SHAREDLIB_DIR");
// loading possibly necessary libraries. // loading possibly necessary libraries.
for (String lib : compressionLibs_) { for (String lib : compressionLibs_) {
try { try {
@ -45,7 +44,7 @@ public class RocksDB extends RocksObject {
} }
try try
{ {
NativeLibraryLoader.loadLibraryFromJar(); NativeLibraryLoader.loadLibraryFromJar(tmpDir);
} }
catch (IOException e) catch (IOException e)
{ {

Loading…
Cancel
Save