Skip to content

Commit

Permalink
Add more testing for edge cases (#56)
Browse files Browse the repository at this point in the history
* Add more testing for edge cases

* Some more tests

Signed-off-by: naveensrinivasan <[email protected]>

* Update pkg/graph/cache_test.go

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Update pkg/graph/cache_test.go

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Fixed the failing test

Signed-off-by: naveensrinivasan <[email protected]>

---------

Signed-off-by: naveensrinivasan <[email protected]>
Co-authored-by: naveensrinivasan <[email protected]>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 22, 2024
1 parent f743b5c commit 4698696
Show file tree
Hide file tree
Showing 13 changed files with 778 additions and 449 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

# Output
bin

coverage.out
8 changes: 6 additions & 2 deletions cmd/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"encoding/json"
"fmt"
"net/http"
"os"
"path/filepath"
"strconv"
Expand All @@ -18,8 +19,8 @@ import (
type options struct {
storage graph.Storage
outputdir string
maxOutput int
addr string
maxOutput int
visualize bool
}

Expand Down Expand Up @@ -78,7 +79,10 @@ func (o *options) Run(_ *cobra.Command, args []string) error {
}

if o.visualize {
shutdown, err := graph.RunGraphVisualizer(o.storage, execute, script, "8081")
server := &http.Server{
Addr: ":" + o.addr,
}
shutdown, err := graph.RunGraphVisualizer(o.storage, execute, script, server)
if err != nil {
return err
}
Expand Down
48 changes: 14 additions & 34 deletions pkg/graph/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,14 @@ func Cache(storage Storage) error {
return fmt.Errorf("error getting all nodes: %w", err)
}

childSCC, err := findCycles(storage, ChildrenDirection, len(keys), allNodes)
if err != nil {
return err
}

cachedChildren, err := buildCache(storage, uncachedNodes, ChildrenDirection, childSCC, allNodes)
if err != nil {
return err
}
scc := findCycles(len(keys), allNodes)

parentSCC, err := findCycles(storage, ParentsDirection, len(keys), allNodes)
cachedChildren, err := buildCache(uncachedNodes, ChildrenDirection, scc, allNodes)
if err != nil {
return err
}

cachedParents, err := buildCache(storage, uncachedNodes, ParentsDirection, parentSCC, allNodes)
cachedParents, err := buildCache(uncachedNodes, ParentsDirection, scc, allNodes)
if err != nil {
return err
}
Expand Down Expand Up @@ -78,36 +70,27 @@ func Cache(storage Storage) error {
return storage.ClearCacheStack()
}

func findCycles(storage Storage, direction Direction, numOfNodes int, allNodes map[uint32]*Node) (map[uint32]uint32, error) {
func findCycles(numOfNodes int, allNodes map[uint32]*Node) map[uint32]uint32 {
var stack []uint32
var tarjanDFS func(nodeID uint32) error
var tarjanDFS func(nodeID uint32)

currentTarjanID := 0
nodeToTarjanID := map[uint32]uint32{}
lowLink := make(map[uint32]uint32)
inStack := roaring.New()

tarjanDFS = func(nodeID uint32) error {
tarjanDFS = func(nodeID uint32) {
currentNode := allNodes[nodeID]

var nextNodes []uint32
if direction == ChildrenDirection {
nextNodes = currentNode.Children.ToArray()
} else {
nextNodes = currentNode.Parents.ToArray()
}

currentTarjanID++
stack = append(stack, nodeID)
inStack.Add(nodeID)
nodeToTarjanID[nodeID] = uint32(currentTarjanID)
lowLink[nodeID] = uint32(currentTarjanID)

for _, nextNode := range nextNodes {
for _, nextNode := range currentNode.Children.ToArray() {
if _, visited := nodeToTarjanID[nextNode]; !visited {
if err := tarjanDFS(nextNode); err != nil {
return err
}
tarjanDFS(nextNode)

lowLink[nodeID] = min(lowLink[nodeID], lowLink[nextNode])
} else if inStack.Contains(nextNode) {
lowLink[nodeID] = min(lowLink[nodeID], nodeToTarjanID[nextNode])
Expand All @@ -125,30 +108,27 @@ func findCycles(storage Storage, direction Direction, numOfNodes int, allNodes m
}
}
}
return nil
}

for id := 1; id < numOfNodes+1; id++ {
if _, visited := nodeToTarjanID[uint32(id)]; !visited {
if err := tarjanDFS(uint32(id)); err != nil {
return nil, err
}
tarjanDFS(uint32(id))
}
}

return lowLink, nil
return lowLink
}

type stackElm struct {
id uint32
todoIndex int
}

func buildCache(storage Storage, uncachedNodes []uint32, direction Direction, scc map[uint32]uint32, allNodes map[uint32]*Node) (*NativeKeyManagement, error) {
func buildCache(uncachedNodes []uint32, direction Direction, scc map[uint32]uint32, allNodes map[uint32]*Node) (*NativeKeyManagement, error) {
cache, children, parents := NewNativeKeyManagement(), NewNativeKeyManagement(), NewNativeKeyManagement()
alreadyCached := roaring.New()

err := addCyclesToBindMap(storage, scc, cache, children, parents, allNodes)
err := addCyclesToBindMap(scc, cache, children, parents, allNodes)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -197,7 +177,7 @@ func buildCache(storage Storage, uncachedNodes []uint32, direction Direction, sc
return cache, nil
}

func addCyclesToBindMap(storage Storage, scc map[uint32]uint32, cache, children, parents *NativeKeyManagement, allNodes map[uint32]*Node) error {
func addCyclesToBindMap(scc map[uint32]uint32, cache, children, parents *NativeKeyManagement, allNodes map[uint32]*Node) error {
parentToKeys := map[uint32][]string{}

for k, v := range scc {
Expand Down
Loading

0 comments on commit 4698696

Please sign in to comment.