Skip to content

Commit 49ede4f

Browse files
committed
Added functions to iterate over removed resources
1 parent ac175f2 commit 49ede4f

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/resource/resource_partition.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ impl ResourcePartition {
222222
self.packages.len().saturating_sub(1)
223223
}
224224

225+
/// Returns the latest valid version of all resources.
226+
///
227+
/// This function iterates through the resources in the partition
228+
/// Will only contain the latest version of a resource and will ignore resources if they are removed by a package.
225229
pub fn latest_resources(&self) -> Vec<(&ResourceInfo, PatchId)> {
226230
self.resources
227231
.iter()
@@ -252,6 +256,40 @@ impl ResourcePartition {
252256
let resource_type: String = String::from_utf8_lossy(&G::resource_type()).into_owned();
253257
self.latest_resources_of_type(resource_type.as_str())
254258
}
259+
260+
/// Returns a list of resources that have been removed.
261+
///
262+
/// This function goes through the partition and returns a list of resource marked as unneeded (i.e., deleted).
263+
/// Only resources actually deleted resources will be returned, if a resource is removed and the added again it will be ignored.
264+
/// If a package deletes a resource that was never present in any previous package, it will not be included in the returned list
265+
pub fn removed_resources(&self) -> Vec<(&ResourceInfo, PatchId)> {
266+
267+
self.packages
268+
.iter()
269+
.flat_map(|(patch_id, package)| {
270+
package
271+
.unneeded_resource_ids()
272+
.iter()
273+
.map(|&deletion| (*patch_id, *deletion)).collect::<Vec<_>>()
274+
}).filter_map(|(_, deletion_rid)| {
275+
let patches = self.resource_patch_indices(&deletion_rid);
276+
if patches.is_empty() { return None } //resource is not present in any patch
277+
if self.resources.contains_key(&deletion_rid) { return None } //resource was deleted, but was added again
278+
patches.iter().max().and_then(|&latest_patch| {
279+
self.resource_info_from(&deletion_rid, latest_patch)
280+
.ok().map(|resource_info| (resource_info, latest_patch))
281+
})
282+
}).collect()
283+
}
284+
285+
pub fn removed_resources_of_type(&self, resource_type: &str) -> Vec<(&ResourceInfo, PatchId)> {
286+
self.removed_resources().iter().filter(|(res_info, _)| res_info.data_type() == resource_type).cloned().collect()
287+
}
288+
289+
pub fn removed_resources_of_glacier_type<G: GlacierResource>(&self) -> Vec<(&ResourceInfo, PatchId)>{
290+
let resource_type: String = String::from_utf8_lossy(&G::resource_type()).into_owned();
291+
self.removed_resources_of_type(resource_type.as_str())
292+
}
255293

256294
pub fn read_resource(
257295
&self,

0 commit comments

Comments
 (0)