index: move ReadonlyFile::associate_with_operation() to IndexStore

After this patch, the `index` module no longer knows about the
".jj/index/operations/" directory; that knowledge is now only in
`IndexStore`.
This commit is contained in:
Martin von Zweigbergk 2021-03-02 22:53:20 -08:00
parent c4fe7aab10
commit 502ba895f5
4 changed files with 28 additions and 14 deletions

View file

@ -28,8 +28,6 @@ use tempfile::NamedTempFile;
use crate::commit::Commit; use crate::commit::Commit;
use crate::op_store::OperationId;
use crate::store::{ChangeId, CommitId}; use crate::store::{ChangeId, CommitId};
use std::fmt::{Debug, Formatter}; use std::fmt::{Debug, Formatter};
@ -1344,19 +1342,14 @@ impl ReadonlyIndex {
})) }))
} }
/// Records a link from the given operation to the this index version.
pub fn associate_with_operation(&self, op_id: &OperationId) -> io::Result<()> {
let mut temp_file = NamedTempFile::new_in(&self.dir)?;
let file = temp_file.as_file_mut();
file.write_all(&self.name.as_bytes()).unwrap();
temp_file.persist(&self.dir.join("operations").join(op_id.hex()))?;
Ok(())
}
pub fn num_commits(&self) -> u32 { pub fn num_commits(&self) -> u32 {
CompositeIndex(self).num_commits() CompositeIndex(self).num_commits()
} }
pub fn name(&self) -> &str {
&self.name
}
pub fn stats(&self) -> IndexStats { pub fn stats(&self) -> IndexStats {
CompositeIndex(self).stats() CompositeIndex(self).stats()
} }

View file

@ -23,9 +23,10 @@ use crate::store_wrapper::StoreWrapper;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::fs::File; use std::fs::File;
use std::io; use std::io;
use std::io::Read; use std::io::{Read, Write};
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::Arc; use std::sync::Arc;
use tempfile::NamedTempFile;
pub struct IndexStore { pub struct IndexStore {
dir: PathBuf, dir: PathBuf,
@ -132,10 +133,23 @@ impl IndexStore {
let index_file = data.save()?; let index_file = data.save()?;
index_file.associate_with_operation(operation.id())?; self.associate_file_with_operation(&index_file, operation.id())?;
Ok(index_file) Ok(index_file)
} }
/// Records a link from the given operation to the this index version.
pub fn associate_file_with_operation(
&self,
index: &ReadonlyIndex,
op_id: &OperationId,
) -> io::Result<()> {
let mut temp_file = NamedTempFile::new_in(&self.dir)?;
let file = temp_file.as_file_mut();
file.write_all(&index.name().as_bytes()).unwrap();
temp_file.persist(&self.dir.join("operations").join(op_id.hex()))?;
Ok(())
}
} }
// Returns the ancestors of heads with parents and predecessors come before the // Returns the ancestors of heads with parents and predecessors come before the

View file

@ -298,6 +298,10 @@ impl ReadonlyRepo {
&self.store &self.store
} }
pub fn index_store(&self) -> &IndexStore {
&self.index_store
}
pub fn settings(&self) -> &RepoSettings { pub fn settings(&self) -> &RepoSettings {
&self.settings &self.settings
} }

View file

@ -202,10 +202,13 @@ impl<'r> Transaction<'r> {
pub fn commit(mut self) -> Operation { pub fn commit(mut self) -> Operation {
let mut_repo = Arc::try_unwrap(self.repo.take().unwrap()).ok().unwrap(); let mut_repo = Arc::try_unwrap(self.repo.take().unwrap()).ok().unwrap();
let index_store = mut_repo.base_repo().index_store();
let (mut_index, mut_view) = mut_repo.consume(); let (mut_index, mut_view) = mut_repo.consume();
let index = mut_index.save().unwrap(); let index = mut_index.save().unwrap();
let operation = mut_view.save(self.description.clone(), self.start_time.clone()); let operation = mut_view.save(self.description.clone(), self.start_time.clone());
index.associate_with_operation(operation.id()).unwrap(); index_store
.associate_file_with_operation(&index, operation.id())
.unwrap();
self.closed = true; self.closed = true;
operation operation
} }