|
1 | | -//todo: uncomment once version 2.1 of GroveDB is released. |
2 | | -// mod v0; |
3 | | -// |
4 | | -// use crate::drive::Drive; |
5 | | -// use crate::error::drive::DriveError; |
6 | | -// use crate::error::Error; |
7 | | -// use crate::fees::op::LowLevelDriveOperation; |
8 | | -// use dpp::version::drive_versions::DriveVersion; |
9 | | -// use grovedb::{Element, TransactionArg}; |
10 | | -// use grovedb_path::SubtreePath; |
11 | | -// |
12 | | -// impl Drive { |
13 | | -// /// Inserts an element into groveDB only if the specified path and key do not exist. |
14 | | -// /// This operation costs are then stored in `drive_operations`. |
15 | | -// /// |
16 | | -// /// # Parameters |
17 | | -// /// * `path`: The groveDB hierarchical authenticated structure path where the new element is to be inserted. |
18 | | -// /// * `key`: The key where the new element should be inserted in the subtree. |
19 | | -// /// * `element`: The element to be inserted. |
20 | | -// /// * `transaction`: The groveDB transaction associated with this operation. |
21 | | -// /// * `drive_operations`: A vector to collect the costs of operations for later computation. In this case, |
22 | | -// /// it collects the cost of this insert operation if the path and key did not exist. |
23 | | -// /// * `platform_version`: The platform version to select the correct function version to run. |
24 | | -// /// |
25 | | -// /// # Returns |
26 | | -// /// * `Ok(true)` if the insertion was successful. |
27 | | -// /// * `Ok(false)` if the path and key already existed. |
28 | | -// /// * `Err(DriveError::UnknownVersionMismatch)` if the platform version does not match known versions. |
29 | | -// pub fn grove_insert_if_not_exists_return_existing_element<B: AsRef<[u8]>>( |
30 | | -// &self, |
31 | | -// path: SubtreePath<'_, B>, |
32 | | -// key: &[u8], |
33 | | -// element: Element, |
34 | | -// transaction: TransactionArg, |
35 | | -// drive_operations: Option<&mut Vec<LowLevelDriveOperation>>, |
36 | | -// drive_version: &DriveVersion, |
37 | | -// ) -> Result<Option<Element>, Error> { |
38 | | -// match drive_version.grove_methods.basic.grove_insert_if_not_exists { |
39 | | -// 0 => self.grove_insert_if_not_exists_return_existing_element_v0( |
40 | | -// path, |
41 | | -// key, |
42 | | -// element, |
43 | | -// transaction, |
44 | | -// drive_operations, |
45 | | -// drive_version, |
46 | | -// ), |
47 | | -// version => Err(Error::Drive(DriveError::UnknownVersionMismatch { |
48 | | -// method: "grove_insert_if_not_exists_return_existing_element".to_string(), |
49 | | -// known_versions: vec![0], |
50 | | -// received: version, |
51 | | -// })), |
52 | | -// } |
53 | | -// } |
54 | | -// } |
| 1 | +mod v0; |
| 2 | + |
| 3 | +use crate::drive::Drive; |
| 4 | +use crate::error::drive::DriveError; |
| 5 | +use crate::error::Error; |
| 6 | +use crate::fees::op::LowLevelDriveOperation; |
| 7 | +use dpp::version::drive_versions::DriveVersion; |
| 8 | +use grovedb::{Element, TransactionArg}; |
| 9 | +use grovedb_path::SubtreePath; |
| 10 | + |
| 11 | +impl Drive { |
| 12 | + /// Inserts an element into groveDB only if the specified path and key do not exist. |
| 13 | + /// This operation costs are then stored in `drive_operations`. |
| 14 | + /// |
| 15 | + /// # Parameters |
| 16 | + /// * `path`: The groveDB hierarchical authenticated structure path where the new element is to be inserted. |
| 17 | + /// * `key`: The key where the new element should be inserted in the subtree. |
| 18 | + /// * `element`: The element to be inserted. |
| 19 | + /// * `transaction`: The groveDB transaction associated with this operation. |
| 20 | + /// * `drive_operations`: A vector to collect the costs of operations for later computation. In this case, |
| 21 | + /// it collects the cost of this insert operation if the path and key did not exist. |
| 22 | + /// * `platform_version`: The platform version to select the correct function version to run. |
| 23 | + /// |
| 24 | + /// # Returns |
| 25 | + /// * `Ok(true)` if the insertion was successful. |
| 26 | + /// * `Ok(false)` if the path and key already existed. |
| 27 | + /// * `Err(DriveError::UnknownVersionMismatch)` if the platform version does not match known versions. |
| 28 | + pub fn grove_insert_if_not_exists_return_existing_element<B: AsRef<[u8]>>( |
| 29 | + &self, |
| 30 | + path: SubtreePath<'_, B>, |
| 31 | + key: &[u8], |
| 32 | + element: Element, |
| 33 | + transaction: TransactionArg, |
| 34 | + drive_operations: Option<&mut Vec<LowLevelDriveOperation>>, |
| 35 | + drive_version: &DriveVersion, |
| 36 | + ) -> Result<Option<Element>, Error> { |
| 37 | + match drive_version.grove_methods.basic.grove_insert_if_not_exists { |
| 38 | + 0 => self.grove_insert_if_not_exists_return_existing_element_v0( |
| 39 | + path, |
| 40 | + key, |
| 41 | + element, |
| 42 | + transaction, |
| 43 | + drive_operations, |
| 44 | + drive_version, |
| 45 | + ), |
| 46 | + version => Err(Error::Drive(DriveError::UnknownVersionMismatch { |
| 47 | + method: "grove_insert_if_not_exists_return_existing_element".to_string(), |
| 48 | + known_versions: vec![0], |
| 49 | + received: version, |
| 50 | + })), |
| 51 | + } |
| 52 | + } |
| 53 | +} |
0 commit comments