index: move load() to IndexStore

This commit is contained in:
Martin von Zweigbergk 2021-03-02 22:27:14 -08:00
parent 403e86c138
commit 2fdf9721c0
3 changed files with 20 additions and 20 deletions

View file

@ -30,7 +30,6 @@ use crate::commit::Commit;
use crate::dag_walk;
use crate::op_store::OperationId;
use crate::operation::Operation;
use crate::repo::ReadonlyRepo;
use crate::store::{ChangeId, CommitId};
use crate::store_wrapper::StoreWrapper;
use std::fmt::{Debug, Formatter};
@ -1352,18 +1351,6 @@ impl IndexEntry<'_> {
}
impl ReadonlyIndex {
pub fn load(repo: &ReadonlyRepo, dir: PathBuf, op_id: OperationId) -> Arc<ReadonlyIndex> {
let op_id_hex = op_id.hex();
let op_id_file = dir.join("operations").join(&op_id_hex);
if op_id_file.exists() {
let op_id = OperationId(hex::decode(op_id_hex).unwrap());
ReadonlyIndex::load_at_operation(dir, repo.store().hash_length(), &op_id).unwrap()
} else {
let op = repo.view().as_view_ref().get_operation(&op_id).unwrap();
ReadonlyIndex::index(repo.store(), dir, &op).unwrap()
}
}
fn load_from(
file: &mut dyn Read,
dir: PathBuf,
@ -1425,7 +1412,7 @@ impl ReadonlyIndex {
}))
}
fn load_at_operation(
pub fn load_at_operation(
dir: PathBuf,
hash_length: usize,
op_id: &OperationId,
@ -1442,7 +1429,7 @@ impl ReadonlyIndex {
ReadonlyIndex::load_from(&mut index_file, dir, index_file_id_hex, hash_length)
}
fn index(
pub fn index(
store: &StoreWrapper,
dir: PathBuf,
operation: &Operation,

View file

@ -12,7 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::index::ReadonlyIndex;
use crate::op_store::OperationId;
use crate::repo::ReadonlyRepo;
use std::path::PathBuf;
use std::sync::Arc;
pub struct IndexStore {
dir: PathBuf,
@ -32,4 +36,17 @@ impl IndexStore {
pub fn load(dir: PathBuf) -> IndexStore {
IndexStore { dir }
}
pub fn get_index_at_op(&self, repo: &ReadonlyRepo, op_id: OperationId) -> 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() {
let op_id = OperationId(hex::decode(op_id_hex).unwrap());
ReadonlyIndex::load_at_operation(self.dir.clone(), repo.store().hash_length(), &op_id)
.unwrap()
} else {
let op = repo.view().as_view_ref().get_operation(&op_id).unwrap();
ReadonlyIndex::index(repo.store(), self.dir.clone(), &op).unwrap()
}
}
}

View file

@ -272,11 +272,7 @@ impl ReadonlyRepo {
let mut locked_index = self.index.lock().unwrap();
if locked_index.is_none() {
let op_id = self.view.base_op_head_id().clone();
locked_index.replace(ReadonlyIndex::load(
self,
self.repo_path.join("index"),
op_id,
));
locked_index.replace(self.index_store.get_index_at_op(self, op_id));
}
locked_index.as_ref().unwrap().clone()
}