-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
searcher.go
143 lines (132 loc) · 2.57 KB
/
searcher.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
package searcher
import (
"bytes"
"errors"
"io"
"sync"
"time"
)
const loadSize = 1024 * 1024
// Searcher represents a searcher.
type Searcher struct {
r io.ReaderAt
bytes []byte
loopCh chan struct{}
cursor int64
pattern string
mu *sync.Mutex
}
// NewSearcher creates a new searcher.
func NewSearcher(r io.ReaderAt) *Searcher {
return &Searcher{r: r, mu: new(sync.Mutex)}
}
type errNotFound string
func (err errNotFound) Error() string {
return "pattern not found: " + string(err)
}
// Search the pattern.
func (s *Searcher) Search(cursor int64, pattern string, forward bool) <-chan any {
s.mu.Lock()
defer s.mu.Unlock()
if s.bytes == nil {
s.bytes = make([]byte, loadSize)
}
s.cursor, s.pattern = cursor, pattern
ch := make(chan any)
if forward {
s.loop(s.forward, ch)
} else {
s.loop(s.backward, ch)
}
return ch
}
func (s *Searcher) forward() (int64, error) {
s.mu.Lock()
defer s.mu.Unlock()
target, err := patternToTarget(s.pattern)
if err != nil {
return -1, err
}
base := s.cursor + 1
n, err := s.r.ReadAt(s.bytes, base)
if err != nil && err != io.EOF {
return -1, err
}
if n == 0 {
return -1, errNotFound(s.pattern)
}
if err == io.EOF {
s.cursor += int64(n)
} else {
s.cursor += int64(n - len(target) + 1)
}
i := bytes.Index(s.bytes[:n], target)
if i >= 0 {
return base + int64(i), nil
}
return -1, nil
}
func (s *Searcher) backward() (int64, error) {
s.mu.Lock()
defer s.mu.Unlock()
target, err := patternToTarget(s.pattern)
if err != nil {
return -1, err
}
base := max(0, s.cursor-int64(loadSize))
size := int(min(int64(loadSize), s.cursor))
n, err := s.r.ReadAt(s.bytes[:size], base)
if err != nil && err != io.EOF {
return -1, err
}
if n == 0 {
return -1, errNotFound(s.pattern)
}
if s.cursor == int64(n) {
s.cursor = 0
} else {
s.cursor = base + int64(len(target)-1)
}
i := bytes.LastIndex(s.bytes[:n], target)
if i >= 0 {
return base + int64(i), nil
}
return -1, nil
}
func (s *Searcher) loop(f func() (int64, error), ch chan<- any) {
if s.loopCh != nil {
close(s.loopCh)
}
loopCh := make(chan struct{})
s.loopCh = loopCh
go func() {
defer close(ch)
for {
select {
case <-loopCh:
return
case <-time.After(10 * time.Millisecond):
idx, err := f()
if err != nil {
ch <- err
return
}
if idx >= 0 {
ch <- idx
return
}
}
}
}()
}
// Abort the searching.
func (s *Searcher) Abort() error {
s.mu.Lock()
defer s.mu.Unlock()
if s.loopCh != nil {
close(s.loopCh)
s.loopCh = nil
return errors.New("search is aborted")
}
return nil
}