support getUsage and getPinnedUsage in JavaAPI for Cache (#7925)

Summary:
support getUsage and getPinnedUsage in JavaAPI for Cache
also fix a typo in LRUCacheTest.java that the highPriPoolRatio is not valid(set 5, I guess it means 0.05)

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

Reviewed By: mrambacher

Differential Revision: D26900241

Pulled By: ajkr

fbshipit-source-id: 735d1e40a16fa8919c89c7c7154ba7f81208ec33
main
Xiaopeng Zhang 3 years ago committed by Facebook GitHub Bot
parent 326670d265
commit c603f2f898
  1. 2
      java/CMakeLists.txt
  2. 1
      java/Makefile
  3. 35
      java/rocksjni/cache.cc
  4. 27
      java/src/main/java/org/rocksdb/Cache.java
  5. 16
      java/src/test/java/org/rocksdb/LRUCacheTest.java
  6. 1
      src.mk

@ -13,6 +13,7 @@ set(JNI_NATIVE_SOURCES
rocksjni/cassandra_value_operator.cc
rocksjni/checkpoint.cc
rocksjni/clock_cache.cc
rocksjni/cache.cc
rocksjni/columnfamilyhandle.cc
rocksjni/compaction_filter.cc
rocksjni/compaction_filter_factory.cc
@ -426,6 +427,7 @@ if(${CMAKE_VERSION} VERSION_LESS "3.11.4" OR (${Java_VERSION_MINOR} STREQUAL "7"
org.rocksdb.CassandraValueMergeOperator
org.rocksdb.Checkpoint
org.rocksdb.ClockCache
org.rocksdb.Cache
org.rocksdb.ColumnFamilyHandle
org.rocksdb.ColumnFamilyOptions
org.rocksdb.CompactionJobInfo

@ -14,6 +14,7 @@ NATIVE_JAVA_CLASSES = \
org.rocksdb.BloomFilter\
org.rocksdb.Checkpoint\
org.rocksdb.ClockCache\
org.rocksdb.Cache\
org.rocksdb.CassandraCompactionFilter\
org.rocksdb.CassandraValueMergeOperator\
org.rocksdb.ColumnFamilyHandle\

@ -0,0 +1,35 @@
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
// This source code is licensed under both the GPLv2 (found in the
// COPYING file in the root directory) and Apache 2.0 License
// (found in the LICENSE.Apache file in the root directory).
//
// This file implements the "bridge" between Java and C++ for
// ROCKSDB_NAMESPACE::Cache.
#include "rocksdb/cache.h"
#include <jni.h>
#include "include/org_rocksdb_Cache.h"
/*
* Class: org_rocksdb_Cache
* Method: getUsage
* Signature: (J)J
*/
jlong Java_org_rocksdb_Cache_getUsage(JNIEnv*, jclass, jlong jhandle) {
auto* sptr_cache =
reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Cache>*>(jhandle);
return static_cast<jlong>(sptr_cache->get()->GetUsage());
}
/*
* Class: org_rocksdb_Cache
* Method: getPinnedUsage
* Signature: (J)J
*/
jlong Java_org_rocksdb_Cache_getPinnedUsage(JNIEnv*, jclass, jlong jhandle) {
auto* sptr_cache =
reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::Cache>*>(jhandle);
return static_cast<jlong>(sptr_cache->get()->GetPinnedUsage());
}

@ -10,4 +10,31 @@ public abstract class Cache extends RocksObject {
protected Cache(final long nativeHandle) {
super(nativeHandle);
}
/**
* Returns the memory size for the entries
* residing in cache.
*
* @return cache usage size.
*
*/
public long getUsage() {
assert (isOwningHandle());
return getUsage(this.nativeHandle_);
}
/**
* Returns the memory size for the entries
* being pinned in cache.
*
* @return cache pinned usage size.
*
*/
public long getPinnedUsage() {
assert (isOwningHandle());
return getPinnedUsage(this.nativeHandle_);
}
private native static long getUsage(final long handle);
private native static long getPinnedUsage(final long handle);
}

@ -5,23 +5,27 @@
package org.rocksdb;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.ClassRule;
import org.junit.Test;
public class LRUCacheTest {
static {
RocksDB.loadLibrary();
}
@ClassRule
public static final RocksNativeLibraryResource ROCKS_NATIVE_LIBRARY_RESOURCE =
new RocksNativeLibraryResource();
@Test
public void newLRUCache() {
final long capacity = 1000;
final long capacity = 80000000;
final int numShardBits = 16;
final boolean strictCapacityLimit = true;
final double highPriPoolRatio = 5;
final double highPriPoolRatio = 0.05;
try(final Cache lruCache = new LRUCache(capacity,
numShardBits, strictCapacityLimit, highPriPoolRatio)) {
//no op
assertThat(lruCache.getUsage()).isGreaterThanOrEqualTo(0);
assertThat(lruCache.getPinnedUsage()).isGreaterThanOrEqualTo(0);
}
}
}

@ -562,6 +562,7 @@ JNI_NATIVE_SOURCES = \
java/rocksjni/backupablejni.cc \
java/rocksjni/checkpoint.cc \
java/rocksjni/clock_cache.cc \
java/rocksjni/cache.cc \
java/rocksjni/columnfamilyhandle.cc \
java/rocksjni/compact_range_options.cc \
java/rocksjni/compaction_filter.cc \

Loading…
Cancel
Save