-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JNI wrapper for Rust's BTreeMap (#76)
* fix merge conflicts Signed-off-by: pushkarm029 <[email protected]> * fix Signed-off-by: pushkarm029 <[email protected]> * remove comment Signed-off-by: pushkarm029 <[email protected]> * fix Signed-off-by: pushkarm029 <[email protected]> --------- Signed-off-by: pushkarm029 <[email protected]>
- Loading branch information
1 parent
ec2be3e
commit ddec4cf
Showing
12 changed files
with
246 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use crate::base_storage::BaseStorage; | ||
use std::cell::RefCell; | ||
use std::collections::btree_map::Iter; | ||
use std::rc::Rc; | ||
|
||
pub struct BTreeMapIterator<'a, K, V> { | ||
iter: Rc<RefCell<Iter<'a, K, V>>>, | ||
} | ||
|
||
impl<'a, K, V> BTreeMapIterator<'a, K, V> | ||
where | ||
K: Ord + Clone, | ||
V: Clone, | ||
{ | ||
pub fn new<S: BaseStorage<K, V>>(storage: &'a S) -> Self { | ||
BTreeMapIterator { | ||
iter: Rc::new(RefCell::new(storage.as_ref().iter())), | ||
} | ||
} | ||
|
||
pub fn has_next(&mut self) -> bool { | ||
let borrow = self.iter.borrow(); | ||
borrow.clone().peekable().peek().is_some() | ||
} | ||
|
||
pub fn native_next(&mut self) -> Option<(K, V)> { | ||
let mut borrow = self.iter.borrow_mut(); | ||
borrow.next().map(|(k, v)| (k.clone(), v.clone())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use crate::btreemap_iter::BTreeMapIterator; | ||
use crate::file_storage::FileStorage; | ||
// This is the interface to the JVM that we'll call the majority of our | ||
// methods on. | ||
use jni::JNIEnv; | ||
|
||
// These objects are what you should use as arguments to your native | ||
// function. They carry extra lifetime information to prevent them escaping | ||
// this context and getting used after being GC'd. | ||
use jni::objects::{JClass, JValue}; | ||
|
||
// This is just a pointer. We'll be returning it from our function. We | ||
// can't return one of the objects with lifetime information because the | ||
// lifetime checker won't let us. | ||
use jni::sys::{jboolean, jlong, jobject}; | ||
|
||
impl BTreeMapIterator<'_, String, String> { | ||
pub fn from_jlong(value: jlong) -> &'static mut Self { | ||
unsafe { &mut *(value as *mut BTreeMapIterator<String, String>) } | ||
} | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "system" fn Java_dev_arkbuilders_core_BTreeMapIterator_create( | ||
_env: JNIEnv<'_>, | ||
_class: JClass, | ||
file_storage_ptr: jlong, | ||
) -> jlong { | ||
let file_storage = FileStorage::from_jlong(file_storage_ptr); | ||
let iter = BTreeMapIterator::new(file_storage); | ||
Box::into_raw(Box::new(iter)) as jlong | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "system" fn Java_dev_arkbuilders_core_BTreeMapIterator_hasNext( | ||
_env: JNIEnv<'_>, | ||
_class: JClass, | ||
btreemap_ptr: jlong, | ||
) -> jboolean { | ||
let iter = BTreeMapIterator::from_jlong(btreemap_ptr); | ||
iter.has_next() as jboolean | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "system" fn Java_dev_arkbuilders_core_BTreeMapIterator_next( | ||
mut env: JNIEnv<'_>, | ||
_class: JClass, | ||
btreemap_ptr: jlong, | ||
) -> jobject { | ||
let iter = BTreeMapIterator::from_jlong(btreemap_ptr); | ||
let (key, value) = iter.native_next().unwrap(); | ||
let key = env.new_string(key).unwrap(); | ||
let value = env.new_string(value).unwrap(); | ||
let pair = env | ||
.new_object( | ||
"java/util/AbstractMap$SimpleImmutableEntry", | ||
"(Ljava/lang/Object;Ljava/lang/Object;)V", | ||
&[JValue::Object(&key), JValue::Object(&value)], | ||
) | ||
.expect("Failed to create new Java Map object"); | ||
pair.as_raw() | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "system" fn Java_dev_arkbuilders_core_BTreeMapIterator_drop( | ||
_env: JNIEnv<'_>, | ||
_class: JClass, | ||
btreemap_ptr: jlong, | ||
) { | ||
unsafe { | ||
let _ = Box::from_raw( | ||
btreemap_ptr as *mut BTreeMapIterator<String, String>, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod btreemap_iter; | ||
pub mod file_storage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
|
||
# Ignore Gradle build output directory | ||
build | ||
*.log |
46 changes: 46 additions & 0 deletions
46
java/lib/src/main/java/dev/arkbuilders/core/BTreeMapIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package dev.arkbuilders.core; | ||
|
||
import java.util.Iterator; | ||
import java.util.Map; | ||
import java.util.NoSuchElementException; | ||
|
||
/** | ||
* Represents an iterator over a BTreeMap. | ||
*/ | ||
public class BTreeMapIterator implements Iterator<Map.Entry<String, String>>, AutoCloseable { | ||
private long btreemap_ptr; | ||
|
||
private static native long create(long file_storage_ptr); | ||
|
||
private static native boolean hasNext(long btreemap_ptr); | ||
|
||
private static native Object next(long btreemap_ptr); | ||
|
||
private static native void drop(long btreemap_ptr); | ||
|
||
BTreeMapIterator(long file_storage_ptr) { | ||
this.btreemap_ptr = create(file_storage_ptr); | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return hasNext(this.btreemap_ptr); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public Map.Entry<String, String> next() { | ||
if (!hasNext()) { | ||
throw new NoSuchElementException(); | ||
} | ||
return (Map.Entry<String, String>) next(this.btreemap_ptr); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
if (this.btreemap_ptr != 0) { | ||
drop(this.btreemap_ptr); | ||
this.btreemap_ptr = 0; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters