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::sync::{Arc, Mutex};
use crate::evolution::ReadonlyEvolution;
use crate::index::ReadonlyIndex;
use crate::op_store;
use crate::op_store::{OperationId, OperationMetadata};
use crate::operation::Operation;
use crate::repo::{MutableRepo, ReadonlyRepo, RepoLoader};
use crate::store::Timestamp;
use crate::view::ReadonlyView;
use crate::working_copy::WorkingCopy;
pub struct Transaction {
repo: Option<Arc<MutableRepo>>,
parents: Vec<OperationId>,
description: String,
start_time: Timestamp,
tags: HashMap<String, String>,
closed: bool,
}
impl Transaction {
pub fn new(mut_repo: Arc<MutableRepo>, description: &str) -> Transaction {
let parents = vec![mut_repo.base_repo().op_id().clone()];
Transaction {
repo: Some(mut_repo),
parents,
description: description.to_owned(),
start_time: Timestamp::now(),
tags: Default::default(),
closed: false,
}
}
pub fn base_repo(&self) -> &ReadonlyRepo {
self.repo.as_ref().unwrap().base_repo()
}
pub fn set_parents(&mut self, parents: Vec<OperationId>) {
self.parents = parents;
}
pub fn set_tag(&mut self, key: String, value: String) {
self.tags.insert(key, value);
}
pub fn mut_repo(&mut self) -> &mut MutableRepo {
Arc::get_mut(self.repo.as_mut().unwrap()).unwrap()
}
pub fn commit(self) -> Arc<ReadonlyRepo> {
self.write().publish()
}
pub fn write(mut self) -> UnpublishedOperation {
let mut_repo = Arc::try_unwrap(self.repo.take().unwrap()).ok().unwrap();
let base_repo = mut_repo.base_repo().clone();
let (mut_index, mut_view, maybe_mut_evolution) = mut_repo.consume();
let maybe_evolution =
maybe_mut_evolution.map(|mut_evolution| Arc::new(mut_evolution.freeze()));
let index = base_repo.index_store().write_index(mut_index).unwrap();
let view = mut_view.freeze();
let view_id = base_repo.op_store().write_view(view.store_view()).unwrap();
let mut operation_metadata =
OperationMetadata::new(self.description.clone(), self.start_time.clone());
operation_metadata.tags = self.tags.clone();
let store_operation = op_store::Operation {
view_id,
parents: self.parents.clone(),
metadata: operation_metadata,
};
let new_op_id = base_repo
.op_store()
.write_operation(&store_operation)
.unwrap();
let operation = Operation::new(base_repo.op_store().clone(), new_op_id, store_operation);
base_repo
.index_store()
.associate_file_with_operation(&index, operation.id())
.unwrap();
self.closed = true;
UnpublishedOperation::new(
base_repo.loader(),
operation,
view,
base_repo.working_copy().clone(),
index,
maybe_evolution,
)
}
pub fn discard(mut self) {
self.closed = true;
}
}
impl Drop for Transaction {
fn drop(&mut self) {
if !std::thread::panicking() {
debug_assert!(self.closed, "Transaction was dropped without being closed.");
}
}
}
struct NewRepoData {
operation: Operation,
view: ReadonlyView,
working_copy: Arc<Mutex<WorkingCopy>>,
index: Arc<ReadonlyIndex>,
evolution: Option<Arc<ReadonlyEvolution>>,
}
pub struct UnpublishedOperation {
repo_loader: RepoLoader,
data: Option<NewRepoData>,
closed: bool,
}
impl UnpublishedOperation {
fn new(
repo_loader: RepoLoader,
operation: Operation,
view: ReadonlyView,
working_copy: Arc<Mutex<WorkingCopy>>,
index: Arc<ReadonlyIndex>,
evolution: Option<Arc<ReadonlyEvolution>>,
) -> Self {
let data = Some(NewRepoData {
operation,
view,
working_copy,
index,
evolution,
});
UnpublishedOperation {
repo_loader,
data,
closed: false,
}
}
pub fn operation(&self) -> &Operation {
&self.data.as_ref().unwrap().operation
}
pub fn publish(mut self) -> Arc<ReadonlyRepo> {
let data = self.data.take().unwrap();
self.repo_loader
.op_heads_store()
.update_op_heads(&data.operation);
let repo = self.repo_loader.create_from(
data.operation,
data.view,
data.working_copy,
data.index,
data.evolution,
);
self.closed = true;
repo
}
pub fn leave_unpublished(mut self) -> Arc<ReadonlyRepo> {
let data = self.data.take().unwrap();
let repo = self.repo_loader.create_from(
data.operation,
data.view,
data.working_copy,
data.index,
data.evolution,
);
self.closed = true;
repo
}
}
impl Drop for UnpublishedOperation {
fn drop(&mut self) {
if !std::thread::panicking() {
debug_assert!(
self.closed,
"UnpublishedOperation was dropped without being closed."
);
}
}
}