Skip to content

Commit

Permalink
Use BaseStorage ref
Browse files Browse the repository at this point in the history
  • Loading branch information
oluiscabral committed Dec 23, 2024
1 parent 66c9362 commit 169eda5
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 11 deletions.
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

0 comments on commit 169eda5

Please sign in to comment.