Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: pushkarm029 <pushkarmishra029@gmail.com>
  • Loading branch information
Pushkarm029 committed Jun 30, 2024
1 parent db6a00f commit 4bc1c19
Show file tree
Hide file tree
Showing 15 changed files with 249 additions and 263 deletions.
3 changes: 3 additions & 0 deletions fs-storage/src/base_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ pub trait BaseStorage<K, V>: AsRef<BTreeMap<K, V>> {
/// from pre-configured location in the filesystem.
fn read_fs(&mut self) -> Result<&BTreeMap<K, V>>;

/// Get a value from the internal key-value mapping.
fn get(&self, id: &K) -> Result<&V>;

/// Write the internal key-value mapping
/// to pre-configured location in the filesystem.
fn write_fs(&mut self) -> Result<()>;
Expand Down
30 changes: 30 additions & 0 deletions fs-storage/src/btreemap_iter.rs
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()))
}
}
7 changes: 7 additions & 0 deletions fs-storage/src/file_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,13 @@ where
Ok(&self.data.entries)
}

/// Get a value from the internal mapping
fn get(&self, id: &K) -> Result<&V> {
self.data.entries.get(id).ok_or_else(|| {
ArklibError::Storage(self.label.clone(), "Key not found".to_owned())
})
}

/// Write the data to file
///
/// Update the modified timestamp in file metadata to avoid OS timing issues
Expand Down
75 changes: 75 additions & 0 deletions fs-storage/src/jni/btreemap_iter.rs
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)],
)
.unwrap();
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>,
);
}
}
29 changes: 27 additions & 2 deletions fs-storage/src/jni/file_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ use jni::objects::{JClass, JObject, JString, 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::{jlong, jobject};
use jni::sys::{jlong, jobject, jstring};
use jnix::{IntoJava, JnixEnv};

use crate::base_storage::BaseStorage;

use crate::file_storage::FileStorage;

impl FileStorage<String, String> {
fn from_jlong<'a>(value: jlong) -> &'a mut Self {
pub fn from_jlong<'a>(value: jlong) -> &'a mut Self {
unsafe { &mut *(value as *mut FileStorage<String, String>) }
}
}
Expand Down Expand Up @@ -172,6 +172,31 @@ pub extern "system" fn Java_dev_arkbuilders_core_FileStorage_readFS(
linked_hash_map.as_raw()
}

#[no_mangle]
pub extern "system" fn Java_dev_arkbuilders_core_FileStorage_get<'local>(
mut env: JNIEnv<'local>,
_class: JClass<'local>,
id: JString<'local>,
file_storage_ptr: jlong,
) -> jstring {
let id: String = env
.get_string(&id)
.expect("Failed to get string from JNI")
.into();
let file_storage = FileStorage::from_jlong(file_storage_ptr);

match file_storage.get(&id) {
Ok(value) => env
.new_string(value)
.expect("Failed to create new JString")
.into_raw(),
Err(err) => {
env.throw_new("java/lang/RuntimeException", &err.to_string())
.unwrap();
env.new_string("").expect("").into_raw()
}
}
}
#[no_mangle]
pub extern "system" fn Java_dev_arkbuilders_core_FileStorage_writeFS(
mut env: JNIEnv<'_>,
Expand Down
1 change: 1 addition & 0 deletions fs-storage/src/jni/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod btreemap_iter;
pub mod file_storage;
86 changes: 0 additions & 86 deletions fs-storage/src/jni/wrapper_btreemap.rs

This file was deleted.

2 changes: 1 addition & 1 deletion fs-storage/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pub mod base_storage;
pub mod btreemap_iter;
pub mod file_storage;
#[cfg(feature = "jni-bindings")]
pub mod jni;
pub mod monoid;
pub mod wrapper_btreemap;

mod utils;
pub const ARK_FOLDER: &str = ".ark";
Expand Down
79 changes: 0 additions & 79 deletions fs-storage/src/wrapper_btreemap.rs

This file was deleted.

1 change: 1 addition & 0 deletions java/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

# Ignore Gradle build output directory
build
*.log
Loading

0 comments on commit 4bc1c19

Please sign in to comment.