Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
fominok committed Nov 26, 2024
1 parent 0d92ceb commit 131e9a5
Show file tree
Hide file tree
Showing 2 changed files with 434 additions and 250 deletions.
15 changes: 6 additions & 9 deletions grovedb/src/element/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,15 +248,14 @@ impl Element {
/// be loaded by this moment If transaction is not passed, the batch
/// will be written immediately. If transaction is passed, the operation
/// will be committed on the transaction commit.
/// The bool represents whether a propagation of references is needed.
/// If the value changed, it returns the old element under `Some`.
pub fn insert_if_changed_value<'db, S: StorageContext<'db>>(
self,
merk: &mut Merk<S>,
key: &[u8],
options: Option<MerkOptions>,
grove_version: &GroveVersion,
) -> CostResult<(bool, Option<Element>), Error> {
) -> CostResult<Option<Element>, Error> {
check_grovedb_v0_with_cost!(
"insert_if_changed_value",
grove_version
Expand All @@ -282,14 +281,13 @@ impl Element {
.unwrap_or(true);

if changed {
let has_references = to_insert.has_backward_references();
cost_return_on_error!(
&mut cost,
to_insert.insert(merk, key, options, grove_version)
);
Ok((has_references, previous_element)).wrap_with_cost(cost)
Ok(previous_element).wrap_with_cost(cost)
} else {
Ok((false, None)).wrap_with_cost(cost)
Ok(None).wrap_with_cost(cost)
}
}

Expand Down Expand Up @@ -636,14 +634,13 @@ mod tests {

merk.commit(grove_version);

let (inserted, previous) = Element::new_item(b"value".to_vec())
let previous = Element::new_item(b"value".to_vec())
.insert_if_changed_value(&mut merk, b"another-key", None, grove_version)
.unwrap()
.expect("expected successful insertion 2");

merk.commit(grove_version);

assert!(!inserted);
assert_eq!(previous, None);
assert_eq!(
Element::get(&merk, b"another-key", true, grove_version)
Expand Down Expand Up @@ -678,7 +675,7 @@ mod tests {

let batch = StorageBatch::new();
let mut merk = empty_path_merk(&*storage, &batch, &tx, grove_version);
let (_, previous) = Element::new_item(b"value2".to_vec())
let previous = Element::new_item(b"value2".to_vec())
.insert_if_changed_value(&mut merk, b"another-key", None, grove_version)
.unwrap()
.expect("expected successful insertion 2");
Expand Down Expand Up @@ -707,7 +704,7 @@ mod tests {
.insert(&mut merk, b"mykey", None, grove_version)
.unwrap()
.expect("expected successful insertion");
let (_, previous) = Element::new_item(b"value2".to_vec())
let previous = Element::new_item(b"value2".to_vec())
.insert_if_changed_value(&mut merk, b"another-key", None, grove_version)
.unwrap()
.expect("expected successful insertion 2");
Expand Down
Loading

0 comments on commit 131e9a5

Please sign in to comment.