Skip to content

Commit 35b5d63

Browse files
committed
refactor(core): internal petgraph design
1 parent 49d0b56 commit 35b5d63

File tree

4 files changed

+56
-38
lines changed

4 files changed

+56
-38
lines changed

src/relation/graph.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,24 @@ use std::collections::HashMap;
55
use std::fmt::{Display, Formatter};
66
use std::sync::Arc;
77

8+
#[derive(PartialEq, Eq)]
89
pub(crate) enum NodeType {
910
File(Option<FileData>),
1011
Commit(Option<CommitData>),
1112
Issue(Option<IssueData>),
1213
Author(Option<AuthorData>),
1314
}
1415

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

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

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

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

2328
#[derive(Debug)]
@@ -37,20 +42,26 @@ impl Display for EdgeType {
3742
}
3843
}
3944

45+
#[derive(PartialEq, Eq)]
4046
pub struct NodeData {
41-
pub(crate) _name: Arc<String>,
47+
pub(crate) name: Arc<String>,
4248
pub(crate) _node_type: NodeType,
43-
pub(crate) node_index: NodeIndex,
4449
}
4550

46-
pub(crate) type NodeMapping = HashMap<Arc<String>, NodeData>;
51+
impl Display for NodeData {
52+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
53+
write!(f, "{:?}", self.name)
54+
}
55+
}
56+
57+
pub(crate) type NodeMapping = HashMap<Arc<String>, NodeIndex>;
4758

4859
pub struct RelationGraph {
4960
pub(crate) file_mapping: NodeMapping,
5061
pub(crate) commit_mapping: NodeMapping,
5162
pub(crate) issue_mapping: NodeMapping,
5263
pub(crate) author_mapping: NodeMapping,
53-
pub(crate) g: UnGraph<Arc<String>, EdgeType>,
64+
pub(crate) g: UnGraph<NodeData, EdgeType>,
5465
pub(crate) conf: CollectorConfig,
5566
}
5667

src/relation/graph_core.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ impl RelationGraph {
1010
commit_mapping: NodeMapping::new(),
1111
issue_mapping: NodeMapping::new(),
1212
author_mapping: NodeMapping::new(),
13-
g: UnGraph::<Arc<String>, EdgeType>::new_undirected(),
13+
g: UnGraph::<NodeData, EdgeType>::new_undirected(),
1414
conf: crate::collector::config::Config::default(),
1515
};
1616
}
@@ -25,13 +25,12 @@ impl RelationGraph {
2525

2626
if !mapping.contains_key(name) {
2727
let name_rc: Arc<String> = Arc::from(name.to_string());
28-
let node_index = self.g.add_node(name_rc.clone());
2928
let node_data = NodeData {
30-
_name: name_rc.clone(),
29+
name: name_rc.clone(),
3130
_node_type: node_type,
32-
node_index,
3331
};
34-
mapping.insert(name_rc, node_data);
32+
let node_index = self.g.add_node(node_data);
33+
mapping.insert(name_rc, node_index);
3534
}
3635
}
3736

@@ -60,35 +59,29 @@ impl RelationGraph {
6059
}
6160

