-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcopy.rs
177 lines (160 loc) · 5.53 KB
/
copy.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use std::collections::BTreeSet;
use log::trace;
use rayon::prelude::{IntoParallelRefIterator, ParallelBridge, ParallelIterator};
use crate::{
backend::{decrypt::DecryptWriteBackend, node::NodeType},
blob::{packer::Packer, tree::TreeStreamerOnce, BlobType},
error::RusticResult,
index::{indexer::Indexer, ReadIndex},
progress::{Progress, ProgressBars},
repofile::SnapshotFile,
repository::{IndexedFull, IndexedIds, IndexedTree, Open, Repository},
};
/// This struct enhances `[SnapshotFile]` with the attribute `relevant`
/// which indicates if the snapshot is relevant for copying.
#[derive(Debug)]
pub struct CopySnapshot {
/// The snapshot
pub sn: SnapshotFile,
/// Whether it is relevant
pub relevant: bool,
}
/// Copy the given snapshots to the destination repository.
///
/// # Type Parameters
///
/// * `Q` - The progress bar type.
/// * `R` - The type of the indexed tree.
/// * `P` - The progress bar type.
/// * `S` - The type of the indexed tree.
///
/// # Arguments
///
/// * `repo` - The repository to copy from
/// * `repo_dest` - The repository to copy to
/// * `snapshots` - The snapshots to copy
pub(crate) fn copy<'a, Q, R: IndexedFull, P: ProgressBars, S: IndexedIds>(
repo: &Repository<Q, R>,
repo_dest: &Repository<P, S>,
snapshots: impl IntoIterator<Item = &'a SnapshotFile>,
) -> RusticResult<()> {
let be_dest = repo_dest.dbe();
let pb = &repo_dest.pb;
let (snap_trees, snaps): (Vec<_>, Vec<_>) = snapshots
.into_iter()
.cloned()
.map(|sn| (sn.tree, SnapshotFile::clear_ids(sn)))
.unzip();
let be = repo.dbe();
let index = repo.index();
let index_dest = repo_dest.index();
let indexer = Indexer::new(be_dest.clone()).into_shared();
let data_packer = Packer::new(
be_dest.clone(),
BlobType::Data,
indexer.clone(),
repo_dest.config(),
index.total_size(BlobType::Data),
)?;
let tree_packer = Packer::new(
be_dest.clone(),
BlobType::Tree,
indexer.clone(),
repo_dest.config(),
index.total_size(BlobType::Tree),
)?;
let p = pb.progress_bytes("copying blobs...");
snap_trees
.par_iter()
.try_for_each(|id| -> RusticResult<_> {
trace!("copy tree blob {id}");
if !index_dest.has_tree(id) {
let data = index.get_tree(id).unwrap().read_data(be)?;
p.inc(data.len() as u64);
tree_packer.add(data, *id)?;
}
Ok(())
})?;
let tree_streamer = TreeStreamerOnce::new(be, index, snap_trees, pb.progress_hidden())?;
tree_streamer
.par_bridge()
.try_for_each(|item| -> RusticResult<_> {
let (_, tree) = item?;
tree.nodes.par_iter().try_for_each(|node| {
match node.node_type {
NodeType::File => {
node.content.par_iter().flatten().try_for_each(
|id| -> RusticResult<_> {
trace!("copy data blob {id}");
if !index_dest.has_data(id) {
let data = index.get_data(id).unwrap().read_data(be)?;
p.inc(data.len() as u64);
data_packer.add(data, *id)?;
}
Ok(())
},
)?;
}
NodeType::Dir => {
let id = node.subtree.unwrap();
trace!("copy tree blob {id}");
if !index_dest.has_tree(&id) {
let data = index.get_tree(&id).unwrap().read_data(be)?;
p.inc(data.len() as u64);
tree_packer.add(data, id)?;
}
}
_ => {} // nothing to copy
}
Ok(())
})
})?;
_ = data_packer.finalize()?;
_ = tree_packer.finalize()?;
indexer.write().unwrap().finalize()?;
let p = pb.progress_counter("saving snapshots...");
be_dest.save_list(snaps.iter(), p)?;
Ok(())
}
/// Filter out relevant snapshots from the given list of snapshots.
///
/// # Type Parameters
///
/// * `F` - The type of the filter.
/// * `P` - The progress bar type.
/// * `S` - The state of the repository.
///
/// # Arguments
///
/// * `snaps` - The snapshots to filter
/// * `dest_repo` - The destination repository
/// * `filter` - The filter to apply to the snapshots
///
/// # Returns
///
/// A list of snapshots with the attribute `relevant` set to `true` if the snapshot is relevant for copying.
pub(crate) fn relevant_snapshots<F, P: ProgressBars, S: Open>(
snaps: &[SnapshotFile],
dest_repo: &Repository<P, S>,
filter: F,
) -> RusticResult<Vec<CopySnapshot>>
where
F: FnMut(&SnapshotFile) -> bool,
{
let p = dest_repo
.pb
.progress_counter("finding relevant snapshots...");
// save snapshots in destination in BTreeSet, as we want to efficiently search within to filter out already existing snapshots before copying.
let snapshots_dest: BTreeSet<_> = SnapshotFile::all_from_backend(dest_repo.dbe(), filter, &p)?
.into_iter()
.collect();
let relevant = snaps
.iter()
.cloned()
.map(|sn| CopySnapshot {
relevant: !snapshots_dest.contains(&sn),
sn,
})
.collect();
Ok(relevant)
}