Trait matterdb::DatabaseExt[][src]

pub trait DatabaseExt: Database {
    fn merge_with_backup(&self, patch: Patch) -> Result<Patch> { ... }
}

Extension trait for Database.

Provided methods

fn merge_with_backup(&self, patch: Patch) -> Result<Patch>[src]

Merges a patch into the database and creates a backup patch that reverses all the merged changes.

Safety

It is logically unsound to merge other patches to the database between the merge_with_backup call and merging the backup patch. This may lead to merge artifacts and an inconsistent database state.

An exception to this rule is creating backups for several merged patches and then applying backups in the reverse order:

let db = TemporaryDB::new();
let fork = db.fork();
fork.get_list("list").push(1_u32);
let backup1 = db.merge_with_backup(fork.into_patch()).unwrap();
let fork = db.fork();
fork.get_list("list").push(2_u32);
let backup2 = db.merge_with_backup(fork.into_patch()).unwrap();
let fork = db.fork();
fork.get_list("list").extend(vec![3_u32, 4]);
let backup3 = db.merge_with_backup(fork.into_patch()).unwrap();

fn enumerate_list<A: Access + Copy>(view: A) -> Vec<u32> {
    view.get_list("list").iter().collect()
}

assert_eq!(enumerate_list(&db.snapshot()), vec![1, 2, 3, 4]);
// Rollback the most recent merge.
db.merge(backup3).unwrap();
assert_eq!(enumerate_list(&db.snapshot()), vec![1, 2]);
// ...Then the penultimate merge.
db.merge(backup2).unwrap();
assert_eq!(enumerate_list(&db.snapshot()), vec![1]);
// ...Then the oldest one.
db.merge(backup1).unwrap();
assert!(enumerate_list(&db.snapshot()).is_empty());

Performance notes

This method is linear w.r.t. patch size (i.e., the total number of changes in it) plus, for each clear operation, the corresponding index size before clearing. As such, the method may be inappropriate to use with large patches.

Errors

Returns an error in the same situations as Database::merge().

Loading content...

Implementors

impl<T: Database> DatabaseExt for T[src]

Loading content...