Merge pull request #353 from djmitche/issue352
Add `Replica.num_local_operations`
This commit is contained in:
@@ -109,6 +109,8 @@ static void test_replica_working_set(void) {
|
||||
|
||||
tc_working_set_free(ws);
|
||||
|
||||
TEST_ASSERT_EQUAL(19, tc_replica_num_local_operations(rep));
|
||||
|
||||
tc_replica_free(rep);
|
||||
}
|
||||
|
||||
|
||||
@@ -360,6 +360,19 @@ pub unsafe extern "C" fn tc_replica_undo(rep: *mut TCReplica, undone_out: *mut i
|
||||
)
|
||||
}
|
||||
|
||||
/// Get the number of local, un-synchronized operations, or -1 on error
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn tc_replica_num_local_operations(rep: *mut TCReplica) -> i64 {
|
||||
wrap(
|
||||
rep,
|
||||
|rep| {
|
||||
let count = rep.num_local_operations()? as i64;
|
||||
Ok(count)
|
||||
},
|
||||
-1,
|
||||
)
|
||||
}
|
||||
|
||||
/// Add an UndoPoint, if one has not already been added by this Replica. This occurs automatically
|
||||
/// when a change is made. The `force` flag allows forcing a new UndoPoint even if one has already
|
||||
/// been created by this Replica, and may be useful when a Replica instance is held for a long time
|
||||
|
||||
@@ -557,6 +557,11 @@ TCResult tc_replica_sync(struct TCReplica *rep, struct TCServer *server, bool av
|
||||
*/
|
||||
TCResult tc_replica_undo(struct TCReplica *rep, int32_t *undone_out);
|
||||
|
||||
/**
|
||||
* Get the number of local, un-synchronized operations, or -1 on error
|
||||
*/
|
||||
int64_t tc_replica_num_local_operations(struct TCReplica *rep);
|
||||
|
||||
/**
|
||||
* Add an UndoPoint, if one has not already been added by this Replica. This occurs automatically
|
||||
* when a change is made. The `force` flag allows forcing a new UndoPoint even if one has already
|
||||
|
||||
@@ -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<usize> {
|
||||
self.taskdb.num_operations()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -399,6 +404,8 @@ mod tests {
|
||||
},
|
||||
]
|
||||
);
|
||||
|
||||
assert_eq!(rep.num_local_operations().unwrap(), 10);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -91,6 +91,10 @@ impl<'t> StorageTxn for Txn<'t> {
|
||||
Ok(self.data_ref().operations.clone())
|
||||
}
|
||||
|
||||
fn num_operations(&mut self) -> anyhow::Result<usize> {
|
||||
Ok(self.data_ref().operations.len())
|
||||
}
|
||||
|
||||
fn add_operation(&mut self, op: ReplicaOp) -> anyhow::Result<()> {
|
||||
self.mut_data_ref().operations.push(op);
|
||||
Ok(())
|
||||
|
||||
@@ -82,6 +82,10 @@ pub trait StorageTxn {
|
||||
/// server yet)
|
||||
fn operations(&mut self) -> Result<Vec<ReplicaOp>>;
|
||||
|
||||
/// Get the current set of outstanding operations (operations that have not been sync'd to the
|
||||
/// server yet)
|
||||
fn num_operations(&mut self) -> Result<usize>;
|
||||
|
||||
/// 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<()>;
|
||||
|
||||
@@ -257,6 +257,12 @@ impl<'t> StorageTxn for Txn<'t> {
|
||||
Ok(ret)
|
||||
}
|
||||
|
||||
fn num_operations(&mut self) -> anyhow::Result<usize> {
|
||||
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(())
|
||||
}
|
||||
|
||||
@@ -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<usize> {
|
||||
let mut txn = self.storage.txn().unwrap();
|
||||
txn.num_operations()
|
||||
}
|
||||
|
||||
// functions for supporting tests
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user