Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use BaseStorage ref #97

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fs-storage/src/base_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,5 @@ pub trait BaseStorage<K, V>: AsRef<BTreeMap<K, V>> {
fn erase(&self) -> Result<()>;

/// Merge values from another key-value mapping.
fn merge_from(&mut self, other: impl AsRef<BTreeMap<K, V>>) -> Result<()>;
fn merge_from(&mut self, other: &BTreeMap<K, V>) -> Result<()>;
}
9 changes: 4 additions & 5 deletions fs-storage/src/file_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ where
SyncStatus::StorageStale => self.write_fs().map(|_| ()),
SyncStatus::Diverge => {
let data = self.load_fs_data()?;
self.merge_from(&data)?;
self.merge_from(&data.entries)?;
self.write_fs()?;
Ok(())
}
Expand Down Expand Up @@ -289,12 +289,11 @@ where
}

/// Merge the data from another storage instance into this storage instance
fn merge_from(&mut self, other: impl AsRef<BTreeMap<K, V>>) -> Result<()>
fn merge_from(&mut self, other: &BTreeMap<K, V>) -> Result<()>
where
V: Monoid<V>,
{
let other_entries = other.as_ref();
for (key, value) in other_entries {
for (key, value) in other {
if let Some(existing_value) = self.data.entries.get(key) {
let resolved_value = V::combine(existing_value, value);
self.set(key.clone(), resolved_value);
Expand Down Expand Up @@ -463,7 +462,7 @@ mod tests {
file_storage_2.set("key3".to_string(), 9);

file_storage_1
.merge_from(&file_storage_2)
.merge_from(&file_storage_2.data.entries)
.unwrap();
assert_eq!(file_storage_1.as_ref().get("key1"), Some(&3));
assert_eq!(file_storage_1.as_ref().get("key2"), Some(&6));
Expand Down
7 changes: 3 additions & 4 deletions fs-storage/src/folder_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,11 @@ where
}

/// Merge the data from another folder storage instance into this folder storage instance
fn merge_from(&mut self, other: impl AsRef<BTreeMap<K, V>>) -> Result<()>
fn merge_from(&mut self, other: &BTreeMap<K, V>) -> Result<()>
where
V: Monoid<V>,
{
let other_entries = other.as_ref();
for (key, value) in other_entries {
for (key, value) in other {
if let Some(existing_value) = self.data.get(key) {
let resolved_value = V::combine(existing_value, value);
self.set(key.clone(), resolved_value);
Expand Down Expand Up @@ -580,7 +579,7 @@ mod tests {
storage2.set("key1".to_string(), 3);
storage2.set("key3".to_string(), 9);

storage1.merge_from(&storage2).unwrap();
storage1.merge_from(&storage2.data).unwrap();
assert_eq!(storage1.as_ref().get("key1"), Some(&3));
assert_eq!(storage1.as_ref().get("key2"), Some(&6));
assert_eq!(storage1.as_ref().get("key3"), Some(&9));
Expand Down
2 changes: 1 addition & 1 deletion fs-storage/src/jni/file_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ pub extern "system" fn Java_dev_arkbuilders_core_FileStorage_merge(
other_file_storage_ptr: jlong,
) {
FileStorage::from_jlong(file_storage_ptr)
.merge_from(FileStorage::from_jlong(other_file_storage_ptr))
.merge_from(FileStorage::from_jlong(other_file_storage_ptr).as_ref())
.unwrap_or_else(|err| {
env.throw_new("java/lang/RuntimeException", err.to_string())
.unwrap();
Expand Down
Loading