Skip to content

Commit

Permalink
Add CompactRangeOptions for Java (facebook#4220)
Browse files Browse the repository at this point in the history
Summary:
Closes facebook#4195

CompactRangeOptions are available the CPP API, but not in the Java API. This PR adds CompactRangeOptions to the Java API and adds an overloaded compactRange() method. See facebook#4195 for the original discussion.

This change supports all fields of CompactRangeOptions, including the required enum converters in the JNI portal.

Significant changes:
- Make CompactRangeOptions available in the compactRange() for Java.
- Deprecate other compactRange() methods that have individual option params, like in the CPP code.
- Migrate rocksdb_compactrange_helper() to  CompactRangeOptions.
- Add Java unit tests for CompactRangeOptions.
Pull Request resolved: facebook#4220

Differential Revision: D9380007

Pulled By: sagar0

fbshipit-source-id: 6af6c334f221427f1997b33fb24c3986b092fed6
  • Loading branch information
Christian Esken authored and facebook-github-bot committed Aug 17, 2018
1 parent fa4de6e commit c7cf981
Show file tree
Hide file tree
Showing 9 changed files with 639 additions and 6 deletions.
3 changes: 3 additions & 0 deletions java/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ set(JNI_NATIVE_SOURCES
rocksjni/compaction_filter_factory_jnicallback.cc
rocksjni/compaction_options_fifo.cc
rocksjni/compaction_options_universal.cc
rocksjni/compact_range_options.cc
rocksjni/comparator.cc
rocksjni/comparatorjnicallback.cc
rocksjni/compression_options.cc
Expand Down Expand Up @@ -79,6 +80,7 @@ set(NATIVE_JAVA_CLASSES
org.rocksdb.ColumnFamilyOptions
org.rocksdb.CompactionOptionsFIFO
org.rocksdb.CompactionOptionsUniversal
org.rocksdb.CompactRangeOptions
org.rocksdb.Comparator
org.rocksdb.ComparatorOptions
org.rocksdb.CompressionOptions
Expand Down Expand Up @@ -192,6 +194,7 @@ add_jar(
src/main/java/org/rocksdb/CompactionOptionsFIFO.java
src/main/java/org/rocksdb/CompactionOptionsUniversal.java
src/main/java/org/rocksdb/CompactionPriority.java
src/main/java/org/rocksdb/CompactRangeOptions.java
src/main/java/org/rocksdb/CompactionStopStyle.java
src/main/java/org/rocksdb/CompactionStyle.java
src/main/java/org/rocksdb/Comparator.java
Expand Down
1 change: 1 addition & 0 deletions java/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ NATIVE_JAVA_CLASSES = org.rocksdb.AbstractCompactionFilter\
org.rocksdb.ColumnFamilyOptions\
org.rocksdb.CompactionOptionsFIFO\
org.rocksdb.CompactionOptionsUniversal\
org.rocksdb.CompactRangeOptions\
org.rocksdb.Comparator\
org.rocksdb.ComparatorOptions\
org.rocksdb.CompressionOptions\
Expand Down
196 changes: 196 additions & 0 deletions java/rocksjni/compact_range_options.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
// 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::CompactRangeOptions.

#include <jni.h>

#include "include/org_rocksdb_CompactRangeOptions.h"
#include "rocksdb/options.h"
#include "rocksjni/portal.h"

/*
* Class: org_rocksdb_CompactRangeOptions
* Method: newCompactRangeOptions
* Signature: ()J
*/
jlong Java_org_rocksdb_CompactRangeOptions_newCompactRangeOptions(
JNIEnv* /*env*/, jclass /*jclazz*/) {
auto* options = new rocksdb::CompactRangeOptions();
return reinterpret_cast<jlong>(options);
}


/*
* Class: org_rocksdb_CompactRangeOptions
* Method: exclusiveManualCompaction
* Signature: (J)Z
*/
jboolean Java_org_rocksdb_CompactRangeOptions_exclusiveManualCompaction(
JNIEnv* /*env*/, jobject /*jobj*/, jlong jhandle) {
auto* options = reinterpret_cast<rocksdb::CompactRangeOptions*>(jhandle);
return static_cast<jboolean>(options->exclusive_manual_compaction);
}

/*
* Class: org_rocksdb_CompactRangeOptions
* Method: setExclusiveManualCompaction
* Signature: (JZ)V
*/
void Java_org_rocksdb_CompactRangeOptions_setExclusiveManualCompaction(
JNIEnv* /*env*/, jobject /*jobj*/, jlong jhandle, jboolean exclusive_manual_compaction) {
auto* options =
reinterpret_cast<rocksdb::CompactRangeOptions*>(jhandle);
options->exclusive_manual_compaction = static_cast<bool>(exclusive_manual_compaction);
}


/*
* Class: org_rocksdb_CompactRangeOptions
* Method: bottommostLevelCompaction
* Signature: (J)I
*/
jint Java_org_rocksdb_CompactRangeOptions_bottommostLevelCompaction(
JNIEnv* /*env*/, jobject /*jobj*/, jlong jhandle) {
auto* options = reinterpret_cast<rocksdb::CompactRangeOptions*>(jhandle);
return rocksdb::BottommostLevelCompactionJni::toJavaBottommostLevelCompaction(
options->bottommost_level_compaction);
}

/*
* Class: org_rocksdb_CompactRangeOptions
* Method: setBottommostLevelCompaction
* Signature: (JI)V
*/
void Java_org_rocksdb_CompactRangeOptions_setBottommostLevelCompaction(
JNIEnv* /*env*/, jobject /*jobj*/, jlong jhandle,
jint bottommost_level_compaction) {
auto* options = reinterpret_cast<rocksdb::CompactRangeOptions*>(jhandle);
options->bottommost_level_compaction =
rocksdb::BottommostLevelCompactionJni::toCppBottommostLevelCompaction(bottommost_level_compaction);
}

/*
* Class: org_rocksdb_CompactRangeOptions
* Method: changeLevel
* Signature: (J)Z
*/
jboolean Java_org_rocksdb_CompactRangeOptions_changeLevel
(JNIEnv* /*env*/, jobject /*jobj*/, jlong jhandle) {
auto* options = reinterpret_cast<rocksdb::CompactRangeOptions*>(jhandle);
return static_cast<jboolean>(options->change_level);
}

/*
* Class: org_rocksdb_CompactRangeOptions
* Method: setChangeLevel
* Signature: (JZ)V
*/
void Java_org_rocksdb_CompactRangeOptions_setChangeLevel
(JNIEnv* /*env*/, jobject /*jobj*/, jlong jhandle, jboolean change_level) {
auto* options = reinterpret_cast<rocksdb::CompactRangeOptions*>(jhandle);
options->change_level = static_cast<bool>(change_level);
}

/*
* Class: org_rocksdb_CompactRangeOptions
* Method: targetLevel
* Signature: (J)I
*/
jint Java_org_rocksdb_CompactRangeOptions_targetLevel
(JNIEnv* /*env*/, jobject /*jobj*/, jlong jhandle) {
auto* options = reinterpret_cast<rocksdb::CompactRangeOptions*>(jhandle);
return static_cast<jint>(options->target_level);
}

/*
* Class: org_rocksdb_CompactRangeOptions
* Method: setTargetLevel
* Signature: (JI)V
*/
void Java_org_rocksdb_CompactRangeOptions_setTargetLevel
(JNIEnv* /*env*/, jobject /*jobj*/, jlong jhandle, jint target_level) {
auto* options = reinterpret_cast<rocksdb::CompactRangeOptions*>(jhandle);
options->target_level = static_cast<int>(target_level);
}

/*
* Class: org_rocksdb_CompactRangeOptions
* Method: targetPathId
* Signature: (J)I
*/
jint Java_org_rocksdb_CompactRangeOptions_targetPathId
(JNIEnv* /*env*/, jobject /*jobj*/, jlong jhandle) {
auto* options = reinterpret_cast<rocksdb::CompactRangeOptions*>(jhandle);
return static_cast<jint>(options->target_path_id);
}

/*
* Class: org_rocksdb_CompactRangeOptions
* Method: setTargetPathId
* Signature: (JI)V
*/
void Java_org_rocksdb_CompactRangeOptions_setTargetPathId
(JNIEnv* /*env*/, jobject /*jobj*/, jlong jhandle, jint target_path_id) {
auto* options = reinterpret_cast<rocksdb::CompactRangeOptions*>(jhandle);
options->target_path_id = static_cast<uint32_t>(target_path_id);
}

/*
* Class: org_rocksdb_CompactRangeOptions
* Method: allowWriteStall
* Signature: (J)Z
*/
jboolean Java_org_rocksdb_CompactRangeOptions_allowWriteStall
(JNIEnv* /*env*/, jobject /*jobj*/, jlong jhandle) {
auto* options = reinterpret_cast<rocksdb::CompactRangeOptions*>(jhandle);
return static_cast<jboolean>(options->allow_write_stall);
}

/*
* Class: org_rocksdb_CompactRangeOptions
* Method: setAllowWriteStall
* Signature: (JZ)V
*/
void Java_org_rocksdb_CompactRangeOptions_setAllowWriteStall
(JNIEnv* /*env*/, jobject /*jobj*/, jlong jhandle, jboolean allow_write_stall) {
auto* options = reinterpret_cast<rocksdb::CompactRangeOptions*>(jhandle);
options->allow_write_stall = static_cast<bool>(allow_write_stall);
}


/*
* Class: org_rocksdb_CompactRangeOptions
* Method: maxSubcompactions
* Signature: (J)I
*/
jint Java_org_rocksdb_CompactRangeOptions_maxSubcompactions
(JNIEnv* /*env*/, jobject /*jobj*/, jlong jhandle) {
auto* options = reinterpret_cast<rocksdb::CompactRangeOptions*>(jhandle);
return static_cast<jint>(options->max_subcompactions);
}

/*
* Class: org_rocksdb_CompactRangeOptions
* Method: setMaxSubcompactions
* Signature: (JI)V
*/
void Java_org_rocksdb_CompactRangeOptions_setMaxSubcompactions
(JNIEnv* /*env*/, jobject /*jobj*/, jlong jhandle, jint max_subcompactions) {
auto* options = reinterpret_cast<rocksdb::CompactRangeOptions*>(jhandle);
options->max_subcompactions = static_cast<uint32_t>(max_subcompactions);
}

/*
* Class: org_rocksdb_CompactRangeOptions
* Method: disposeInternal
* Signature: (J)V
*/
void Java_org_rocksdb_CompactRangeOptions_disposeInternal(
JNIEnv* /*env*/, jobject /*jobj*/, jlong jhandle) {
auto* options = reinterpret_cast<rocksdb::CompactRangeOptions*>(jhandle);
delete options;
}
37 changes: 37 additions & 0 deletions java/rocksjni/portal.h
Original file line number Diff line number Diff line change
Expand Up @@ -2896,6 +2896,43 @@ class BatchResultJni : public JavaClass {
}
};

// The portal class for org.rocksdb.BottommostLevelCompaction
class BottommostLevelCompactionJni {
public:
// Returns the equivalent org.rocksdb.BottommostLevelCompaction for the provided
// C++ rocksdb::BottommostLevelCompaction enum
static jint toJavaBottommostLevelCompaction(
const rocksdb::BottommostLevelCompaction& bottommost_level_compaction) {
switch(bottommost_level_compaction) {
case rocksdb::BottommostLevelCompaction::kSkip:
return 0x0;
case rocksdb::BottommostLevelCompaction::kIfHaveCompactionFilter:
return 0x1;
case rocksdb::BottommostLevelCompaction::kForce:
return 0x2;
default:
return 0x7F; // undefined
}
}

// Returns the equivalent C++ rocksdb::BottommostLevelCompaction enum for the
// provided Java org.rocksdb.BottommostLevelCompaction
static rocksdb::BottommostLevelCompaction toCppBottommostLevelCompaction(
jint bottommost_level_compaction) {
switch(bottommost_level_compaction) {
case 0x0:
return rocksdb::BottommostLevelCompaction::kSkip;
case 0x1:
return rocksdb::BottommostLevelCompaction::kIfHaveCompactionFilter;
case 0x2:
return rocksdb::BottommostLevelCompaction::kForce;
default:
// undefined/default
return rocksdb::BottommostLevelCompaction::kIfHaveCompactionFilter;
}
}
};

// The portal class for org.rocksdb.CompactionStopStyle
class CompactionStopStyleJni {
public:
Expand Down
40 changes: 34 additions & 6 deletions java/rocksjni/rocksjni.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1955,8 +1955,7 @@ bool rocksdb_compactrange_helper(JNIEnv* env, rocksdb::DB* db,
rocksdb::ColumnFamilyHandle* cf_handle,
jbyteArray jbegin, jint jbegin_len,
jbyteArray jend, jint jend_len,
jboolean jreduce_level, jint jtarget_level,
jint jtarget_path_id) {
const rocksdb::CompactRangeOptions& compact_options) {
jbyte* begin = env->GetByteArrayElements(jbegin, nullptr);
if (begin == nullptr) {
// exception thrown: OutOfMemoryError
Expand All @@ -1974,10 +1973,6 @@ bool rocksdb_compactrange_helper(JNIEnv* env, rocksdb::DB* db,
const rocksdb::Slice end_slice(reinterpret_cast<char*>(end), jend_len);

rocksdb::Status s;
rocksdb::CompactRangeOptions compact_options;
compact_options.change_level = jreduce_level;
compact_options.target_level = jtarget_level;
compact_options.target_path_id = static_cast<uint32_t>(jtarget_path_id);
if (cf_handle != nullptr) {
s = db->CompactRange(compact_options, cf_handle, &begin_slice, &end_slice);
} else {
Expand All @@ -1996,6 +1991,25 @@ bool rocksdb_compactrange_helper(JNIEnv* env, rocksdb::DB* db,
return false;
}

/**
* @return true if the compact range succeeded, false if a Java Exception
* was thrown
*/
bool rocksdb_compactrange_helper(JNIEnv* env, rocksdb::DB* db,
rocksdb::ColumnFamilyHandle* cf_handle,
jbyteArray jbegin, jint jbegin_len,
jbyteArray jend, jint jend_len,
jboolean jreduce_level, jint jtarget_level,
jint jtarget_path_id) {
rocksdb::CompactRangeOptions compact_options;
compact_options.change_level = jreduce_level;
compact_options.target_level = jtarget_level;
compact_options.target_path_id = static_cast<uint32_t>(jtarget_path_id);

return rocksdb_compactrange_helper(env, db, cf_handle, jbegin, jbegin_len,
jend, jend_len, compact_options);
}

/*
* Class: org_rocksdb_RocksDB
* Method: compactRange0
Expand Down Expand Up @@ -2027,6 +2041,20 @@ void Java_org_rocksdb_RocksDB_compactRange__J_3BI_3BIZIIJ(
jtarget_path_id);
}


void Java_org_rocksdb_RocksDB_compactRange__J_3BI_3BIJJ(
JNIEnv* env, jobject /*jdb*/, jlong jdb_handle, jbyteArray jbegin,
jint jbegin_len, jbyteArray jend, jint jend_len,
jlong jcompact_options_handle, jlong jcf_handle) {
auto* db = reinterpret_cast<rocksdb::DB*>(jdb_handle);
auto* cf_handle = reinterpret_cast<rocksdb::ColumnFamilyHandle*>(jcf_handle);
auto* compact_options = reinterpret_cast<rocksdb::CompactRangeOptions*>(jcompact_options_handle);

rocksdb_compactrange_helper(env, db, cf_handle, jbegin, jbegin_len, jend,
jend_len, *compact_options);
}


//////////////////////////////////////////////////////////////////////////////
// rocksdb::DB::PauseBackgroundWork

Expand Down
Loading

0 comments on commit c7cf981

Please sign in to comment.