Skip to content

Commit

Permalink
The graph RwLock is no longer needed since the GraphMap is now thread…
Browse files Browse the repository at this point in the history
… safe.

Signed-off-by: Hiram Chirino <[email protected]>
  • Loading branch information
chirino committed Jan 31, 2025
1 parent c07df68 commit b1bea9d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 24 deletions.
18 changes: 5 additions & 13 deletions modules/analysis/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,9 @@ impl GraphMap {
}

// Add a new graph with the given key (write access)
pub fn insert(
&mut self,
key: String,
graph: Graph<PackageNode, Relationship, petgraph::Directed>,
) {
pub fn insert(&self, key: String, graph: Graph<PackageNode, Relationship, petgraph::Directed>) {
self.map.insert(key, Arc::new(graph));
self.map.run_pending_tasks();
}

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

// Clear all graphs from the map
pub fn clear(&mut self) {
pub fn clear(&self) {
self.map.invalidate_all();
self.map.run_pending_tasks();
}
}

// impl Default for GraphMap {
// fn default() -> Self {
// Self::new()
// }
// }
}
4 changes: 2 additions & 2 deletions modules/analysis/src/service/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ impl AnalysisService {
/// Load the SBOM matching the provided ID
#[instrument(skip(self, connection))]
pub async fn load_graph<C: ConnectionTrait>(&self, connection: &C, distinct_sbom_id: &str) {
if self.graph.read().contains_key(distinct_sbom_id) {
if self.graph.contains_key(distinct_sbom_id) {
// early return if we already loaded it
return;
}
Expand Down Expand Up @@ -327,7 +327,7 @@ impl AnalysisService {
// Set the result. A parallel call might have done the same. We wasted some time, but the
// state is still correct.

self.graph.write().insert(distinct_sbom_id.to_string(), g);
self.graph.insert(distinct_sbom_id.to_string(), g);
}

/// Load all SBOMs by the provided IDs
Expand Down
15 changes: 6 additions & 9 deletions modules/analysis/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
},
Error,
};
use parking_lot::RwLock;

use petgraph::{
algo::is_cyclic_directed,
graph::{Graph, NodeIndex},
Expand All @@ -38,7 +38,7 @@ use uuid::Uuid;

#[derive(Clone)]
pub struct AnalysisService {
graph: Arc<RwLock<GraphMap>>,
graph: Arc<GraphMap>,
}

pub fn dep_nodes(
Expand Down Expand Up @@ -157,7 +157,7 @@ impl AnalysisService {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Self {
graph: Arc::new(RwLock::new(GraphMap::new(1024 * 1024 * 100))),
graph: Arc::new(GraphMap::new(1024 * 1024 * 100)),
}
}

Expand All @@ -181,8 +181,7 @@ impl AnalysisService {
}

pub fn clear_all_graphs(&self) -> Result<(), Error> {
let mut manager = self.graph.write();
manager.clear();
self.graph.clear();
Ok(())
}

Expand All @@ -197,10 +196,9 @@ impl AnalysisService {
.all(connection)
.await?;

let manager = self.graph.read();
Ok(AnalysisStatus {
sbom_count: distinct_sbom_ids.len() as u32,
graph_count: manager.len() as u32,
graph_count: self.graph.len() as u32,
})
}

Expand Down Expand Up @@ -241,9 +239,8 @@ impl AnalysisService {
let query = query.into();

// RwLock for reading hashmap<graph>
let graph_read_guard = self.graph.read();
for distinct_sbom_id in &distinct_sbom_ids {
if let Some(graph) = graph_read_guard.get(distinct_sbom_id.to_string().as_str()) {
if let Some(graph) = self.graph.get(distinct_sbom_id.to_string().as_str()) {
if is_cyclic_directed(graph.deref()) {
log::warn!(
"analysis graph of sbom {} has circular references!",
Expand Down

0 comments on commit b1bea9d

Please sign in to comment.