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

feat: added a convenience method that will return an existing item du… #342

Merged
merged 6 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions grovedb-version/src/version/grovedb_versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ pub struct GroveDBOperationsInsertVersions {
pub add_element_on_transaction: FeatureVersion,
pub add_element_without_transaction: FeatureVersion,
pub insert_if_not_exists: FeatureVersion,
pub insert_if_not_exists_return_existing_element: FeatureVersion,
pub insert_if_changed_value: FeatureVersion,
}

Expand Down
1 change: 1 addition & 0 deletions grovedb-version/src/version/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ pub const GROVE_V1: GroveVersion = GroveVersion {
add_element_on_transaction: 0,
add_element_without_transaction: 0,
insert_if_not_exists: 0,
insert_if_not_exists_return_existing_element: 0,
insert_if_changed_value: 0,
},
delete: GroveDBOperationsDeleteVersions {
Expand Down
40 changes: 40 additions & 0 deletions grovedb/src/operations/insert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,16 @@
})
}

fn insert_on_transaction<'db, 'b, B: AsRef<[u8]>>(
&self,
path: SubtreePath<'b, B>,
key: &[u8],
element: Element,
options: InsertOptions,
transaction: &'db Transaction,
batch: &StorageBatch,
grove_version: &GroveVersion,
) -> CostResult<(), Error> {

Check warning on line 126 in grovedb/src/operations/insert/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

this function has too many arguments (8/7)

warning: this function has too many arguments (8/7) --> grovedb/src/operations/insert/mod.rs:117:5 | 117 | / fn insert_on_transaction<'db, 'b, B: AsRef<[u8]>>( 118 | | &self, 119 | | path: SubtreePath<'b, B>, 120 | | key: &[u8], ... | 125 | | grove_version: &GroveVersion, 126 | | ) -> CostResult<(), Error> { | |______________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments
check_grovedb_v0_with_cost!(
"insert_on_transaction",
grove_version
Expand Down Expand Up @@ -214,16 +214,16 @@
/// first make sure other merk exist
/// if it exists, then create merk to be inserted, and get root hash
/// we only care about root hash of merk to be inserted
fn add_element_on_transaction<'db, B: AsRef<[u8]>>(
&'db self,
path: SubtreePath<B>,
key: &[u8],
element: Element,
options: InsertOptions,
transaction: &'db Transaction,
batch: &'db StorageBatch,
grove_version: &GroveVersion,
) -> CostResult<Merk<PrefixedRocksDbTransactionContext<'db>>, Error> {

Check warning on line 226 in grovedb/src/operations/insert/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

this function has too many arguments (8/7)

warning: this function has too many arguments (8/7) --> grovedb/src/operations/insert/mod.rs:217:5 | 217 | / fn add_element_on_transaction<'db, B: AsRef<[u8]>>( 218 | | &'db self, 219 | | path: SubtreePath<B>, 220 | | key: &[u8], ... | 225 | | grove_version: &GroveVersion, 226 | | ) -> CostResult<Merk<PrefixedRocksDbTransactionContext<'db>>, Error> { | |________________________________________________________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments
check_grovedb_v0_with_cost!(
"add_element_on_transaction",
grove_version
Expand Down Expand Up @@ -486,6 +486,7 @@
}

/// Insert if not exists
/// The bool return is whether we were able to insert
QuantumExplorer marked this conversation as resolved.
Show resolved Hide resolved
pub fn insert_if_not_exists<'b, B, P>(
&self,
path: P,
Expand Down Expand Up @@ -522,6 +523,45 @@
}
}

/// Insert if not exists
/// If the item does exist return it
pub fn insert_if_not_exists_return_existing_element<'b, B, P>(
QuantumExplorer marked this conversation as resolved.
Show resolved Hide resolved
&self,
path: P,
key: &[u8],
element: Element,
transaction: TransactionArg,
grove_version: &GroveVersion,
) -> CostResult<Option<Element>, Error>
where
B: AsRef<[u8]> + 'b,
P: Into<SubtreePath<'b, B>>,
{
check_grovedb_v0_with_cost!(
"insert_if_not_exists",
grove_version
.grovedb_versions
.operations
.insert
.insert_if_not_exists_return_existing_element
QuantumExplorer marked this conversation as resolved.
Show resolved Hide resolved
);

let mut cost = OperationCost::default();
let subtree_path: SubtreePath<_> = path.into();

let previous_element = cost_return_on_error!(
&mut cost,
self.get_raw_optional(subtree_path.clone(), key, transaction, grove_version)
);
if previous_element.is_some() {
Ok(previous_element).wrap_with_cost(cost)
} else {
self.insert(subtree_path, key, element, None, transaction, grove_version)
.map_ok(|_| None)
.add_cost(cost)
}
}
QuantumExplorer marked this conversation as resolved.
Show resolved Hide resolved

/// Insert if the value changed
/// We return if the value was inserted
/// If the value was changed then we return the previous element
Expand Down
Loading