6261
pub fn add_edge_file2commit(&mut self, file_name: &String, commit_name: &String) {
63-
if let (Some(file_data), Some(commit_data)) = (
62+
if let (Some(file_index), Some(commit_index)) = (
6463
self.file_mapping.get(file_name),
6564
self.commit_mapping.get(commit_name),
6665
) {
67-
let file_index = file_data.node_index;
68-
let commit_index = commit_data.node_index;
69-
self.add_edge(file_index, commit_index, EdgeType::File2Commit);
66+
self.add_edge(*file_index, *commit_index, EdgeType::File2Commit);
7067
}
7168
}
7269

7370
pub fn add_edge_file2issue(&mut self, file_name: &String, issue: &String) {
74-
if let (Some(file_data), Some(issue_data)) = (
71+
if let (Some(file_index), Some(issue_index)) = (
7572
self.file_mapping.get(file_name),
7673
self.issue_mapping.get(issue),
7774
) {
78-
let file_index = file_data.node_index;
79-
let issue_index = issue_data.node_index;
80-
self.add_edge(file_index, issue_index, EdgeType::File2Issue);
75+
self.add_edge(*file_index, *issue_index, EdgeType::File2Issue);
8176
}
8277
}
8378

8479
pub fn add_edge_commit2issue(&mut self, commit_name: &String, issue: &String) {
85-
if let (Some(commit_data), Some(issue_data)) = (
80+
if let (Some(commit_index), Some(issue_index)) = (
8681
self.commit_mapping.get(commit_name),
8782
self.issue_mapping.get(issue),
8883
) {
89-
let commit_index = commit_data.node_index;
90-
let issue_index = issue_data.node_index;
91-
self.add_edge(commit_index, issue_index, EdgeType::Commit2Issue);
84+
self.add_edge(*commit_index, *issue_index, EdgeType::Commit2Issue);
9285
}
9386
}
9487
}

src/relation/graph_ext.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,20 @@ impl RelationGraph {
99
}
1010

1111
pub fn add_edge_author2commit(&mut self, author_name: &String, commit_name: &String) {
12-
if let (Some(commit_data), Some(author_data)) = (
12+
if let (Some(commit_index), Some(author_index)) = (
1313
self.commit_mapping.get(commit_name),
1414
self.author_mapping.get(author_name),
1515
) {
16-
let commit_index = commit_data.node_index;
17-
let author_index = author_data.node_index;
18-
self.add_edge(commit_index, author_index, EdgeType::Author2Commit);
16+
self.add_edge(*commit_index, *author_index, EdgeType::Author2Commit);
1917
}
2018
}
2119

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

2628
pub fn author_related_commits(&self, author_name: &String) -> Result<Vec<String>, Error> {
@@ -41,7 +43,7 @@ impl RelationGraph {
4143
fn file_edge_counter(&self) -> HashMap<String, usize> {
4244
let mut edges_count_map: HashMap<_, usize> = HashMap::new();
4345
for (each_name, each) in &self.file_mapping {
44-
let edges = self.g.edges(each.node_index);
46+
let edges = self.g.edges(*each);
4547
let edge_count = edges.count();
4648
edges_count_map.insert(each_name.to_string(), edge_count);
4749
}

src/relation/graph_query.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,27 @@ use std::fmt::Error;
44
/// query API
55
impl RelationGraph {
66
pub fn get_file_node(&self, name: &String) -> Option<&NodeData> {
7-
self.file_mapping.get(name)
7+
if !self.file_mapping.contains_key(name) {
8+
return None;
9+
}
10+
let node_index = self.file_mapping.get(name).unwrap();
11+
return Some(&self.g[*node_index]);
812
}
913

1014
pub fn get_commit_node(&self, name: &String) -> Option<&NodeData> {
11-
self.commit_mapping.get(name)
15+
if !self.commit_mapping.contains_key(name) {
16+
return None;
17+
}
18+
let node_index = self.commit_mapping.get(name).unwrap();
19+
return Some(&self.g[*node_index]);
1220
}
1321

1422
pub fn get_issue_node(&self, name: &String) -> Option<&NodeData> {
15-
self.issue_mapping.get(name)
23+
if !self.issue_mapping.contains_key(name) {
24+
return None;
25+
}
26+
let node_index = self.issue_mapping.get(name).unwrap();
27+
return Some(&self.g[*node_index]);
1628
}
1729

1830
pub(crate) fn get_keys(&self, node_mapping: &NodeMapping) -> Vec<String> {
@@ -43,16 +55,16 @@ impl RelationGraph {
4355
if !src.contains_key(entry) {
4456
return Err(Error::default());
4557
}
46-
let neighbors = self.g.neighbors(src[entry].node_index);
47-
let related: Vec<String> = neighbors
58+
let related: Vec<String> = self
59+
.g
60+
.neighbors(src[entry])
4861
.filter(|node_index| {
49-
let data = self.g[*node_index].to_string();
50-
if !target.contains_key(&data) {
51-
return false;
52-
}
53-
return true;
62+
let data = &self.g[*node_index];
63+
return target.contains_key(&data.name);
64+
})
65+
.map(|node_index| {
66+
return self.g[node_index].name.to_string().clone();
5467
})
55-
.map(|node_index| self.g[node_index].to_string())
5668
.collect();
5769

5870
Ok(related)

0 commit comments

Comments
 (0)