ok/jj
1
0
Fork 0
forked from mirrors/jj

store: remove (weak) self-reference and take &Arc<Self> arguments instead

The `weak_self` stuff was from before I knew that `self` could be of
type `&Arc<Self>`.
This commit is contained in:
Martin von Zweigbergk 2021-09-16 08:19:17 -07:00
parent e76dc5a425
commit 0c1ce664ea
5 changed files with 19 additions and 42 deletions

View file

@ -50,7 +50,7 @@ impl IndexStore {
IndexStore { dir }
}
pub fn get_index_at_op(&self, op: &Operation, store: &Store) -> Arc<ReadonlyIndex> {
pub fn get_index_at_op(&self, op: &Operation, store: &Arc<Store>) -> Arc<ReadonlyIndex> {
let op_id_hex = op.id().hex();
let op_id_file = self.dir.join("operations").join(&op_id_hex);
if op_id_file.exists() {
@ -89,7 +89,7 @@ impl IndexStore {
fn index_at_operation(
&self,
store: &Store,
store: &Arc<Store>,
operation: &Operation,
) -> io::Result<Arc<ReadonlyIndex>> {
let view = operation.view();
@ -163,7 +163,7 @@ impl IndexStore {
// Returns the ancestors of heads with parents and predecessors come before the
// commit itself
fn topo_order_earlier_first(
store: &Store,
store: &Arc<Store>,
heads: Vec<CommitId>,
parent_file: Option<Arc<ReadonlyIndex>>,
) -> Vec<Commit> {

View file

@ -13,7 +13,6 @@
// limitations under the License.
#![feature(assert_matches)]
#![feature(get_mut_unchecked)]
#![feature(map_first_last)]
#![deny(unused_must_use)]

View file

@ -324,7 +324,7 @@ impl ReadonlyRepo {
if locked_index.is_none() {
locked_index.replace(
self.index_store
.get_index_at_op(&self.operation, self.store.as_ref()),
.get_index_at_op(&self.operation, &self.store),
);
}
let index: &Arc<ReadonlyIndex> = locked_index.as_ref().unwrap();

View file

@ -16,7 +16,7 @@ use std::collections::HashMap;
use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock, Weak};
use std::sync::{Arc, RwLock};
use crate::backend;
use crate::backend::{
@ -34,7 +34,6 @@ use crate::tree_builder::TreeBuilder;
/// adds the root commit and adds caching.
#[derive(Debug)]
pub struct Store {
weak_self: Option<Weak<Store>>,
backend: Box<dyn Backend>,
root_commit_id: CommitId,
commit_cache: RwLock<HashMap<CommitId, Arc<backend::Commit>>>,
@ -44,17 +43,12 @@ pub struct Store {
impl Store {
pub fn new(backend: Box<dyn Backend>) -> Arc<Self> {
let root_commit_id = CommitId(vec![0; backend.hash_length()]);
let mut wrapper = Arc::new(Store {
weak_self: None,
Arc::new(Store {
backend,
root_commit_id,
commit_cache: Default::default(),
tree_cache: Default::default(),
});
let weak_self = Arc::downgrade(&wrapper);
let mut ref_mut = unsafe { Arc::get_mut_unchecked(&mut wrapper) };
ref_mut.weak_self = Some(weak_self);
wrapper
})
}
pub fn load_store(repo_path: &Path) -> Arc<Store> {
@ -96,17 +90,13 @@ impl Store {
&self.root_commit_id
}
pub fn root_commit(&self) -> Commit {
pub fn root_commit(self: &Arc<Self>) -> Commit {
self.get_commit(&self.root_commit_id).unwrap()
}
pub fn get_commit(&self, id: &CommitId) -> BackendResult<Commit> {
pub fn get_commit(self: &Arc<Self>, id: &CommitId) -> BackendResult<Commit> {
let data = self.get_backend_commit(id)?;
Ok(Commit::new(
self.weak_self.as_ref().unwrap().upgrade().unwrap(),
id.clone(),
data,
))
Ok(Commit::new(self.clone(), id.clone(), data))
}
fn make_root_commit(&self) -> backend::Commit {
@ -151,29 +141,20 @@ impl Store {
Ok(data)
}
pub fn write_commit(&self, commit: backend::Commit) -> Commit {
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());
}
let commit = Commit::new(
self.weak_self.as_ref().unwrap().upgrade().unwrap(),
commit_id,
data,
);
commit
Commit::new(self.clone(), commit_id, data)
}
pub fn get_tree(&self, dir: &RepoPath, id: &TreeId) -> BackendResult<Tree> {
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.weak_self.as_ref().unwrap().upgrade().unwrap(),
dir.clone(),
id.clone(),
data,
))
Ok(Tree::new(self.clone(), dir.clone(), id.clone(), data))
}
fn get_backend_tree(&self, dir: &RepoPath, id: &TreeId) -> BackendResult<Arc<backend::Tree>> {
@ -219,10 +200,7 @@ impl Store {
self.backend.write_conflict(contents)
}
pub fn tree_builder(&self, base_tree_id: TreeId) -> TreeBuilder {
TreeBuilder::new(
self.weak_self.as_ref().unwrap().upgrade().unwrap(),
base_tree_id,
)
pub fn tree_builder(self: &Arc<Self>, base_tree_id: TreeId) -> TreeBuilder {
TreeBuilder::new(self.clone(), base_tree_id)
}
}

View file

@ -492,7 +492,7 @@ pub fn merge_trees(
base_tree: &Tree,
side2_tree: &Tree,
) -> Result<TreeId, BackendError> {
let store = base_tree.store().as_ref();
let store = base_tree.store();
let dir = base_tree.dir();
assert_eq!(side1_tree.dir(), dir);
assert_eq!(side2_tree.dir(), dir);
@ -534,7 +534,7 @@ pub fn merge_trees(
}
fn merge_tree_value(
store: &Store,
store: &Arc<Store>,
dir: &RepoPath,
basename: &RepoPathComponent,
maybe_base: Option<&TreeValue>,