core_crypto_keystore/transaction/savepoint.rs
1use crate::{CryptoKeystoreError, Transaction};
2
3impl Transaction {
4 /// Do an operation within the context of a savepoint, with a custom check if the outcome indicates the savepoint
5 /// should commit.
6 ///
7 /// See [`Self::with_savepoint`].
8 ///
9 /// The only difference here is the `outcome_check`: this function is used to determine
10 /// whether or not the savepoint should be committed or rolled back. If it returns `true`,
11 /// the savepoint is committed. If `false`, it is rolled back.
12 pub async fn with_savepoint_with_outcome_check<T, E>(
13 &self,
14 savepoint_name: &str,
15 operation: impl AsyncFnOnce() -> Result<T, E>,
16 map_err: impl Fn(&'static str) -> Box<dyn FnOnce(CryptoKeystoreError) -> E>,
17 outcome_check: impl FnOnce(&Result<T, E>) -> bool,
18 ) -> Result<T, E> {
19 // note that in this function we don't bother caching the sql statements; they're kind of
20 // ad-hoc given the variable savepoint name, so we'd expect mostly cache misses anyway
21
22 // We'll need to release this guard and its locks so the interior operation can acquire them
23 {
24 let conn = self
25 .conn()
26 .map_err(map_err("getting sql connection to create savepoint"))?;
27 let mut stmt = conn
28 .prepare(&format!("SAVEPOINT {savepoint_name}"))
29 .map_err(Into::into)
30 .map_err(map_err("preparing statement to create savepoint"))?;
31 stmt.execute([])
32 .map_err(Into::into)
33 .map_err(map_err("creating savepoint"))?;
34 }
35
36 let outcome = operation().await;
37
38 // time to finalize the savepoint one way or the other
39 let conn = self
40 .conn()
41 .map_err(map_err("getting sql connection to finalize savepoint"))?;
42
43 if outcome_check(&outcome) {
44 let mut stmt = conn
45 .prepare(&format!("RELEASE SAVEPOINT {savepoint_name}"))
46 .map_err(Into::into)
47 .map_err(map_err("creating savepoint release stmt"))?;
48 stmt.execute([])
49 .map_err(Into::into)
50 .map_err(map_err("releasing savepoint"))?;
51 } else {
52 let mut stmt = conn
53 .prepare(&format!("ROLLBACK TO SAVEPOINT {savepoint_name}"))
54 .map_err(Into::into)
55 .map_err(map_err("creating savepoint rollback stmt"))?;
56 stmt.execute([])
57 .map_err(Into::into)
58 .map_err(map_err("rolling back savepoint"))?;
59 // after rolling back, releasing the savepoint frees the name, which is not semantically necessary
60 // but keeps the amount of savepoint data in memory small
61 let mut stmt = conn
62 .prepare(&format!("RELEASE SAVEPOINT {savepoint_name}"))
63 .map_err(Into::into)
64 .map_err(map_err("creating savepoint release stmt after rollback"))?;
65 stmt.execute([])
66 .map_err(Into::into)
67 .map_err(map_err("releasing savepoint after rollback"))?;
68 }
69
70 outcome
71 }
72
73 /// Do an operation within the context of a savepoint.
74 ///
75 /// <https://sqlite.org/lang_savepoint.html>
76 ///
77 /// A savepoint is like a transaction, but has a name and can be nested.
78 /// This allows for more fine-grained control over what makes it into the history,
79 /// or not.
80 ///
81 /// The operation produces an arbitrary result which is forwarded unmodified to the
82 /// output of this method. If that result is ok, the savepoint is released (i.e. committed).
83 /// If that result is an error, the savepoint is rolled back.
84 ///
85 /// Error-mapping is the most complicated part of this. When an error is produced in the course
86 /// of this method, we run the `map_err` function with a static context string describing what was
87 /// happening. That function must return a second function which transforms a [`CryptoKeystoreError`]
88 /// into an instance of `E`.
89 ///
90 /// This is like [`Self::with_savepoint_with_outcome_check`], but it uses a default outcome check: `Result::is_ok`.
91 pub async fn with_savepoint<T, E>(
92 &self,
93 savepoint_name: &str,
94 operation: impl AsyncFnOnce() -> Result<T, E>,
95 map_err: impl Fn(&'static str) -> Box<dyn FnOnce(CryptoKeystoreError) -> E>,
96 ) -> Result<T, E> {
97 self.with_savepoint_with_outcome_check(savepoint_name, operation, map_err, Result::is_ok)
98 .await
99 }
100}