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 May 13, 2024
1 parent e7ee2f1 commit 15fd80c
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 5 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 {

Check warning on line 321 in crates/core/src/index.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/index.rs#L321

Added line #L321 was not covered by tests
Self {
index: Arc::new(self.into_index().drop_data()),

Check warning on line 323 in crates/core/src/index.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/index.rs#L323

Added line #L323 was not covered by tests
}
}
}

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 {

Check warning on line 83 in crates/core/src/index/binarysorted.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/index/binarysorted.rs#L80-L83

Added lines #L80 - L83 were not covered by tests
packs: Vec::new(),
entries: EntriesVariants::None,
total_size: 0,
}
} else {
i

Check warning on line 89 in crates/core/src/index/binarysorted.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/index/binarysorted.rs#L89

Added line #L89 was not covered by tests
}
}))
}
}

impl IndexCollector {
#[must_use]
pub fn new(tpe: IndexType) -> Self {
Expand Down
4 changes: 3 additions & 1 deletion crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,7 @@ pub use crate::{
repofile::snapshotfile::{
PathList, SnapshotGroup, SnapshotGroupCriterion, SnapshotOptions, StringList,
},
repository::{IndexedFull, OpenStatus, Repository, RepositoryOptions},
repository::{
IndexedFull, IndexedIds, IndexedTree, Open, OpenStatus, Repository, RepositoryOptions,
},
};
67 changes: 63 additions & 4 deletions crates/core/src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1264,18 +1264,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()

Check warning on line 1284 in crates/core/src/repository.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/repository.rs#L1283-L1284

Added lines #L1283 - L1284 were not covered by tests
}
}

#[derive(Clone, Copy, Debug)]
Expand Down Expand Up @@ -1330,6 +1337,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 @@ -1351,13 +1364,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

Check warning on line 1369 in crates/core/src/repository.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/repository.rs#L1368-L1369

Added lines #L1368 - L1369 were not covered by tests
}
}

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

Check warning on line 1374 in crates/core/src/repository.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/repository.rs#L1374

Added line #L1374 was not covered by tests
Self {
index: self.index.drop_data(),

Check warning on line 1376 in crates/core/src/repository.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/repository.rs#L1376

Added line #L1376 was not covered by tests
..self
}
}
}

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

Check warning on line 1383 in crates/core/src/repository.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/repository.rs#L1383

Added line #L1383 was not covered by tests
Self {
index: self.index.drop_data(),

Check warning on line 1385 in crates/core/src/repository.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/repository.rs#L1385

Added line #L1385 was not covered by tests
..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()

Check warning on line 1393 in crates/core/src/repository.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/repository.rs#L1392-L1393

Added lines #L1392 - L1393 were not covered by tests
}
}

impl<S: Open> IndexedFull for IndexedStatus<FullIndex, S> {
fn get_blob_or_insert_with(
Expand Down Expand Up @@ -1533,6 +1568,18 @@ impl<P, S: IndexedTree> Repository<P, S> {
) -> RusticResult<FindMatches> {
Tree::find_matching_nodes(self.dbe(), self.index(), ids, matches)
}

/// drop the `Repository` index leaving an `Open` `Repository`
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,

Check warning on line 1579 in crates/core/src/repository.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/repository.rs#L1579

Added line #L1579 was not covered by tests
status: self.status.into_open(),
}
}
}

impl<P: ProgressBars, S: IndexedTree> Repository<P, S> {
Expand Down Expand Up @@ -1758,6 +1805,18 @@ 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))
}

/// drop the data pack information from the `Repository` index leaving an `IndexedTree` `Repository`
pub fn drop_data_from_index(self) -> Repository<P, impl IndexedTree> {

Check warning on line 1810 in crates/core/src/repository.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/repository.rs#L1810

Added line #L1810 was not covered by tests
Repository {
name: self.name,
be: self.be,
be_hot: self.be_hot,
opts: self.opts,
pb: self.pb,
status: self.status.into_indexed_tree(),

Check warning on line 1817 in crates/core/src/repository.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/repository.rs#L1812-L1817

Added lines #L1812 - L1817 were not covered by tests
}
}
}

impl<P: ProgressBars, S: IndexedFull> Repository<P, S> {
Expand Down
2 changes: 2 additions & 0 deletions crates/core/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,8 @@ fn test_prune(
let paths = PathList::from_iter(Some(source.0.path().join("0/0/9/3")));
let _ = repo.backup(&opts, &paths, SnapshotFile::default())?;

// drop index
let repo = repo.drop_index();
repo.delete_snapshots(&[snapshot1.id])?;

// get prune plan
Expand Down

0 comments on commit 15fd80c

Please sign in to comment.