From 02dc07491650b0c8d8470a24806ba86d09ea31a4 Mon Sep 17 00:00:00 2001 From: "Chen, You" Date: Wed, 19 Sep 2018 17:44:12 -0700 Subject: [PATCH] add GetAggregatedLongProperty for Java API (#4379) Summary: Add Java API `getAggregatedLongProperty(final String property)` Pull Request resolved: https://github.com/facebook/rocksdb/pull/4379 Differential Revision: D9921463 Pulled By: sagar0 fbshipit-source-id: a02512e1b2aff4765a10b77de9a7bf7b1909d954 --- java/rocksjni/rocksjni.cc | 26 ++++++++++++++++++ java/src/main/java/org/rocksdb/RocksDB.java | 27 +++++++++++++++++++ .../java/org/rocksdb/ColumnFamilyTest.java | 4 +++ 3 files changed, 57 insertions(+) diff --git a/java/rocksjni/rocksjni.cc b/java/rocksjni/rocksjni.cc index c5b9fd591..6e50c32f7 100644 --- a/java/rocksjni/rocksjni.cc +++ b/java/rocksjni/rocksjni.cc @@ -1844,6 +1844,32 @@ jlong Java_org_rocksdb_RocksDB_getLongProperty__JJLjava_lang_String_2I( return 0; } +/* + * Class: org_rocksdb_RocksDB + * Method: getAggregatedLongProperty + * Signature: (JLjava/lang/String;I)J + */ +jlong Java_org_rocksdb_RocksDB_getAggregatedLongProperty( + JNIEnv* env, jobject, jlong db_handle, jstring jproperty, jint jproperty_len) { + const char* property = env->GetStringUTFChars(jproperty, nullptr); + if (property == nullptr) { + return 0; + } + rocksdb::Slice property_slice(property, jproperty_len); + auto* db = reinterpret_cast(db_handle); + uint64_t property_value = 0; + bool retCode = db->GetAggregatedIntProperty(property_slice, &property_value); + env->ReleaseStringUTFChars(jproperty, property); + + if (retCode) { + return property_value; + } + + rocksdb::RocksDBExceptionJni::ThrowNew(env, rocksdb::Status::NotFound()); + return 0; +} + + ////////////////////////////////////////////////////////////////////////////// // rocksdb::DB::Flush diff --git a/java/src/main/java/org/rocksdb/RocksDB.java b/java/src/main/java/org/rocksdb/RocksDB.java index 3e2ba7b56..38be3333f 100644 --- a/java/src/main/java/org/rocksdb/RocksDB.java +++ b/java/src/main/java/org/rocksdb/RocksDB.java @@ -1518,6 +1518,31 @@ public class RocksDB extends RocksObject { property, property.length()); } + /** + *

Return sum of the getLongProperty of all the column families

+ * + *

Note: As the returned property is of type + * {@code uint64_t} on C++ side the returning value can be negative + * because Java supports in Java 7 only signed long values.

+ * + *

Java 7: To mitigate the problem of the non + * existent unsigned long tpye, values should be encapsulated using + * {@link java.math.BigInteger} to reflect the correct value. The correct + * behavior is guaranteed if {@code 2^64} is added to negative values.

+ * + *

Java 8: In Java 8 the value should be treated as + * unsigned long using provided methods of type {@link Long}.

+ * + * @param property to be fetched. + * + * @return numerical property value + * + * @throws RocksDBException if an error happens in the underlying native code. + */ + public long getAggregatedLongProperty(final String property) throws RocksDBException { + return getAggregatedLongProperty(nativeHandle_, property, property.length()); + } + /** *

Return a heap-allocated iterator over the contents of the * database. The result of newIterator() is initially invalid @@ -2383,6 +2408,8 @@ public class RocksDB extends RocksObject { int propertyLength) throws RocksDBException; protected native long getLongProperty(long nativeHandle, long cfHandle, String property, int propertyLength) throws RocksDBException; + protected native long getAggregatedLongProperty(long nativeHandle, String property, + int propertyLength) throws RocksDBException; protected native long iterator(long handle); protected native long iterator(long handle, long readOptHandle); protected native long iteratorCF(long handle, long cfHandle); diff --git a/java/src/test/java/org/rocksdb/ColumnFamilyTest.java b/java/src/test/java/org/rocksdb/ColumnFamilyTest.java index 3df63c65f..0b943ac96 100644 --- a/java/src/test/java/org/rocksdb/ColumnFamilyTest.java +++ b/java/src/test/java/org/rocksdb/ColumnFamilyTest.java @@ -404,6 +404,10 @@ public class ColumnFamilyTest { "rocksdb.stats")).isNotNull(); assertThat(db.getProperty(columnFamilyHandleList.get(1), "rocksdb.sstables")).isNotNull(); + assertThat(db.getAggregatedLongProperty("rocksdb.estimate-num-keys")). + isNotNull(); + assertThat(db.getAggregatedLongProperty("rocksdb.estimate-num-keys")). + isGreaterThanOrEqualTo(0); } finally { for (final ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) {