changeset 329:036e3084f8f0

snapshot: Make snapshots properly cloneable.
author Lewin Bormann <lbo@spheniscida.de>
date Sun, 01 Oct 2017 20:38:26 +0200
parents b5d3a9d12fa0
children cb3090f42af3
files src/snapshot.rs
diffstat 1 files changed, 24 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/snapshot.rs	Sun Oct 01 20:38:07 2017 +0200
+++ b/src/snapshot.rs	Sun Oct 01 20:38:26 2017 +0200
@@ -1,20 +1,37 @@
 use std::collections::HashMap;
 use types::{share, MAX_SEQUENCE_NUMBER, SequenceNumber, Shared};
 
+use std::rc::Rc;
+
 /// Opaque snapshot handle; Represents index to SnapshotList.map
 type SnapshotHandle = u64;
 
-pub struct Snapshot {
+/// An InnerSnapshot is shared by several Snapshots. This enables cloning snapshots, and a snapshot
+/// is released once the last instance is dropped.
+#[derive(Clone)]
+struct InnerSnapshot {
     id: SnapshotHandle,
+    seq: SequenceNumber,
     sl: Shared<InnerSnapshotList>,
 }
 
-impl Drop for Snapshot {
+impl Drop for InnerSnapshot {
     fn drop(&mut self) {
         self.sl.borrow_mut().delete(self.id);
     }
 }
 
+#[derive(Clone)]
+pub struct Snapshot {
+    inner: Rc<InnerSnapshot>,
+}
+
+impl Snapshot {
+    pub fn sequence(&self) -> SequenceNumber {
+        (*self.inner).seq
+    }
+}
+
 /// A list of all snapshots is kept in the DB.
 struct InnerSnapshotList {
     map: HashMap<SnapshotHandle, SequenceNumber>,
@@ -50,16 +67,14 @@
         }
 
         Snapshot {
-            id: sl.newest,
-            sl: inner,
+            inner: Rc::new(InnerSnapshot {
+                id: sl.newest,
+                seq: seq,
+                sl: inner,
+            }),
         }
     }
 
-    pub fn sequence_at(&self, ss: &Snapshot) -> Option<SequenceNumber> {
-        let sl = self.inner.borrow_mut();
-        sl.map.get(&ss.id).map(|sn| *sn)
-    }
-
     /// oldest returns the lowest sequence number of all snapshots. It returns 0 if no snapshots
     /// are present.
     pub fn oldest(&self) -> SequenceNumber {