Skip to content

Commit d456270

Browse files
committed
Add repository::get_file
1 parent 20c7054 commit d456270

File tree

5 files changed

+27
-8
lines changed

5 files changed

+27
-8
lines changed

crates/core/src/backend/decrypt.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pub trait DecryptReadBackend: ReadBackend + Clone + 'static {
133133
/// # Errors
134134
///
135135
/// * If the file could not be read.
136-
fn get_file<F: RepoFile>(&self, id: &Id) -> RusticResult<F> {
136+
fn get_file<F: RepoFile>(&self, id: &F::Id) -> RusticResult<F> {
137137
let data = if F::ENCRYPTED {
138138
self.read_encrypted_full(F::TYPE, id)?
139139
} else {
@@ -163,6 +163,7 @@ pub trait DecryptReadBackend: ReadBackend + Clone + 'static {
163163
/// If the files could not be read.
164164
fn stream_all<F: RepoFile>(&self, p: &impl Progress) -> StreamResult<F::Id, F> {
165165
let list = self.list(F::TYPE)?;
166+
let list: Vec<_> = list.into_iter().map(F::Id::from).collect();
166167
self.stream_list(&list, p)
167168
}
168169

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

185190
list.into_par_iter()
186191
.for_each_with((self, p, tx), |(be, p, tx), id| {
187-
let file = be.get_file::<F>(id).map(|file| (F::Id::from(*id), file));
192+
let file = be.get_file::<F>(id).map(|file| (*id, file));
188193
p.inc(1);
189194
tx.send(file).unwrap();
190195
});

crates/core/src/commands/prune.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1587,7 +1587,8 @@ fn find_used_blobs(
15871587
let list: Vec<_> = be
15881588
.list(FileType::Snapshot)?
15891589
.into_iter()
1590-
.filter(|id| !ignore_snaps.contains(&SnapshotId::from(*id)))
1590+
.map(SnapshotId::from)
1591+
.filter(|id| !ignore_snaps.contains(&id))
15911592
.collect();
15921593
let snap_trees: Vec<_> = be
15931594
.stream_list::<SnapshotFile>(&list, &p)?

crates/core/src/repofile.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ pub trait RepoFile: Serialize + DeserializeOwned + Sized + Send + Sync + 'static
1515
/// Indicate whether the files are stored encrypted
1616
const ENCRYPTED: bool = true;
1717
/// The Id type associated with the repository file
18-
type Id: From<Id> + Send;
18+
type Id: RepoId;
1919
}
2020

2121
/// Marker trait for Ids which identify repository files
22-
pub trait RepoId: Deref<Target = Id> + From<Id> + Sized + Send + Sync + 'static {
22+
pub trait RepoId: Deref<Target = Id> + From<Id> + Sized + Copy + Send + Sync + 'static {
2323
/// The [`FileType`] associated with Id type
2424
const TYPE: FileType;
2525
}

crates/core/src/repofile/snapshotfile.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,8 +671,8 @@ impl SnapshotFile {
671671
let mut snaps: BTreeMap<_, _> = current.into_iter().map(|snap| (snap.id, snap)).collect();
672672
let missing_ids: Vec<_> = ids
673673
.iter()
674-
.filter(|id| !snaps.contains_key(&SnapshotId::from(**id)))
675-
.copied()
674+
.map(|id| SnapshotId::from(*id))
675+
.filter(|id| !snaps.contains_key(id))
676676
.collect();
677677
for res in be.stream_list::<Self>(&missing_ids, p)? {
678678
let (id, snap) = res?;

crates/core/src/repository.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,6 +1354,19 @@ impl<P: ProgressBars, S: Open> Repository<P, S> {
13541354
commands::repoinfo::collect_index_infos(self)
13551355
}
13561356

1357+
/// Read a given [`RepoFile`]
1358+
///
1359+
/// # Errors
1360+
///
1361+
/// If the file cannot be read or processed
1362+
///
1363+
/// # Returns
1364+
///
1365+
/// The file
1366+
pub fn get_file<F: RepoFile>(&self, id: &F::Id) -> RusticResult<F> {
1367+
self.dbe().get_file(id)
1368+
}
1369+
13571370
/// Read all files of a given [`RepoFile`]
13581371
///
13591372
/// # Errors

0 commit comments

Comments
 (0)