forked from mirrors/jj
evolution: don't wrap State in Mutex<Option<>>
This commit is contained in:
parent
fce5ec21f6
commit
110c083e78
1 changed files with 10 additions and 33 deletions
|
@ -13,7 +13,7 @@
|
|||
// limitations under the License.
|
||||
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::sync::{Arc, Mutex, MutexGuard};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use crate::commit::Commit;
|
||||
use crate::commit_builder::CommitBuilder;
|
||||
|
@ -302,60 +302,43 @@ impl<'r> ReadonlyEvolution<'r> {
|
|||
}
|
||||
|
||||
pub fn start_modification<'m>(&self, repo: &'m MutableRepo<'r>) -> MutableEvolution<'r, 'm> {
|
||||
let locked_state = self.state.lock().unwrap();
|
||||
let state = locked_state.as_ref().map(|state| state.as_ref().clone());
|
||||
MutableEvolution {
|
||||
repo,
|
||||
state: Mutex::new(state),
|
||||
state: self.get_state().as_ref().clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MutableEvolution<'r, 'm: 'r> {
|
||||
repo: &'m MutableRepo<'r>,
|
||||
state: Mutex<Option<State>>,
|
||||
state: State,
|
||||
}
|
||||
|
||||
impl Evolution for MutableEvolution<'_, '_> {
|
||||
fn successors(&self, commit_id: &CommitId) -> HashSet<CommitId> {
|
||||
self.locked_state().as_ref().unwrap().successors(commit_id)
|
||||
self.state.successors(commit_id)
|
||||
}
|
||||
|
||||
fn is_obsolete(&self, commit_id: &CommitId) -> bool {
|
||||
self.locked_state().as_ref().unwrap().is_obsolete(commit_id)
|
||||
self.state.is_obsolete(commit_id)
|
||||
}
|
||||
|
||||
fn is_orphan(&self, commit_id: &CommitId) -> bool {
|
||||
self.locked_state().as_ref().unwrap().is_orphan(commit_id)
|
||||
self.state.is_orphan(commit_id)
|
||||
}
|
||||
|
||||
fn is_divergent(&self, change_id: &ChangeId) -> bool {
|
||||
self.locked_state()
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.is_divergent(change_id)
|
||||
self.state.is_divergent(change_id)
|
||||
}
|
||||
|
||||
fn new_parent(&self, old_parent_id: &CommitId) -> HashSet<CommitId> {
|
||||
self.locked_state()
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.new_parent(self.repo.store(), old_parent_id)
|
||||
self.state.new_parent(self.repo.store(), old_parent_id)
|
||||
}
|
||||
}
|
||||
|
||||
impl MutableEvolution<'_, '_> {
|
||||
fn locked_state(&self) -> MutexGuard<Option<State>> {
|
||||
let mut locked_state = self.state.lock().unwrap();
|
||||
if locked_state.is_none() {
|
||||
locked_state.replace(State::calculate(self.repo.store(), self.repo.view()));
|
||||
}
|
||||
locked_state
|
||||
}
|
||||
|
||||
pub fn invalidate(&mut self) {
|
||||
let mut locked_state = self.state.lock();
|
||||
locked_state.as_mut().unwrap().take();
|
||||
self.state = State::calculate(self.repo.store(), self.repo.view());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -366,13 +349,7 @@ pub fn evolve(
|
|||
) {
|
||||
let store = tx.store().clone();
|
||||
// TODO: update the state in the transaction
|
||||
let state = tx
|
||||
.as_repo_mut()
|
||||
.evolution_mut()
|
||||
.locked_state()
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.clone();
|
||||
let state = tx.as_repo_mut().evolution_mut().state.clone();
|
||||
|
||||
// Resolving divergence can creates new orphans but not vice versa, so resolve
|
||||
// divergence first.
|
||||
|
|
Loading…
Reference in a new issue