Add Replica::num_undo_points and exclude undo points from num_operations

This commit is contained in:
Dustin J. Mitchell
2022-07-17 23:22:27 +00:00
parent 17726ddfe4
commit 4a1556ccb9
6 changed files with 79 additions and 6 deletions

View File

@@ -259,6 +259,11 @@ impl Replica {
pub fn num_local_operations(&mut self) -> anyhow::Result<usize> {
self.taskdb.num_operations()
}
/// Get the number of undo points available (number of times `undo` will succeed).
pub fn num_undo_points(&mut self) -> anyhow::Result<usize> {
self.taskdb.num_undo_points()
}
}
#[cfg(test)]
@@ -405,7 +410,11 @@ mod tests {
]
);
assert_eq!(rep.num_local_operations().unwrap(), 10);
// num_local_operations includes all but the undo point
assert_eq!(rep.num_local_operations().unwrap(), 9);
// num_undo_points includes only the undo point
assert_eq!(rep.num_undo_points().unwrap(), 1);
}
#[test]

View File

@@ -59,6 +59,11 @@ impl ReplicaOp {
}
}
/// Determine whether this is an undo point.
pub fn is_undo_point(&self) -> bool {
self == &Self::UndoPoint
}
/// Generate a sequence of SyncOp's to reverse the effects of this ReplicaOp.
pub fn reverse_ops(self) -> Vec<SyncOp> {
match self {

View File

@@ -128,10 +128,25 @@ impl TaskDb {
undo::undo(txn.as_mut())
}
/// Get the number of un-synchronized operations in storage.
/// Get the number of un-synchronized operations in storage, excluding undo
/// operations.
pub fn num_operations(&mut self) -> anyhow::Result<usize> {
let mut txn = self.storage.txn().unwrap();
txn.num_operations()
Ok(txn
.operations()?
.iter()
.filter(|o| !o.is_undo_point())
.count())
}
/// Get the number of (un-synchronized) undo points in storage.
pub fn num_undo_points(&mut self) -> anyhow::Result<usize> {
let mut txn = self.storage.txn().unwrap();
Ok(txn
.operations()?
.iter()
.filter(|o| o.is_undo_point())
.count())
}
// functions for supporting tests
@@ -196,6 +211,30 @@ mod tests {
assert_eq!(db.operations(), vec![ReplicaOp::UndoPoint]);
}
#[test]
fn test_num_operations() {
let mut db = TaskDb::new_inmemory();
db.apply(SyncOp::Create {
uuid: Uuid::new_v4(),
})
.unwrap();
db.add_undo_point().unwrap();
db.apply(SyncOp::Create {
uuid: Uuid::new_v4(),
})
.unwrap();
assert_eq!(db.num_operations().unwrap(), 2);
}
#[test]
fn test_num_undo_points() {
let mut db = TaskDb::new_inmemory();
db.add_undo_point().unwrap();
assert_eq!(db.num_undo_points().unwrap(), 1);
db.add_undo_point().unwrap();
assert_eq!(db.num_undo_points().unwrap(), 2);
}
fn newdb() -> TaskDb {
TaskDb::new(Box::new(InMemoryStorage::new()))
}