Skip to content

Commit

Permalink
refactor(core): internal petgraph design
Browse files Browse the repository at this point in the history
  • Loading branch information
williamfzc committed Apr 11, 2024
1 parent 49d0b56 commit 35b5d63
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 38 deletions.
19 changes: 15 additions & 4 deletions src/relation/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@ use std::collections::HashMap;
use std::fmt::{Display, Formatter};
use std::sync::Arc;

#[derive(PartialEq, Eq)]
pub(crate) enum NodeType {
File(Option<FileData>),
Commit(Option<CommitData>),
Issue(Option<IssueData>),
Author(Option<AuthorData>),
}

#[derive(PartialEq, Eq, Debug)]
pub(crate) struct FileData {}

#[derive(PartialEq, Eq, Debug)]
pub(crate) struct CommitData {}

#[derive(PartialEq, Eq, Debug)]
pub(crate) struct IssueData {}

#[derive(PartialEq, Eq, Debug)]
pub(crate) struct AuthorData {}

#[derive(Debug)]
Expand All @@ -37,20 +42,26 @@ impl Display for EdgeType {
}
}

#[derive(PartialEq, Eq)]
pub struct NodeData {
pub(crate) _name: Arc<String>,
pub(crate) name: Arc<String>,
pub(crate) _node_type: NodeType,
pub(crate) node_index: NodeIndex,
}

pub(crate) type NodeMapping = HashMap<Arc<String>, NodeData>;
impl Display for NodeData {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.name)
}
}

pub(crate) type NodeMapping = HashMap<Arc<String>, NodeIndex>;

pub struct RelationGraph {
pub(crate) file_mapping: NodeMapping,
pub(crate) commit_mapping: NodeMapping,
pub(crate) issue_mapping: NodeMapping,
pub(crate) author_mapping: NodeMapping,
pub(crate) g: UnGraph<Arc<String>, EdgeType>,
pub(crate) g: UnGraph<NodeData, EdgeType>,
pub(crate) conf: CollectorConfig,
}

Expand Down
27 changes: 10 additions & 17 deletions src/relation/graph_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ impl RelationGraph {
commit_mapping: NodeMapping::new(),
issue_mapping: NodeMapping::new(),
author_mapping: NodeMapping::new(),
g: UnGraph::<Arc<String>, EdgeType>::new_undirected(),
g: UnGraph::<NodeData, EdgeType>::new_undirected(),
conf: crate::collector::config::Config::default(),
};
}
Expand All @@ -25,13 +25,12 @@ impl RelationGraph {

if !mapping.contains_key(name) {
let name_rc: Arc<String> = Arc::from(name.to_string());
let node_index = self.g.add_node(name_rc.clone());
let node_data = NodeData {
_name: name_rc.clone(),
name: name_rc.clone(),
_node_type: node_type,
node_index,
};
mapping.insert(name_rc, node_data);
let node_index = self.g.add_node(node_data);
mapping.insert(name_rc, node_index);
}
}

Expand Down Expand Up @@ -60,35 +59,29 @@ impl RelationGraph {
}

