1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use std::collections::HashMap;
use std::io::Read;
use std::path::PathBuf;
use std::sync::{Arc, RwLock};
use crate::backend;
use crate::backend::{
Backend, BackendResult, ChangeId, CommitId, Conflict, ConflictId, FileId, MillisSinceEpoch,
Signature, SymlinkId, Timestamp, TreeId,
};
use crate::commit::Commit;
use crate::git_backend::GitBackend;
use crate::local_backend::LocalBackend;
use crate::repo_path::RepoPath;
use crate::tree::Tree;
use crate::tree_builder::TreeBuilder;
#[derive(Debug)]
pub struct Store {
backend: Box<dyn Backend>,
root_commit_id: CommitId,
commit_cache: RwLock<HashMap<CommitId, Arc<backend::Commit>>>,
tree_cache: RwLock<HashMap<(RepoPath, TreeId), Arc<backend::Tree>>>,
}
impl Store {
fn new(backend: Box<dyn Backend>) -> Arc<Self> {
let root_commit_id = CommitId::new(vec![0; backend.hash_length()]);
Arc::new(Store {
backend,
root_commit_id,
commit_cache: Default::default(),
tree_cache: Default::default(),
})
}
pub fn init_local(store_path: PathBuf) -> Arc<Self> {
Store::new(Box::new(LocalBackend::init(store_path)))
}
pub fn init_internal_git(store_path: PathBuf) -> Arc<Self> {
Store::new(Box::new(GitBackend::init_internal(store_path)))
}
pub fn init_external_git(store_path: PathBuf, git_repo_path: PathBuf) -> Arc<Self> {
Store::new(Box::new(GitBackend::init_external(
store_path,
git_repo_path,
)))
}
pub fn load_store(store_path: PathBuf) -> Arc<Store> {
let git_target_path = store_path.join("git_target");
let backend: Box<dyn Backend> = if git_target_path.is_file() {
Box::new(GitBackend::load(store_path))
} else {
Box::new(LocalBackend::load(store_path))
};
Store::new(backend)
}
pub fn hash_length(&self) -> usize {
self.backend.hash_length()
}
pub fn git_repo(&self) -> Option<git2::Repository> {
self.backend.git_repo()
}
pub fn empty_tree_id(&self) -> &TreeId {
self.backend.empty_tree_id()
}
pub fn root_commit_id(&self) -> &CommitId {
&self.root_commit_id
}
pub fn root_commit(self: &Arc<Self>) -> Commit {
self.get_commit(&self.root_commit_id).unwrap()
}
pub fn get_commit(self: &Arc<Self>, id: &CommitId) -> BackendResult<Commit> {
let data = self.get_backend_commit(id)?;
Ok(Commit::new(self.clone(), id.clone(), data))
}
fn make_root_commit(&self) -> backend::Commit {
let timestamp = Timestamp {
timestamp: MillisSinceEpoch(0),
tz_offset: 0,
};
let signature = Signature {
name: String::new(),
email: String::new(),
timestamp,
};
let change_id = ChangeId::new(vec![0; 16]);
backend::Commit {
parents: vec![],
predecessors: vec![],
root_tree: self.backend.empty_tree_id().clone(),
change_id,
description: String::new(),
author: signature.clone(),
committer: signature,
is_open: false,
}
}
fn get_backend_commit(&self, id: &CommitId) -> BackendResult<Arc<backend::Commit>> {
{
let read_locked_cached = self.commit_cache.read().unwrap();
if let Some(data) = read_locked_cached.get(id).cloned() {
return Ok(data);
}
}
let commit = if id == self.root_commit_id() {
self.make_root_commit()
} else {
self.backend.read_commit(id)?
};
let data = Arc::new(commit);
let mut write_locked_cache = self.commit_cache.write().unwrap();
write_locked_cache.insert(id.clone(), data.clone());
Ok(data)
}
pub fn write_commit(self: &Arc<Self>, commit: backend::Commit) -> Commit {
let commit_id = self.backend.write_commit(&commit).unwrap();
let data = Arc::new(commit);
{
let mut write_locked_cache = self.commit_cache.write().unwrap();
write_locked_cache.insert(commit_id.clone(), data.clone());
}
Commit::new(self.clone(), commit_id, data)
}
pub fn get_tree(self: &Arc<Self>, dir: &RepoPath, id: &TreeId) -> BackendResult<Tree> {
let data = self.get_backend_tree(dir, id)?;
Ok(Tree::new(self.clone(), dir.clone(), id.clone(), data))
}
fn get_backend_tree(&self, dir: &RepoPath, id: &TreeId) -> BackendResult<Arc<backend::Tree>> {
let key = (dir.clone(), id.clone());
{
let read_locked_cache = self.tree_cache.read().unwrap();
if let Some(data) = read_locked_cache.get(&key).cloned() {
return Ok(data);
}
}
let data = Arc::new(self.backend.read_tree(dir, id)?);
let mut write_locked_cache = self.tree_cache.write().unwrap();
write_locked_cache.insert(key, data.clone());
Ok(data)
}
pub fn write_tree(&self, path: &RepoPath, contents: &backend::Tree) -> BackendResult<TreeId> {
self.backend.write_tree(path, contents)
}
pub fn read_file(&self, path: &RepoPath, id: &FileId) -> BackendResult<Box<dyn Read>> {
self.backend.read_file(path, id)
}
pub fn write_file(&self, path: &RepoPath, contents: &mut dyn Read) -> BackendResult<FileId> {
self.backend.write_file(path, contents)
}
pub fn read_symlink(&self, path: &RepoPath, id: &SymlinkId) -> BackendResult<String> {
self.backend.read_symlink(path, id)
}
pub fn write_symlink(&self, path: &RepoPath, contents: &str) -> BackendResult<SymlinkId> {
self.backend.write_symlink(path, contents)
}
pub fn read_conflict(&self, id: &ConflictId) -> BackendResult<Conflict> {
self.backend.read_conflict(id)
}
pub fn write_conflict(&self, contents: &Conflict) -> BackendResult<ConflictId> {
self.backend.write_conflict(contents)
}
pub fn tree_builder(self: &Arc<Self>, base_tree_id: TreeId) -> TreeBuilder {
TreeBuilder::new(self.clone(), base_tree_id)
}
}