Skip to content

Commit b3286f7

Browse files
Merge pull request goinaction#14 from gbbr/search
Search revamp
2 parents 215e896 + cbdfd03 commit b3286f7

3 files changed

Lines changed: 72 additions & 86 deletions

File tree

‎chapter7/patterns/search/main/main.go‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,27 @@ import (
1313

1414
// main is the entry point for all Go programs.
1515
func main() {
16-
// Define the options we want for this search.
17-
options := search.Options{
18-
SearchTerm: "golang",
19-
Google: true,
20-
Bing: true,
21-
Yahoo: true,
22-
First: true,
23-
}
24-
2516
// Submit the search and display the results.
26-
results := search.Submit(&options)
17+
results := search.Submit(
18+
"golang",
19+
search.OnlyFirst,
20+
search.Google,
21+
search.Bing,
22+
search.Yahoo,
23+
)
24+
2725
for _, result := range results {
2826
log.Printf("main : Results : Info : %+v\n", result)
2927
}
3028

3129
// This time we want to wait for all the results.
32-
options.First = false
33-
results = search.Submit(&options)
30+
results = search.Submit(
31+
"golang",
32+
search.Google,
33+
search.Bing,
34+
search.Yahoo,
35+
)
36+
3437
for _, result := range results {
3538
log.Printf("main : Results : Info : %+v\n", result)
3639
}

‎chapter7/patterns/search/search.go‎

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,7 @@
33
// Package search : search.go manages the searching of results against Google, Yahoo and Bing.
44
package search
55

6-
import (
7-
"log"
8-
)
9-
10-
// Options provides the search options for performing searches.
11-
type Options struct {
12-
SearchTerm string
13-
Google bool
14-
Bing bool
15-
Yahoo bool
16-
First bool
17-
}
6+
import "log"
187

198
// Result represents a search result that was found.
209
type Result struct {
@@ -30,61 +19,70 @@ type Searcher interface {
3019
Search(searchTerm string, searchResults chan<- []Result)
3120
}
3221

33-
// Submit uses goroutines and channels to perform a search against the three
34-
// leading search engines concurrently.
35-
func Submit(options *Options) []Result {
36-
log.Printf("search : Submit : Started : %#v\n", options)
22+
type searchSession struct {
23+
searchers map[string]Searcher
24+
first bool
25+
resultChan chan []Result
26+
}
3727

38-
var final []Result
39-
searchers := make(map[string]Searcher)
40-
searchResults := make(chan []Result)
28+
func Google(s *searchSession) {
29+
log.Println("search : Submit : Info : Adding Google")
30+
s.searchers["google"] = NewGoogle()
31+
}
4132

42-
// Create a Google Searcher if checked.
43-
if options.Google {
44-
log.Println("search : Submit : Info : Adding Google")
45-
searchers["google"] = NewGoogle()
46-
}
33+
func Bing(s *searchSession) {
34+
log.Println("search : Submit : Info : Adding Bing")
35+
s.searchers["bing"] = NewBing()
36+
}
4737

48-
// Create a Bing Searcher if checked.
49-
if options.Bing {
50-
log.Println("search : Submit : Info : Adding Bing")
51-
searchers["bing"] = NewBing()
52-
}
38+
func Yahoo(s *searchSession) {
39+
log.Println("search : Submit : Info : Adding Yahoo")
40+
s.searchers["yahoo"] = NewYahoo()
41+
}
5342

54-
// Create a Yahoo Searcher if checked.
55-
if options.Yahoo {
56-
log.Println("search : Submit : Info : Adding Yahoo")
57-
searchers["yahoo"] = NewYahoo()
43+
func OnlyFirst(s *searchSession) { s.first = true }
44+
45+
// Submit uses goroutines and channels to perform a search against the three
46+
// leading search engines concurrently.
47+
func Submit(query string, options ...func(*searchSession)) []Result {
48+
var session searchSession
49+
session.searchers = make(map[string]Searcher)
50+
session.resultChan = make(chan []Result)
51+
52+
for _, opt := range options {
53+
opt(&session)
5854
}
5955

6056
// Perform the searches concurrently. Using a map because
6157
// it returns the searchers in a random order every time.
62-
for _, searcher := range searchers {
63-
go searcher.Search(options.SearchTerm, searchResults)
58+
for _, s := range session.searchers {
59+
go s.Search(query, session.resultChan)
6460
}
6561

62+
var results []Result
63+
6664
// Wait for the results to come back.
67-
for search := 0; search < len(searchers); search++ {
65+
for search := 0; search < len(session.searchers); search++ {
6866
// If we just want the first result, don't wait any longer by
6967
// concurrently discarding the remaining searchResults.
7068
// Failing to do so will leave the Searchers blocked forever.
71-
if options.First && search > 0 {
69+
if session.first && search > 0 {
7270
go func() {
73-
results := <-searchResults
74-
log.Printf("search : Submit : Info : Results Discarded : Results[%d]\n", len(results))
71+
r := <-session.resultChan
72+
log.Printf("search : Submit : Info : Results Discarded : Results[%d]\n", len(r))
7573
}()
7674
continue
7775
}
7876

7977
// Wait to recieve results.
8078
log.Println("search : Submit : Info : Waiting For Results...")
81-
results := <-searchResults
79+
result := <-session.resultChan
8280

8381
// Save the results to the final slice.
84-
log.Printf("search : Submit : Info : Results Used : Results[%d]\n", len(results))
85-
final = append(final, results...)
82+
log.Printf("search : Submit : Info : Results Used : Results[%d]\n", len(result))
83+
results = append(results, result...)
8684
}
8785

88-
log.Printf("search : Submit : Completed : Found [%d] Results\n", len(final))
89-
return final
86+
log.Printf("search : Submit : Completed : Found [%d] Results\n", len(results))
87+
return results
9088
}

‎chapter7/patterns/search/searchers.go‎

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,21 @@ func init() {
1616
}
1717

1818
// Google provides support for Google searches.
19-
type Google struct{}
19+
type google struct{}
2020

2121
// NewGoogle returns a Google Searcher value.
22-
func NewGoogle() Searcher {
23-
return Google{}
22+
func NewGoogle() google {
23+
return google{}
2424
}
2525

2626
// Search implements the Searcher interface. It performs a search
2727
// against Google.
28-
func (g Google) Search(searchTerm string, searchResults chan<- []Result) {
28+
func (g google) Search(searchTerm string, searchResults chan<- []Result) {
2929
log.Printf("Google : Search : Started : searchTerm[%s]\n", searchTerm)
3030

3131
// Slice for the results.
3232
var results []Result
3333

34-
// On return send the results we have.
35-
defer func() {
36-
log.Println("Google : Search : Info : Sending Results")
37-
searchResults <- results
38-
}()
39-
4034
// Simulate an amount of time for the search.
4135
time.Sleep(time.Millisecond * time.Duration(rand.Int63n(900)))
4236

@@ -49,30 +43,25 @@ func (g Google) Search(searchTerm string, searchResults chan<- []Result) {
4943
})
5044

5145
log.Printf("Google : Search : Completed : Found[%d]\n", len(results))
46+
searchResults <- results
5247
}
5348

5449
// Bing provides support for Bing searches.
55-
type Bing struct{}
50+
type bing struct{}
5651

5752
// NewBing returns a Bing Searcher value.
58-
func NewBing() Searcher {
59-
return Bing{}
53+
func NewBing() bing {
54+
return bing{}
6055
}
6156

6257
// Search implements the Searcher interface. It performs a search
6358
// against Bing.
64-
func (b Bing) Search(searchTerm string, searchResults chan<- []Result) {
59+
func (b bing) Search(searchTerm string, searchResults chan<- []Result) {
6560
log.Printf("Bing : Search : Started : searchTerm[%s]\n", searchTerm)
6661

6762
// Slice for the results.
6863
var results []Result
6964

70-
// On return send the results we have.
71-
defer func() {
72-
log.Println("Bing : Search : Info : Sending Results")
73-
searchResults <- results
74-
}()
75-
7665
// Simulate an amount of time for the search.
7766
time.Sleep(time.Millisecond * time.Duration(rand.Int63n(900)))
7867

@@ -85,30 +74,25 @@ func (b Bing) Search(searchTerm string, searchResults chan<- []Result) {
8574
})
8675

8776
log.Printf("Bing : Search : Completed : Found[%d]\n", len(results))
77+
searchResults <- results
8878
}
8979

9080
// Yahoo provides support for Yahoo searches.
91-
type Yahoo struct{}
81+
type yahoo struct{}
9282

9383
// NewYahoo returns a Yahoo Searcher value.
94-
func NewYahoo() Searcher {
95-
return Yahoo{}
84+
func NewYahoo() yahoo {
85+
return yahoo{}
9686
}
9787

9888
// Search implements the Searcher interface. It performs a search
9989
// against Yahoo.
100-
func (y Yahoo) Search(searchTerm string, searchResults chan<- []Result) {
90+
func (y yahoo) Search(searchTerm string, searchResults chan<- []Result) {
10191
log.Printf("Yahoo : Search : Started : searchTerm[%s]\n", searchTerm)
10292

10393
// Slice for the results.
10494
var results []Result
10595

106-
// On return send the results we have.
107-
defer func() {
108-
log.Println("Yahoo : Search : Info : Sending Results")
109-
searchResults <- results
110-
}()
111-
11296
// Simulate an amount of time for the search.
11397
time.Sleep(time.Millisecond * time.Duration(rand.Int63n(900)))
11498

@@ -121,4 +105,5 @@ func (y Yahoo) Search(searchTerm string, searchResults chan<- []Result) {
121105
})
122106

123107
log.Printf("Yahoo : Search : Completed : Found[%d]\n", len(results))
108+
searchResults <- results
124109
}

0 commit comments

Comments
 (0)