A repository is a generic interface for storing and retrieving data.
| 71 | |
| 72 | /// A repository is a generic interface for storing and retrieving data. |
| 73 | pub trait Repository<Key, Value, Mem>: StableDb<Key, Value, Mem> |
| 74 | where |
| 75 | Key: Eq + std::hash::Hash + Clone + Ord + Storable, |
| 76 | Value: Clone + Storable, |
| 77 | Mem: Memory, |
| 78 | { |
| 79 | /// Returns the list of records from the repository. |
| 80 | fn list(&self) -> Vec<Value> { |
| 81 | let mut entries = Vec::with_capacity(self.len()); |
| 82 | |
| 83 | Self::with_db(|db| db.iter().for_each(|(_, entry)| entries.push(entry))); |
| 84 | |
| 85 | entries |
| 86 | } |
| 87 | |
| 88 | /// Returns whether a record exists in the repository. |
| 89 | fn exists(&self, key: &Key) -> bool { |
| 90 | Self::with_db(|db| db.contains_key(key)) |
| 91 | } |
| 92 | |
| 93 | /// Returns the record from the repository if it exists. |
| 94 | fn get(&self, key: &Key) -> Option<Value> { |
| 95 | Self::with_db(|db| db.get(key)) |
| 96 | } |
| 97 | |
| 98 | /// Inserts a record into the repository. |
| 99 | fn insert(&self, key: Key, value: Value) -> Option<Value> { |
| 100 | Self::with_db(|db| db.insert(key, value)) |
| 101 | } |
| 102 | |
| 103 | /// Removes a record from the repository and returns it if it exists. |
| 104 | fn remove(&self, key: &Key) -> Option<Value> { |
| 105 | Self::with_db(|db| db.remove(key)) |
| 106 | } |
| 107 | |
| 108 | /// Returns the number of records stored in the repository. |
| 109 | fn len(&self) -> usize { |
| 110 | Self::with_db(|db| db.len() as usize) |
| 111 | } |
| 112 | |
| 113 | /// Returns whether the repository is empty or not. |
| 114 | fn is_empty(&self) -> bool { |
| 115 | self.len() == 0 |
| 116 | } |
| 117 | |
| 118 | fn find_with_filters<'a>( |
| 119 | &self, |
| 120 | filters: Vec<Box<dyn SelectionFilter<'a, IdType = UUID> + 'a>>, |
| 121 | ) -> HashSet<UUID> { |
| 122 | let mut found_ids = None; |
| 123 | |
| 124 | for filter in filters { |
| 125 | found_ids = Some(filter.apply(found_ids.as_ref())); |
| 126 | } |
| 127 | |
| 128 | found_ids.unwrap_or_default() |
| 129 | } |
| 130 |
no outgoing calls
no test coverage detected