Skip to content

Commit

Permalink
RocksDB static build
Browse files Browse the repository at this point in the history
Make file changes to download and build the dependencies
.Load the shared library when RocksDB is initialized
  • Loading branch information
Naveen committed Aug 18, 2014
1 parent 68eed8c commit ddb8039
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 5 deletions.
32 changes: 31 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ endif # PLATFORM_SHARED_EXT

.PHONY: blackbox_crash_test check clean coverage crash_test ldb_tests \
release tags valgrind_check whitebox_crash_test format static_lib shared_lib all \
dbg
dbg rocksdbjavastatic rocksdbjava

all: $(LIBRARY) $(PROGRAMS) $(TESTS)

Expand Down Expand Up @@ -480,6 +480,36 @@ ROCKSDBJNILIB = librocksdbjni.jnilib
JAVA_INCLUDE = -I/System/Library/Frameworks/JavaVM.framework/Headers/
endif


rocksdbjavastatic:
#build zlib
curl -O http://zlib.net/zlib-1.2.8.tar.gz
tar xvzf zlib-1.2.8.tar.gz
cd zlib-1.2.8 && CFLAGS='-fPIC' ./configure --static && make
cp zlib-1.2.8/libz.a .
rm -rf zlib-1.2.8.tar.gz zlib-1.2.8

#build bzip
curl -O http://www.bzip.org/1.0.6/bzip2-1.0.6.tar.gz
tar xvzf bzip2-1.0.6.tar.gz
cd bzip2-1.0.6 && make CFLAGS='-fPIC -Wall -Winline -O2 -g -D_FILE_OFFSET_BITS=64'
cp bzip2-1.0.6/libbz2.a .
rm -rf bzip2-1.0.6.tar.gz bzip2-1.0.6

#build snappy
curl -O https://snappy.googlecode.com/files/snappy-1.1.1.tar.gz
tar xvzf snappy-1.1.1.tar.gz
cd snappy-1.1.1 && ./configure --with-pic --enable-static
cd snappy-1.1.1 && make
cp snappy-1.1.1/.libs/libsnappy.a .
rm -rf snappy-1.1.1 snappy-1.1.1.tar.gz
OPT="-fPIC -DNDEBUG -O2" $(MAKE) $(LIBRARY) -j
cd java;$(MAKE) java;
rm -f ./java/$(ROCKSDBJNILIB)
$(CXX) $(CXXFLAGS) -I./java/. $(JAVA_INCLUDE) -shared -fPIC -o ./java/$(ROCKSDBJNILIB) $(JNI_NATIVE_SOURCES) $(LIBOBJECTS) $(COVERAGEFLAGS) libz.a libbz2.a libsnappy.a
cd java;jar -cf $(ROCKSDB_JAR) org/rocksdb/*.class org/rocksdb/util/*.class HISTORY*.md $(ROCKSDBJNILIB)


rocksdbjava:
OPT="-fPIC -DNDEBUG -O2" $(MAKE) $(LIBRARY) -j32
cd java;$(MAKE) java;
Expand Down
46 changes: 46 additions & 0 deletions java/org/rocksdb/NativeLibraryLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.rocksdb;

import java.io.*;

public class NativeLibraryLoader
{

private static String sharedLibraryName = "librocksdbjni.so";
private static String tempFilePrefix = "librocksdbjni";
private static String tempFileSuffix = ".so";
/**
* Private constructor - this class will never be instanced
*/
private NativeLibraryLoader() {
}

public static void loadLibraryFromJar() throws IOException {

File temp = File.createTempFile(tempFilePrefix, tempFileSuffix);
temp.deleteOnExit();

if (!temp.exists()) {
throw new FileNotFoundException("File " + temp.getAbsolutePath() + " does not exist.");
}

byte[] buffer = new byte[1024];
int readBytes;

InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream(sharedLibraryName);
if (is == null) {
throw new FileNotFoundException(sharedLibraryName + " was not found inside JAR.");
}

OutputStream os = new FileOutputStream(temp);
try {
while ((readBytes = is.read(buffer)) != -1) {
os.write(buffer, 0, readBytes);
}
} finally {
os.close();
is.close();
}

System.load(temp.getAbsolutePath());
}
}
3 changes: 3 additions & 0 deletions java/org/rocksdb/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
* native resources will be released as part of the process.
*/
public class Options extends RocksObject {
static{
RocksDB.loadLibrary();
}
static final long DEFAULT_CACHE_SIZE = 8 << 20;
static final int DEFAULT_NUM_SHARD_BITS = -1;
/**
Expand Down
24 changes: 20 additions & 4 deletions java/org/rocksdb/RocksDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,32 @@
import java.io.Closeable;
import java.io.IOException;
import org.rocksdb.util.Environment;
import org.rocksdb.NativeLibraryLoader;

/**
* A RocksDB is a persistent ordered map from keys to values. It is safe for
* concurrent access from multiple threads without any external synchronization.
* All methods of this class could potentially throw RocksDBException, which
* indicates sth wrong at the rocksdb library side and the call failed.
*/
public class RocksDB extends RocksObject {
public class RocksDB extends org.rocksdb.RocksObject
{

public static final int NOT_FOUND = -1;
private static final String[] compressionLibs_ = {
"snappy", "z", "bzip2", "lz4", "lz4hc"};

static {
loadLibrary();
}


/**
* Loads the necessary library files.
* Calling this method twice will have no effect.
*/
public static synchronized void loadLibrary() {
public static synchronized void loadLibrary()
{
// loading possibly necessary libraries.
for (String lib : compressionLibs_) {
try {
Expand All @@ -36,8 +45,15 @@ public static synchronized void loadLibrary() {
// 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");

try
{
NativeLibraryLoader.loadLibraryFromJar();
}
catch (IOException e)
{
e.printStackTrace();
}
}

/**
Expand Down

0 comments on commit ddb8039

Please sign in to comment.