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
use jfs; use std::path; use std::str::FromStr; use sda_protocol::{AgentId, Aggregation, AggregationId, Committee, Encryption, Participation, ParticipationId, Snapshot, SnapshotId}; use SdaServerResult; use ::jfs_stores::JfsStoreExt; use stores::{BaseStore, AggregationsStore}; #[derive(Debug, Serialize, Deserialize, PartialEq)] struct SnapshotContent { participations: Vec<ParticipationId>, } pub struct JfsAggregationsStore { participations: path::PathBuf, aggregations: jfs::Store, committees: jfs::Store, snapshots: jfs::Store, snapshot_contents: jfs::Store, snapshot_masks: jfs::Store, } impl JfsAggregationsStore { pub fn new<P: AsRef<path::Path>>(prefix: P) -> SdaServerResult<JfsAggregationsStore> { let aggregations = prefix.as_ref().join("aggregations"); let committees = prefix.as_ref().join("committees"); let snapshots = prefix.as_ref().join("snapshots"); let snapshot_contents = prefix.as_ref().join("snapshot_contents"); let snapshot_masks = prefix.as_ref().join("snapshot_masks"); Ok(JfsAggregationsStore { participations: prefix.as_ref().join("participations"), aggregations: jfs::Store::new(aggregations.to_str().ok_or("pathbuf to string")?)?, committees: jfs::Store::new(committees.to_str().ok_or("pathbuf to string")?)?, snapshots: jfs::Store::new(snapshots.to_str().ok_or("pathbuf to string")?)?, snapshot_contents: jfs::Store::new(snapshot_contents.to_str() .ok_or("pathbuf to string")?)?, snapshot_masks: jfs::Store::new(snapshot_masks.to_str() .ok_or("pathbuf to string")?)?, }) } fn aggregation_store(&self, aggregation: &AggregationId) -> SdaServerResult<jfs::Store> { let path = self.participations.join(aggregation.to_string()); Ok(jfs::Store::new(path.to_str().ok_or("path to string")?)?) } } impl BaseStore for JfsAggregationsStore { fn ping(&self) -> SdaServerResult<()> { Ok(()) } } impl AggregationsStore for JfsAggregationsStore { fn list_aggregations(&self, filter: Option<&str>, recipient: Option<&AgentId>) -> SdaServerResult<Vec<AggregationId>> { Ok(self.aggregations .all::<Aggregation>()? .iter() .filter(|&(_, ref agg)| { filter.map(|f| agg.title.contains(f)).unwrap_or(true) && recipient.map(|r| &agg.recipient == r).unwrap_or(true) }) .map(|(_, v)| v.id) .collect()) } fn create_aggregation(&self, aggregation: &Aggregation) -> SdaServerResult<()> { self.aggregations.create(aggregation) } fn get_aggregation(&self, aggregation: &AggregationId) -> SdaServerResult<Option<Aggregation>> { self.aggregations.get_option(aggregation) } fn delete_aggregation(&self, aggregation: &AggregationId) -> SdaServerResult<()> { self.aggregations.delete(&aggregation.to_string())?; Ok(()) } fn get_committee(&self, owner: &AggregationId) -> SdaServerResult<Option<Committee>> { self.committees.get_option(owner) } fn create_committee(&self, committee: &Committee) -> SdaServerResult<()> { self.committees.create_with_id(committee, &committee.aggregation) } fn create_participation(&self, participation: &Participation) -> SdaServerResult<()> { let store = self.aggregation_store(&participation.aggregation)?; store.create(participation) } fn create_snapshot(&self, snapshot: &Snapshot) -> SdaServerResult<()> { self.snapshots.create(snapshot) } fn count_participations(&self, aggregation: &AggregationId) -> SdaServerResult<usize> { let store = self.aggregation_store(aggregation)?; Ok(store.all::<Participation>()?.len()) } fn snapshot_participations(&self, aggregation: &AggregationId, snapshot: &SnapshotId) -> SdaServerResult<()> { let store = self.aggregation_store(aggregation)?; let list: SdaServerResult<Vec<ParticipationId>> = store.all::<Participation>()? .into_iter() .map(|p| Ok(ParticipationId::from_str(&p.0)?)) .collect(); let snap = SnapshotContent { participations: list? }; self.snapshot_contents.create_with_id(&snap, snapshot) } fn list_snapshots(&self, aggregation: &AggregationId) -> SdaServerResult<Vec<SnapshotId>> { Ok(self.snapshots .all::<Snapshot>()? .into_iter() .map(|p| p.1) .filter(|s| &s.aggregation == aggregation) .map(|s| s.id) .collect()) } fn get_snapshot(&self, _aggregation: &AggregationId, snapshot: &SnapshotId) -> SdaServerResult<Option<Snapshot>> { self.snapshots.get_option(snapshot) } fn iter_snapped_participations<'a, 'b> (&'b self, aggregation: &AggregationId, snapshot: &SnapshotId) -> SdaServerResult<Box<Iterator<Item = SdaServerResult<Participation>> + 'a>> where 'b: 'a { let store = self.aggregation_store(aggregation)?; let snap = self.snapshot_contents.get::<SnapshotContent>(&snapshot.to_string())?; let mut participations = vec![]; for id in snap.participations { let part = store.get_option(&id)? .ok_or_else(|| { format!("participation id={:?} for agg={:?} not found", &id, &aggregation) })?; participations.push(Ok(part)); } Ok(Box::new(participations.into_iter())) } fn create_snapshot_mask(&self, snapshot: &SnapshotId, mask: Vec<Encryption>) -> SdaServerResult<()> { self.snapshot_masks.create_with_id(&mask, snapshot) } fn get_snapshot_mask(&self, snapshot: &SnapshotId) -> SdaServerResult<Option<Vec<Encryption>>> { self.snapshot_masks.get_option(snapshot) } }