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
use super::BundleStore;
use crate::core::bundlepack::{BundlePack, Constraint};
use anyhow::{bail, Result};
use bp7::Bundle;
use log::debug;
use std::collections::HashMap;
use std::fmt::Debug;
#[derive(Debug, Clone, PartialEq, Default)]
pub struct InMemoryBundleStore {
bundles: HashMap<String, Bundle>,
metadata: HashMap<String, BundlePack>,
}
impl BundleStore for InMemoryBundleStore {
fn push(&mut self, bndl: &Bundle) -> Result<()> {
let bp = BundlePack::from(bndl);
if self.bundles.contains_key(bp.id()) {
debug!("Bundle {} already in store, updating it!", bndl.id());
} else {
self.metadata.insert(bp.id().to_string(), bp);
}
let bid = bndl.id();
debug!("inserting bundle {} in to store", bid);
let b = bndl.clone();
let _ret = self.bundles.insert(bid, b);
Ok(())
}
fn update_metadata(&mut self, bp: &BundlePack) -> Result<()> {
if !self.metadata.contains_key(bp.id()) {
bail!("Bundle not in store!");
}
self.metadata.insert(bp.id().to_string(), bp.clone());
Ok(())
}
fn remove(&mut self, bid: &str) -> Result<()> {
if self.bundles.remove(bid).is_none() {
bail!("Bundle not in store!");
}
Ok(())
}
fn count(&self) -> u64 {
self.bundles.len() as u64
}
fn all_ids(&self) -> Vec<String> {
self.bundles.keys().cloned().collect()
}
fn has_item(&self, bid: &str) -> bool {
self.bundles.contains_key(bid)
}
fn pending(&self) -> Vec<String> {
self.metadata
.values()
.filter(|&e| {
!e.has_constraint(Constraint::ReassemblyPending)
&& (e.has_constraint(Constraint::ForwardPending)
|| e.has_constraint(Constraint::Contraindicated))
})
.map(|b| b.id().into())
.collect()
}
fn forwarding(&self) -> Vec<String> {
self.metadata
.values()
.filter(|&e| e.has_constraint(Constraint::ForwardPending))
.map(|b| b.id().into())
.collect()
}
fn bundles(&self) -> Vec<BundlePack> {
self.metadata.values().cloned().collect::<Vec<BundlePack>>()
}
fn get_bundle(&self, bpid: &str) -> Option<Bundle> {
debug!("get_bundle {}", bpid);
self.bundles.get(bpid).cloned()
}
fn get_metadata(&self, bpid: &str) -> Option<BundlePack> {
self.metadata.get(bpid).cloned()
}
}
impl InMemoryBundleStore {
pub fn new() -> InMemoryBundleStore {
InMemoryBundleStore {
bundles: HashMap::new(),
metadata: HashMap::new(),
}
}
}