Skip to content

Commit b1bea9d

Browse files
committed
The graph RwLock is no longer needed since the GraphMap is now thread safe.
Signed-off-by: Hiram Chirino <[email protected]>
1 parent c07df68 commit b1bea9d

File tree

3 files changed

+13
-24
lines changed

3 files changed

+13
-24
lines changed

modules/analysis/src/model.rs

+5-13
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,9 @@ impl GraphMap {
219219
}
220220

221221
// Add a new graph with the given key (write access)
222-
pub fn insert(
223-
&mut self,
224-
key: String,
225-
graph: Graph<PackageNode, Relationship, petgraph::Directed>,
226-
) {
222+
pub fn insert(&self, key: String, graph: Graph<PackageNode, Relationship, petgraph::Directed>) {
227223
self.map.insert(key, Arc::new(graph));
224+
self.map.run_pending_tasks();
228225
}
229226

230227
// Retrieve a reference to a graph by its key (read access)
@@ -236,13 +233,8 @@ impl GraphMap {
236233
}
237234

238235
// Clear all graphs from the map
239-
pub fn clear(&mut self) {
236+
pub fn clear(&self) {
240237
self.map.invalidate_all();
238+
self.map.run_pending_tasks();
241239
}
242-
}
243-
244-
// impl Default for GraphMap {
245-
// fn default() -> Self {
246-
// Self::new()
247-
// }
248-
// }
240+
}

modules/analysis/src/service/load.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ impl AnalysisService {
216216
/// Load the SBOM matching the provided ID
217217
#[instrument(skip(self, connection))]
218218
pub async fn load_graph<C: ConnectionTrait>(&self, connection: &C, distinct_sbom_id: &str) {
219-
if self.graph.read().contains_key(distinct_sbom_id) {
219+
if self.graph.contains_key(distinct_sbom_id) {
220220
// early return if we already loaded it
221221
return;
222222
}
@@ -327,7 +327,7 @@ impl AnalysisService {
327327
// Set the result. A parallel call might have done the same. We wasted some time, but the
328328
// state is still correct.
329329

330-
self.graph.write().insert(distinct_sbom_id.to_string(), g);
330+
self.graph.insert(distinct_sbom_id.to_string(), g);
331331
}
332332

333333
/// Load all SBOMs by the provided IDs

modules/analysis/src/service/mod.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313
},
1414
Error,
1515
};
16-
use parking_lot::RwLock;
16+
1717
use petgraph::{
1818
algo::is_cyclic_directed,
1919
graph::{Graph, NodeIndex},
@@ -38,7 +38,7 @@ use uuid::Uuid;
3838

3939
#[derive(Clone)]
4040
pub struct AnalysisService {
41-
graph: Arc<RwLock<GraphMap>>,
41+
graph: Arc<GraphMap>,
4242
}
4343

4444
pub fn dep_nodes(
@@ -157,7 +157,7 @@ impl AnalysisService {
157157
#[allow(clippy::new_without_default)]
158158
pub fn new() -> Self {
159159
Self {
160-
graph: Arc::new(RwLock::new(GraphMap::new(1024 * 1024 * 100))),
160+
graph: Arc::new(GraphMap::new(1024 * 1024 * 100)),
161161
}
162162
}
163163

@@ -181,8 +181,7 @@ impl AnalysisService {
181181
}
182182

183183
pub fn clear_all_graphs(&self) -> Result<(), Error> {
184-
let mut manager = self.graph.write();
185-
manager.clear();
184+
self.graph.clear();
186185
Ok(())
187186
}
188187

@@ -197,10 +196,9 @@ impl AnalysisService {
197196
.all(connection)
198197
.await?;
199198

200-
let manager = self.graph.read();
201199
Ok(AnalysisStatus {
202200
sbom_count: distinct_sbom_ids.len() as u32,
203-
graph_count: manager.len() as u32,
201+
graph_count: self.graph.len() as u32,
204202
})
205203
}
206204

@@ -241,9 +239,8 @@ impl AnalysisService {
241239
let query = query.into();
242240

243241
// RwLock for reading hashmap<graph>
244-
let graph_read_guard = self.graph.read();
245242
for distinct_sbom_id in &distinct_sbom_ids {
246-
if let Some(graph) = graph_read_guard.get(distinct_sbom_id.to_string().as_str()) {
243+
if let Some(graph) = self.graph.get(distinct_sbom_id.to_string().as_str()) {
247244
if is_cyclic_directed(graph.deref()) {
248245
log::warn!(
249246
"analysis graph of sbom {} has circular references!",

0 commit comments

Comments
 (0)