Skip to content

Commit 058ce80

Browse files
committed
JNI implementations for new Java bindings
1 parent 55165c7 commit 058ce80

3 files changed

Lines changed: 135 additions & 0 deletions

File tree

java/rocksjni/memory_allocator.cc

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// This file implements the JNI bindings for MemoryAllocator
2+
3+
#include <jni.h>
4+
5+
#include "include/org_rocksdb_MemoryAllocator.h"
6+
#include "rocksdb/memory_allocator.h"
7+
#include "rocksjni/cplusplus_to_java_convert.h"
8+
#include "rocksjni/portal.h"
9+
10+
/*
11+
* Class: org_rocksdb_MemoryAllocator_DefaultMemoryAllocator
12+
* Method: newDefaultMemoryAllocatorInstance
13+
* Signature: ()J
14+
*/
15+
jlong Java_org_rocksdb_MemoryAllocator_00024DefaultMemoryAllocator_newDefaultMemoryAllocatorInstance(
16+
JNIEnv* /*env*/, jclass /*jcls*/) {
17+
// Create default memory allocator (nullptr means use system allocator)
18+
std::shared_ptr<ROCKSDB_NAMESPACE::MemoryAllocator>* allocator =
19+
new std::shared_ptr<ROCKSDB_NAMESPACE::MemoryAllocator>(nullptr);
20+
return GET_CPLUSPLUS_POINTER(allocator);
21+
}
22+
23+
/*
24+
* Class: org_rocksdb_MemoryAllocator_JEMallocMemoryAllocator
25+
* Method: newJEMallocMemoryAllocatorInstance
26+
* Signature: ()J
27+
*/
28+
jlong Java_org_rocksdb_MemoryAllocator_00024JEMallocMemoryAllocator_newJEMallocMemoryAllocatorInstance(
29+
JNIEnv* env, jclass /*jcls*/) {
30+
#ifdef ROCKSDB_JEMALLOC
31+
auto allocator = ROCKSDB_NAMESPACE::NewJemallocNodumpAllocator();
32+
if (allocator.ok()) {
33+
std::shared_ptr<ROCKSDB_NAMESPACE::MemoryAllocator>* allocator_ptr =
34+
new std::shared_ptr<ROCKSDB_NAMESPACE::MemoryAllocator>(
35+
std::move(allocator.value()));
36+
return GET_CPLUSPLUS_POINTER(allocator_ptr);
37+
} else {
38+
ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(
39+
env, "JEMalloc allocator creation failed: " + allocator.status().ToString());
40+
return 0;
41+
}
42+
#else
43+
ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(
44+
env, "JEMalloc is not available in this build");
45+
return 0;
46+
#endif
47+
}
48+
49+
/*
50+
* Class: org_rocksdb_MemoryAllocator
51+
* Method: disposeInternalJni
52+
* Signature: (J)V
53+
*/
54+
void Java_org_rocksdb_MemoryAllocator_disposeInternalJni(JNIEnv* /*env*/,
55+
jobject /*jobj*/,
56+
jlong jhandle) {
57+
auto* allocator =
58+
reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::MemoryAllocator>*>(
59+
jhandle);
60+
assert(allocator != nullptr);
61+
delete allocator;
62+
}

java/rocksjni/ribbon_filter.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// This file implements the JNI bindings for RibbonFilter
2+
3+
#include <jni.h>
4+
5+
#include "include/org_rocksdb_RibbonFilter.h"
6+
#include "rocksdb/filter_policy.h"
7+
#include "rocksjni/cplusplus_to_java_convert.h"
8+
9+
/*
10+
* Class: org_rocksdb_RibbonFilter
11+
* Method: createNewRibbonFilter
12+
* Signature: (DI)J
13+
*/
14+
jlong Java_org_rocksdb_RibbonFilter_createNewRibbonFilter(
15+
JNIEnv* /*env*/, jclass /*jcls*/, jdouble jbits_per_key,
16+
jint jbloom_before_level) {
17+
auto* filter_policy =
18+
new std::shared_ptr<const ROCKSDB_NAMESPACE::FilterPolicy>(
19+
ROCKSDB_NAMESPACE::NewRibbonFilterPolicy(
20+
static_cast<double>(jbits_per_key),
21+
static_cast<int>(jbloom_before_level)));
22+
return GET_CPLUSPLUS_POINTER(filter_policy);
23+
}

java/rocksjni/secondary_cache.cc

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// This file implements the JNI bindings for SecondaryCache
2+
3+
#include <jni.h>
4+
5+
#include "include/org_rocksdb_SecondaryCache.h"
6+
#include "rocksdb/secondary_cache.h"
7+
#include "rocksjni/cplusplus_to_java_convert.h"
8+
#include "rocksjni/portal.h"
9+
10+
/*
11+
* Class: org_rocksdb_SecondaryCache_CompressedSecondaryCache
12+
* Method: newCompressedSecondaryCacheInstance
13+
* Signature: (J)J
14+
*/
15+
jlong Java_org_rocksdb_SecondaryCache_00024CompressedSecondaryCache_newCompressedSecondaryCacheInstance__J(
16+
JNIEnv* /*env*/, jclass /*jcls*/, jlong jcapacity) {
17+
auto cache = ROCKSDB_NAMESPACE::NewCompressedSecondaryCache(
18+
static_cast<size_t>(jcapacity));
19+
auto* cache_ptr = new std::shared_ptr<ROCKSDB_NAMESPACE::SecondaryCache>(cache);
20+
return GET_CPLUSPLUS_POINTER(cache_ptr);
21+
}
22+
23+
/*
24+
* Class: org_rocksdb_SecondaryCache_CompressedSecondaryCache
25+
* Method: newCompressedSecondaryCacheInstance
26+
* Signature: (JIZD)J
27+
*/
28+
jlong Java_org_rocksdb_SecondaryCache_00024CompressedSecondaryCache_newCompressedSecondaryCacheInstance__JIZD(
29+
JNIEnv* /*env*/, jclass /*jcls*/, jlong jcapacity, jint jnum_shard_bits,
30+
jboolean jstrict_capacity_limit, jdouble jhigh_pri_pool_ratio) {
31+
auto cache = ROCKSDB_NAMESPACE::NewCompressedSecondaryCache(
32+
static_cast<size_t>(jcapacity), static_cast<int>(jnum_shard_bits),
33+
static_cast<bool>(jstrict_capacity_limit),
34+
static_cast<double>(jhigh_pri_pool_ratio));
35+
auto* cache_ptr = new std::shared_ptr<ROCKSDB_NAMESPACE::SecondaryCache>(cache);
36+
return GET_CPLUSPLUS_POINTER(cache_ptr);
37+
}
38+
39+
/*
40+
* Class: org_rocksdb_SecondaryCache
41+
* Method: disposeInternalJni
42+
* Signature: (J)V
43+
*/
44+
void Java_org_rocksdb_SecondaryCache_disposeInternalJni(JNIEnv* /*env*/,
45+
jobject /*jobj*/,
46+
jlong jhandle) {
47+
auto* cache = reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::SecondaryCache>*>(jhandle);
48+
assert(cache != nullptr);
49+
delete cache;
50+
}

0 commit comments

Comments
 (0)