Skip to main content

memfork_core/
view.rs

1//! Read-only views over a commit (DESIGN §4.2, §4.3).
2
3use std::sync::Arc;
4
5use crate::commit::Commit;
6use crate::entry::Entry;
7use crate::id::CommitId;
8use crate::store::Store;
9
10/// A consistent, immutable view of a branch at one commit.
11///
12/// A view holds an `Arc` to a commit that can never change, so readers never
13/// block and never observe a half-applied change set (DESIGN §4.3). Holding a
14/// view keeps that commit alive even if its branch is discarded meanwhile.
15#[derive(Debug, Clone)]
16pub struct ReadView {
17    commit: Arc<Commit>,
18}
19
20impl ReadView {
21    pub(crate) fn new(commit: Arc<Commit>) -> Self {
22        Self { commit }
23    }
24
25    /// The commit this view is pinned to.
26    pub fn commit(&self) -> &Arc<Commit> {
27        &self.commit
28    }
29
30    /// The content address of the commit this view is pinned to.
31    pub fn commit_id(&self) -> CommitId {
32        self.commit.id
33    }
34
35    /// The sequence number of the commit this view is pinned to.
36    pub fn seq(&self) -> u64 {
37        self.commit.seq
38    }
39
40    /// Read one key.
41    pub fn get(&self, key: &str) -> Option<Arc<Entry>> {
42        self.commit.root.get(key).cloned()
43    }
44
45    /// Whether a key is present.
46    pub fn contains(&self, key: &str) -> bool {
47        self.commit.root.get(key).is_some()
48    }
49
50    /// How many keys the view holds.
51    pub fn len(&self) -> usize {
52        self.commit.root.len()
53    }
54
55    /// Whether the view holds no keys.
56    pub fn is_empty(&self) -> bool {
57        self.commit.root.is_empty()
58    }
59
60    /// Every key with the given prefix, in ascending key order, at most `limit`
61    /// of them. An empty prefix lists everything.
62    pub fn list(&self, prefix: &str, limit: Option<usize>) -> Vec<(String, Arc<Entry>)> {
63        let it = self
64            .commit
65            .root
66            .range_prefix(prefix)
67            .map(|(k, v)| (k.clone(), Arc::clone(v)));
68        match limit {
69            Some(n) => it.take(n).collect(),
70            None => it.collect(),
71        }
72    }
73
74    /// Every key, in ascending order.
75    pub fn keys(&self) -> Vec<String> {
76        self.commit.root.iter().map(|(k, _)| k.clone()).collect()
77    }
78}
79
80/// Summary of one branch (DESIGN §4.1).
81#[derive(Debug, Clone)]
82pub struct BranchInfo {
83    /// The branch name.
84    pub name: String,
85    /// The commit the branch currently points at.
86    pub head: CommitId,
87    /// The sequence number of that commit.
88    pub seq: u64,
89    /// How many keys the branch holds.
90    pub key_count: usize,
91    /// Whether this is the database's default branch, which cannot be discarded.
92    pub is_default: bool,
93    /// The commit the branch was created at: its fork point, or genesis for
94    /// the default branch.
95    pub forked_at: CommitId,
96}
97
98/// How a key differs between two commits (DESIGN §4.2).
99#[derive(Debug, Clone, Copy, PartialEq, Eq)]
100pub enum ChangeKind {
101    /// Present in `b`, absent in `a`.
102    Added,
103    /// Present in `a`, absent in `b`.
104    Removed,
105    /// Present in both with different content.
106    Modified,
107}
108
109impl ChangeKind {
110    /// A one-character marker, in the style of a diff.
111    pub fn marker(self) -> char {
112        match self {
113            ChangeKind::Added => '+',
114            ChangeKind::Removed => '-',
115            ChangeKind::Modified => '~',
116        }
117    }
118}
119
120/// One entry of a diff, sorted by key with the rest of the diff.
121#[derive(Debug, Clone)]
122pub struct Change {
123    /// The key that differs.
124    pub key: String,
125    /// How it differs.
126    pub kind: ChangeKind,
127}