/
scanner.go
57 lines (48 loc) · 1.06 KB
/
scanner.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
package query
import (
"encoding/gob"
"io"
"log"
"sync"
)
// scanner is used to read in Queries from a Reader where they are
// Go-encoded and then distribute them to workers
type scanner struct {
r io.Reader
limit *uint64
}
// newScanner returns a new scanner for a given Reader and its limit
func newScanner(limit *uint64) *scanner {
return &scanner{limit: limit}
}
// setReader sets the source, an io.Reader, that the scanner reads/decodes from
func (s *scanner) setReader(r io.Reader) *scanner {
s.r = r
return s
}
// scan reads encoded Queries and places them into a channel
func (s *scanner) scan(pool *sync.Pool, c chan Query) {
decoder := gob.NewDecoder(s.r)
n := uint64(0)
for {
if *s.limit > 0 && n >= *s.limit {
// request queries limit reached, time to quit
break
}
q := pool.Get().(Query)
err := decoder.Decode(q)
if err == io.EOF {
// EOF, all done
break
}
if err != nil {
// Can't read, time to quit
log.Fatal(err)
}
// We have a query, send it to the runner
q.SetID(n)
c <- q
// Queries counter
n++
}
}