Skip to content

Commit

Permalink
Add repository::get_file
Browse files Browse the repository at this point in the history
  • Loading branch information
aawsome committed Dec 10, 2024
1 parent 20c7054 commit d456270
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
11 changes: 8 additions & 3 deletions crates/core/src/backend/decrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub trait DecryptReadBackend: ReadBackend + Clone + 'static {
/// # Errors
///
/// * If the file could not be read.
fn get_file<F: RepoFile>(&self, id: &Id) -> RusticResult<F> {
fn get_file<F: RepoFile>(&self, id: &F::Id) -> RusticResult<F> {
let data = if F::ENCRYPTED {

Check warning on line 137 in crates/core/src/backend/decrypt.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/backend/decrypt.rs#L137

Added line #L137 was not covered by tests
self.read_encrypted_full(F::TYPE, id)?
} else {
Expand Down Expand Up @@ -163,6 +163,7 @@ pub trait DecryptReadBackend: ReadBackend + Clone + 'static {
/// If the files could not be read.
fn stream_all<F: RepoFile>(&self, p: &impl Progress) -> StreamResult<F::Id, F> {
let list = self.list(F::TYPE)?;
let list: Vec<_> = list.into_iter().map(F::Id::from).collect();

Check warning on line 166 in crates/core/src/backend/decrypt.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/backend/decrypt.rs#L166

Added line #L166 was not covered by tests
self.stream_list(&list, p)
}

Expand All @@ -178,13 +179,17 @@ pub trait DecryptReadBackend: ReadBackend + Clone + 'static {
/// # Errors
///
/// If the files could not be read.
fn stream_list<F: RepoFile>(&self, list: &[Id], p: &impl Progress) -> StreamResult<F::Id, F> {
fn stream_list<F: RepoFile>(
&self,
list: &[F::Id],
p: &impl Progress,
) -> StreamResult<F::Id, F> {
p.set_length(list.len() as u64);
let (tx, rx) = unbounded();

list.into_par_iter()
.for_each_with((self, p, tx), |(be, p, tx), id| {
let file = be.get_file::<F>(id).map(|file| (F::Id::from(*id), file));
let file = be.get_file::<F>(id).map(|file| (*id, file));
p.inc(1);
tx.send(file).unwrap();
});
Expand Down
3 changes: 2 additions & 1 deletion crates/core/src/commands/prune.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1587,7 +1587,8 @@ fn find_used_blobs(
let list: Vec<_> = be
.list(FileType::Snapshot)?
.into_iter()
.filter(|id| !ignore_snaps.contains(&SnapshotId::from(*id)))
.map(SnapshotId::from)
.filter(|id| !ignore_snaps.contains(&id))
.collect();
let snap_trees: Vec<_> = be
.stream_list::<SnapshotFile>(&list, &p)?
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/repofile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ pub trait RepoFile: Serialize + DeserializeOwned + Sized + Send + Sync + 'static
/// Indicate whether the files are stored encrypted
const ENCRYPTED: bool = true;
/// The Id type associated with the repository file
type Id: From<Id> + Send;
type Id: RepoId;
}

/// Marker trait for Ids which identify repository files
pub trait RepoId: Deref<Target = Id> + From<Id> + Sized + Send + Sync + 'static {
pub trait RepoId: Deref<Target = Id> + From<Id> + Sized + Copy + Send + Sync + 'static {
/// The [`FileType`] associated with Id type
const TYPE: FileType;
}
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/repofile/snapshotfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,8 +671,8 @@ impl SnapshotFile {
let mut snaps: BTreeMap<_, _> = current.into_iter().map(|snap| (snap.id, snap)).collect();
let missing_ids: Vec<_> = ids
.iter()
.filter(|id| !snaps.contains_key(&SnapshotId::from(**id)))
.copied()
.map(|id| SnapshotId::from(*id))

Check warning on line 674 in crates/core/src/repofile/snapshotfile.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/repofile/snapshotfile.rs#L674

Added line #L674 was not covered by tests
.filter(|id| !snaps.contains_key(id))
.collect();
for res in be.stream_list::<Self>(&missing_ids, p)? {
let (id, snap) = res?;
Expand Down
13 changes: 13 additions & 0 deletions crates/core/src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,19 @@ impl<P: ProgressBars, S: Open> Repository<P, S> {
commands::repoinfo::collect_index_infos(self)
}

/// Read a given [`RepoFile`]
///
/// # Errors
///
/// If the file cannot be read or processed
///
/// # Returns
///
/// The file
pub fn get_file<F: RepoFile>(&self, id: &F::Id) -> RusticResult<F> {
self.dbe().get_file(id)

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

View check run for this annotation

Codecov / codecov/patch

crates/core/src/repository.rs#L1366-L1367

Added lines #L1366 - L1367 were not covered by tests
}

/// Read all files of a given [`RepoFile`]
///
/// # Errors
Expand Down

0 comments on commit d456270

Please sign in to comment.