-
Notifications
You must be signed in to change notification settings - Fork 0
/
locate.go
187 lines (149 loc) · 3.35 KB
/
locate.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
package locate
import (
"container/list"
"encoding/gob"
"log"
"os"
"path/filepath"
"time"
)
func init() {
gob.Register(Record{})
}
type Record struct {
Path string
Name string
IsDir bool
}
func predicate(r Record, pattern string) bool {
return r.Name == pattern
}
type filepathWalker string
func (r filepathWalker) Walk(f func(string, os.FileInfo, error) error) error {
return filepath.Walk(string(r), f)
}
type walker interface {
Walk(func(string, os.FileInfo, error) error) error
}
type Index interface {
Search(pattern string) []Record
}
func NewIndex(nthreads int, period time.Duration, root string) Index {
return newIndex(nthreads, period, filepathWalker(root))
}
func newIndex(nthreads int, period time.Duration, w walker) *index {
idx := &index{
nthreads: nthreads,
indexPeriod: period,
data: [][]Record{},
w: w,
searchReq: make(chan searchReqMsg),
searchDone: make(chan searchDoneMsg),
createDone: make(chan createDoneMsg),
}
go idx.create()
go idx.director()
return idx
}
type searchReqMsg struct {
pattern string
reply chan []Record
}
type searchDoneMsg struct {
pattern string
results []Record
}
type createDoneMsg [][]Record
type index struct {
nthreads int
data [][]Record
indexPeriod time.Duration
w walker
searchReq chan searchReqMsg
searchDone chan searchDoneMsg
createDone chan createDoneMsg
}
func (p *index) Search(pattern string) []Record {
start := time.Now()
defer func() {
log.Printf("completed search for '%s' in %v", pattern, time.Since(start))
}()
reply := make(chan []Record)
p.searchReq <- searchReqMsg{pattern, reply}
return <-reply
}
func (p *index) search(pattern string) {
results := make(chan []Record)
for i := 0; i < p.nthreads; i++ {
go func(n int) {
results <- p.searchi(pattern, n)
}(i)
}
ret := []Record{}
for i := 0; i < p.nthreads; i++ {
ret = append(ret, (<-results)...)
}
p.searchDone <- searchDoneMsg{pattern, ret}
}
func (p *index) searchi(pattern string, i int) []Record {
ret := []Record{}
for _, r := range p.data[i] {
if predicate(r, pattern) {
ret = append(ret, r)
}
}
return ret
}
func (p *index) director() {
pendingSearches := make(map[string]*list.List)
lappend := func(msg searchReqMsg) {
_, ok := pendingSearches[msg.pattern]
if !ok {
pendingSearches[msg.pattern] = list.New()
}
pendingSearches[msg.pattern].PushFront(msg)
}
lreply := func(pattern string) chan []Record {
reqs, ok := pendingSearches[pattern]
defer reqs.Init()
if ok {
for e := reqs.Front(); e != nil; e = e.Next() {
return e.Value.(searchReqMsg).reply
}
}
return nil
}
doIndex := time.NewTicker(p.indexPeriod)
p.data = <-p.createDone
for {
select {
case msg := <-p.searchReq:
go p.search(msg.pattern)
lappend(msg)
case msg := <-p.searchDone:
lreply(msg.pattern) <- msg.results
case <-doIndex.C:
go p.create()
case msg := <-p.createDone:
p.data = msg
}
}
}
func (p *index) create() {
start := time.Now()
defer func() {
log.Println("created index in about", time.Since(start))
}()
data := make([][]Record, p.nthreads)
i := 0
p.w.Walk(func(fpath string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
r := Record{fpath, info.Name(), info.IsDir()}
data[i%p.nthreads] = append(data[i%p.nthreads], r)
i++
return nil
})
p.createDone <- data
}