2020-12-12 08:00:42 +00:00
|
|
|
// Copyright 2020 Google LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
use crate::commit::Commit;
|
2021-02-13 21:30:45 +00:00
|
|
|
use crate::evolution::MutableEvolution;
|
2021-01-31 02:50:27 +00:00
|
|
|
use crate::index::MutableIndex;
|
2021-03-12 23:46:06 +00:00
|
|
|
use crate::op_heads_store::OpHeadsStore;
|
2020-12-12 08:00:42 +00:00
|
|
|
use crate::op_store;
|
2021-03-13 17:12:05 +00:00
|
|
|
use crate::op_store::OperationId;
|
2021-03-14 05:38:37 +00:00
|
|
|
use crate::op_store::OperationMetadata;
|
2020-12-12 08:00:42 +00:00
|
|
|
use crate::operation::Operation;
|
2021-01-31 07:44:31 +00:00
|
|
|
use crate::repo::{MutableRepo, ReadonlyRepo, RepoRef};
|
2020-12-12 08:00:42 +00:00
|
|
|
use crate::settings::UserSettings;
|
|
|
|
use crate::store;
|
2020-12-26 07:55:18 +00:00
|
|
|
use crate::store::{CommitId, Timestamp};
|
2020-12-12 08:00:42 +00:00
|
|
|
use crate::store_wrapper::StoreWrapper;
|
2021-02-13 21:30:45 +00:00
|
|
|
use crate::view::MutableView;
|
2020-12-12 08:00:42 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
pub struct Transaction<'r> {
|
|
|
|
repo: Option<Arc<MutableRepo<'r>>>,
|
2021-03-13 17:12:05 +00:00
|
|
|
parents: Vec<OperationId>,
|
2020-12-12 08:00:42 +00:00
|
|
|
description: String,
|
|
|
|
start_time: Timestamp,
|
|
|
|
closed: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'r> Transaction<'r> {
|
2021-02-01 02:07:37 +00:00
|
|
|
pub fn new(mut_repo: Arc<MutableRepo<'r>>, description: &str) -> Transaction<'r> {
|
2021-03-13 17:12:05 +00:00
|
|
|
let parents = vec![mut_repo.base_repo().op_id().clone()];
|
2021-02-01 01:12:40 +00:00
|
|
|
Transaction {
|
|
|
|
repo: Some(mut_repo),
|
2021-03-13 17:12:05 +00:00
|
|
|
parents,
|
2020-12-12 08:00:42 +00:00
|
|
|
description: description.to_owned(),
|
|
|
|
start_time: Timestamp::now(),
|
|
|
|
closed: false,
|
2021-02-01 01:12:40 +00:00
|
|
|
}
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn base_repo(&self) -> &'r ReadonlyRepo {
|
2021-01-31 18:03:47 +00:00
|
|
|
self.repo.as_ref().unwrap().base_repo()
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2021-03-13 17:12:05 +00:00
|
|
|
pub fn set_parents(&mut self, parents: Vec<OperationId>) {
|
|
|
|
self.parents = parents;
|
|
|
|
}
|
2020-12-12 08:00:42 +00:00
|
|
|
pub fn store(&self) -> &Arc<StoreWrapper> {
|
2021-01-31 18:03:47 +00:00
|
|
|
self.repo.as_ref().unwrap().store()
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2021-01-31 07:44:31 +00:00
|
|
|
pub fn as_repo_ref(&self) -> RepoRef {
|
|
|
|
self.repo.as_ref().unwrap().as_repo_ref()
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2021-03-14 07:31:00 +00:00
|
|
|
pub fn mut_repo(&mut self) -> &mut MutableRepo<'r> {
|
2020-12-12 08:00:42 +00:00
|
|
|
Arc::get_mut(self.repo.as_mut().unwrap()).unwrap()
|
|
|
|
}
|
|
|
|
|
2021-01-31 02:50:27 +00:00
|
|
|
pub fn index(&self) -> &MutableIndex {
|
|
|
|
self.repo.as_ref().unwrap().index()
|
|
|
|
}
|
|
|
|
|
2021-02-13 21:30:45 +00:00
|
|
|
pub fn view(&self) -> &MutableView {
|
|
|
|
self.repo.as_ref().unwrap().view()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn evolution(&self) -> &MutableEvolution {
|
|
|
|
self.repo.as_ref().unwrap().evolution()
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
pub fn write_commit(&mut self, commit: store::Commit) -> Commit {
|
2021-03-07 23:11:34 +00:00
|
|
|
let mut_repo = Arc::get_mut(self.repo.as_mut().unwrap()).unwrap();
|
|
|
|
mut_repo.write_commit(commit)
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn check_out(&mut self, settings: &UserSettings, commit: &Commit) -> Commit {
|
|
|
|
let mut_repo = Arc::get_mut(self.repo.as_mut().unwrap()).unwrap();
|
2021-03-07 23:11:34 +00:00
|
|
|
mut_repo.check_out(settings, commit)
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_checkout(&mut self, id: CommitId) {
|
|
|
|
let mut_repo = Arc::get_mut(self.repo.as_mut().unwrap()).unwrap();
|
2021-03-07 23:11:34 +00:00
|
|
|
mut_repo.set_checkout(id);
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_head(&mut self, head: &Commit) {
|
|
|
|
let mut_repo = Arc::get_mut(self.repo.as_mut().unwrap()).unwrap();
|
2021-03-07 23:11:34 +00:00
|
|
|
mut_repo.add_head(head)
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn remove_head(&mut self, head: &Commit) {
|
|
|
|
let mut_repo = Arc::get_mut(self.repo.as_mut().unwrap()).unwrap();
|
2021-03-07 23:11:34 +00:00
|
|
|
mut_repo.remove_head(head)
|
2021-01-16 18:42:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_public_head(&mut self, head: &Commit) {
|
|
|
|
let mut_repo = Arc::get_mut(self.repo.as_mut().unwrap()).unwrap();
|
2021-03-07 23:11:34 +00:00
|
|
|
mut_repo.add_public_head(head)
|
2021-01-16 18:42:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn remove_public_head(&mut self, head: &Commit) {
|
|
|
|
let mut_repo = Arc::get_mut(self.repo.as_mut().unwrap()).unwrap();
|
2021-03-07 23:11:34 +00:00
|
|
|
mut_repo.remove_public_head(head);
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2021-01-03 08:26:57 +00:00
|
|
|
pub fn insert_git_ref(&mut self, name: String, commit_id: CommitId) {
|
|
|
|
let mut_repo = Arc::get_mut(self.repo.as_mut().unwrap()).unwrap();
|
2021-03-07 23:11:34 +00:00
|
|
|
mut_repo.insert_git_ref(name, commit_id);
|
2021-01-03 08:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn remove_git_ref(&mut self, name: &str) {
|
|
|
|
let mut_repo = Arc::get_mut(self.repo.as_mut().unwrap()).unwrap();
|
2021-03-07 23:11:34 +00:00
|
|
|
mut_repo.remove_git_ref(name);
|
2021-01-03 08:26:57 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
pub fn set_view(&mut self, data: op_store::View) {
|
|
|
|
let mut_repo = Arc::get_mut(self.repo.as_mut().unwrap()).unwrap();
|
2021-03-07 23:11:34 +00:00
|
|
|
mut_repo.set_view(data);
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 23:46:06 +00:00
|
|
|
/// Writes the transaction to the operation store and publishes it.
|
|
|
|
pub fn commit(self) -> Operation {
|
|
|
|
self.write().publish()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Writes the transaction to the operation store, but does not publish it.
|
|
|
|
/// That means that a repo can be loaded at the operation, but the
|
|
|
|
/// operation will not be seen when loading the repo at head.
|
|
|
|
pub fn write(mut self) -> UnpublishedOperation {
|
2021-01-31 18:03:47 +00:00
|
|
|
let mut_repo = Arc::try_unwrap(self.repo.take().unwrap()).ok().unwrap();
|
2021-03-10 23:07:59 +00:00
|
|
|
let base_repo = mut_repo.base_repo();
|
2021-02-08 01:59:30 +00:00
|
|
|
let (mut_index, mut_view) = mut_repo.consume();
|
2021-03-14 05:23:30 +00:00
|
|
|
let index = base_repo.index_store().write_index(mut_index).unwrap();
|
|
|
|
|
2021-03-14 05:38:37 +00:00
|
|
|
let view_id = base_repo
|
|
|
|
.op_store()
|
|
|
|
.write_view(mut_view.store_view())
|
|
|
|
.unwrap();
|
|
|
|
let operation_metadata =
|
|
|
|
OperationMetadata::new(self.description.clone(), self.start_time.clone());
|
2021-03-14 05:23:30 +00:00
|
|
|
let store_operation = op_store::Operation {
|
|
|
|
view_id,
|
2021-03-13 17:12:05 +00:00
|
|
|
parents: self.parents.clone(),
|
2021-03-14 05:23:30 +00:00
|
|
|
metadata: operation_metadata,
|
|
|
|
};
|
2021-03-14 05:38:37 +00:00
|
|
|
let new_op_id = base_repo
|
|
|
|
.op_store()
|
|
|
|
.write_operation(&store_operation)
|
|
|
|
.unwrap();
|
2021-03-14 05:23:30 +00:00
|
|
|
let operation = Operation::new(base_repo.op_store().clone(), new_op_id, store_operation);
|
|
|
|
|
2021-03-14 05:38:37 +00:00
|
|
|
base_repo
|
|
|
|
.index_store()
|
2021-03-03 06:53:20 +00:00
|
|
|
.associate_file_with_operation(&index, operation.id())
|
|
|
|
.unwrap();
|
2020-12-12 08:00:42 +00:00
|
|
|
self.closed = true;
|
2021-03-12 23:46:06 +00:00
|
|
|
UnpublishedOperation::new(base_repo.op_heads_store().clone(), operation)
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn discard(mut self) {
|
|
|
|
self.closed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-12 23:46:06 +00:00
|
|
|
impl Drop for Transaction<'_> {
|
2020-12-12 08:00:42 +00:00
|
|
|
fn drop(&mut self) {
|
|
|
|
if !std::thread::panicking() {
|
2021-02-07 07:33:15 +00:00
|
|
|
debug_assert!(self.closed, "Transaction was dropped without being closed.");
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-12 23:46:06 +00:00
|
|
|
|
|
|
|
pub struct UnpublishedOperation {
|
|
|
|
op_heads_store: Arc<OpHeadsStore>,
|
|
|
|
operation: Option<Operation>,
|
|
|
|
closed: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UnpublishedOperation {
|
|
|
|
fn new(op_heads_store: Arc<OpHeadsStore>, operation: Operation) -> Self {
|
|
|
|
UnpublishedOperation {
|
|
|
|
op_heads_store,
|
|
|
|
operation: Some(operation),
|
|
|
|
closed: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn operation(&self) -> &Operation {
|
|
|
|
self.operation.as_ref().unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn publish(mut self) -> Operation {
|
|
|
|
let operation = self.operation.take().unwrap();
|
|
|
|
self.op_heads_store.update_op_heads(&operation);
|
|
|
|
self.closed = true;
|
|
|
|
operation
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn leave_unpublished(mut self) -> Operation {
|
|
|
|
self.closed = true;
|
|
|
|
self.operation.take().unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for UnpublishedOperation {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if !std::thread::panicking() {
|
|
|
|
debug_assert!(
|
|
|
|
self.closed,
|
|
|
|
"UnpublishedOperation was dropped without being closed."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|