-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstorage.go
57 lines (52 loc) · 966 Bytes
/
storage.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 traces
import (
"bufio"
"bytes"
"context"
"io"
"strconv"
)
type storageProvider struct {
r *bufio.Reader
}
// NewStorageProvider returns a Provider with items are from
// Storage traces by the University of Massachusetts
// (http://traces.cs.umass.edu/index.php/Storage/Storage).
func NewStorageProvider(r io.Reader) Provider {
return &storageProvider{
r: bufio.NewReader(r),
}
}
func (p *storageProvider) Provide(ctx context.Context, keys chan<- interface{}) {
defer close(keys)
for {
b, err := p.r.ReadBytes('\n')
if err != nil {
return
}
k := p.parse(b)
if k > 0 {
select {
case <-ctx.Done():
return
case keys <- k:
}
}
}
}
func (p *storageProvider) parse(b []byte) uint64 {
idx := bytes.IndexByte(b, ',')
if idx < 0 {
return 0
}
b = b[idx+1:]
idx = bytes.IndexByte(b, ',')
if idx < 0 {
return 0
}
k, err := strconv.ParseUint(string(b[:idx]), 10, 64)
if err != nil {
return 0
}
return k
}