rename DB to TaskDB for consistency

This commit is contained in:
Dustin J. Mitchell 2020-11-23 16:03:04 -05:00
parent 8e2b4c3f6c
commit 2296d0fa35
4 changed files with 34 additions and 34 deletions

View file

@ -1,7 +1,7 @@
use crate::errors::Error; use crate::errors::Error;
use crate::server::Server; use crate::server::Server;
use crate::task::{Status, Task}; use crate::task::{Status, Task};
use crate::taskdb::DB; use crate::taskdb::TaskDB;
use crate::taskstorage::{Operation, TaskMap, TaskStorage}; use crate::taskstorage::{Operation, TaskMap, TaskStorage};
use chrono::Utc; use chrono::Utc;
use failure::Fallible; use failure::Fallible;
@ -11,13 +11,13 @@ use uuid::Uuid;
/// A replica represents an instance of a user's task data, providing an easy interface /// A replica represents an instance of a user's task data, providing an easy interface
/// for querying and modifying that data. /// for querying and modifying that data.
pub struct Replica { pub struct Replica {
taskdb: DB, taskdb: TaskDB,
} }
impl Replica { impl Replica {
pub fn new(storage: Box<dyn TaskStorage>) -> Replica { pub fn new(storage: Box<dyn TaskStorage>) -> Replica {
return Replica { return Replica {
taskdb: DB::new(storage), taskdb: TaskDB::new(storage),
}; };
} }

View file

@ -7,7 +7,7 @@ use std::collections::HashSet;
use std::str; use std::str;
use uuid::Uuid; use uuid::Uuid;
pub struct DB { pub struct TaskDB {
storage: Box<dyn TaskStorage>, storage: Box<dyn TaskStorage>,
} }
@ -17,24 +17,24 @@ struct Version {
operations: Vec<Operation>, operations: Vec<Operation>,
} }
impl DB { impl TaskDB {
/// Create a new DB with the given backend storage /// Create a new TaskDB with the given backend storage
pub fn new(storage: Box<dyn TaskStorage>) -> DB { pub fn new(storage: Box<dyn TaskStorage>) -> TaskDB {
DB { storage } TaskDB { storage }
} }
#[cfg(test)] #[cfg(test)]
pub fn new_inmemory() -> DB { pub fn new_inmemory() -> TaskDB {
DB::new(Box::new(crate::taskstorage::InMemoryStorage::new())) TaskDB::new(Box::new(crate::taskstorage::InMemoryStorage::new()))
} }
/// Apply an operation to the DB. Aside from synchronization operations, this is the only way /// Apply an operation to the TaskDB. Aside from synchronization operations, this is the only way
/// to modify the DB. In cases where an operation does not make sense, this function will do /// to modify the TaskDB. In cases where an operation does not make sense, this function will do
/// nothing and return an error (but leave the DB in a consistent state). /// nothing and return an error (but leave the TaskDB in a consistent state).
pub fn apply(&mut self, op: Operation) -> Fallible<()> { pub fn apply(&mut self, op: Operation) -> Fallible<()> {
// TODO: differentiate error types here? // TODO: differentiate error types here?
let mut txn = self.storage.txn()?; let mut txn = self.storage.txn()?;
if let err @ Err(_) = DB::apply_op(txn.as_mut(), &op) { if let err @ Err(_) = TaskDB::apply_op(txn.as_mut(), &op) {
return err; return err;
} }
txn.add_operation(op)?; txn.add_operation(op)?;
@ -183,7 +183,7 @@ impl DB {
assert_eq!(version.version, txn.base_version()? + 1); assert_eq!(version.version, txn.base_version()? + 1);
println!("applying version {:?} from server", version.version); println!("applying version {:?} from server", version.version);
DB::apply_version(txn.as_mut(), version)?; TaskDB::apply_version(txn.as_mut(), version)?;
} }
let operations: Vec<Operation> = txn.operations()?.iter().map(|o| o.clone()).collect(); let operations: Vec<Operation> = txn.operations()?.iter().map(|o| o.clone()).collect();
@ -254,7 +254,7 @@ impl DB {
} }
} }
if let Some(o) = svr_op { if let Some(o) = svr_op {
if let Err(e) = DB::apply_op(txn, &o) { if let Err(e) = TaskDB::apply_op(txn, &o) {
println!("Invalid operation when syncing: {} (ignored)", e); println!("Invalid operation when syncing: {} (ignored)", e);
} }
} }
@ -309,7 +309,7 @@ mod tests {
#[test] #[test]
fn test_apply_create() { fn test_apply_create() {
let mut db = DB::new_inmemory(); let mut db = TaskDB::new_inmemory();
let uuid = Uuid::new_v4(); let uuid = Uuid::new_v4();
let op = Operation::Create { uuid }; let op = Operation::Create { uuid };
db.apply(op.clone()).unwrap(); db.apply(op.clone()).unwrap();
@ -320,7 +320,7 @@ mod tests {
#[test] #[test]
fn test_apply_create_exists() { fn test_apply_create_exists() {
let mut db = DB::new_inmemory(); let mut db = TaskDB::new_inmemory();
let uuid = Uuid::new_v4(); let uuid = Uuid::new_v4();
let op = Operation::Create { uuid }; let op = Operation::Create { uuid };
db.apply(op.clone()).unwrap(); db.apply(op.clone()).unwrap();
@ -335,7 +335,7 @@ mod tests {
#[test] #[test]
fn test_apply_create_update() { fn test_apply_create_update() {
let mut db = DB::new_inmemory(); let mut db = TaskDB::new_inmemory();
let uuid = Uuid::new_v4(); let uuid = Uuid::new_v4();
let op1 = Operation::Create { uuid }; let op1 = Operation::Create { uuid };
db.apply(op1.clone()).unwrap(); db.apply(op1.clone()).unwrap();
@ -356,7 +356,7 @@ mod tests {
#[test] #[test]
fn test_apply_create_update_delete_prop() { fn test_apply_create_update_delete_prop() {
let mut db = DB::new_inmemory(); let mut db = TaskDB::new_inmemory();
let uuid = Uuid::new_v4(); let uuid = Uuid::new_v4();
let op1 = Operation::Create { uuid }; let op1 = Operation::Create { uuid };
db.apply(op1.clone()).unwrap(); db.apply(op1.clone()).unwrap();
@ -398,7 +398,7 @@ mod tests {
#[test] #[test]
fn test_apply_update_does_not_exist() { fn test_apply_update_does_not_exist() {
let mut db = DB::new_inmemory(); let mut db = TaskDB::new_inmemory();
let uuid = Uuid::new_v4(); let uuid = Uuid::new_v4();
let op = Operation::Update { let op = Operation::Update {
uuid, uuid,
@ -417,7 +417,7 @@ mod tests {
#[test] #[test]
fn test_apply_create_delete() { fn test_apply_create_delete() {
let mut db = DB::new_inmemory(); let mut db = TaskDB::new_inmemory();
let uuid = Uuid::new_v4(); let uuid = Uuid::new_v4();
let op1 = Operation::Create { uuid }; let op1 = Operation::Create { uuid };
db.apply(op1.clone()).unwrap(); db.apply(op1.clone()).unwrap();
@ -431,7 +431,7 @@ mod tests {
#[test] #[test]
fn test_apply_delete_not_present() { fn test_apply_delete_not_present() {
let mut db = DB::new_inmemory(); let mut db = TaskDB::new_inmemory();
let uuid = Uuid::new_v4(); let uuid = Uuid::new_v4();
let op1 = Operation::Delete { uuid }; let op1 = Operation::Delete { uuid };
@ -446,7 +446,7 @@ mod tests {
#[test] #[test]
fn rebuild_working_set() -> Fallible<()> { fn rebuild_working_set() -> Fallible<()> {
let mut db = DB::new_inmemory(); let mut db = TaskDB::new_inmemory();
let uuids = vec![ let uuids = vec![
Uuid::new_v4(), // 0: pending, not already in working set Uuid::new_v4(), // 0: pending, not already in working set
Uuid::new_v4(), // 1: pending, already in working set Uuid::new_v4(), // 1: pending, already in working set
@ -455,7 +455,7 @@ mod tests {
Uuid::new_v4(), // 4: pending, already in working set Uuid::new_v4(), // 4: pending, already in working set
]; ];
// add everything to the DB // add everything to the TaskDB
for uuid in &uuids { for uuid in &uuids {
db.apply(Operation::Create { uuid: uuid.clone() })?; db.apply(Operation::Create { uuid: uuid.clone() })?;
} }
@ -513,8 +513,8 @@ mod tests {
Ok(()) Ok(())
} }
fn newdb() -> DB { fn newdb() -> TaskDB {
DB::new(Box::new(InMemoryStorage::new())) TaskDB::new(Box::new(InMemoryStorage::new()))
} }
#[test] #[test]
@ -666,7 +666,7 @@ mod tests {
#[test] #[test]
// check that various sequences of operations on mulitple db's do not get the db's into an // check that various sequences of operations on mulitple db's do not get the db's into an
// incompatible state. The main concern here is that there might be a sequence of create // incompatible state. The main concern here is that there might be a sequence of create
// and delete operations that results in a task existing in one DB but not existing in // and delete operations that results in a task existing in one TaskDB but not existing in
// another. So, the generated sequences focus on a single task UUID. // another. So, the generated sequences focus on a single task UUID.
fn transform_sequences_of_operations(action_sequence in action_sequence_strategy()) { fn transform_sequences_of_operations(action_sequence in action_sequence_strategy()) {
let mut server = TestServer::new(); let mut server = TestServer::new();

View file

@ -68,7 +68,7 @@ pub trait TaskStorageTxn {
fn operations<'a>(&mut self) -> Fallible<Vec<Operation>>; fn operations<'a>(&mut self) -> Fallible<Vec<Operation>>;
/// Add an operation to the end of the list of operations in the storage. Note that this /// 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 DB to apply it. /// merely *stores* the operation; it is up to the TaskDB to apply it.
fn add_operation(&mut self, op: Operation) -> Fallible<()>; fn add_operation(&mut self, op: Operation) -> Fallible<()>;
/// Replace the current list of operations with a new list. /// Replace the current list of operations with a new list.

View file

@ -128,7 +128,7 @@ impl Operation {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
use crate::taskdb::DB; use crate::taskdb::TaskDB;
use crate::taskstorage::InMemoryStorage; use crate::taskstorage::InMemoryStorage;
use chrono::{Duration, Utc}; use chrono::{Duration, Utc};
use proptest::prelude::*; use proptest::prelude::*;
@ -148,7 +148,7 @@ mod test {
// check that the two operation sequences have the same effect, enforcing the invariant of // check that the two operation sequences have the same effect, enforcing the invariant of
// the transform function. // the transform function.
let mut db1 = DB::new_inmemory(); let mut db1 = TaskDB::new_inmemory();
if let Some(ref o) = setup { if let Some(ref o) = setup {
db1.apply(o.clone()).unwrap(); db1.apply(o.clone()).unwrap();
} }
@ -157,7 +157,7 @@ mod test {
db1.apply(o).unwrap(); db1.apply(o).unwrap();
} }
let mut db2 = DB::new_inmemory(); let mut db2 = TaskDB::new_inmemory();
if let Some(ref o) = setup { if let Some(ref o) = setup {
db2.apply(o.clone()).unwrap(); db2.apply(o.clone()).unwrap();
} }
@ -310,8 +310,8 @@ mod test {
fn transform_invariant_holds(o1 in operation_strategy(), o2 in operation_strategy()) { fn transform_invariant_holds(o1 in operation_strategy(), o2 in operation_strategy()) {
let (o1p, o2p) = Operation::transform(o1.clone(), o2.clone()); let (o1p, o2p) = Operation::transform(o1.clone(), o2.clone());
let mut db1 = DB::new(Box::new(InMemoryStorage::new())); let mut db1 = TaskDB::new(Box::new(InMemoryStorage::new()));
let mut db2 = DB::new(Box::new(InMemoryStorage::new())); let mut db2 = TaskDB::new(Box::new(InMemoryStorage::new()));
// Ensure that any expected tasks already exist // Ensure that any expected tasks already exist
if let Operation::Update{ ref uuid, .. } = o1 { if let Operation::Update{ ref uuid, .. } = o1 {