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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
use std::{sync::Arc, thread::sleep, time::Duration};
use bytes::Bytes;
use derive_more::Constructor;
use crate::{
backend::{FileType, decrypt::DecryptReadBackend},
blob::{BlobId, BlobLocation, BlobType, DataId, tree::TreeId},
error::{ErrorKind, RusticError, RusticResult},
index::binarysorted::{Index, IndexCollector, IndexType},
progress::Progress,
repofile::{
indexfile::{IndexBlob, IndexFile},
packfile::PackId,
},
};
pub(crate) mod binarysorted;
pub(crate) mod indexer;
/// An entry in the index
#[derive(Debug, Clone, Copy, PartialEq, Eq, Constructor)]
pub struct IndexEntry {
/// The type of the blob
blob_type: BlobType,
/// The pack the blob is in
pub pack: PackId,
/// The location of the blob in the pack
pub location: BlobLocation,
}
impl IndexEntry {
/// Create an [`IndexEntry`] from an [`IndexBlob`]
///
/// # Arguments
///
/// * `blob` - The [`IndexBlob`] to create the [`IndexEntry`] from
/// * `pack` - The pack the blob is in
#[must_use]
pub const fn from_index_blob(blob: &IndexBlob, pack: PackId) -> Self {
Self {
blob_type: blob.tpe,
pack,
location: blob.location,
}
}
/// Get a blob described by [`IndexEntry`] from the backend
///
/// # Arguments
///
/// * `be` - The backend to read from
///
/// # Errors
///
// TODO: add error! This function will return an error if the blob is not found in the backend.
pub fn read_data<B: DecryptReadBackend>(&self, be: &B) -> RusticResult<Bytes> {
let data = be.read_encrypted_partial(
FileType::Pack,
&self.pack,
self.blob_type.is_cacheable(),
self.location,
)?;
Ok(data)
}
/// Get the length of the data described by the [`IndexEntry`]
#[must_use]
pub const fn data_length(&self) -> u32 {
self.location.data_length()
}
}
/// The index of the repository
///
/// The index is a list of [`IndexEntry`]s
pub trait ReadIndex {
/// Get an [`IndexEntry`] from the index
///
/// # Arguments
///
/// * `tpe` - The type of the blob
/// * `id` - The id of the blob
///
/// # Returns
///
/// The [`IndexEntry`] - If it exists otherwise `None`
fn get_id(&self, tpe: BlobType, id: &BlobId) -> Option<IndexEntry>;
/// Get the total size of all blobs of the given type
///
/// # Arguments
///
/// * `tpe` - The type of the blobs
fn total_size(&self, tpe: BlobType) -> u64;
/// Check if the index contains the given blob
///
/// # Arguments
///
/// * `tpe` - The type of the blob
/// * `id` - The id of the blob
fn has(&self, tpe: BlobType, id: &BlobId) -> bool;
/// Get a tree from the index
///
/// # Arguments
///
/// * `id` - The id of the tree
///
/// # Returns
///
/// The [`IndexEntry`] of the tree if it exists otherwise `None`
fn get_tree(&self, id: &TreeId) -> Option<IndexEntry> {
self.get_id(BlobType::Tree, &BlobId::from(**id))
}
/// Get a data blob from the index
///
/// # Arguments
///
/// * `id` - The id of the data blob
///
/// # Returns
///
/// The [`IndexEntry`] of the data blob if it exists otherwise `None`
fn get_data(&self, id: &DataId) -> Option<IndexEntry> {
self.get_id(BlobType::Data, &BlobId::from(**id))
}
/// Check if the index contains the given tree
///
/// # Arguments
///
/// * `id` - The id of the tree
///
/// # Returns
///
/// `true` if the index contains the tree otherwise `false`
fn has_tree(&self, id: &TreeId) -> bool {
self.has(BlobType::Tree, &BlobId::from(**id))
}
/// Check if the index contains the given data blob
///
/// # Arguments
///
/// * `id` - The id of the data blob
///
/// # Returns
///
/// `true` if the index contains the data blob otherwise `false`
fn has_data(&self, id: &DataId) -> bool {
self.has(BlobType::Data, &BlobId::from(**id))
}
/// Get a blob from the backend
///
/// # Arguments
///
/// * `tpe` - The type of the blob
/// * `id` - The id of the blob
///
/// # Errors
///
/// * If the blob could not be found in the index
fn blob_from_backend(
&self,
be: &impl DecryptReadBackend,
tpe: BlobType,
id: &BlobId,
) -> RusticResult<Bytes> {
self.get_id(tpe, id).map_or_else(
|| {
Err(RusticError::new(
ErrorKind::Internal,
"Blob `{id}` with type `{type}` not found in index",
)
.attach_context("id", id.to_string())
.attach_context("type", tpe.to_string()))
},
|ie| ie.read_data(be),
)
}
}
/// A trait for a global index
pub trait ReadGlobalIndex: ReadIndex + Clone + Sync + Send + 'static {}
/// A global index
#[derive(Clone, Debug)]
pub struct GlobalIndex {
/// The atomic reference counted, sharable index.
index: Arc<Index>,
}
impl ReadIndex for GlobalIndex {
/// Get an [`IndexEntry`] from the index
///
/// # Arguments
///
/// * `tpe` - The type of the blob
/// * `id` - The id of the blob
///
/// # Returns
///
/// The [`IndexEntry`] - If it exists otherwise `None`
fn get_id(&self, tpe: BlobType, id: &BlobId) -> Option<IndexEntry> {
self.index.get_id(tpe, id)
}
/// Get the total size of all blobs of the given type
///
/// # Arguments
///
/// * `tpe` - The type of the blobs
fn total_size(&self, tpe: BlobType) -> u64 {
self.index.total_size(tpe)
}
/// Check if the index contains the given blob
///
/// # Arguments
///
/// * `tpe` - The type of the blob
/// * `id` - The id of the blob
///
/// # Returns
///
/// `true` if the index contains the blob otherwise `false`
fn has(&self, tpe: BlobType, id: &BlobId) -> bool {
self.index.has(tpe, id)
}
}
impl GlobalIndex {
/// Create a new [`GlobalIndex`] from an [`Index`]
///
/// # Type Parameters
///
/// * `BE` - The backend type
///
/// # Arguments
///
/// * `be` - The backend to read from
/// * `index` - The index to use
pub fn new_from_index(index: Index) -> Self {
Self {
index: Arc::new(index),
}
}
/// Create a new [`GlobalIndex`] from an [`IndexCollector`]
///
/// # Arguments
///
/// * `be` - The backend to read from
/// * `p` - The progress tracker
/// * `collector` - The [`IndexCollector`] to use
///
/// # Errors
///
/// * If the index could not be read
fn new_from_collector(
be: &impl DecryptReadBackend,
p: &Progress,
mut collector: IndexCollector,
) -> RusticResult<Self> {
p.set_title("reading index...");
for index in be.stream_all::<IndexFile>(p)? {
collector.extend(index?.1.packs);
}
p.finish();
Ok(Self::new_from_index(collector.into_index()))
}
/// Create a new [`GlobalIndex`]
///
/// # Arguments
///
/// * `be` - The backend to read from
/// * `p` - The progress tracker
pub fn new(be: &impl DecryptReadBackend, p: &Progress) -> RusticResult<Self> {
Self::new_from_collector(be, p, IndexCollector::new(IndexType::Full))
}
/// Create a new [`GlobalIndex`] with only full trees
///
/// # Arguments
///
/// * `be` - The backend to read from
/// * `p` - The progress tracker
///
/// # Errors
///
/// * If the index could not be read
pub fn only_full_trees(be: &impl DecryptReadBackend, p: &Progress) -> RusticResult<Self> {
Self::new_from_collector(be, p, IndexCollector::new(IndexType::DataIds))
}
/// Convert the `Arc<Index>` to an Index
pub fn into_index(self) -> Index {
match Arc::try_unwrap(self.index) {
Ok(index) => index,
Err(arc) => {
// Seems index is still in use; this could be due to some threads using it which didn't yet completely shut down.
// sleep a bit to let threads using the index shut down, after this index should be available to unwrap
sleep(Duration::from_millis(100));
Arc::try_unwrap(arc).expect("index still in use")
}
}
}
pub(crate) fn drop_data(self) -> Self {
Self {
index: Arc::new(self.into_index().drop_data()),
}
}
}
impl ReadGlobalIndex for GlobalIndex {}