Adds a function to RocksJava for retrieving the version (#7083)

Summary:
Adds the function `RocksDB#rocksdbVersion()` for retrieving the RocksDB version.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/7083

Reviewed By: cheng-chang

Differential Revision: D22391628

Pulled By: pdillinger

fbshipit-source-id: e1cabcf28aa81f5ee8dcdce5c9eca6b3155a279e
This commit is contained in:
Adam Retter 2020-07-06 11:04:37 -07:00 committed by Facebook GitHub Bot
parent 147f7b472a
commit 0117cbfc96
3 changed files with 84 additions and 10 deletions

View File

@ -9,6 +9,7 @@
#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <functional>
#include <memory>
@ -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
@ -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(
@ -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()) {
@ -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;
@ -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);
}

View File

@ -59,18 +59,21 @@ public class RocksDB extends RocksObject {
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;
}
@ -107,7 +110,7 @@ public class RocksDB extends RocksObject {
System.load(path + "/" + Environment.getSharedLibraryFileName(
compressionType.getLibraryName()));
break;
} catch (UnsatisfiedLinkError e) {
} catch (final UnsatisfiedLinkError e) {
// since they are optional, we ignore loading fails.
}
}
@ -120,7 +123,7 @@ public class RocksDB extends RocksObject {
Environment.getJniLibraryFileName("rocksdbjni"));
success = true;
break;
} catch (UnsatisfiedLinkError e) {
} catch (final UnsatisfiedLinkError e) {
err = e;
}
}
@ -129,6 +132,9 @@ public class RocksDB extends RocksObject {
throw err;
}
final int encodedVersion = version();
version = Version.fromEncodedVersion(encodedVersion);
libraryLoaded.set(LibraryState.LOADED);
return;
}
@ -142,6 +148,10 @@ public class RocksDB extends RocksObject {
}
}
public static Version rocksdbVersion() {
return version;
}
/**
* Private constructor.
*
@ -4531,5 +4541,47 @@ public class RocksDB extends RocksObject {
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);
}
}
}

View File

@ -1683,6 +1683,13 @@ public class RocksDBTest {
}
}
@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;