|
| 1 | +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +use fnv::FnvHashMap; |
| 12 | +use std::hash::Hash; |
| 13 | +use std::ops; |
| 14 | + |
| 15 | +#[cfg(test)] |
| 16 | +mod test; |
| 17 | + |
| 18 | +pub struct SnapshotMap<K, V> |
| 19 | + where K: Hash + Clone + Eq |
| 20 | +{ |
| 21 | + map: FnvHashMap<K, V>, |
| 22 | + undo_log: Vec<UndoLog<K, V>>, |
| 23 | +} |
| 24 | + |
| 25 | +pub struct Snapshot { |
| 26 | + len: usize |
| 27 | +} |
| 28 | + |
| 29 | +enum UndoLog<K, V> { |
| 30 | + OpenSnapshot, |
| 31 | + CommittedSnapshot, |
| 32 | + Inserted(K), |
| 33 | + Overwrite(K, V), |
| 34 | +} |
| 35 | + |
| 36 | +impl<K, V> SnapshotMap<K, V> |
| 37 | + where K: Hash + Clone + Eq |
| 38 | +{ |
| 39 | + pub fn new() -> Self { |
| 40 | + SnapshotMap { |
| 41 | + map: FnvHashMap(), |
| 42 | + undo_log: vec![] |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + pub fn insert(&mut self, key: K, value: V) -> bool { |
| 47 | + match self.map.insert(key.clone(), value) { |
| 48 | + None => { |
| 49 | + if !self.undo_log.is_empty() { |
| 50 | + self.undo_log.push(UndoLog::Inserted(key)); |
| 51 | + } |
| 52 | + true |
| 53 | + } |
| 54 | + Some(old_value) => { |
| 55 | + if !self.undo_log.is_empty() { |
| 56 | + self.undo_log.push(UndoLog::Overwrite(key, old_value)); |
| 57 | + } |
| 58 | + false |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + pub fn remove(&mut self, key: K) -> bool { |
| 64 | + match self.map.remove(&key) { |
| 65 | + Some(old_value) => { |
| 66 | + if !self.undo_log.is_empty() { |
| 67 | + self.undo_log.push(UndoLog::Overwrite(key, old_value)); |
| 68 | + } |
| 69 | + true |
| 70 | + } |
| 71 | + None => { |
| 72 | + false |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + pub fn get(&self, key: &K) -> Option<&V> { |
| 78 | + self.map.get(key) |
| 79 | + } |
| 80 | + |
| 81 | + pub fn snapshot(&mut self) -> Snapshot { |
| 82 | + self.undo_log.push(UndoLog::OpenSnapshot); |
| 83 | + let len = self.undo_log.len() - 1; |
| 84 | + Snapshot { len: len } |
| 85 | + } |
| 86 | + |
| 87 | + fn assert_open_snapshot(&self, snapshot: &Snapshot) { |
| 88 | + assert!(snapshot.len < self.undo_log.len()); |
| 89 | + assert!(match self.undo_log[snapshot.len] { |
| 90 | + UndoLog::OpenSnapshot => true, |
| 91 | + _ => false |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + pub fn commit(&mut self, snapshot: Snapshot) { |
| 96 | + self.assert_open_snapshot(&snapshot); |
| 97 | + if snapshot.len == 0 { |
| 98 | + // The root snapshot. |
| 99 | + self.undo_log.truncate(0); |
| 100 | + } else { |
| 101 | + self.undo_log[snapshot.len] = UndoLog::CommittedSnapshot; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + pub fn rollback_to(&mut self, snapshot: Snapshot) { |
| 106 | + self.assert_open_snapshot(&snapshot); |
| 107 | + while self.undo_log.len() > snapshot.len + 1 { |
| 108 | + match self.undo_log.pop().unwrap() { |
| 109 | + UndoLog::OpenSnapshot => { |
| 110 | + panic!("cannot rollback an uncommitted snapshot"); |
| 111 | + } |
| 112 | + |
| 113 | + UndoLog::CommittedSnapshot => { } |
| 114 | + |
| 115 | + UndoLog::Inserted(key) => { |
| 116 | + self.map.remove(&key); |
| 117 | + } |
| 118 | + |
| 119 | + UndoLog::Overwrite(key, old_value) => { |
| 120 | + self.map.insert(key, old_value); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + let v = self.undo_log.pop().unwrap(); |
| 126 | + assert!(match v { UndoLog::OpenSnapshot => true, _ => false }); |
| 127 | + assert!(self.undo_log.len() == snapshot.len); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +impl<'k, K, V> ops::Index<&'k K> for SnapshotMap<K, V> |
| 132 | + where K: Hash + Clone + Eq |
| 133 | +{ |
| 134 | + type Output = V; |
| 135 | + fn index(&self, key: &'k K) -> &V { |
| 136 | + &self.map[key] |
| 137 | + } |
| 138 | +} |
0 commit comments