diff --git a/java/rocksjni/rocksjni.cc b/java/rocksjni/rocksjni.cc index 56f79830af..252fe0836f 100644 --- a/java/rocksjni/rocksjni.cc +++ b/java/rocksjni/rocksjni.cc @@ -9,6 +9,7 @@ #include #include #include + #include #include #include @@ -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(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(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 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(encodedVersion); +} diff --git a/java/src/main/java/org/rocksdb/RocksDB.java b/java/src/main/java/org/rocksdb/RocksDB.java index 05d9ca2010..b3cf337630 100644 --- a/java/src/main/java/org/rocksdb/RocksDB.java +++ b/java/src/main/java/org/rocksdb/RocksDB.java @@ -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); + } + } } diff --git a/java/src/test/java/org/rocksdb/RocksDBTest.java b/java/src/test/java/org/rocksdb/RocksDBTest.java index 48c991d2d4..fc62dc80e1 100644 --- a/java/src/test/java/org/rocksdb/RocksDBTest.java +++ b/java/src/test/java/org/rocksdb/RocksDBTest.java @@ -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 writes = new ArrayList<>(); private volatile boolean closed = false;