Skip to content

Commit

Permalink
feat: Add Repository::drop_index and ::drop_data_from_index
Browse files Browse the repository at this point in the history
  • Loading branch information
aawsome committed Feb 18, 2024
1 parent 178c28e commit 6e46514
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 4 deletions.
6 changes: 6 additions & 0 deletions crates/core/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,12 @@ impl GlobalIndex {
}
}
}

pub(crate) fn drop_data(self) -> Self {
Self {
index: Arc::new(self.into_index().drop_data()),
}
}
}

impl ReadGlobalIndex for GlobalIndex {}
17 changes: 17 additions & 0 deletions crates/core/src/index/binarysorted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,23 @@ pub(crate) struct TypeIndex {
#[derive(Debug)]
pub struct Index(BlobTypeMap<TypeIndex>);

impl Index {
/// drop all index entries related to data blobs
pub(crate) fn drop_data(self) -> Self {
Self(self.0.map(|blob_type, i| {
if blob_type == BlobType::Data {
TypeIndex {
packs: Vec::new(),
entries: EntriesVariants::None,
total_size: 0,
}
} else {
i
}
}))
}
}

impl IndexCollector {
#[must_use]
pub fn new(tpe: IndexType) -> Self {
Expand Down
66 changes: 62 additions & 4 deletions crates/core/src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1171,18 +1171,25 @@ pub trait IndexedTree: Open {
type I: ReadGlobalIndex;

fn index(&self) -> &Self::I;
fn into_open(self) -> impl Open;
}

/// A repository which is indexed such that all tree blobs are contained in the index
/// and additionally the `Id`s of data blobs are also contained in the index.
pub trait IndexedIds: IndexedTree {}
pub trait IndexedIds: IndexedTree {
fn into_indexed_tree(self) -> impl IndexedTree;
}

impl<P, S: IndexedTree> IndexedTree for Repository<P, S> {
type I = S::I;

fn index(&self) -> &Self::I {
self.status.index()
}

fn into_open(self) -> impl Open {
self.status.into_open()
}
}

#[derive(Clone, Copy, Debug)]
Expand Down Expand Up @@ -1228,6 +1235,12 @@ pub struct IndexedStatus<T, S: Open> {
open: S,
}

#[derive(Debug, Clone, Copy)]
/// A type of an index, that only contains [`Id`]s.
///
/// Used for the [`IndexedTrees`] state of a repository in [`IndexedStatus`].
pub struct TreeIndex;

#[derive(Debug, Clone, Copy)]
/// A type of an index, that only contains [`Id`]s.
///
Expand All @@ -1249,13 +1262,35 @@ impl<T, S: Open> IndexedTree for IndexedStatus<T, S> {
fn index(&self) -> &Self::I {
&self.index
}

fn into_open(self) -> impl Open {
self.open
}
}

impl<S: Open> IndexedIds for IndexedStatus<IdIndex, S> {}
impl<S: Open> IndexedIds for IndexedStatus<IdIndex, S> {
fn into_indexed_tree(self) -> impl IndexedTree {
Self {
index: self.index.drop_data(),
..self
}
}
}

impl<S: Open> IndexedIds for IndexedStatus<FullIndex, S> {}
impl<S: Open> IndexedIds for IndexedStatus<FullIndex, S> {
fn into_indexed_tree(self) -> impl IndexedTree {
Self {
index: self.index.drop_data(),
..self
}
}
}

impl<P, S: IndexedFull> IndexedIds for Repository<P, S> {}
impl<P, S: IndexedFull> IndexedIds for Repository<P, S> {
fn into_indexed_tree(self) -> impl IndexedTree {
self.status.into_indexed_tree()
}
}

impl<S: Open> IndexedFull for IndexedStatus<FullIndex, S> {
fn get_blob_or_insert_with(
Expand Down Expand Up @@ -1395,6 +1430,18 @@ impl<P, S: IndexedTree> Repository<P, S> {
pub fn node_from_path(&self, root_tree: Id, path: &Path) -> RusticResult<Node> {
Tree::node_from_path(self.dbe(), self.index(), root_tree, Path::new(path))
}

/// drop the `Repository`index
pub fn drop_index(self) -> Repository<P, impl Open> {
Repository {
name: self.name,
be: self.be,
be_hot: self.be_hot,
opts: self.opts,
pb: self.pb,
status: self.status.into_open(),
}
}
}

impl<P: ProgressBars, S: IndexedTree> Repository<P, S> {
Expand Down Expand Up @@ -1620,6 +1667,17 @@ impl<P, S: IndexedFull> Repository<P, S> {
pub fn get_blob_cached(&self, id: &Id, tpe: BlobType) -> RusticResult<Bytes> {
self.get_blob_or_insert_with(id, || self.index().blob_from_backend(self.dbe(), tpe, id))
}

pub fn drop_data_from_index(self) -> Repository<P, impl IndexedTree> {
Repository {
name: self.name,
be: self.be,
be_hot: self.be_hot,
opts: self.opts,
pb: self.pb,
status: self.status.into_indexed_tree(),
}
}
}

impl<P: ProgressBars, S: IndexedFull> Repository<P, S> {
Expand Down

0 comments on commit 6e46514

Please sign in to comment.