@ -25,7 +25,7 @@
* Method : open
* Signature : ( JLjava / lang / String ; ) V
*/
void Java_org_rocksdb_RocksDB_open (
void Java_org_rocksdb_RocksDB_open__JLjava_lang_String_2 (
JNIEnv * env , jobject jdb , jlong jopt_handle , jstring jdb_path ) {
auto opt = reinterpret_cast < rocksdb : : Options * > ( jopt_handle ) ;
rocksdb : : DB * db = nullptr ;
@ -40,12 +40,215 @@ void Java_org_rocksdb_RocksDB_open(
rocksdb : : RocksDBExceptionJni : : ThrowNew ( env , s ) ;
}
/*
* Class : org_rocksdb_RocksDB
* Method : openROnly
* Signature : ( JLjava / lang / String ; ) V
*/
void Java_org_rocksdb_RocksDB_openROnly__JLjava_lang_String_2 (
JNIEnv * env , jobject jdb , jlong jopt_handle , jstring jdb_path ) {
auto opt = reinterpret_cast < rocksdb : : Options * > ( jopt_handle ) ;
rocksdb : : DB * db = nullptr ;
const char * db_path = env - > GetStringUTFChars ( jdb_path , 0 ) ;
rocksdb : : Status s = rocksdb : : DB : : OpenForReadOnly ( * opt ,
db_path , & db ) ;
env - > ReleaseStringUTFChars ( jdb_path , db_path ) ;
if ( s . ok ( ) ) {
rocksdb : : RocksDBJni : : setHandle ( env , jdb , db ) ;
return ;
}
rocksdb : : RocksDBExceptionJni : : ThrowNew ( env , s ) ;
}
/*
* Class : org_rocksdb_RocksDB
* Method : openROnly
* Signature : ( JLjava / lang / String ; Ljava / util / List ; I ) Ljava / util / List ;
*/
jobject
Java_org_rocksdb_RocksDB_openROnly__JLjava_lang_String_2Ljava_util_List_2I (
JNIEnv * env , jobject jdb , jlong jopt_handle , jstring jdb_path ,
jobject jcfname_list , jint jcfname_count ) {
auto opt = reinterpret_cast < rocksdb : : Options * > ( jopt_handle ) ;
rocksdb : : DB * db = nullptr ;
const char * db_path = env - > GetStringUTFChars ( jdb_path , 0 ) ;
std : : vector < const char * > cfnames_to_free ;
std : : vector < jstring > jcfnames_for_free ;
std : : vector < rocksdb : : ColumnFamilyDescriptor > column_families ;
std : : vector < rocksdb : : ColumnFamilyHandle * > handles ;
// get iterator for cfnames
jobject iteratorObj = env - > CallObjectMethod (
jcfname_list , rocksdb : : ListJni : : getIteratorMethod ( env ) ) ;
// iterate over cfnames and convert cfnames to
// ColumnFamilyDescriptor instances
while ( env - > CallBooleanMethod (
iteratorObj , rocksdb : : ListJni : : getHasNextMethod ( env ) ) = = JNI_TRUE ) {
jstring jstr = ( jstring ) env - > CallObjectMethod ( iteratorObj ,
rocksdb : : ListJni : : getNextMethod ( env ) ) ;
const char * cfname = env - > GetStringUTFChars ( jstr , 0 ) ;
// free allocated cfnames after call to open
cfnames_to_free . push_back ( cfname ) ;
jcfnames_for_free . push_back ( jstr ) ;
column_families . push_back ( rocksdb : : ColumnFamilyDescriptor ( cfname ,
rocksdb : : ColumnFamilyOptions ( ) ) ) ;
}
rocksdb : : Status s = rocksdb : : DB : : OpenForReadOnly ( * opt ,
db_path , column_families , & handles , & db ) ;
env - > ReleaseStringUTFChars ( jdb_path , db_path ) ;
// free jbyte allocations
for ( std : : vector < jbyte * > : : size_type i = 0 ;
i ! = cfnames_to_free . size ( ) ; i + + ) {
// free cfnames
env - > ReleaseStringUTFChars ( jcfnames_for_free [ i ] , cfnames_to_free [ i ] ) ;
}
jobject jcfhandle_list = nullptr ;
// check if open operation was successful
if ( s . ok ( ) ) {
rocksdb : : RocksDBJni : : setHandle ( env , jdb , db ) ;
jclass jclazz = env - > FindClass ( " java/util/ArrayList " ) ;
jmethodID mid = rocksdb : : ListJni : : getArrayListConstructorMethodId (
env , jclazz ) ;
jobject jcfhandle_list = env - > NewObject ( jclazz , mid , handles . size ( ) ) ;
// insert in java list
for ( std : : vector < rocksdb : : ColumnFamilyHandle * > : : size_type i = 0 ;
i ! = handles . size ( ) ; i + + ) {
// jlong must be converted to Long due to collections restrictions
jclass jclazz = env - > FindClass ( " java/lang/Long " ) ;
jmethodID mid = env - > GetMethodID ( jclazz , " <init> " , " (J)V " ) ;
jobject obj = env - > NewObject ( jclazz , mid ,
reinterpret_cast < jlong > ( handles [ i ] ) ) ;
env - > CallBooleanMethod ( jcfhandle_list ,
rocksdb : : ListJni : : getListAddMethodId ( env ) , obj ) ;
}
return jcfhandle_list ;
}
rocksdb : : RocksDBExceptionJni : : ThrowNew ( env , s ) ;
return jcfhandle_list ;
}
/*
* Class : org_rocksdb_RocksDB
* Method : open
* Signature : ( JLjava / lang / String ; Ljava / util / List ; I ) Ljava / util / List ;
*/
jobject Java_org_rocksdb_RocksDB_open__JLjava_lang_String_2Ljava_util_List_2I (
JNIEnv * env , jobject jdb , jlong jopt_handle , jstring jdb_path ,
jobject jcfname_list , jint jcfname_count ) {
auto opt = reinterpret_cast < rocksdb : : Options * > ( jopt_handle ) ;
rocksdb : : DB * db = nullptr ;
const char * db_path = env - > GetStringUTFChars ( jdb_path , 0 ) ;
std : : vector < const char * > cfnames_to_free ;
std : : vector < jstring > jcfnames_for_free ;
std : : vector < rocksdb : : ColumnFamilyDescriptor > column_families ;
std : : vector < rocksdb : : ColumnFamilyHandle * > handles ;
// get iterator for cfnames
jobject iteratorObj = env - > CallObjectMethod (
jcfname_list , rocksdb : : ListJni : : getIteratorMethod ( env ) ) ;
// iterate over cfnames and convert cfnames to
// ColumnFamilyDescriptor instances
while ( env - > CallBooleanMethod (
iteratorObj , rocksdb : : ListJni : : getHasNextMethod ( env ) ) = = JNI_TRUE ) {
jstring jstr = ( jstring ) env - > CallObjectMethod ( iteratorObj ,
rocksdb : : ListJni : : getNextMethod ( env ) ) ;
const char * cfname = env - > GetStringUTFChars ( jstr , 0 ) ;
// free allocated cfnames after call to open
cfnames_to_free . push_back ( cfname ) ;
jcfnames_for_free . push_back ( jstr ) ;
column_families . push_back ( rocksdb : : ColumnFamilyDescriptor ( cfname ,
rocksdb : : ColumnFamilyOptions ( ) ) ) ;
}
rocksdb : : Status s = rocksdb : : DB : : Open ( * opt , db_path , column_families ,
& handles , & db ) ;
env - > ReleaseStringUTFChars ( jdb_path , db_path ) ;
// free jbyte allocations
for ( std : : vector < jbyte * > : : size_type i = 0 ;
i ! = cfnames_to_free . size ( ) ; i + + ) {
// free cfnames
env - > ReleaseStringUTFChars ( jcfnames_for_free [ i ] , cfnames_to_free [ i ] ) ;
}
jobject jcfhandle_list = nullptr ;
// check if open operation was successful
if ( s . ok ( ) ) {
rocksdb : : RocksDBJni : : setHandle ( env , jdb , db ) ;
jclass jclazz = env - > FindClass ( " java/util/ArrayList " ) ;
jmethodID mid = rocksdb : : ListJni : : getArrayListConstructorMethodId (
env , jclazz ) ;
jobject jcfhandle_list = env - > NewObject ( jclazz , mid , handles . size ( ) ) ;
// insert in java list
for ( std : : vector < rocksdb : : ColumnFamilyHandle * > : : size_type i = 0 ;
i ! = handles . size ( ) ; i + + ) {
// jlong must be converted to Long due to collections restrictions
jclass jclazz = env - > FindClass ( " java/lang/Long " ) ;
jmethodID mid = env - > GetMethodID ( jclazz , " <init> " , " (J)V " ) ;
jobject obj = env - > NewObject ( jclazz , mid ,
reinterpret_cast < jlong > ( handles [ i ] ) ) ;
env - > CallBooleanMethod ( jcfhandle_list ,
rocksdb : : ListJni : : getListAddMethodId ( env ) , obj ) ;
}
return jcfhandle_list ;
}
rocksdb : : RocksDBExceptionJni : : ThrowNew ( env , s ) ;
return jcfhandle_list ;
}
//////////////////////////////////////////////////////////////////////////////
// rocksdb::DB::ListColumnFamilies
/*
* Class : org_rocksdb_RocksDB
* Method : listColumnFamilies
* Signature : ( JLjava / lang / String ; ) Ljava / util / List ;
*/
jobject Java_org_rocksdb_RocksDB_listColumnFamilies (
JNIEnv * env , jclass jclazz , jlong jopt_handle , jstring jdb_path ) {
std : : vector < std : : string > column_family_names ;
auto opt = reinterpret_cast < rocksdb : : Options * > ( jopt_handle ) ;
const char * db_path = env - > GetStringUTFChars ( jdb_path , 0 ) ;
jobject jvalue_list = nullptr ;
rocksdb : : Status s = rocksdb : : DB : : ListColumnFamilies ( * opt , db_path ,
& column_family_names ) ;
env - > ReleaseStringUTFChars ( jdb_path , db_path ) ;
if ( s . ok ( ) ) {
// Don't reuse class pointer
jclass jclazz = env - > FindClass ( " java/util/ArrayList " ) ;
jmethodID mid = rocksdb : : ListJni : : getArrayListConstructorMethodId ( env ,
jclazz ) ;
jvalue_list = env - > NewObject ( jclazz , mid , column_family_names . size ( ) ) ;
for ( std : : vector < std : : string > : : size_type i = 0 ;
i < column_family_names . size ( ) ; i + + ) {
jbyteArray jvalue = env - > NewByteArray ( column_family_names [ i ] . size ( ) ) ;
env - > SetByteArrayRegion ( jvalue , 0 , column_family_names [ i ] . size ( ) ,
reinterpret_cast < const jbyte * > ( column_family_names [ i ] . c_str ( ) ) ) ;
env - > CallBooleanMethod ( jvalue_list ,
rocksdb : : ListJni : : getListAddMethodId ( env ) , jvalue ) ;
}
}
return jvalue_list ;
}
//////////////////////////////////////////////////////////////////////////////
// rocksdb::DB::Put
void rocksdb_put_helper (
JNIEnv * env , rocksdb : : DB * db , const rocksdb : : WriteOptions & write_options ,
jbyteArray jkey , jint jkey_len ,
rocksdb : : ColumnFamilyHandle * cf_handle , jbyteArray jkey , jint jkey_len ,
jbyteArray jvalue , jint jvalue_len ) {
jbyte * key = env - > GetByteArrayElements ( jkey , 0 ) ;
@ -53,7 +256,13 @@ void rocksdb_put_helper(
rocksdb : : Slice key_slice ( reinterpret_cast < char * > ( key ) , jkey_len ) ;
rocksdb : : Slice value_slice ( reinterpret_cast < char * > ( value ) , jvalue_len ) ;
rocksdb : : Status s = db - > Put ( write_options , key_slice , value_slice ) ;
rocksdb : : Status s ;
if ( cf_handle ! = nullptr ) {
s = db - > Put ( write_options , cf_handle , key_slice , value_slice ) ;
} else {
// backwards compatibility
s = db - > Put ( write_options , key_slice , value_slice ) ;
}
// trigger java unref on key and value.
// by passing JNI_ABORT, it will simply release the reference without
@ -80,10 +289,31 @@ void Java_org_rocksdb_RocksDB_put__J_3BI_3BI(
static const rocksdb : : WriteOptions default_write_options =
rocksdb : : WriteOptions ( ) ;
rocksdb_put_helper ( env , db , default_write_options ,
rocksdb_put_helper ( env , db , default_write_options , nullptr ,
jkey , jkey_len ,
jvalue , jvalue_len ) ;
}
/*
* Class : org_rocksdb_RocksDB
* Method : put
* Signature : ( J [ BI [ BIJ ) V
*/
void Java_org_rocksdb_RocksDB_put__J_3BI_3BIJ (
JNIEnv * env , jobject jdb , jlong jdb_handle ,
jbyteArray jkey , jint jkey_len ,
jbyteArray jvalue , jint jvalue_len , jlong jcf_handle ) {
auto db = reinterpret_cast < rocksdb : : DB * > ( jdb_handle ) ;
static const rocksdb : : WriteOptions default_write_options =
rocksdb : : WriteOptions ( ) ;
auto cf_handle = reinterpret_cast < rocksdb : : ColumnFamilyHandle * > ( jcf_handle ) ;
if ( cf_handle ! = nullptr ) {
rocksdb_put_helper ( env , db , default_write_options , cf_handle ,
jkey , jkey_len , jvalue , jvalue_len ) ;
} else {
rocksdb : : RocksDBExceptionJni : : ThrowNew ( env ,
rocksdb : : Status : : InvalidArgument ( " Invalid ColumnFamilyHandle. " ) ) ;
}
}
/*
* Class : org_rocksdb_RocksDB
@ -99,11 +329,34 @@ void Java_org_rocksdb_RocksDB_put__JJ_3BI_3BI(
auto write_options = reinterpret_cast < rocksdb : : WriteOptions * > (
jwrite_options_handle ) ;
rocksdb_put_helper ( env , db , * write_options ,
rocksdb_put_helper ( env , db , * write_options , nullptr ,
jkey , jkey_len ,
jvalue , jvalue_len ) ;
}
/*
* Class : org_rocksdb_RocksDB
* Method : put
* Signature : ( JJ [ BI [ BIJ ) V
*/
void Java_org_rocksdb_RocksDB_put__JJ_3BI_3BIJ (
JNIEnv * env , jobject jdb ,
jlong jdb_handle , jlong jwrite_options_handle ,
jbyteArray jkey , jint jkey_len ,
jbyteArray jvalue , jint jvalue_len , jlong jcf_handle ) {
auto db = reinterpret_cast < rocksdb : : DB * > ( jdb_handle ) ;
auto write_options = reinterpret_cast < rocksdb : : WriteOptions * > (
jwrite_options_handle ) ;
auto cf_handle = reinterpret_cast < rocksdb : : ColumnFamilyHandle * > ( jcf_handle ) ;
if ( cf_handle ! = nullptr ) {
rocksdb_put_helper ( env , db , * write_options , cf_handle ,
jkey , jkey_len , jvalue , jvalue_len ) ;
} else {
rocksdb : : RocksDBExceptionJni : : ThrowNew ( env ,
rocksdb : : Status : : InvalidArgument ( " Invalid ColumnFamilyHandle. " ) ) ;
}
}
//////////////////////////////////////////////////////////////////////////////
// rocksdb::DB::Write
/*
@ -126,20 +379,94 @@ void Java_org_rocksdb_RocksDB_write(
}
}
//////////////////////////////////////////////////////////////////////////////
// rocksdb::DB::KeyMayExist
jboolean key_may_exist_helper ( JNIEnv * env , rocksdb : : DB * db ,
const rocksdb : : ReadOptions & read_opt ,
rocksdb : : ColumnFamilyHandle * cf_handle , jbyteArray jkey , jint jkey_len ,
jobject jvalue ) {
std : : string value ;
bool value_found = false ;
jboolean isCopy ;
jbyte * key = env - > GetByteArrayElements ( jkey , & isCopy ) ;
rocksdb : : Slice key_slice ( reinterpret_cast < char * > ( key ) , jkey_len ) ;
bool keyMaxExist = db - > KeyMayExist ( read_opt , cf_handle , key_slice ,
& value , & value_found ) ;
if ( value_found & & ! value . empty ( ) ) {
jclass clazz = env - > GetObjectClass ( jvalue ) ;
jmethodID mid = env - > GetMethodID ( clazz , " append " ,
" (Ljava/lang/String;)Ljava/lang/StringBuffer; " ) ;
jstring new_value_str = env - > NewStringUTF ( value . c_str ( ) ) ;
env - > CallObjectMethod ( jvalue , mid , new_value_str ) ;
}
env - > ReleaseByteArrayElements ( jkey , key , JNI_ABORT ) ;
return static_cast < jboolean > ( keyMaxExist ) ;
}
/*
* Class : org_rocksdb_RocksDB
* Method : keyMayExist
* Signature : ( [ BIJLjava / lang / StringBuffer ; ) Z
*/
jboolean Java_org_rocksdb_RocksDB_keyMayExist___3BIJLjava_lang_StringBuffer_2 (
JNIEnv * env , jobject jdb , jbyteArray jkey , jint jkey_len ,
jlong jcf_handle , jobject jvalue ) {
rocksdb : : DB * db = rocksdb : : RocksDBJni : : getHandle ( env , jdb ) ;
auto cf_handle = reinterpret_cast < rocksdb : : ColumnFamilyHandle * > (
jcf_handle ) ;
if ( cf_handle ! = nullptr ) {
return key_may_exist_helper ( env , db , rocksdb : : ReadOptions ( ) ,
cf_handle , jkey , jkey_len , jvalue ) ;
} else {
rocksdb : : RocksDBExceptionJni : : ThrowNew ( env ,
rocksdb : : Status : : InvalidArgument ( " Invalid ColumnFamilyHandle. " ) ) ;
}
return true ;
}
/*
* Class : org_rocksdb_RocksDB
* Method : keyMayExist
* Signature : ( J [ BIJLjava / lang / StringBuffer ; ) Z
*/
jboolean Java_org_rocksdb_RocksDB_keyMayExist__J_3BIJLjava_lang_StringBuffer_2 (
JNIEnv * env , jobject jdb , jlong jread_options_handle ,
jbyteArray jkey , jint jkey_len , jlong jcf_handle , jobject jvalue ) {
rocksdb : : DB * db = rocksdb : : RocksDBJni : : getHandle ( env , jdb ) ;
auto & read_options = * reinterpret_cast < rocksdb : : ReadOptions * > (
jread_options_handle ) ;
auto cf_handle = reinterpret_cast < rocksdb : : ColumnFamilyHandle * > (
jcf_handle ) ;
if ( cf_handle ! = nullptr ) {
return key_may_exist_helper ( env , db , read_options , cf_handle ,
jkey , jkey_len , jvalue ) ;
} else {
rocksdb : : RocksDBExceptionJni : : ThrowNew ( env ,
rocksdb : : Status : : InvalidArgument ( " Invalid ColumnFamilyHandle. " ) ) ;
}
return true ;
}
//////////////////////////////////////////////////////////////////////////////
// rocksdb::DB::Get
jbyteArray rocksdb_get_helper (
JNIEnv * env , rocksdb : : DB * db , const rocksdb : : ReadOptions & read_opt ,
jbyteArray jkey , jint jkey_len ) {
rocksdb : : ColumnFamilyHandle * column_family_handle , jbyteArray jkey ,
jint jkey_len ) {
jboolean isCopy ;
jbyte * key = env - > GetByteArrayElements ( jkey , & isCopy ) ;
rocksdb : : Slice key_slice (
reinterpret_cast < char * > ( key ) , jkey_len ) ;
std : : string value ;
rocksdb : : Status s = db - > Get (
read_opt , key_slice , & value ) ;
rocksdb : : Status s ;
if ( column_family_handle ! = nullptr ) {
s = db - > Get ( read_opt , column_family_handle , key_slice , & value ) ;
} else {
// backwards compatibility
s = db - > Get ( read_opt , key_slice , & value ) ;
}
// trigger java unref on key.
// by passing JNI_ABORT, it will simply release the reference without
@ -172,10 +499,31 @@ jbyteArray Java_org_rocksdb_RocksDB_get__J_3BI(
jbyteArray jkey , jint jkey_len ) {
return rocksdb_get_helper ( env ,
reinterpret_cast < rocksdb : : DB * > ( jdb_handle ) ,
rocksdb : : ReadOptions ( ) ,
rocksdb : : ReadOptions ( ) , nullptr ,
jkey , jkey_len ) ;
}
/*
* Class : org_rocksdb_RocksDB
* Method : get
* Signature : ( J [ BIJ ) [ B
*/
jbyteArray Java_org_rocksdb_RocksDB_get__J_3BIJ (
JNIEnv * env , jobject jdb , jlong jdb_handle ,
jbyteArray jkey , jint jkey_len , jlong jcf_handle ) {
auto db_handle = reinterpret_cast < rocksdb : : DB * > ( jdb_handle ) ;
auto cf_handle = reinterpret_cast < rocksdb : : ColumnFamilyHandle * > ( jcf_handle ) ;
if ( cf_handle ! = nullptr ) {
return rocksdb_get_helper ( env , db_handle , rocksdb : : ReadOptions ( ) ,
cf_handle , jkey , jkey_len ) ;
} else {
rocksdb : : RocksDBExceptionJni : : ThrowNew ( env ,
rocksdb : : Status : : InvalidArgument ( " Invalid ColumnFamilyHandle. " ) ) ;
// will never be evaluated
return env - > NewByteArray ( 0 ) ;
}
}
/*
* Class : org_rocksdb_RocksDB
* Method : get
@ -186,14 +534,36 @@ jbyteArray Java_org_rocksdb_RocksDB_get__JJ_3BI(
jbyteArray jkey , jint jkey_len ) {
return rocksdb_get_helper ( env ,
reinterpret_cast < rocksdb : : DB * > ( jdb_handle ) ,
* reinterpret_cast < rocksdb : : ReadOptions * > ( jropt_handle ) ,
* reinterpret_cast < rocksdb : : ReadOptions * > ( jropt_handle ) , nullptr ,
jkey , jkey_len ) ;
}
/*
* Class : org_rocksdb_RocksDB
* Method : get
* Signature : ( JJ [ BIJ ) [ B
*/
jbyteArray Java_org_rocksdb_RocksDB_get__JJ_3BIJ (
JNIEnv * env , jobject jdb , jlong jdb_handle , jlong jropt_handle ,
jbyteArray jkey , jint jkey_len , jlong jcf_handle ) {
auto db_handle = reinterpret_cast < rocksdb : : DB * > ( jdb_handle ) ;
auto & ro_opt = * reinterpret_cast < rocksdb : : ReadOptions * > ( jropt_handle ) ;
auto cf_handle = reinterpret_cast < rocksdb : : ColumnFamilyHandle * > ( jcf_handle ) ;
if ( cf_handle ! = nullptr ) {
return rocksdb_get_helper ( env , db_handle , ro_opt , cf_handle ,
jkey , jkey_len ) ;
} else {
rocksdb : : RocksDBExceptionJni : : ThrowNew ( env ,
rocksdb : : Status : : InvalidArgument ( " Invalid ColumnFamilyHandle. " ) ) ;
// will never be evaluated
return env - > NewByteArray ( 0 ) ;
}
}
jint rocksdb_get_helper (
JNIEnv * env , rocksdb : : DB * db , const rocksdb : : ReadOptions & read_options ,
jbyteArray jkey , jint jkey_len ,
jbyteArray jvalue , jint jvalue_len ) {
rocksdb : : ColumnFamilyHandle * column_family_handle , jbyteArray jkey ,
jint jkey_len , j byteArray jvalue , jint jvalue_len ) {
static const int kNotFound = - 1 ;
static const int kStatusError = - 2 ;
@ -204,8 +574,13 @@ jint rocksdb_get_helper(
// TODO(yhchiang): we might save one memory allocation here by adding
// a DB::Get() function which takes preallocated jbyte* as input.
std : : string cvalue ;
rocksdb : : Status s = db - > Get (
read_options , key_slice , & cvalue ) ;
rocksdb : : Status s ;
if ( column_family_handle ! = nullptr ) {
s = db - > Get ( read_options , column_family_handle , key_slice , & cvalue ) ;
} else {
// backwards compatibility
s = db - > Get ( read_options , key_slice , & cvalue ) ;
}
// trigger java unref on key.
// by passing JNI_ABORT, it will simply release the reference without
@ -236,11 +611,31 @@ jint rocksdb_get_helper(
return cvalue_len ;
}
// cf multi get
jobject multi_get_helper ( JNIEnv * env , jobject jdb , rocksdb : : DB * db ,
const rocksdb : : ReadOptions & rOpt , jobject jkey_list , jint jkeys_count ) {
const rocksdb : : ReadOptions & rOpt , jobject jkey_list , jint jkeys_count ,
jobject jcfhandle_list ) {
std : : vector < rocksdb : : Slice > keys ;
std : : vector < jbyte * > keys_to_free ;
std : : vector < rocksdb : : ColumnFamilyHandle * > cf_handles ;
if ( jcfhandle_list ! = nullptr ) {
// get cf iterator
jobject cfIteratorObj = env - > CallObjectMethod (
jcfhandle_list , rocksdb : : ListJni : : getIteratorMethod ( env ) ) ;
// iterate over keys and convert java byte array to slice
while ( env - > CallBooleanMethod (
cfIteratorObj , rocksdb : : ListJni : : getHasNextMethod ( env ) ) = = JNI_TRUE ) {
jobject jobj = ( jbyteArray ) env - > CallObjectMethod (
cfIteratorObj , rocksdb : : ListJni : : getNextMethod ( env ) ) ;
rocksdb : : ColumnFamilyHandle * cfHandle =
rocksdb : : ColumnFamilyHandleJni : : getHandle ( env , jobj ) ;
cf_handles . push_back ( cfHandle ) ;
}
}
// Process key list
// get iterator
jobject iteratorObj = env - > CallObjectMethod (
jkey_list , rocksdb : : ListJni : : getIteratorMethod ( env ) ) ;
@ -263,7 +658,12 @@ jobject multi_get_helper(JNIEnv* env, jobject jdb, rocksdb::DB* db,
}
std : : vector < std : : string > values ;
std : : vector < rocksdb : : Status > s = db - > MultiGet ( rOpt , keys , & values ) ;
std : : vector < rocksdb : : Status > s ;
if ( cf_handles . size ( ) = = 0 ) {
s = db - > MultiGet ( rOpt , keys , & values ) ;
} else {
s = db - > MultiGet ( rOpt , cf_handles , keys , & values ) ;
}
// Don't reuse class pointer
jclass jclazz = env - > FindClass ( " java/util/ArrayList " ) ;
@ -285,13 +685,11 @@ jobject multi_get_helper(JNIEnv* env, jobject jdb, rocksdb::DB* db,
jvalue_list , rocksdb : : ListJni : : getListAddMethodId ( env ) , nullptr ) ;
}
}
// free up allocated byte arrays
for ( std : : vector < jbyte * > : : size_type i = 0 ; i ! = keys_to_free . size ( ) ; i + + ) {
delete [ ] keys_to_free [ i ] ;
}
keys_to_free . clear ( ) ;
return jvalue_list ;
}
@ -304,7 +702,20 @@ jobject Java_org_rocksdb_RocksDB_multiGet__JLjava_util_List_2I(
JNIEnv * env , jobject jdb , jlong jdb_handle ,
jobject jkey_list , jint jkeys_count ) {
return multi_get_helper ( env , jdb , reinterpret_cast < rocksdb : : DB * > ( jdb_handle ) ,
rocksdb : : ReadOptions ( ) , jkey_list , jkeys_count ) ;
rocksdb : : ReadOptions ( ) , jkey_list , jkeys_count , nullptr ) ;
}
/*
* Class : org_rocksdb_RocksDB
* Method : multiGet
* Signature : ( JLjava / util / List ; ILjava / util / List ; ) Ljava / util / List ;
*/
jobject
Java_org_rocksdb_RocksDB_multiGet__JLjava_util_List_2ILjava_util_List_2 (
JNIEnv * env , jobject jdb , jlong jdb_handle ,
jobject jkey_list , jint jkeys_count , jobject jcfhandle_list ) {
return multi_get_helper ( env , jdb , reinterpret_cast < rocksdb : : DB * > ( jdb_handle ) ,
rocksdb : : ReadOptions ( ) , jkey_list , jkeys_count , jcfhandle_list ) ;
}
/*
@ -317,7 +728,22 @@ jobject Java_org_rocksdb_RocksDB_multiGet__JJLjava_util_List_2I(
jlong jropt_handle , jobject jkey_list , jint jkeys_count ) {
return multi_get_helper ( env , jdb , reinterpret_cast < rocksdb : : DB * > ( jdb_handle ) ,
* reinterpret_cast < rocksdb : : ReadOptions * > ( jropt_handle ) , jkey_list ,
jkeys_count ) ;
jkeys_count , nullptr ) ;
}
/*
* Class : org_rocksdb_RocksDB
* Method : multiGet
* Signature : ( JJLjava / util / List ; ILjava / util / List ; ) Ljava / util / List ;
*/
jobject
Java_org_rocksdb_RocksDB_multiGet__JJLjava_util_List_2ILjava_util_List_2 (
JNIEnv * env , jobject jdb , jlong jdb_handle ,
jlong jropt_handle , jobject jkey_list , jint jkeys_count ,
jobject jcfhandle_list ) {
return multi_get_helper ( env , jdb , reinterpret_cast < rocksdb : : DB * > ( jdb_handle ) ,
* reinterpret_cast < rocksdb : : ReadOptions * > ( jropt_handle ) , jkey_list ,
jkeys_count , jcfhandle_list ) ;
}
/*
@ -331,10 +757,32 @@ jint Java_org_rocksdb_RocksDB_get__J_3BI_3BI(
jbyteArray jvalue , jint jvalue_len ) {
return rocksdb_get_helper ( env ,
reinterpret_cast < rocksdb : : DB * > ( jdb_handle ) ,
rocksdb : : ReadOptions ( ) ,
rocksdb : : ReadOptions ( ) , nullptr ,
jkey , jkey_len , jvalue , jvalue_len ) ;
}
/*
* Class : org_rocksdb_RocksDB
* Method : get
* Signature : ( J [ BI [ BIJ ) I
*/
jint Java_org_rocksdb_RocksDB_get__J_3BI_3BIJ (
JNIEnv * env , jobject jdb , jlong jdb_handle ,
jbyteArray jkey , jint jkey_len ,
jbyteArray jvalue , jint jvalue_len , jlong jcf_handle ) {
auto db_handle = reinterpret_cast < rocksdb : : DB * > ( jdb_handle ) ;
auto cf_handle = reinterpret_cast < rocksdb : : ColumnFamilyHandle * > ( jcf_handle ) ;
if ( cf_handle ! = nullptr ) {
return rocksdb_get_helper ( env , db_handle , rocksdb : : ReadOptions ( ) , cf_handle ,
jkey , jkey_len , jvalue , jvalue_len ) ;
} else {
rocksdb : : RocksDBExceptionJni : : ThrowNew ( env ,
rocksdb : : Status : : InvalidArgument ( " Invalid ColumnFamilyHandle. " ) ) ;
// will never be evaluated
return 0 ;
}
}
/*
* Class : org_rocksdb_RocksDB
* Method : get
@ -347,19 +795,46 @@ jint Java_org_rocksdb_RocksDB_get__JJ_3BI_3BI(
return rocksdb_get_helper ( env ,
reinterpret_cast < rocksdb : : DB * > ( jdb_handle ) ,
* reinterpret_cast < rocksdb : : ReadOptions * > ( jropt_handle ) ,
jkey , jkey_len , jvalue , jvalue_len ) ;
nullptr , jkey , jkey_len , jvalue , jvalue_len ) ;
}
/*
* Class : org_rocksdb_RocksDB
* Method : get
* Signature : ( JJ [ BI [ BIJ ) I
*/
jint Java_org_rocksdb_RocksDB_get__JJ_3BI_3BIJ (
JNIEnv * env , jobject jdb , jlong jdb_handle , jlong jropt_handle ,
jbyteArray jkey , jint jkey_len ,
jbyteArray jvalue , jint jvalue_len , jlong jcf_handle ) {
auto db_handle = reinterpret_cast < rocksdb : : DB * > ( jdb_handle ) ;
auto & ro_opt = * reinterpret_cast < rocksdb : : ReadOptions * > ( jropt_handle ) ;
auto cf_handle = reinterpret_cast < rocksdb : : ColumnFamilyHandle * > ( jcf_handle ) ;
if ( cf_handle ! = nullptr ) {
return rocksdb_get_helper ( env , db_handle , ro_opt , cf_handle , jkey ,
jkey_len , jvalue , jvalue_len ) ;
} else {
rocksdb : : RocksDBExceptionJni : : ThrowNew ( env ,
rocksdb : : Status : : InvalidArgument ( " Invalid ColumnFamilyHandle. " ) ) ;
// will never be evaluated
return 0 ;
}
}
//////////////////////////////////////////////////////////////////////////////
// rocksdb::DB::Delete()
void rocksdb_remove_helper (
JNIEnv * env , rocksdb : : DB * db , const rocksdb : : WriteOptions & write_options ,
jbyteArray jkey , jint jkey_len ) {
rocksdb : : ColumnFamilyHandle * cf_handle , jbyteArray jkey , jint jkey_len ) {
jbyte * key = env - > GetByteArrayElements ( jkey , 0 ) ;
rocksdb : : Slice key_slice ( reinterpret_cast < char * > ( key ) , jkey_len ) ;
rocksdb : : Status s = db - > Delete ( write_options , key_slice ) ;
rocksdb : : Status s ;
if ( cf_handle ! = nullptr ) {
s = db - > Delete ( write_options , cf_handle , key_slice ) ;
} else {
// backwards compatibility
s = db - > Delete ( write_options , key_slice ) ;
}
// trigger java unref on key and value.
// by passing JNI_ABORT, it will simply release the reference without
// copying the result back to the java byte array.
@ -382,24 +857,63 @@ void Java_org_rocksdb_RocksDB_remove__J_3BI(
auto db = reinterpret_cast < rocksdb : : DB * > ( jdb_handle ) ;
static const rocksdb : : WriteOptions default_write_options =
rocksdb : : WriteOptions ( ) ;
rocksdb_remove_helper ( env , db , default_write_options , nullptr ,
jkey , jkey_len ) ;
}
rocksdb_remove_helper ( env , db , default_write_options , jkey , jkey_len ) ;
/*
* Class : org_rocksdb_RocksDB
* Method : remove
* Signature : ( J [ BIJ ) V
*/
void Java_org_rocksdb_RocksDB_remove__J_3BIJ (
JNIEnv * env , jobject jdb , jlong jdb_handle ,
jbyteArray jkey , jint jkey_len , jlong jcf_handle ) {
auto db = reinterpret_cast < rocksdb : : DB * > ( jdb_handle ) ;
static const rocksdb : : WriteOptions default_write_options =
rocksdb : : WriteOptions ( ) ;
auto cf_handle = reinterpret_cast < rocksdb : : ColumnFamilyHandle * > ( jcf_handle ) ;
if ( cf_handle ! = nullptr ) {
rocksdb_remove_helper ( env , db , default_write_options , cf_handle ,
jkey , jkey_len ) ;
} else {
rocksdb : : RocksDBExceptionJni : : ThrowNew ( env ,
rocksdb : : Status : : InvalidArgument ( " Invalid ColumnFamilyHandle. " ) ) ;
}
}
/*
* Class : org_rocksdb_RocksDB
* Method : remove
* Signature : ( JJ [ BI ) V
* Signature : ( JJ [ BIJ ) V
*/
void Java_org_rocksdb_RocksDB_remove__JJ_3BI (
JNIEnv * env , jobject jdb , jlong jdb_handle ,
jlong jwrite_options , jbyteArray jkey , jint jkey_len ) {
auto db = reinterpret_cast < rocksdb : : DB * > ( jdb_handle ) ;
auto write_options = reinterpret_cast < rocksdb : : WriteOptions * > ( jwrite_options ) ;
rocksdb_remove_helper ( env , db , * write_options , jkey , jkey_len ) ;
rocksdb_remove_helper ( env , db , * write_options , nullptr , jkey , jkey_len ) ;
}
/*
* Class : org_rocksdb_RocksDB
* Method : remove
* Signature : ( JJ [ BIJ ) V
*/
void Java_org_rocksdb_RocksDB_remove__JJ_3BIJ (
JNIEnv * env , jobject jdb , jlong jdb_handle ,
jlong jwrite_options , jbyteArray jkey , jint jkey_len ,
jlong jcf_handle ) {
auto db = reinterpret_cast < rocksdb : : DB * > ( jdb_handle ) ;
auto write_options = reinterpret_cast < rocksdb : : WriteOptions * > ( jwrite_options ) ;
auto cf_handle = reinterpret_cast < rocksdb : : ColumnFamilyHandle * > ( jcf_handle ) ;
if ( cf_handle ! = nullptr ) {
rocksdb_remove_helper ( env , db , * write_options , cf_handle , jkey , jkey_len ) ;
} else {
rocksdb : : RocksDBExceptionJni : : ThrowNew ( env ,
rocksdb : : Status : : InvalidArgument ( " Invalid ColumnFamilyHandle. " ) ) ;
}
}
//////////////////////////////////////////////////////////////////////////////
// rocksdb::DB::~DB()
@ -418,19 +932,111 @@ void Java_org_rocksdb_RocksDB_disposeInternal(
* Method : iterator0
* Signature : ( J ) J
*/
jlong Java_org_rocksdb_RocksDB_iterator0 (
jlong Java_org_rocksdb_RocksDB_iterator0__J (
JNIEnv * env , jobject jdb , jlong db_handle ) {
auto db = reinterpret_cast < rocksdb : : DB * > ( db_handle ) ;
rocksdb : : Iterator * iterator = db - > NewIterator ( rocksdb : : ReadOptions ( ) ) ;
return reinterpret_cast < jlong > ( iterator ) ;
}
/*
* Class : org_rocksdb_RocksDB
* Method : iterator0
* Signature : ( JJ ) J
*/
jlong Java_org_rocksdb_RocksDB_iterator0__JJ (
JNIEnv * env , jobject jdb , jlong db_handle , jlong jcf_handle ) {
auto db = reinterpret_cast < rocksdb : : DB * > ( db_handle ) ;
auto cf_handle = reinterpret_cast < rocksdb : : ColumnFamilyHandle * > ( jcf_handle ) ;
rocksdb : : Iterator * iterator = db - > NewIterator ( rocksdb : : ReadOptions ( ) ,
cf_handle ) ;
return reinterpret_cast < jlong > ( iterator ) ;
}
/*
* Class : org_rocksdb_RocksDB
* Method : iterators
* Signature : ( JLjava / util / List ; ) [ J
*/
jlongArray Java_org_rocksdb_RocksDB_iterators (
JNIEnv * env , jobject jdb , jlong db_handle , jobject jcfhandle_list ) {
auto db = reinterpret_cast < rocksdb : : DB * > ( db_handle ) ;
std : : vector < rocksdb : : ColumnFamilyHandle * > cf_handles ;
std : : vector < rocksdb : : Iterator * > iterators ;
if ( jcfhandle_list ! = nullptr ) {
// get cf iterator
jobject cfIteratorObj = env - > CallObjectMethod (
jcfhandle_list , rocksdb : : ListJni : : getIteratorMethod ( env ) ) ;
// iterate over keys and convert java byte array to slice
while ( env - > CallBooleanMethod (
cfIteratorObj , rocksdb : : ListJni : : getHasNextMethod ( env ) ) = = JNI_TRUE ) {
jobject jobj = ( jbyteArray ) env - > CallObjectMethod (
cfIteratorObj , rocksdb : : ListJni : : getNextMethod ( env ) ) ;
rocksdb : : ColumnFamilyHandle * cfHandle =
rocksdb : : ColumnFamilyHandleJni : : getHandle ( env , jobj ) ;
cf_handles . push_back ( cfHandle ) ;
}
}
rocksdb : : Status s = db - > NewIterators ( rocksdb : : ReadOptions ( ) ,
cf_handles , & iterators ) ;
if ( s . ok ( ) ) {
jlongArray jLongArray = env - > NewLongArray ( iterators . size ( ) ) ;
for ( std : : vector < rocksdb : : Iterator * > : : size_type i = 0 ;
i < iterators . size ( ) ; i + + ) {
env - > SetLongArrayRegion ( jLongArray , i , 1 ,
reinterpret_cast < const jlong * > ( & iterators [ i ] ) ) ;
}
return jLongArray ;
}
rocksdb : : RocksDBExceptionJni : : ThrowNew ( env , s ) ;
return env - > NewLongArray ( 0 ) ;
}
/*
* Class : org_rocksdb_RocksDB
* Method : createColumnFamily
* Signature : ( JLjava / lang / String ; ) J ;
*/
jlong Java_org_rocksdb_RocksDB_createColumnFamily (
JNIEnv * env , jobject jdb , jlong jdb_handle , jstring jcfname ) {
rocksdb : : ColumnFamilyHandle * handle ;
const char * cfname = env - > GetStringUTFChars ( jcfname , 0 ) ;
auto db_handle = reinterpret_cast < rocksdb : : DB * > ( jdb_handle ) ;
rocksdb : : Status s = db_handle - > CreateColumnFamily (
rocksdb : : ColumnFamilyOptions ( ) , cfname , & handle ) ;
env - > ReleaseStringUTFChars ( jcfname , cfname ) ;
if ( s . ok ( ) ) {
return reinterpret_cast < jlong > ( handle ) ;
}
rocksdb : : RocksDBExceptionJni : : ThrowNew ( env , s ) ;
return 0 ;
}
/*
* Class : org_rocksdb_RocksDB
* Method : dropColumnFamily
* Signature : ( JJ ) V ;
*/
void Java_org_rocksdb_RocksDB_dropColumnFamily (
JNIEnv * env , jobject jdb , jlong jdb_handle , jlong jcf_handle ) {
auto cf_handle = reinterpret_cast < rocksdb : : ColumnFamilyHandle * > ( jcf_handle ) ;
auto db_handle = reinterpret_cast < rocksdb : : DB * > ( jdb_handle ) ;
rocksdb : : Status s = db_handle - > DropColumnFamily ( cf_handle ) ;
if ( ! s . ok ( ) ) {
rocksdb : : RocksDBExceptionJni : : ThrowNew ( env , s ) ;
}
}
/*
* Class : org_rocksdb_RocksDB
* Method : getProperty0
* Signature : ( JLjava / lang / String ; I ) Ljava / lang / String ;
*/
jstring Java_org_rocksdb_RocksDB_getProperty0 (
jstring Java_org_rocksdb_RocksDB_getProperty0__JLjava_lang_String_2I (
JNIEnv * env , jobject jdb , jlong db_handle , jstring jproperty ,
jint jproperty_len ) {
auto db = reinterpret_cast < rocksdb : : DB * > ( db_handle ) ;
@ -448,3 +1054,28 @@ jstring Java_org_rocksdb_RocksDB_getProperty0(
return env - > NewStringUTF ( property_value . data ( ) ) ;
}
/*
* Class : org_rocksdb_RocksDB
* Method : getProperty0
* Signature : ( JJLjava / lang / String ; I ) Ljava / lang / String ;
*/
jstring Java_org_rocksdb_RocksDB_getProperty0__JJLjava_lang_String_2I (
JNIEnv * env , jobject jdb , jlong db_handle , jlong jcf_handle ,
jstring jproperty , jint jproperty_len ) {
auto db = reinterpret_cast < rocksdb : : DB * > ( db_handle ) ;
auto cf_handle = reinterpret_cast < rocksdb : : ColumnFamilyHandle * > ( jcf_handle ) ;
const char * property = env - > GetStringUTFChars ( jproperty , 0 ) ;
rocksdb : : Slice property_slice ( property , jproperty_len ) ;
std : : string property_value ;
bool retCode = db - > GetProperty ( cf_handle , property_slice , & property_value ) ;
env - > ReleaseStringUTFChars ( jproperty , property ) ;
if ( ! retCode ) {
rocksdb : : RocksDBExceptionJni : : ThrowNew ( env , rocksdb : : Status : : NotFound ( ) ) ;
}
return env - > NewStringUTF ( property_value . data ( ) ) ;
}