-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathbfs.go
188 lines (167 loc) · 3.83 KB
/
bfs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package traversal
import (
"context"
"fmt"
"log"
"strings"
"sync"
"github.com/karthikkalarikal/Qube/models"
"github.com/karthikkalarikal/Qube/pkg/util"
)
type Node struct {
Entity string
Type string
From *Node
Link string
}
func NewNode(start, target string) {
node := bfs(start, target)
printPath(node)
}
func bfs(start, end string) *Node {
var visited sync.Map
var once sync.Once
var wg sync.WaitGroup
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
type safeQueue struct {
mu sync.Mutex
cond *sync.Cond
items []Node
}
queue := &safeQueue{items: make([]Node, 0)}
queue.cond = sync.NewCond(&queue.mu)
result := make(chan *Node, 1)
queue.mu.Lock()
queue.items = append(queue.items, Node{Entity: start, Type: "person"})
queue.cond.Signal()
queue.mu.Unlock()
worker := func() {
defer wg.Done()
for {
select {
case <-ctx.Done():
return
default:
queue.mu.Lock()
// Wait for work or cancellation
for len(queue.items) == 0 {
if ctx.Err() != nil {
queue.mu.Unlock()
return
}
queue.cond.Wait()
}
// Dequeue node
current := queue.items[0]
queue.items = queue.items[1:]
queue.mu.Unlock()
// Skip if already processed
if _, loaded := visited.LoadOrStore(current.Entity, true); loaded {
continue
}
// Early termination check
if current.Entity == end {
once.Do(func() {
result <- ¤t
cancel()
})
return
}
// Process node
if current.Type == "person" {
var person models.Actor
if err := util.GetByURL(current.Entity, &person); err != nil {
if checkErrorForbidden(err) {
visited.Delete(current.Entity)
}
log.Printf("Retryable error on %s: %v", current.Entity, err)
continue
}
for _, credit := range person.Movies {
child := Node{
Entity: credit.URL,
Type: "movie",
From: ¤t,
Link: "Movie: " + credit.Name + " (" + credit.Role + ")",
}
queue.mu.Lock()
queue.items = append(queue.items, child)
queue.cond.Signal()
queue.mu.Unlock()
}
} else {
var movie models.Movie
if err := util.GetByURL(current.Entity, &movie); err != nil {
if checkErrorForbidden(err) {
visited.Delete(current.Entity)
}
log.Printf("Retryable error on %s: %v", current.Entity, err) //since some of the links are not accessible, the retry logic is specific.
continue
}
for _, cast := range movie.Cast {
child := Node{
Entity: cast.URL,
Type: "person",
From: ¤t,
Link: "Cast: " + cast.Name + " (" + cast.Role + ")",
}
queue.mu.Lock()
queue.items = append(queue.items, child)
queue.cond.Signal()
queue.mu.Unlock()
}
for _, crew := range movie.Crew {
child := Node{
Entity: crew.URL,
Type: "person",
From: ¤t,
Link: "Crew: " + crew.Name + " (" + crew.Role + ")",
}
queue.mu.Lock()
queue.items = append(queue.items, child)
queue.cond.Signal()
queue.mu.Unlock()
}
}
}
}
}
// Start workers
numWorkers := 30
wg.Add(numWorkers)
for i := 0; i < numWorkers; i++ {
go worker()
}
go func() {
wg.Wait()
once.Do(func() { close(result) })
}()
select {
case res := <-result:
return res
case <-ctx.Done():
return nil
}
}
func printPath(node *Node) {
if node == nil {
fmt.Println("No path found.")
return
}
var path []*Node
for node != nil {
path = append([]*Node{node}, path...)
node = node.From
}
for i, n := range path {
if i == 0 {
fmt.Printf("%d. Start: %s\n", i+1, n.Entity)
} else {
fmt.Printf("%d. %s\n", i+1, n.Link)
}
}
}
func checkErrorForbidden(err error) bool {
return strings.Contains(err.Error(), "access denied for URL:")
}