From fd504b7d66e2684bd5d9e441c4cad4a1e4ffe775 Mon Sep 17 00:00:00 2001 From: "Dustin J. Mitchell" Date: Sat, 23 Apr 2022 18:51:40 +0000 Subject: [PATCH] add num_local_operations to Replica --- taskchampion/src/replica.rs | 7 +++++++ taskchampion/src/storage/inmemory.rs | 4 ++++ taskchampion/src/storage/mod.rs | 4 ++++ taskchampion/src/storage/sqlite.rs | 9 +++++++++ taskchampion/src/taskdb/mod.rs | 6 ++++++ 5 files changed, 30 insertions(+) diff --git a/taskchampion/src/replica.rs b/taskchampion/src/replica.rs index 3787f3b25..4bbbc9c32 100644 --- a/taskchampion/src/replica.rs +++ b/taskchampion/src/replica.rs @@ -254,6 +254,11 @@ impl Replica { } Ok(()) } + + /// Get the number of operations local to this replica and not yet synchronized to the server. + pub fn num_local_operations(&mut self) -> anyhow::Result { + self.taskdb.num_operations() + } } #[cfg(test)] @@ -399,6 +404,8 @@ mod tests { }, ] ); + + assert_eq!(rep.num_local_operations().unwrap(), 10); } #[test] diff --git a/taskchampion/src/storage/inmemory.rs b/taskchampion/src/storage/inmemory.rs index 4a4463c19..bde13637b 100644 --- a/taskchampion/src/storage/inmemory.rs +++ b/taskchampion/src/storage/inmemory.rs @@ -91,6 +91,10 @@ impl<'t> StorageTxn for Txn<'t> { Ok(self.data_ref().operations.clone()) } + fn num_operations(&mut self) -> anyhow::Result { + Ok(self.data_ref().operations.len()) + } + fn add_operation(&mut self, op: ReplicaOp) -> anyhow::Result<()> { self.mut_data_ref().operations.push(op); Ok(()) diff --git a/taskchampion/src/storage/mod.rs b/taskchampion/src/storage/mod.rs index dd3c9786a..d577103e5 100644 --- a/taskchampion/src/storage/mod.rs +++ b/taskchampion/src/storage/mod.rs @@ -82,6 +82,10 @@ pub trait StorageTxn { /// server yet) fn operations(&mut self) -> Result>; + /// Get the current set of outstanding operations (operations that have not been sync'd to the + /// server yet) + fn num_operations(&mut self) -> Result; + /// Add an operation to the end of the list of operations in the storage. Note that this /// merely *stores* the operation; it is up to the TaskDb to apply it. fn add_operation(&mut self, op: ReplicaOp) -> Result<()>; diff --git a/taskchampion/src/storage/sqlite.rs b/taskchampion/src/storage/sqlite.rs index 1f1fe239a..7e30931a4 100644 --- a/taskchampion/src/storage/sqlite.rs +++ b/taskchampion/src/storage/sqlite.rs @@ -257,6 +257,12 @@ impl<'t> StorageTxn for Txn<'t> { Ok(ret) } + fn num_operations(&mut self) -> anyhow::Result { + let t = self.get_txn()?; + let count: usize = t.query_row("SELECT count(*) FROM operations", [], |x| x.get(0))?; + Ok(count) + } + fn add_operation(&mut self, op: ReplicaOp) -> anyhow::Result<()> { let t = self.get_txn()?; @@ -627,6 +633,8 @@ mod test { ReplicaOp::Create { uuid: uuid2 }, ] ); + + assert_eq!(txn.num_operations()?, 2); } // set them to a different bunch @@ -678,6 +686,7 @@ mod test { }, ] ); + assert_eq!(txn.num_operations()?, 4); } Ok(()) } diff --git a/taskchampion/src/taskdb/mod.rs b/taskchampion/src/taskdb/mod.rs index 7e802313f..71404d968 100644 --- a/taskchampion/src/taskdb/mod.rs +++ b/taskchampion/src/taskdb/mod.rs @@ -128,6 +128,12 @@ impl TaskDb { undo::undo(txn.as_mut()) } + /// Get the number of un-synchronized operations in storage. + pub fn num_operations(&mut self) -> anyhow::Result { + let mut txn = self.storage.txn().unwrap(); + txn.num_operations() + } + // functions for supporting tests #[cfg(test)]