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

This commit is contained in:
Naveen 2014-09-22 18:20:02 -07:00
parent cf7ace8865
commit fd7d3fe604
2 changed files with 16 additions and 10 deletions

View File

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

View File

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