2016-09-29 23:04:41 +02:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02:00
|
|
|
// 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).
|
2016-09-29 23:04:41 +02:00
|
|
|
//
|
|
|
|
// This file implements the "bridge" between Java and C++ and enables
|
2017-05-03 05:33:25 +02:00
|
|
|
// calling C++ rocksdb::SstFileWriter methods
|
2016-09-29 23:04:41 +02:00
|
|
|
// from Java side.
|
|
|
|
|
|
|
|
#include <jni.h>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "include/org_rocksdb_SstFileWriter.h"
|
|
|
|
#include "rocksdb/comparator.h"
|
|
|
|
#include "rocksdb/env.h"
|
|
|
|
#include "rocksdb/options.h"
|
|
|
|
#include "rocksdb/sst_file_writer.h"
|
|
|
|
#include "rocksjni/portal.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_SstFileWriter
|
|
|
|
* Method: newSstFileWriter
|
2018-03-08 20:16:46 +01:00
|
|
|
* Signature: (JJJB)J
|
2016-09-29 23:04:41 +02:00
|
|
|
*/
|
2018-04-13 02:55:14 +02:00
|
|
|
jlong Java_org_rocksdb_SstFileWriter_newSstFileWriter__JJJB(
|
|
|
|
JNIEnv * /*env*/, jclass /*jcls*/, jlong jenvoptions, jlong joptions,
|
|
|
|
jlong jcomparator_handle, jbyte jcomparator_type) {
|
2018-03-08 20:16:46 +01:00
|
|
|
rocksdb::Comparator *comparator = nullptr;
|
2018-04-13 02:55:14 +02:00
|
|
|
switch (jcomparator_type) {
|
|
|
|
// JAVA_COMPARATOR
|
|
|
|
case 0x0:
|
|
|
|
comparator = reinterpret_cast<rocksdb::ComparatorJniCallback *>(
|
|
|
|
jcomparator_handle);
|
|
|
|
break;
|
2018-03-08 20:16:46 +01:00
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
// JAVA_NATIVE_COMPARATOR_WRAPPER
|
Improve RocksJava Comparator (#6252)
Summary:
This is a redesign of the API for RocksJava comparators with the aim of improving performance. It also simplifies the class hierarchy.
**NOTE**: This breaks backwards compatibility for existing 3rd party Comparators implemented in Java... so we need to consider carefully which release branches this goes into.
Previously when implementing a comparator in Java the developer had a choice of subclassing either `DirectComparator` or `Comparator` which would use direct and non-direct byte-buffers resepectively (via `DirectSlice` and `Slice`).
In this redesign there we have eliminated the overhead of using the Java Slice classes, and just use `ByteBuffer`s. The `ComparatorOptions` supplied when constructing a Comparator allow you to choose between direct and non-direct byte buffers by setting `useDirect`.
In addition, the `ComparatorOptions` now allow you to choose whether a ByteBuffer is reused over multiple comparator calls, by setting `maxReusedBufferSize > 0`. When buffers are reused, ComparatorOptions provides a choice of mutex type by setting `useAdaptiveMutex`.
---
[JMH benchmarks previously indicated](https://github.com/facebook/rocksdb/pull/6241#issue-356398306) that the difference between C++ and Java for implementing a comparator was ~7x slowdown in Java.
With these changes, when reusing buffers and guarding access to them via mutexes the slowdown is approximately the same. However, these changes offer a new facility to not reuse mutextes, which reduces the slowdown to ~5.5x in Java. We also offer a `thread_local` mechanism for reusing buffers, which reduces slowdown to ~5.2x in Java (closes https://github.com/facebook/rocksdb/pull/4425).
These changes also form a good base for further optimisation work such as further JNI lookup caching, and JNI critical.
---
These numbers were captured without jemalloc. With jemalloc, the performance improves for all tests, and the Java slowdown reduces to between 4.8x and 5.x.
```
ComparatorBenchmarks.put native_bytewise thrpt 25 124483.795 ± 2032.443 ops/s
ComparatorBenchmarks.put native_reverse_bytewise thrpt 25 114414.536 ± 3486.156 ops/s
ComparatorBenchmarks.put java_bytewise_non-direct_reused-64_adaptive-mutex thrpt 25 17228.250 ± 1288.546 ops/s
ComparatorBenchmarks.put java_bytewise_non-direct_reused-64_non-adaptive-mutex thrpt 25 16035.865 ± 1248.099 ops/s
ComparatorBenchmarks.put java_bytewise_non-direct_reused-64_thread-local thrpt 25 21571.500 ± 871.521 ops/s
ComparatorBenchmarks.put java_bytewise_direct_reused-64_adaptive-mutex thrpt 25 23613.773 ± 8465.660 ops/s
ComparatorBenchmarks.put java_bytewise_direct_reused-64_non-adaptive-mutex thrpt 25 16768.172 ± 5618.489 ops/s
ComparatorBenchmarks.put java_bytewise_direct_reused-64_thread-local thrpt 25 23921.164 ± 8734.742 ops/s
ComparatorBenchmarks.put java_bytewise_non-direct_no-reuse thrpt 25 17899.684 ± 839.679 ops/s
ComparatorBenchmarks.put java_bytewise_direct_no-reuse thrpt 25 22148.316 ± 1215.527 ops/s
ComparatorBenchmarks.put java_reverse_bytewise_non-direct_reused-64_adaptive-mutex thrpt 25 11311.126 ± 820.602 ops/s
ComparatorBenchmarks.put java_reverse_bytewise_non-direct_reused-64_non-adaptive-mutex thrpt 25 11421.311 ± 807.210 ops/s
ComparatorBenchmarks.put java_reverse_bytewise_non-direct_reused-64_thread-local thrpt 25 11554.005 ± 960.556 ops/s
ComparatorBenchmarks.put java_reverse_bytewise_direct_reused-64_adaptive-mutex thrpt 25 22960.523 ± 1673.421 ops/s
ComparatorBenchmarks.put java_reverse_bytewise_direct_reused-64_non-adaptive-mutex thrpt 25 18293.317 ± 1434.601 ops/s
ComparatorBenchmarks.put java_reverse_bytewise_direct_reused-64_thread-local thrpt 25 24479.361 ± 2157.306 ops/s
ComparatorBenchmarks.put java_reverse_bytewise_non-direct_no-reuse thrpt 25 7942.286 ± 626.170 ops/s
ComparatorBenchmarks.put java_reverse_bytewise_direct_no-reuse thrpt 25 11781.955 ± 1019.843 ops/s
```
Pull Request resolved: https://github.com/facebook/rocksdb/pull/6252
Differential Revision: D19331064
Pulled By: pdillinger
fbshipit-source-id: 1f3b794e6a14162b2c3ffb943e8c0e64a0c03738
2020-02-03 21:28:25 +01:00
|
|
|
case 0x1:
|
2018-04-13 02:55:14 +02:00
|
|
|
comparator = reinterpret_cast<rocksdb::Comparator *>(jcomparator_handle);
|
|
|
|
break;
|
2018-03-08 20:16:46 +01:00
|
|
|
}
|
2016-09-29 23:04:41 +02:00
|
|
|
auto *env_options =
|
|
|
|
reinterpret_cast<const rocksdb::EnvOptions *>(jenvoptions);
|
|
|
|
auto *options = reinterpret_cast<const rocksdb::Options *>(joptions);
|
|
|
|
rocksdb::SstFileWriter *sst_file_writer =
|
|
|
|
new rocksdb::SstFileWriter(*env_options, *options, comparator);
|
|
|
|
return reinterpret_cast<jlong>(sst_file_writer);
|
|
|
|
}
|
|
|
|
|
2017-03-30 02:13:43 +02:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_SstFileWriter
|
|
|
|
* Method: newSstFileWriter
|
|
|
|
* Signature: (JJ)J
|
|
|
|
*/
|
2018-04-13 02:55:14 +02:00
|
|
|
jlong Java_org_rocksdb_SstFileWriter_newSstFileWriter__JJ(JNIEnv * /*env*/,
|
|
|
|
jclass /*jcls*/,
|
|
|
|
jlong jenvoptions,
|
|
|
|
jlong joptions) {
|
2017-03-30 02:13:43 +02:00
|
|
|
auto *env_options =
|
|
|
|
reinterpret_cast<const rocksdb::EnvOptions *>(jenvoptions);
|
|
|
|
auto *options = reinterpret_cast<const rocksdb::Options *>(joptions);
|
|
|
|
rocksdb::SstFileWriter *sst_file_writer =
|
|
|
|
new rocksdb::SstFileWriter(*env_options, *options);
|
|
|
|
return reinterpret_cast<jlong>(sst_file_writer);
|
|
|
|
}
|
|
|
|
|
2016-09-29 23:04:41 +02:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_SstFileWriter
|
|
|
|
* Method: open
|
|
|
|
* Signature: (JLjava/lang/String;)V
|
|
|
|
*/
|
2018-04-13 02:55:14 +02:00
|
|
|
void Java_org_rocksdb_SstFileWriter_open(JNIEnv *env, jobject /*jobj*/,
|
2016-09-29 23:04:41 +02:00
|
|
|
jlong jhandle, jstring jfile_path) {
|
2017-02-28 01:26:12 +01:00
|
|
|
const char *file_path = env->GetStringUTFChars(jfile_path, nullptr);
|
2018-04-13 02:55:14 +02:00
|
|
|
if (file_path == nullptr) {
|
2017-02-28 01:26:12 +01:00
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
return;
|
|
|
|
}
|
2016-09-29 23:04:41 +02:00
|
|
|
rocksdb::Status s =
|
|
|
|
reinterpret_cast<rocksdb::SstFileWriter *>(jhandle)->Open(file_path);
|
|
|
|
env->ReleaseStringUTFChars(jfile_path, file_path);
|
|
|
|
|
|
|
|
if (!s.ok()) {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-26 21:05:19 +02:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_SstFileWriter
|
|
|
|
* Method: put
|
|
|
|
* Signature: (JJJ)V
|
|
|
|
*/
|
2018-04-13 02:55:14 +02:00
|
|
|
void Java_org_rocksdb_SstFileWriter_put__JJJ(JNIEnv *env, jobject /*jobj*/,
|
2017-08-22 21:46:29 +02:00
|
|
|
jlong jhandle, jlong jkey_handle,
|
|
|
|
jlong jvalue_handle) {
|
2017-05-26 21:05:19 +02:00
|
|
|
auto *key_slice = reinterpret_cast<rocksdb::Slice *>(jkey_handle);
|
|
|
|
auto *value_slice = reinterpret_cast<rocksdb::Slice *>(jvalue_handle);
|
2018-04-13 02:55:14 +02:00
|
|
|
rocksdb::Status s = reinterpret_cast<rocksdb::SstFileWriter *>(jhandle)->Put(
|
|
|
|
*key_slice, *value_slice);
|
2017-05-26 21:05:19 +02:00
|
|
|
if (!s.ok()) {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-22 21:46:29 +02:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_SstFileWriter
|
|
|
|
* Method: put
|
|
|
|
* Signature: (JJJ)V
|
|
|
|
*/
|
2018-04-13 02:55:14 +02:00
|
|
|
void Java_org_rocksdb_SstFileWriter_put__J_3B_3B(JNIEnv *env, jobject /*jobj*/,
|
|
|
|
jlong jhandle, jbyteArray jkey,
|
|
|
|
jbyteArray jval) {
|
|
|
|
jbyte *key = env->GetByteArrayElements(jkey, nullptr);
|
|
|
|
if (key == nullptr) {
|
2017-08-22 21:46:29 +02:00
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
return;
|
|
|
|
}
|
2018-04-13 02:55:14 +02:00
|
|
|
rocksdb::Slice key_slice(reinterpret_cast<char *>(key),
|
|
|
|
env->GetArrayLength(jkey));
|
2017-08-22 21:46:29 +02:00
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
jbyte *value = env->GetByteArrayElements(jval, nullptr);
|
|
|
|
if (value == nullptr) {
|
2017-08-22 21:46:29 +02:00
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
env->ReleaseByteArrayElements(jkey, key, JNI_ABORT);
|
|
|
|
return;
|
|
|
|
}
|
2018-04-13 02:55:14 +02:00
|
|
|
rocksdb::Slice value_slice(reinterpret_cast<char *>(value),
|
|
|
|
env->GetArrayLength(jval));
|
2017-08-22 21:46:29 +02:00
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
rocksdb::Status s = reinterpret_cast<rocksdb::SstFileWriter *>(jhandle)->Put(
|
|
|
|
key_slice, value_slice);
|
2017-08-22 21:46:29 +02:00
|
|
|
|
|
|
|
env->ReleaseByteArrayElements(jkey, key, JNI_ABORT);
|
|
|
|
env->ReleaseByteArrayElements(jval, value, JNI_ABORT);
|
|
|
|
|
|
|
|
if (!s.ok()) {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-26 21:05:19 +02:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_SstFileWriter
|
|
|
|
* Method: merge
|
|
|
|
* Signature: (JJJ)V
|
|
|
|
*/
|
2018-04-13 02:55:14 +02:00
|
|
|
void Java_org_rocksdb_SstFileWriter_merge__JJJ(JNIEnv *env, jobject /*jobj*/,
|
2017-08-22 21:46:29 +02:00
|
|
|
jlong jhandle, jlong jkey_handle,
|
|
|
|
jlong jvalue_handle) {
|
2017-05-26 21:05:19 +02:00
|
|
|
auto *key_slice = reinterpret_cast<rocksdb::Slice *>(jkey_handle);
|
|
|
|
auto *value_slice = reinterpret_cast<rocksdb::Slice *>(jvalue_handle);
|
|
|
|
rocksdb::Status s =
|
2018-04-13 02:55:14 +02:00
|
|
|
reinterpret_cast<rocksdb::SstFileWriter *>(jhandle)->Merge(*key_slice,
|
|
|
|
*value_slice);
|
2017-05-26 21:05:19 +02:00
|
|
|
if (!s.ok()) {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-22 21:46:29 +02:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_SstFileWriter
|
|
|
|
* Method: merge
|
|
|
|
* Signature: (J[B[B)V
|
|
|
|
*/
|
2018-04-13 02:55:14 +02:00
|
|
|
void Java_org_rocksdb_SstFileWriter_merge__J_3B_3B(JNIEnv *env,
|
|
|
|
jobject /*jobj*/,
|
|
|
|
jlong jhandle,
|
|
|
|
jbyteArray jkey,
|
2017-08-22 21:46:29 +02:00
|
|
|
jbyteArray jval) {
|
2018-04-13 02:55:14 +02:00
|
|
|
jbyte *key = env->GetByteArrayElements(jkey, nullptr);
|
|
|
|
if (key == nullptr) {
|
2017-08-22 21:46:29 +02:00
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
return;
|
|
|
|
}
|
2018-04-13 02:55:14 +02:00
|
|
|
rocksdb::Slice key_slice(reinterpret_cast<char *>(key),
|
|
|
|
env->GetArrayLength(jkey));
|
2017-08-22 21:46:29 +02:00
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
jbyte *value = env->GetByteArrayElements(jval, nullptr);
|
|
|
|
if (value == nullptr) {
|
2017-08-22 21:46:29 +02:00
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
env->ReleaseByteArrayElements(jkey, key, JNI_ABORT);
|
|
|
|
return;
|
|
|
|
}
|
2018-04-13 02:55:14 +02:00
|
|
|
rocksdb::Slice value_slice(reinterpret_cast<char *>(value),
|
|
|
|
env->GetArrayLength(jval));
|
2017-08-22 21:46:29 +02:00
|
|
|
|
|
|
|
rocksdb::Status s =
|
2018-04-13 02:55:14 +02:00
|
|
|
reinterpret_cast<rocksdb::SstFileWriter *>(jhandle)->Merge(key_slice,
|
|
|
|
value_slice);
|
2017-08-22 21:46:29 +02:00
|
|
|
|
|
|
|
env->ReleaseByteArrayElements(jkey, key, JNI_ABORT);
|
|
|
|
env->ReleaseByteArrayElements(jval, value, JNI_ABORT);
|
|
|
|
|
|
|
|
if (!s.ok()) {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_SstFileWriter
|
|
|
|
* Method: delete
|
|
|
|
* Signature: (JJJ)V
|
|
|
|
*/
|
2018-04-13 02:55:14 +02:00
|
|
|
void Java_org_rocksdb_SstFileWriter_delete__J_3B(JNIEnv *env, jobject /*jobj*/,
|
|
|
|
jlong jhandle,
|
|
|
|
jbyteArray jkey) {
|
|
|
|
jbyte *key = env->GetByteArrayElements(jkey, nullptr);
|
|
|
|
if (key == nullptr) {
|
2017-08-22 21:46:29 +02:00
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
return;
|
|
|
|
}
|
2018-04-13 02:55:14 +02:00
|
|
|
rocksdb::Slice key_slice(reinterpret_cast<char *>(key),
|
|
|
|
env->GetArrayLength(jkey));
|
2017-08-22 21:46:29 +02:00
|
|
|
|
|
|
|
rocksdb::Status s =
|
2018-04-13 02:55:14 +02:00
|
|
|
reinterpret_cast<rocksdb::SstFileWriter *>(jhandle)->Delete(key_slice);
|
2017-08-22 21:46:29 +02:00
|
|
|
|
|
|
|
env->ReleaseByteArrayElements(jkey, key, JNI_ABORT);
|
|
|
|
|
|
|
|
if (!s.ok()) {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-26 21:05:19 +02:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_SstFileWriter
|
|
|
|
* Method: delete
|
|
|
|
* Signature: (JJJ)V
|
|
|
|
*/
|
2018-04-13 02:55:14 +02:00
|
|
|
void Java_org_rocksdb_SstFileWriter_delete__JJ(JNIEnv *env, jobject /*jobj*/,
|
|
|
|
jlong jhandle,
|
|
|
|
jlong jkey_handle) {
|
2017-05-26 21:05:19 +02:00
|
|
|
auto *key_slice = reinterpret_cast<rocksdb::Slice *>(jkey_handle);
|
|
|
|
rocksdb::Status s =
|
2018-04-13 02:55:14 +02:00
|
|
|
reinterpret_cast<rocksdb::SstFileWriter *>(jhandle)->Delete(*key_slice);
|
2017-05-26 21:05:19 +02:00
|
|
|
if (!s.ok()) {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-29 23:04:41 +02:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_SstFileWriter
|
|
|
|
* Method: finish
|
|
|
|
* Signature: (J)V
|
|
|
|
*/
|
2018-04-13 02:55:14 +02:00
|
|
|
void Java_org_rocksdb_SstFileWriter_finish(JNIEnv *env, jobject /*jobj*/,
|
2016-09-29 23:04:41 +02:00
|
|
|
jlong jhandle) {
|
|
|
|
rocksdb::Status s =
|
|
|
|
reinterpret_cast<rocksdb::SstFileWriter *>(jhandle)->Finish();
|
|
|
|
if (!s.ok()) {
|
|
|
|
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_SstFileWriter
|
|
|
|
* Method: disposeInternal
|
|
|
|
* Signature: (J)V
|
|
|
|
*/
|
2018-04-13 02:55:14 +02:00
|
|
|
void Java_org_rocksdb_SstFileWriter_disposeInternal(JNIEnv * /*env*/,
|
|
|
|
jobject /*jobj*/,
|
2016-09-29 23:04:41 +02:00
|
|
|
jlong jhandle) {
|
|
|
|
delete reinterpret_cast<rocksdb::SstFileWriter *>(jhandle);
|
|
|
|
}
|