2014-08-03 20:11:44 +00:00
|
|
|
// Copyright (c) 2014, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
//
|
|
|
|
// 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 20:55:51 +00:00
|
|
|
// <editor-fold desc="org.rocksdb.AbstractSlice>
|
2014-08-03 20:11:44 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_AbstractSlice
|
|
|
|
* Method: createNewSliceFromString
|
|
|
|
* Signature: (Ljava/lang/String;)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_AbstractSlice_createNewSliceFromString(
|
2014-12-09 23:37:08 +00:00
|
|
|
JNIEnv* env, jobject jobj, jstring jstr) {
|
|
|
|
|
2014-12-18 21:15:00 +00:00
|
|
|
const auto* str = env->GetStringUTFChars(jstr, 0);
|
2015-01-17 00:22:29 +00:00
|
|
|
const size_t len = strlen(str);
|
|
|
|
char* buf = new char[len + 1];
|
|
|
|
memcpy(buf, str, len);
|
|
|
|
buf[len] = 0;
|
2014-12-09 23:37:08 +00:00
|
|
|
env->ReleaseStringUTFChars(jstr, str);
|
|
|
|
|
2014-12-18 21:15:00 +00:00
|
|
|
const auto* slice = new rocksdb::Slice(buf);
|
2014-08-03 20:11:44 +00:00
|
|
|
rocksdb::AbstractSliceJni::setHandle(env, jobj, slice);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_AbstractSlice
|
|
|
|
* Method: size0
|
|
|
|
* Signature: (J)I
|
|
|
|
*/
|
|
|
|
jint Java_org_rocksdb_AbstractSlice_size0(
|
|
|
|
JNIEnv* env, jobject jobj, jlong handle) {
|
2014-12-18 21:15:00 +00:00
|
|
|
const auto* slice = reinterpret_cast<rocksdb::Slice*>(handle);
|
2014-11-11 21:47:22 +00:00
|
|
|
return static_cast<jint>(slice->size());
|
2014-08-03 20:11:44 +00: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 21:15:00 +00:00
|
|
|
const auto* slice = reinterpret_cast<rocksdb::Slice*>(handle);
|
2014-08-03 20:11:44 +00: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 21:15:00 +00:00
|
|
|
const auto* slice = reinterpret_cast<rocksdb::Slice*>(handle);
|
2014-08-03 20:11:44 +00: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 21:15:00 +00:00
|
|
|
const auto* slice = reinterpret_cast<rocksdb::Slice*>(handle);
|
|
|
|
const auto* otherSlice =
|
2014-08-21 20:55:51 +00:00
|
|
|
reinterpret_cast<rocksdb::Slice*>(otherHandle);
|
2014-08-03 20:11:44 +00: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 21:15:00 +00:00
|
|
|
const auto* slice = reinterpret_cast<rocksdb::Slice*>(handle);
|
|
|
|
const auto* otherSlice =
|
2014-08-21 20:55:51 +00:00
|
|
|
reinterpret_cast<rocksdb::Slice*>(otherHandle);
|
2014-08-03 20:11:44 +00: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 20:55:51 +00:00
|
|
|
// </editor-fold>
|
2014-08-03 20:11:44 +00:00
|
|
|
|
2014-08-21 20:55:51 +00:00
|
|
|
// <editor-fold desc="org.rocksdb.Slice>
|
2014-08-03 20:11:44 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_Slice
|
|
|
|
* Method: createNewSlice0
|
|
|
|
* Signature: ([BI)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_Slice_createNewSlice0(
|
|
|
|
JNIEnv * env, jobject jobj, jbyteArray data, jint offset) {
|
|
|
|
|
|
|
|
const jsize dataSize = env->GetArrayLength(data);
|
|
|
|
const int len = dataSize - offset;
|
|
|
|
jbyte* ptrData = new jbyte[len];
|
|
|
|
env->GetByteArrayRegion(data, offset, len, ptrData);
|
|
|
|
|
2014-12-18 21:15:00 +00:00
|
|
|
const auto* slice = new rocksdb::Slice((const char*)ptrData, len);
|
2014-08-03 20:11:44 +00:00
|
|
|
rocksdb::AbstractSliceJni::setHandle(env, jobj, slice);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_Slice
|
|
|
|
* Method: createNewSlice1
|
|
|
|
* Signature: ([B)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_Slice_createNewSlice1(
|
|
|
|
JNIEnv * env, jobject jobj, jbyteArray data) {
|
|
|
|
|
2014-12-09 23:37:08 +00:00
|
|
|
const int len = env->GetArrayLength(data) + 1;
|
2014-08-15 13:42:03 +00:00
|
|
|
|
2014-08-03 20:11:44 +00:00
|
|
|
jboolean isCopy;
|
|
|
|
jbyte* ptrData = env->GetByteArrayElements(data, &isCopy);
|
2014-12-09 23:37:08 +00:00
|
|
|
char* buf = new char[len];
|
|
|
|
|
|
|
|
memcpy(buf, ptrData, len - 1);
|
|
|
|
buf[len-1]='\0';
|
2014-08-03 20:11:44 +00:00
|
|
|
|
2014-12-18 21:15:00 +00:00
|
|
|
const auto* slice =
|
2014-12-09 23:37:08 +00:00
|
|
|
new rocksdb::Slice(buf, len - 1);
|
2014-08-03 20:11:44 +00:00
|
|
|
|
2014-12-09 23:37:08 +00:00
|
|
|
rocksdb::AbstractSliceJni::setHandle(env, jobj, slice);
|
2014-08-15 13:42:03 +00:00
|
|
|
env->ReleaseByteArrayElements(data, ptrData, JNI_ABORT);
|
2014-08-21 20:55:51 +00:00
|
|
|
// NOTE: buf will be deleted in the org.rocksdb.Slice#dispose method
|
2014-08-03 20:11:44 +00: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 21:15:00 +00:00
|
|
|
const auto* slice = reinterpret_cast<rocksdb::Slice*>(handle);
|
2014-11-11 21:47:22 +00:00
|
|
|
const int len = static_cast<int>(slice->size());
|
2014-08-03 20:11:44 +00:00
|
|
|
const jbyteArray data = env->NewByteArray(len);
|
2014-08-21 20:55:51 +00:00
|
|
|
env->SetByteArrayRegion(data, 0, len,
|
2014-12-09 23:37:08 +00:00
|
|
|
reinterpret_cast<const jbyte*>(slice->data()));
|
2014-08-03 20:11:44 +00:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2014-08-15 13:42:03 +00:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_Slice
|
|
|
|
* Method: disposeInternalBuf
|
|
|
|
* Signature: (J)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_Slice_disposeInternalBuf(
|
|
|
|
JNIEnv * env, jobject jobj, jlong handle) {
|
2014-12-18 21:15:00 +00:00
|
|
|
const auto* slice = reinterpret_cast<rocksdb::Slice*>(handle);
|
2014-12-14 20:34:37 +00:00
|
|
|
delete [] slice->data_;
|
2014-08-15 13:42:03 +00:00
|
|
|
}
|
|
|
|
|
2014-12-14 20:34:37 +00:00
|
|
|
// </editor-fold>
|
|
|
|
|
|
|
|
// <editor-fold desc="org.rocksdb.DirectSlice>
|
|
|
|
|
2014-08-03 20:11:44 +00:00
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_DirectSlice
|
|
|
|
* Method: createNewDirectSlice0
|
|
|
|
* Signature: (Ljava/nio/ByteBuffer;I)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_DirectSlice_createNewDirectSlice0(
|
|
|
|
JNIEnv* env, jobject jobj, jobject data, jint length) {
|
2014-12-18 21:15:00 +00:00
|
|
|
const auto* ptrData =
|
2014-12-14 20:34:37 +00:00
|
|
|
reinterpret_cast<char*>(env->GetDirectBufferAddress(data));
|
2014-12-18 21:15:00 +00:00
|
|
|
const auto* slice = new rocksdb::Slice(ptrData, length);
|
2014-08-03 20:11:44 +00:00
|
|
|
rocksdb::AbstractSliceJni::setHandle(env, jobj, slice);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_DirectSlice
|
|
|
|
* Method: createNewDirectSlice1
|
|
|
|
* Signature: (Ljava/nio/ByteBuffer;)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_DirectSlice_createNewDirectSlice1(
|
|
|
|
JNIEnv* env, jobject jobj, jobject data) {
|
2014-12-18 21:15:00 +00:00
|
|
|
const auto* ptrData =
|
2014-08-21 20:55:51 +00:00
|
|
|
reinterpret_cast<char*>(env->GetDirectBufferAddress(data));
|
2014-12-18 21:15:00 +00:00
|
|
|
const auto* slice = new rocksdb::Slice(ptrData);
|
2014-08-03 20:11:44 +00:00
|
|
|
rocksdb::AbstractSliceJni::setHandle(env, jobj, slice);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 21:15:00 +00:00
|
|
|
const auto* slice = reinterpret_cast<rocksdb::Slice*>(handle);
|
2014-08-21 20:55:51 +00:00
|
|
|
return env->NewDirectByteBuffer(const_cast<char*>(slice->data()),
|
|
|
|
slice->size());
|
2014-08-03 20:11:44 +00: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 21:15:00 +00:00
|
|
|
const auto* slice = reinterpret_cast<rocksdb::Slice*>(handle);
|
2014-12-18 21:27:50 +00:00
|
|
|
return (*slice)[offset];
|
2014-08-03 20:11:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_rocksdb_DirectSlice
|
|
|
|
* Method: clear0
|
|
|
|
* Signature: (J)V
|
|
|
|
*/
|
|
|
|
void Java_org_rocksdb_DirectSlice_clear0(
|
|
|
|
JNIEnv* env, jobject jobj, jlong handle) {
|
2014-12-18 21:27:50 +00:00
|
|
|
auto* slice = reinterpret_cast<rocksdb::Slice*>(handle);
|
2014-08-21 20:55:51 +00:00
|
|
|
delete [] slice->data_;
|
2014-08-03 20:11:44 +00: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 21:27:50 +00:00
|
|
|
auto* slice = reinterpret_cast<rocksdb::Slice*>(handle);
|
2014-08-03 20:11:44 +00:00
|
|
|
slice->remove_prefix(length);
|
|
|
|
}
|
|
|
|
|
2014-08-21 20:55:51 +00:00
|
|
|
// </editor-fold>
|