Skip to content

Commit

Permalink
refactor(#50): id mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
williamfzc committed Jul 23, 2023
1 parent f3420c7 commit f486004
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 46 deletions.
47 changes: 28 additions & 19 deletions graph/file/api_stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,69 +36,78 @@ func (fg *Graph) Stat(f *Vertex) *object.ImpactUnit {

func (fg *Graph) GlobalStat(points []*Vertex) *object.StatGlobal {
sg := &object.StatGlobal{
UnitLevel: object.NodeLevelFile,
UnitLevel: object.NodeLevelFile,
UnitMapping: make(map[string]int),
}

totalUnits := make([]string, 0, len(fg.IdCache))
// creating mapping
curId := 0
for each := range fg.IdCache {
totalUnits = append(totalUnits, each)
sg.UnitMapping[each] = curId
curId++
}
sg.TotalUnits = totalUnits

entries := fg.ListEntries()
totalEntries := make([]string, 0, len(entries))
totalEntries := make([]int, 0, len(entries))
for _, each := range entries {
totalEntries = append(totalUnits, each.Id())
eachId := sg.UnitMapping[each.Id()]
totalEntries = append(totalEntries, eachId)
}
sg.TotalEntries = totalEntries

stats := make(map[string]*object.ImpactUnit, 0)
stats := make(map[int]*object.ImpactUnit, 0)
for _, each := range points {
eachStat := fg.Stat(each)
stats[each.Id()] = eachStat
eachId := sg.UnitMapping[each.Id()]
stats[eachId] = eachStat
}
sg.ImpactUnitsMap = stats

// direct impact
directImpactMap := make(map[string]struct{})
directImpactMap := make(map[int]struct{})
for _, each := range stats {
for _, eachReferenced := range each.ReferencedIds {
directImpactMap[eachReferenced] = struct{}{}
eachId := sg.UnitMapping[eachReferenced]
directImpactMap[eachId] = struct{}{}
}
for _, eachReference := range each.ReferenceIds {
directImpactMap[eachReference] = struct{}{}
eachId := sg.UnitMapping[eachReference]
directImpactMap[eachId] = struct{}{}
}
}
directImpactList := make([]string, 0, len(directImpactMap))
directImpactList := make([]int, 0, len(directImpactMap))
for each := range directImpactMap {
directImpactList = append(directImpactList, each)
}
sg.ImpactUnits = directImpactList

// in-direct impact
indirectImpactMap := make(map[string]struct{})
indirectImpactMap := make(map[int]struct{})
for _, each := range stats {
for _, eachReferenced := range each.TransitiveReferencedIds {
indirectImpactMap[eachReferenced] = struct{}{}
eachId := sg.UnitMapping[eachReferenced]
indirectImpactMap[eachId] = struct{}{}
}
for _, eachReference := range each.TransitiveReferenceIds {
indirectImpactMap[eachReference] = struct{}{}
eachId := sg.UnitMapping[eachReference]
indirectImpactMap[eachId] = struct{}{}
}
}
indirectImpactList := make([]string, 0, len(indirectImpactMap))
indirectImpactList := make([]int, 0, len(indirectImpactMap))
for each := range indirectImpactMap {
indirectImpactList = append(indirectImpactList, each)
}
sg.TransImpactUnits = indirectImpactList

// entries
entriesMap := make(map[string]struct{})
entriesMap := make(map[int]struct{})
for _, each := range stats {
for _, eachEntry := range each.Entries {
entriesMap[eachEntry] = struct{}{}
eachId := sg.UnitMapping[eachEntry]
entriesMap[eachId] = struct{}{}
}
}
entriesList := make([]string, 0, len(entriesMap))
entriesList := make([]int, 0, len(entriesMap))
for each := range entriesMap {
entriesList = append(entriesList, each)
}
Expand Down
47 changes: 28 additions & 19 deletions graph/function/api_stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,69 +32,78 @@ func (fg *Graph) Stat(f *Vertex) *object.ImpactUnit {

func (fg *Graph) GlobalStat(points []*Vertex) *object.StatGlobal {
sg := &object.StatGlobal{
UnitLevel: object.NodeLevelFunc,
UnitLevel: object.NodeLevelFile,
UnitMapping: make(map[string]int),
}

totalUnits := make([]string, 0, len(fg.IdCache))
// creating mapping
curId := 0
for each := range fg.IdCache {
totalUnits = append(totalUnits, each)
sg.UnitMapping[each] = curId
curId++
}
sg.TotalUnits = totalUnits

entries := fg.ListEntries()
totalEntries := make([]string, 0, len(entries))
totalEntries := make([]int, 0, len(entries))
for _, each := range entries {
totalEntries = append(totalUnits, each.Id())
eachId := sg.UnitMapping[each.Id()]
totalEntries = append(totalEntries, eachId)
}
sg.TotalEntries = totalEntries

stats := make(map[string]*object.ImpactUnit, 0)
stats := make(map[int]*object.ImpactUnit, 0)
for _, each := range points {
eachStat := fg.Stat(each)
stats[each.Id()] = eachStat
eachId := sg.UnitMapping[each.Id()]
stats[eachId] = eachStat
}
sg.ImpactUnitsMap = stats

// direct impact
directImpactMap := make(map[string]struct{})
directImpactMap := make(map[int]struct{})
for _, each := range stats {
for _, eachReferenced := range each.ReferencedIds {
directImpactMap[eachReferenced] = struct{}{}
eachId := sg.UnitMapping[eachReferenced]
directImpactMap[eachId] = struct{}{}
}
for _, eachReference := range each.ReferenceIds {
directImpactMap[eachReference] = struct{}{}
eachId := sg.UnitMapping[eachReference]
directImpactMap[eachId] = struct{}{}
}
}
directImpactList := make([]string, 0, len(directImpactMap))
directImpactList := make([]int, 0, len(directImpactMap))
for each := range directImpactMap {
directImpactList = append(directImpactList, each)
}
sg.ImpactUnits = directImpactList

// in-direct impact
indirectImpactMap := make(map[string]struct{})
indirectImpactMap := make(map[int]struct{})
for _, each := range stats {
for _, eachReferenced := range each.TransitiveReferencedIds {
indirectImpactMap[eachReferenced] = struct{}{}
eachId := sg.UnitMapping[eachReferenced]
indirectImpactMap[eachId] = struct{}{}
}
for _, eachReference := range each.TransitiveReferenceIds {
indirectImpactMap[eachReference] = struct{}{}
eachId := sg.UnitMapping[eachReference]
indirectImpactMap[eachId] = struct{}{}
}
}
indirectImpactList := make([]string, 0, len(indirectImpactMap))
indirectImpactList := make([]int, 0, len(indirectImpactMap))
for each := range indirectImpactMap {
indirectImpactList = append(indirectImpactList, each)
}
sg.TransImpactUnits = indirectImpactList

// entries
entriesMap := make(map[string]struct{})
entriesMap := make(map[int]struct{})
for _, each := range stats {
for _, eachEntry := range each.Entries {
entriesMap[eachEntry] = struct{}{}
eachId := sg.UnitMapping[eachEntry]
entriesMap[eachId] = struct{}{}
}
}
entriesList := make([]string, 0, len(entriesMap))
entriesList := make([]int, 0, len(entriesMap))
for each := range entriesMap {
entriesList = append(entriesList, each)
}
Expand Down
15 changes: 7 additions & 8 deletions object/impact_global.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ package object
StatGlobal
designed for offering a global view
todo: current struct is good for human reading but bad for data processing (too much storage cost
*/
type StatGlobal struct {
UnitLevel string `json:"unitLevel,omitempty"`
UnitLevel string `json:"unitLevel,omitempty"`
UnitMapping map[string]int `json:"unitMapping,omitempty"`

TotalUnits []string `json:"totalUnits,omitempty"`
ImpactUnits []string `json:"impactUnits,omitempty"`
TransImpactUnits []string `json:"transImpactUnits,omitempty"`
ImpactUnits []int `json:"impactUnits,omitempty"`
TransImpactUnits []int `json:"transImpactUnits,omitempty"`

TotalEntries []string `json:"entries,omitempty"`
ImpactEntries []string `json:"impactEntries,omitempty"`
TotalEntries []int `json:"entries,omitempty"`
ImpactEntries []int `json:"impactEntries,omitempty"`

ImpactUnitsMap map[string]*ImpactUnit `json:"-"`
ImpactUnitsMap map[int]*ImpactUnit `json:"-"`
}

0 comments on commit f486004

Please sign in to comment.