Skip to content

Commit

Permalink
Adds a function to RocksJava for retrieving the version (facebook#7083)
Browse files Browse the repository at this point in the history
Summary:
Adds the function `RocksDB#rocksdbVersion()` for retrieving the RocksDB version.

Pull Request resolved: facebook#7083

Reviewed By: cheng-chang

Differential Revision: D22391628

Pulled By: pdillinger

fbshipit-source-id: e1cabcf28aa81f5ee8dcdce5c9eca6b3155a279e
  • Loading branch information
adamretter authored and facebook-github-bot committed Jul 6, 2020
1 parent 147f7b4 commit 0117cbf
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 10 deletions.
27 changes: 21 additions & 6 deletions java/rocksjni/rocksjni.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <jni.h>
#include <stdio.h>
#include <stdlib.h>

#include <algorithm>
#include <functional>
#include <memory>
Expand All @@ -22,6 +23,7 @@
#include "rocksdb/db.h"
#include "rocksdb/options.h"
#include "rocksdb/types.h"
#include "rocksdb/version.h"
#include "rocksjni/portal.h"

#ifdef min
Expand Down Expand Up @@ -2821,7 +2823,7 @@ jlong Java_org_rocksdb_RocksDB_getLatestSequenceNumber(
* Method: setPreserveDeletesSequenceNumber
* Signature: (JJ)Z
*/
jboolean JNICALL Java_org_rocksdb_RocksDB_setPreserveDeletesSequenceNumber(
jboolean Java_org_rocksdb_RocksDB_setPreserveDeletesSequenceNumber(
JNIEnv*, jobject, jlong jdb_handle, jlong jseq_number) {
auto* db = reinterpret_cast<ROCKSDB_NAMESPACE::DB*>(jdb_handle);
if (db->SetPreserveDeletesSequenceNumber(
Expand Down Expand Up @@ -3310,8 +3312,7 @@ void Java_org_rocksdb_RocksDB_startTrace(
* Method: endTrace
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_org_rocksdb_RocksDB_endTrace(
JNIEnv* env, jobject, jlong jdb_handle) {
void Java_org_rocksdb_RocksDB_endTrace(JNIEnv* env, jobject, jlong jdb_handle) {
auto* db = reinterpret_cast<ROCKSDB_NAMESPACE::DB*>(jdb_handle);
auto s = db->EndTrace();
if (!s.ok()) {
Expand Down Expand Up @@ -3379,9 +3380,11 @@ bool get_slice_helper(JNIEnv* env, jobjectArray ranges, jsize index,
* Method: deleteFilesInRanges
* Signature: (JJLjava/util/List;Z)V
*/
JNIEXPORT void JNICALL Java_org_rocksdb_RocksDB_deleteFilesInRanges(
JNIEnv* env, jobject /*jdb*/, jlong jdb_handle, jlong jcf_handle,
jobjectArray ranges, jboolean include_end) {
void Java_org_rocksdb_RocksDB_deleteFilesInRanges(JNIEnv* env, jobject /*jdb*/,
jlong jdb_handle,
jlong jcf_handle,
jobjectArray ranges,
jboolean include_end) {
jsize length = env->GetArrayLength(ranges);

std::vector<ROCKSDB_NAMESPACE::RangePtr> rangesVector;
Expand Down Expand Up @@ -3416,3 +3419,15 @@ JNIEXPORT void JNICALL Java_org_rocksdb_RocksDB_deleteFilesInRanges(
ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, s);
}
}

/*
* Class: org_rocksdb_RocksDB
* Method: version
* Signature: ()I
*/
jint Java_org_rocksdb_RocksDB_version(JNIEnv*, jclass) {
uint32_t encodedVersion = (ROCKSDB_MAJOR & 0xff) << 16;
encodedVersion |= (ROCKSDB_MINOR & 0xff) << 8;
encodedVersion |= (ROCKSDB_PATCH & 0xff);
return static_cast<jint>(encodedVersion);
}
60 changes: 56 additions & 4 deletions java/src/main/java/org/rocksdb/RocksDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,21 @@ public static void loadLibrary() {
if (compressionType.getLibraryName() != null) {
System.loadLibrary(compressionType.getLibraryName());
}
} catch (UnsatisfiedLinkError e) {
} catch (final UnsatisfiedLinkError e) {
// since it may be optional, we ignore its loading failure here.
}
}
try {
NativeLibraryLoader.getInstance().loadLibrary(tmpDir);
} catch (IOException e) {
} catch (final IOException e) {
libraryLoaded.set(LibraryState.NOT_LOADED);
throw new RuntimeException("Unable to load the RocksDB shared library",
e);
}

final int encodedVersion = version();
version = Version.fromEncodedVersion(encodedVersion);

libraryLoaded.set(LibraryState.LOADED);
return;
}
Expand Down Expand Up @@ -107,7 +110,7 @@ public static void loadLibrary(final List<String> paths) {
System.load(path + "/" + Environment.getSharedLibraryFileName(
compressionType.getLibraryName()));
break;
} catch (UnsatisfiedLinkError e) {
} catch (final UnsatisfiedLinkError e) {
// since they are optional, we ignore loading fails.
}
}
Expand All @@ -120,7 +123,7 @@ public static void loadLibrary(final List<String> paths) {
Environment.getJniLibraryFileName("rocksdbjni"));
success = true;
break;
} catch (UnsatisfiedLinkError e) {
} catch (final UnsatisfiedLinkError e) {
err = e;
}
}
Expand All @@ -129,6 +132,9 @@ public static void loadLibrary(final List<String> paths) {
throw err;
}

final int encodedVersion = version();
version = Version.fromEncodedVersion(encodedVersion);

libraryLoaded.set(LibraryState.LOADED);
return;
}
Expand All @@ -142,6 +148,10 @@ public static void loadLibrary(final List<String> paths) {
}
}

public static Version rocksdbVersion() {
return version;
}

/**
* Private constructor.
*
Expand Down Expand Up @@ -4531,5 +4541,47 @@ private native void deleteFilesInRanges(long handle, long cfHandle, final byte[]
private native static void destroyDB(final String path,
final long optionsHandle) throws RocksDBException;

private native static int version();

protected DBOptionsInterface options_;
private static Version version;

public static class Version {
private final byte major;
private final byte minor;
private final byte patch;

public Version(final byte major, final byte minor, final byte patch) {
this.major = major;
this.minor = minor;
this.patch = patch;
}

public int getMajor() {
return major;
}

public int getMinor() {
return minor;
}

public int getPatch() {
return patch;
}

@Override
public String toString() {
return getMajor() + "." + getMinor() + "." + getPatch();
}

private static Version fromEncodedVersion(int encodedVersion) {
final byte patch = (byte) (encodedVersion & 0xff);
encodedVersion >>= 8;
final byte minor = (byte) (encodedVersion & 0xff);
encodedVersion >>= 8;
final byte major = (byte) (encodedVersion & 0xff);

return new Version(major, minor, patch);
}
}
}
7 changes: 7 additions & 0 deletions java/src/test/java/org/rocksdb/RocksDBTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1683,6 +1683,13 @@ public void setDBOptions() throws RocksDBException {
}
}

@Test
public void rocksdbVersion() {
final RocksDB.Version version = RocksDB.rocksdbVersion();
assertThat(version).isNotNull();
assertThat(version.getMajor()).isGreaterThan(1);
}

private static class InMemoryTraceWriter extends AbstractTraceWriter {
private final List<byte[]> writes = new ArrayList<>();
private volatile boolean closed = false;
Expand Down

0 comments on commit 0117cbf

Please sign in to comment.