2016-02-10 00:12:00 +01: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).
|
2014-08-03 22:11:44 +02:00
|
|
|
//
|
|
|
|
// This file implements the "bridge" between Java and C++ for
|
|
|
|
// rocksdb::Slice.
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <jni.h>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "include/org_rocksdb_AbstractSlice.h"
|
|
|
|
#include "include/org_rocksdb_Slice.h"
|
|
|
|
#include "include/org_rocksdb_DirectSlice.h"
|
|
|
|
#include "rocksdb/slice.h"
|
|
|
|
#include "rocksjni/portal.h"
|
|
|
|
|
2014-08-21 22:55:51 +02:00
|
|
|
// <editor-fold desc="org.rocksdb.AbstractSlice>
|
2014-08-03 22:11:44 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_AbstractSlice
|
|
|
|
* Method: createNewSliceFromString
|
2016-01-20 18:05:41 +01:00
|
|
|
* Signature: (Ljava/lang/String;)J
|
2014-08-03 22:11:44 +02:00
|
|
|
*/
|
2016-01-20 18:05:41 +01:00
|
|
|
jlong Java_org_rocksdb_AbstractSlice_createNewSliceFromString(
|
|
|
|
JNIEnv * env, jclass jcls, jstring jstr) {
|
2017-02-28 01:26:12 +01:00
|
|
|
const auto* str = env->GetStringUTFChars(jstr, nullptr);
|
|
|
|
if(str == nullptr) {
|
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-17 01:22:29 +01:00
|
|
|
const size_t len = strlen(str);
|
2017-02-28 01:26:12 +01:00
|
|
|
|
|
|
|
// NOTE: buf will be deleted in the
|
|
|
|
// Java_org_rocksdb_Slice_disposeInternalBuf or
|
|
|
|
// or Java_org_rocksdb_DirectSlice_disposeInternalBuf methods
|
2015-01-17 01:22:29 +01:00
|
|
|
char* buf = new char[len + 1];
|
|
|
|
memcpy(buf, str, len);
|
|
|
|
buf[len] = 0;
|
2014-12-10 00:37:08 +01:00
|
|
|
env->ReleaseStringUTFChars(jstr, str);
|
|
|
|
|
2014-12-18 22:15:00 +01:00
|
|
|
const auto* slice = new rocksdb::Slice(buf);
|
2016-01-20 18:05:41 +01:00
|
|
|
return reinterpret_cast<jlong>(slice);
|
2014-08-03 22:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_AbstractSlice
|
|
|
|
* Method: size0
|
|
|
|
* Signature: (J)I
|
|
|
|
*/
|
|
|
|
jint Java_org_rocksdb_AbstractSlice_size0(
|
|
|
|
JNIEnv* env, jobject jobj, jlong handle) {
|
2014-12-18 22:15:00 +01:00
|
|
|
const auto* slice = reinterpret_cast<rocksdb::Slice*>(handle);
|
2014-11-11 22:47:22 +01:00
|
|
|
return static_cast<jint>(slice->size());
|
2014-08-03 22:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_AbstractSlice
|
|
|
|
* Method: empty0
|
|
|
|
* Signature: (J)Z
|
|
|
|
*/
|
|
|
|
jboolean Java_org_rocksdb_AbstractSlice_empty0(
|
|
|
|
JNIEnv* env, jobject jobj, jlong handle) {
|
2014-12-18 22:15:00 +01:00
|
|
|
const auto* slice = reinterpret_cast<rocksdb::Slice*>(handle);
|
2014-08-03 22:11:44 +02:00
|
|
|
return slice->empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_AbstractSlice
|
|
|
|
* Method: toString0
|
|
|
|
* Signature: (JZ)Ljava/lang/String;
|
|
|
|
*/
|
|
|
|
jstring Java_org_rocksdb_AbstractSlice_toString0(
|
|
|
|
JNIEnv* env, jobject jobj, jlong handle, jboolean hex) {
|
2014-12-18 22:15:00 +01:00
|
|
|
const auto* slice = reinterpret_cast<rocksdb::Slice*>(handle);
|
2014-08-03 22:11:44 +02:00
|
|
|
const std::string s = slice->ToString(hex);
|
|
|
|
return env->NewStringUTF(s.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_AbstractSlice
|
|
|
|
* Method: compare0
|
|
|
|
* Signature: (JJ)I;
|
|
|
|
*/
|
|
|
|
jint Java_org_rocksdb_AbstractSlice_compare0(
|
|
|
|
JNIEnv* env, jobject jobj, jlong handle, jlong otherHandle) {
|
2014-12-18 22:15:00 +01:00
|
|
|
const auto* slice = reinterpret_cast<rocksdb::Slice*>(handle);
|
|
|
|
const auto* otherSlice =
|
2014-08-21 22:55:51 +02:00
|
|
|
reinterpret_cast<rocksdb::Slice*>(otherHandle);
|
2014-08-03 22:11:44 +02:00
|
|
|
return slice->compare(*otherSlice);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_AbstractSlice
|
|
|
|
* Method: startsWith0
|
|
|
|
* Signature: (JJ)Z;
|
|
|
|
*/
|
|
|
|
jboolean Java_org_rocksdb_AbstractSlice_startsWith0(
|
|
|
|
JNIEnv* env, jobject jobj, jlong handle, jlong otherHandle) {
|
2014-12-18 22:15:00 +01:00
|
|
|
const auto* slice = reinterpret_cast<rocksdb::Slice*>(handle);
|
|
|
|
const auto* otherSlice =
|
2014-08-21 22:55:51 +02:00
|
|
|
reinterpret_cast<rocksdb::Slice*>(otherHandle);
|
2014-08-03 22:11:44 +02:00
|
|
|
return slice->starts_with(*otherSlice);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_AbstractSlice
|
|
|
|
* Method: disposeInternal
|
|
|
|
* Signature: (J)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_AbstractSlice_disposeInternal(
|
|
|
|
JNIEnv* env, jobject jobj, jlong handle) {
|
|
|
|
delete reinterpret_cast<rocksdb::Slice*>(handle);
|
|
|
|
}
|
|
|
|
|
2014-08-21 22:55:51 +02:00
|
|
|
// </editor-fold>
|
2014-08-03 22:11:44 +02:00
|
|
|
|
2014-08-21 22:55:51 +02:00
|
|
|
// <editor-fold desc="org.rocksdb.Slice>
|
2014-08-03 22:11:44 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_Slice
|
|
|
|
* Method: createNewSlice0
|
2016-01-20 18:05:41 +01:00
|
|
|
* Signature: ([BI)J
|
2014-08-03 22:11:44 +02:00
|
|
|
*/
|
2016-01-20 18:05:41 +01:00
|
|
|
jlong Java_org_rocksdb_Slice_createNewSlice0(
|
|
|
|
JNIEnv * env, jclass jcls, jbyteArray data, jint offset) {
|
2014-08-03 22:11:44 +02:00
|
|
|
const jsize dataSize = env->GetArrayLength(data);
|
|
|
|
const int len = dataSize - offset;
|
|
|
|
|
2017-02-28 01:26:12 +01:00
|
|
|
// NOTE: buf will be deleted in the Java_org_rocksdb_Slice_disposeInternalBuf method
|
|
|
|
jbyte* buf = new jbyte[len];
|
|
|
|
env->GetByteArrayRegion(data, offset, len, buf);
|
|
|
|
if(env->ExceptionCheck()) {
|
|
|
|
// exception thrown: ArrayIndexOutOfBoundsException
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto* slice = new rocksdb::Slice((const char*)buf, len);
|
2016-01-20 18:05:41 +01:00
|
|
|
return reinterpret_cast<jlong>(slice);
|
2014-08-03 22:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_Slice
|
|
|
|
* Method: createNewSlice1
|
2016-01-20 18:05:41 +01:00
|
|
|
* Signature: ([B)J
|
2014-08-03 22:11:44 +02:00
|
|
|
*/
|
2016-01-20 18:05:41 +01:00
|
|
|
jlong Java_org_rocksdb_Slice_createNewSlice1(
|
|
|
|
JNIEnv * env, jclass jcls, jbyteArray data) {
|
2017-02-28 01:26:12 +01:00
|
|
|
jbyte* ptrData = env->GetByteArrayElements(data, nullptr);
|
|
|
|
if(ptrData == nullptr) {
|
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
return 0;
|
|
|
|
}
|
2014-12-10 00:37:08 +01:00
|
|
|
const int len = env->GetArrayLength(data) + 1;
|
2014-08-15 15:42:03 +02:00
|
|
|
|
2017-02-28 01:26:12 +01:00
|
|
|
// NOTE: buf will be deleted in the Java_org_rocksdb_Slice_disposeInternalBuf method
|
2016-01-20 18:05:41 +01:00
|
|
|
char* buf = new char[len];
|
2014-12-10 00:37:08 +01:00
|
|
|
memcpy(buf, ptrData, len - 1);
|
2017-02-28 01:26:12 +01:00
|
|
|
buf[len-1] = '\0';
|
2014-08-03 22:11:44 +02:00
|
|
|
|
2014-12-18 22:15:00 +01:00
|
|
|
const auto* slice =
|
2014-12-10 00:37:08 +01:00
|
|
|
new rocksdb::Slice(buf, len - 1);
|
2014-08-03 22:11:44 +02:00
|
|
|
|
2014-08-15 15:42:03 +02:00
|
|
|
env->ReleaseByteArrayElements(data, ptrData, JNI_ABORT);
|
2016-01-20 18:05:41 +01:00
|
|
|
|
|
|
|
return reinterpret_cast<jlong>(slice);
|
2014-08-03 22:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_Slice
|
|
|
|
* Method: data0
|
|
|
|
* Signature: (J)[B
|
|
|
|
*/
|
|
|
|
jbyteArray Java_org_rocksdb_Slice_data0(
|
|
|
|
JNIEnv* env, jobject jobj, jlong handle) {
|
2014-12-18 22:15:00 +01:00
|
|
|
const auto* slice = reinterpret_cast<rocksdb::Slice*>(handle);
|
2017-02-28 01:26:12 +01:00
|
|
|
const jsize len = static_cast<jsize>(slice->size());
|
2014-08-03 22:11:44 +02:00
|
|
|
const jbyteArray data = env->NewByteArray(len);
|
2017-02-28 01:26:12 +01:00
|
|
|
if(data == nullptr) {
|
|
|
|
// exception thrown: OutOfMemoryError
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-08-21 22:55:51 +02:00
|
|
|
env->SetByteArrayRegion(data, 0, len,
|
2017-03-23 02:05:39 +01:00
|
|
|
const_cast<jbyte*>(reinterpret_cast<const jbyte*>(slice->data())));
|
2017-02-28 01:26:12 +01:00
|
|
|
if(env->ExceptionCheck()) {
|
|
|
|
// exception thrown: ArrayIndexOutOfBoundsException
|
|
|
|
env->DeleteLocalRef(data);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-08-03 22:11:44 +02:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2017-02-28 01:26:12 +01:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_Slice
|
|
|
|
* Method: clear0
|
|
|
|
* Signature: (JZJ)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_Slice_clear0(
|
|
|
|
JNIEnv * env, jobject jobj, jlong handle, jboolean shouldRelease,
|
|
|
|
jlong internalBufferOffset) {
|
|
|
|
auto* slice = reinterpret_cast<rocksdb::Slice*>(handle);
|
|
|
|
if(shouldRelease == JNI_TRUE) {
|
|
|
|
const char* buf = slice->data_ - internalBufferOffset;
|
|
|
|
delete [] buf;
|
|
|
|
}
|
|
|
|
slice->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_Slice
|
|
|
|
* Method: removePrefix0
|
|
|
|
* Signature: (JI)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_Slice_removePrefix0(
|
|
|
|
JNIEnv * env, jobject jobj, jlong handle, jint length) {
|
|
|
|
auto* slice = reinterpret_cast<rocksdb::Slice*>(handle);
|
|
|
|
slice->remove_prefix(length);
|
|
|
|
}
|
|
|
|
|
2014-08-15 15:42:03 +02:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_Slice
|
|
|
|
* Method: disposeInternalBuf
|
2017-02-28 01:26:12 +01:00
|
|
|
* Signature: (JJ)V
|
2014-08-15 15:42:03 +02:00
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_Slice_disposeInternalBuf(
|
2017-02-28 01:26:12 +01:00
|
|
|
JNIEnv * env, jobject jobj, jlong handle, jlong internalBufferOffset) {
|
2014-12-18 22:15:00 +01:00
|
|
|
const auto* slice = reinterpret_cast<rocksdb::Slice*>(handle);
|
2017-02-28 01:26:12 +01:00
|
|
|
const char* buf = slice->data_ - internalBufferOffset;
|
|
|
|
delete [] buf;
|
2014-08-15 15:42:03 +02:00
|
|
|
}
|
|
|
|
|
2014-12-14 21:34:37 +01:00
|
|
|
// </editor-fold>
|
|
|
|
|
|
|
|
// <editor-fold desc="org.rocksdb.DirectSlice>
|
|
|
|
|
2014-08-03 22:11:44 +02:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_DirectSlice
|
|
|
|
* Method: createNewDirectSlice0
|
2016-01-20 18:05:41 +01:00
|
|
|
* Signature: (Ljava/nio/ByteBuffer;I)J
|
2014-08-03 22:11:44 +02:00
|
|
|
*/
|
2016-01-20 18:05:41 +01:00
|
|
|
jlong Java_org_rocksdb_DirectSlice_createNewDirectSlice0(
|
|
|
|
JNIEnv* env, jclass jcls, jobject data, jint length) {
|
2017-02-28 01:26:12 +01:00
|
|
|
assert(data != nullptr);
|
|
|
|
void* data_addr = env->GetDirectBufferAddress(data);
|
|
|
|
if(data_addr == nullptr) {
|
|
|
|
// error: memory region is undefined, given object is not a direct
|
|
|
|
// java.nio.Buffer, or JNI access to direct buffers is not supported by JVM
|
|
|
|
rocksdb::IllegalArgumentExceptionJni::ThrowNew(env,
|
|
|
|
rocksdb::Status::InvalidArgument(
|
|
|
|
"Could not access DirectBuffer"));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-18 22:15:00 +01:00
|
|
|
const auto* ptrData =
|
2017-02-28 01:26:12 +01:00
|
|
|
reinterpret_cast<char*>(data_addr);
|
2014-12-18 22:15:00 +01:00
|
|
|
const auto* slice = new rocksdb::Slice(ptrData, length);
|
2016-01-20 18:05:41 +01:00
|
|
|
return reinterpret_cast<jlong>(slice);
|
2014-08-03 22:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_DirectSlice
|
|
|
|
* Method: createNewDirectSlice1
|
2016-01-20 18:05:41 +01:00
|
|
|
* Signature: (Ljava/nio/ByteBuffer;)J
|
2014-08-03 22:11:44 +02:00
|
|
|
*/
|
2016-01-20 18:05:41 +01:00
|
|
|
jlong Java_org_rocksdb_DirectSlice_createNewDirectSlice1(
|
|
|
|
JNIEnv* env, jclass jcls, jobject data) {
|
2017-02-28 01:26:12 +01:00
|
|
|
void* data_addr = env->GetDirectBufferAddress(data);
|
|
|
|
if(data_addr == nullptr) {
|
|
|
|
// error: memory region is undefined, given object is not a direct
|
|
|
|
// java.nio.Buffer, or JNI access to direct buffers is not supported by JVM
|
|
|
|
rocksdb::IllegalArgumentExceptionJni::ThrowNew(env,
|
|
|
|
rocksdb::Status::InvalidArgument(
|
|
|
|
"Could not access DirectBuffer"));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto* ptrData = reinterpret_cast<char*>(data_addr);
|
2014-12-18 22:15:00 +01:00
|
|
|
const auto* slice = new rocksdb::Slice(ptrData);
|
2016-01-20 18:05:41 +01:00
|
|
|
return reinterpret_cast<jlong>(slice);
|
2014-08-03 22:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_DirectSlice
|
|
|
|
* Method: data0
|
|
|
|
* Signature: (J)Ljava/lang/Object;
|
|
|
|
*/
|
|
|
|
jobject Java_org_rocksdb_DirectSlice_data0(
|
|
|
|
JNIEnv* env, jobject jobj, jlong handle) {
|
2014-12-18 22:15:00 +01:00
|
|
|
const auto* slice = reinterpret_cast<rocksdb::Slice*>(handle);
|
2014-08-21 22:55:51 +02:00
|
|
|
return env->NewDirectByteBuffer(const_cast<char*>(slice->data()),
|
|
|
|
slice->size());
|
2014-08-03 22:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_DirectSlice
|
|
|
|
* Method: get0
|
|
|
|
* Signature: (JI)B
|
|
|
|
*/
|
|
|
|
jbyte Java_org_rocksdb_DirectSlice_get0(
|
|
|
|
JNIEnv* env, jobject jobj, jlong handle, jint offset) {
|
2014-12-18 22:15:00 +01:00
|
|
|
const auto* slice = reinterpret_cast<rocksdb::Slice*>(handle);
|
2014-12-18 22:27:50 +01:00
|
|
|
return (*slice)[offset];
|
2014-08-03 22:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_DirectSlice
|
|
|
|
* Method: clear0
|
2017-02-28 01:26:12 +01:00
|
|
|
* Signature: (JZJ)V
|
2014-08-03 22:11:44 +02:00
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_DirectSlice_clear0(
|
2017-02-28 01:26:12 +01:00
|
|
|
JNIEnv* env, jobject jobj, jlong handle,
|
|
|
|
jboolean shouldRelease, jlong internalBufferOffset) {
|
2014-12-18 22:27:50 +01:00
|
|
|
auto* slice = reinterpret_cast<rocksdb::Slice*>(handle);
|
2017-02-28 01:26:12 +01:00
|
|
|
if(shouldRelease == JNI_TRUE) {
|
|
|
|
const char* buf = slice->data_ - internalBufferOffset;
|
|
|
|
delete [] buf;
|
|
|
|
}
|
2014-08-03 22:11:44 +02:00
|
|
|
slice->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_DirectSlice
|
|
|
|
* Method: removePrefix0
|
|
|
|
* Signature: (JI)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_DirectSlice_removePrefix0(
|
|
|
|
JNIEnv* env, jobject jobj, jlong handle, jint length) {
|
2014-12-18 22:27:50 +01:00
|
|
|
auto* slice = reinterpret_cast<rocksdb::Slice*>(handle);
|
2014-08-03 22:11:44 +02:00
|
|
|
slice->remove_prefix(length);
|
|
|
|
}
|
|
|
|
|
2017-02-28 01:26:12 +01:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_DirectSlice
|
|
|
|
* Method: disposeInternalBuf
|
|
|
|
* Signature: (JJ)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_DirectSlice_disposeInternalBuf(
|
|
|
|
JNIEnv* env, jobject jobj, jlong handle, jlong internalBufferOffset) {
|
|
|
|
const auto* slice = reinterpret_cast<rocksdb::Slice*>(handle);
|
|
|
|
const char* buf = slice->data_ - internalBufferOffset;
|
|
|
|
delete [] buf;
|
|
|
|
}
|
|
|
|
|
2014-08-21 22:55:51 +02:00
|
|
|
// </editor-fold>
|