pub fn add_edge_file2commit(&mut self, file_name: &String, commit_name: &String) {
if let (Some(file_data), Some(commit_data)) = (
if let (Some(file_index), Some(commit_index)) = (
self.file_mapping.get(file_name),
self.commit_mapping.get(commit_name),
) {
let file_index = file_data.node_index;
let commit_index = commit_data.node_index;
self.add_edge(file_index, commit_index, EdgeType::File2Commit);
self.add_edge(*file_index, *commit_index, EdgeType::File2Commit);
}
}

pub fn add_edge_file2issue(&mut self, file_name: &String, issue: &String) {
if let (Some(file_data), Some(issue_data)) = (
if let (Some(file_index), Some(issue_index)) = (
self.file_mapping.get(file_name),
self.issue_mapping.get(issue),
) {
let file_index = file_data.node_index;
let issue_index = issue_data.node_index;
self.add_edge(file_index, issue_index, EdgeType::File2Issue);
self.add_edge(*file_index, *issue_index, EdgeType::File2Issue);
}
}

pub fn add_edge_commit2issue(&mut self, commit_name: &String, issue: &String) {
if let (Some(commit_data), Some(issue_data)) = (
if let (Some(commit_index), Some(issue_index)) = (
self.commit_mapping.get(commit_name),
self.issue_mapping.get(issue),
) {
let commit_index = commit_data.node_index;
let issue_index = issue_data.node_index;
self.add_edge(commit_index, issue_index, EdgeType::Commit2Issue);
self.add_edge(*commit_index, *issue_index, EdgeType::Commit2Issue);
}
}
}
14 changes: 8 additions & 6 deletions src/relation/graph_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ impl RelationGraph {
}

pub fn add_edge_author2commit(&mut self, author_name: &String, commit_name: &String) {
if let (Some(commit_data), Some(author_data)) = (
if let (Some(commit_index), Some(author_index)) = (
self.commit_mapping.get(commit_name),
self.author_mapping.get(author_name),
) {
let commit_index = commit_data.node_index;
let author_index = author_data.node_index;
self.add_edge(commit_index, author_index, EdgeType::Author2Commit);
self.add_edge(*commit_index, *author_index, EdgeType::Author2Commit);
}
}

pub fn get_author_node(&self, name: &String) -> Option<&NodeData> {
return self.author_mapping.get(name);
if !self.author_mapping.contains_key(name) {
return None;
}
let node_index = self.author_mapping.get(name).unwrap();
return Some(&self.g[*node_index]);
}

pub fn author_related_commits(&self, author_name: &String) -> Result<Vec<String>, Error> {
Expand All @@ -41,7 +43,7 @@ impl RelationGraph {
fn file_edge_counter(&self) -> HashMap<String, usize> {
let mut edges_count_map: HashMap<_, usize> = HashMap::new();
for (each_name, each) in &self.file_mapping {
let edges = self.g.edges(each.node_index);
let edges = self.g.edges(*each);
let edge_count = edges.count();
edges_count_map.insert(each_name.to_string(), edge_count);
}
Expand Down
34 changes: 23 additions & 11 deletions src/relation/graph_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,27 @@ use std::fmt::Error;
/// query API
impl RelationGraph {
pub fn get_file_node(&self, name: &String) -> Option<&NodeData> {
self.file_mapping.get(name)
if !self.file_mapping.contains_key(name) {
return None;
}
let node_index = self.file_mapping.get(name).unwrap();
return Some(&self.g[*node_index]);
}

pub fn get_commit_node(&self, name: &String) -> Option<&NodeData> {
self.commit_mapping.get(name)
if !self.commit_mapping.contains_key(name) {
return None;
}
let node_index = self.commit_mapping.get(name).unwrap();
return Some(&self.g[*node_index]);
}

pub fn get_issue_node(&self, name: &String) -> Option<&NodeData> {
self.issue_mapping.get(name)
if !self.issue_mapping.contains_key(name) {
return None;
}
let node_index = self.issue_mapping.get(name).unwrap();
return Some(&self.g[*node_index]);
}

pub(crate) fn get_keys(&self, node_mapping: &NodeMapping) -> Vec<String> {
Expand Down Expand Up @@ -43,16 +55,16 @@ impl RelationGraph {
if !src.contains_key(entry) {
return Err(Error::default());
}
let neighbors = self.g.neighbors(src[entry].node_index);
let related: Vec<String> = neighbors
let related: Vec<String> = self
.g
.neighbors(src[entry])
.filter(|node_index| {
let data = self.g[*node_index].to_string();
if !target.contains_key(&data) {
return false;
}
return true;
let data = &self.g[*node_index];
return target.contains_key(&data.name);
})
.map(|node_index| {
return self.g[node_index].name.to_string().clone();
})
.map(|node_index| self.g[node_index].to_string())
.collect();

Ok(related)
Expand Down

0 comments on commit 35b5d63

Please sign in to comment.