Skip to content

Commit

Permalink
Add event listeners to RocksJava (facebook#7425)
Browse files Browse the repository at this point in the history
Summary:
Allows adding event listeners in RocksJava.

* Adds listeners getter and setter in `Options` and `DBOptions` classes.
* Adds `EventListener` Java interface and base class for implementing custom event listener callbacks - `AbstractEventListener`, which has an underlying native callback class implementing C++ `EventListener` class.
* `AbstractEventListener` class has mechanism for selectively enabling its callback methods in order to prevent invoking Java method if it is not implemented. This decreases performance cost in case only subset of event listener callback methods is needed - the JNI code for remaining "no-op" callbacks is not executed.
* The code is covered by unit tests in `EventListenerTest.java`, there are also tests added for setting/getting listeners field in `OptionsTest.java` and `DBOptionsTest.java`.

Pull Request resolved: facebook#7425

Reviewed By: pdillinger

Differential Revision: D24063390

Pulled By: jay-zhuang

fbshipit-source-id: 508c359538983d6b765e70d9989c351794a944ee
  • Loading branch information
alucarded authored and facebook-github-bot committed Oct 14, 2020
1 parent b99fe1a commit 6528ecc
Show file tree
Hide file tree
Showing 36 changed files with 4,324 additions and 68 deletions.
17 changes: 17 additions & 0 deletions java/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ set(JNI_NATIVE_SOURCES
rocksjni/config_options.cc
rocksjni/env.cc
rocksjni/env_options.cc
rocksjni/event_listener.cc
rocksjni/event_listener_jnicallback.cc
rocksjni/filter.cc
rocksjni/ingest_external_file_options.cc
rocksjni/iterator.cc
Expand Down Expand Up @@ -87,6 +89,7 @@ set(JAVA_MAIN_CLASSES
src/main/java/org/rocksdb/AbstractCompactionFilter.java
src/main/java/org/rocksdb/AbstractCompactionFilterFactory.java
src/main/java/org/rocksdb/AbstractComparator.java
src/main/java/org/rocksdb/AbstractEventListener.java
src/main/java/org/rocksdb/AbstractImmutableNativeReference.java
src/main/java/org/rocksdb/AbstractMutableOptions.java
src/main/java/org/rocksdb/AbstractNativeReference.java
Expand All @@ -100,6 +103,7 @@ set(JAVA_MAIN_CLASSES
src/main/java/org/rocksdb/AccessHint.java
src/main/java/org/rocksdb/AdvancedColumnFamilyOptionsInterface.java
src/main/java/org/rocksdb/AdvancedMutableColumnFamilyOptionsInterface.java
src/main/java/org/rocksdb/BackgroundErrorReason.java
src/main/java/org/rocksdb/BackupableDBOptions.java
src/main/java/org/rocksdb/BackupEngine.java
src/main/java/org/rocksdb/BackupInfo.java
Expand Down Expand Up @@ -140,8 +144,13 @@ set(JAVA_MAIN_CLASSES
src/main/java/org/rocksdb/EncodingType.java
src/main/java/org/rocksdb/Env.java
src/main/java/org/rocksdb/EnvOptions.java
src/main/java/org/rocksdb/EventListener.java
src/main/java/org/rocksdb/Experimental.java
src/main/java/org/rocksdb/ExternalFileIngestionInfo.java
src/main/java/org/rocksdb/Filter.java
src/main/java/org/rocksdb/FileOperationInfo.java
src/main/java/org/rocksdb/FlushJobInfo.java
src/main/java/org/rocksdb/FlushReason.java
src/main/java/org/rocksdb/FlushOptions.java
src/main/java/org/rocksdb/HashLinkedListMemTableConfig.java
src/main/java/org/rocksdb/HashSkipListMemTableConfig.java
Expand All @@ -163,6 +172,7 @@ set(JAVA_MAIN_CLASSES
src/main/java/org/rocksdb/MemoryUsageType.java
src/main/java/org/rocksdb/MemoryUtil.java
src/main/java/org/rocksdb/MemTableConfig.java
src/main/java/org/rocksdb/MemTableInfo.java
src/main/java/org/rocksdb/MergeOperator.java
src/main/java/org/rocksdb/MutableColumnFamilyOptions.java
src/main/java/org/rocksdb/MutableColumnFamilyOptionsInterface.java
Expand Down Expand Up @@ -218,6 +228,10 @@ set(JAVA_MAIN_CLASSES
src/main/java/org/rocksdb/StatsLevel.java
src/main/java/org/rocksdb/Status.java
src/main/java/org/rocksdb/StringAppendOperator.java
src/main/java/org/rocksdb/TableFileCreationBriefInfo.java
src/main/java/org/rocksdb/TableFileCreationInfo.java
src/main/java/org/rocksdb/TableFileCreationReason.java
src/main/java/org/rocksdb/TableFileDeletionInfo.java
src/main/java/org/rocksdb/TableFilter.java
src/main/java/org/rocksdb/TableProperties.java
src/main/java/org/rocksdb/TableFormatConfig.java
Expand Down Expand Up @@ -247,6 +261,8 @@ set(JAVA_MAIN_CLASSES
src/main/java/org/rocksdb/WriteBatchWithIndex.java
src/main/java/org/rocksdb/WriteOptions.java
src/main/java/org/rocksdb/WriteBufferManager.java
src/main/java/org/rocksdb/WriteStallCondition.java
src/main/java/org/rocksdb/WriteStallInfo.java
src/main/java/org/rocksdb/util/ByteUtil.java
src/main/java/org/rocksdb/util/BytewiseComparator.java
src/main/java/org/rocksdb/util/Environment.java
Expand Down Expand Up @@ -391,6 +407,7 @@ if(${CMAKE_VERSION} VERSION_LESS "3.11.4" OR (${Java_VERSION_MINOR} STREQUAL "7"
org.rocksdb.AbstractCompactionFilter
org.rocksdb.AbstractCompactionFilterFactory
org.rocksdb.AbstractComparator
org.rocksdb.AbstractEventListener
org.rocksdb.AbstractImmutableNativeReference
org.rocksdb.AbstractNativeReference
org.rocksdb.AbstractRocksIterator
Expand Down
2 changes: 2 additions & 0 deletions java/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ NATIVE_JAVA_CLASSES = \
org.rocksdb.AbstractCompactionFilter\
org.rocksdb.AbstractCompactionFilterFactory\
org.rocksdb.AbstractComparator\
org.rocksdb.AbstractEventListener\
org.rocksdb.AbstractSlice\
org.rocksdb.AbstractTableFilter\
org.rocksdb.AbstractTraceWriter\
Expand Down Expand Up @@ -129,6 +130,7 @@ JAVA_TESTS = \
org.rocksdb.DirectSliceTest\
org.rocksdb.util.EnvironmentTest\
org.rocksdb.EnvOptionsTest\
org.rocksdb.EventListenerTest\
org.rocksdb.HdfsEnvTest\
org.rocksdb.IngestExternalFileOptionsTest\
org.rocksdb.util.IntComparatorTest\
Expand Down
43 changes: 43 additions & 0 deletions java/rocksjni/event_listener.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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::EventListener.

#include <jni.h>

#include <memory>

#include "include/org_rocksdb_AbstractEventListener.h"
#include "rocksjni/event_listener_jnicallback.h"
#include "rocksjni/portal.h"

/*
* Class: org_rocksdb_AbstractEventListener
* Method: createNewEventListener
* Signature: (J)J
*/
jlong Java_org_rocksdb_AbstractEventListener_createNewEventListener(
JNIEnv* env, jobject jobj, jlong jenabled_event_callback_values) {
auto enabled_event_callbacks =
ROCKSDB_NAMESPACE::EnabledEventCallbackJni::toCppEnabledEventCallbacks(
jenabled_event_callback_values);
auto* sptr_event_listener =
new std::shared_ptr<ROCKSDB_NAMESPACE::EventListener>(
new ROCKSDB_NAMESPACE::EventListenerJniCallback(
env, jobj, enabled_event_callbacks));
return reinterpret_cast<jlong>(sptr_event_listener);
}

/*
* Class: org_rocksdb_AbstractEventListener
* Method: disposeInternal
* Signature: (J)V
*/
void Java_org_rocksdb_AbstractEventListener_disposeInternal(JNIEnv*, jobject,
jlong jhandle) {
delete reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::EventListener>*>(
jhandle);
}
Loading

0 comments on commit 6528ecc

Please sign in to comment.