| 1 |
package fzf |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"runtime" |
| 6 |
"sync" |
| 7 |
"sync/atomic" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/junegunn/fzf/src/util" |
| 11 |
) |
| 12 |
|
| 13 |
// MatchRequest represents a search request |
| 14 |
type MatchRequest struct { |
| 15 |
chunks []*Chunk |
| 16 |
pattern *Pattern |
| 17 |
final bool |
| 18 |
sort bool |
| 19 |
revision revision |
| 20 |
} |
| 21 |
|
| 22 |
type MatchResult struct { |
| 23 |
merger *Merger |
| 24 |
passMerger *Merger |
| 25 |
cancelled bool |
| 26 |
} |
| 27 |
|
| 28 |
func (mr MatchResult) cacheable() bool { |
| 29 |
return mr.merger != nil && mr.merger.cacheable() |
| 30 |
} |
| 31 |
|
| 32 |
func (mr MatchResult) final() bool { |
| 33 |
return mr.merger != nil && mr.merger.final |
| 34 |
} |
| 35 |
|
| 36 |
// Matcher is responsible for performing search |
| 37 |
type Matcher struct { |
| 38 |
cache *ChunkCache |
| 39 |
patternBuilder func([]rune) *Pattern |
| 40 |
sort bool |
| 41 |
tac bool |
| 42 |
eventBox *util.EventBox |
| 43 |
reqBox *util.EventBox |
| 44 |
partitions int |
| 45 |
slab []*util.Slab |
| 46 |
sortBuf [][]Result |
| 47 |
mergerCache map[string]MatchResult |
| 48 |
revision revision |
| 49 |
scanMutex sync.Mutex |
| 50 |
cancelScan *util.AtomicBool |
| 51 |
} |
| 52 |
|
| 53 |
const ( |
| 54 |
reqRetry util.EventType = iota |
| 55 |
reqReset |
| 56 |
) |
| 57 |
|
| 58 |
// NewMatcher returns a new Matcher |
| 59 |
func NewMatcher(cache *ChunkCache, patternBuilder func([]rune) *Pattern, |
| 60 |
sort bool, tac bool, eventBox *util.EventBox, revision revision, threads int) *Matcher { |
| 61 |
partitions := runtime.NumCPU() |
| 62 |
if threads > 0 { |
| 63 |
partitions = threads |
| 64 |
} |
| 65 |
return &Matcher{ |
| 66 |
cache: cache, |
| 67 |
patternBuilder: patternBuilder, |
| 68 |
sort: sort, |
| 69 |
tac: tac, |
| 70 |
eventBox: eventBox, |
| 71 |
reqBox: util.NewEventBox(), |
| 72 |
partitions: partitions, |
| 73 |
slab: make([]*util.Slab, partitions), |
| 74 |
sortBuf: make([][]Result, partitions), |
| 75 |
mergerCache: make(map[string]MatchResult), |
| 76 |
revision: revision, |
| 77 |
cancelScan: util.NewAtomicBool(false)} |
| 78 |
} |
| 79 |
|
| 80 |
// Loop puts Matcher in action |
| 81 |
func (m *Matcher) Loop() { |
| 82 |
prevCount := 0 |
| 83 |
|
| 84 |
for { |
| 85 |
var request MatchRequest |
| 86 |
|
| 87 |
stop := false |
| 88 |
m.reqBox.Wait(func(events *util.Events) { |
| 89 |
for t, val := range *events { |
| 90 |
if t == reqQuit { |
| 91 |
stop = true |
| 92 |
return |
| 93 |
} |
| 94 |
switch val := val.(type) { |
| 95 |
case MatchRequest: |
| 96 |
request = val |
| 97 |
default: |
| 98 |
panic(fmt.Sprintf("Unexpected type: %T", val)) |
| 99 |
} |
| 100 |
} |
| 101 |
events.Clear() |
| 102 |
}) |
| 103 |
if stop { |
| 104 |
break |
| 105 |
} |
| 106 |
|
| 107 |
cacheCleared := false |
| 108 |
if request.sort != m.sort || request.revision != m.revision { |
| 109 |
m.sort = request.sort |
| 110 |
m.mergerCache = make(map[string]MatchResult) |
| 111 |
if !request.revision.compatible(m.revision) { |
| 112 |
m.cache.Clear() |
| 113 |
} |
| 114 |
m.revision = request.revision |
| 115 |
cacheCleared = true |
| 116 |
} |
| 117 |
|
| 118 |
// Restart search |
| 119 |
patternString := request.pattern.AsString() |
| 120 |
var result MatchResult |
| 121 |
count := CountItems(request.chunks) |
| 122 |
|
| 123 |
if !cacheCleared { |
| 124 |
if count == prevCount { |
| 125 |
// Look up mergerCache |
| 126 |
if cached, found := m.mergerCache[patternString]; found && cached.final() == request.final { |
| 127 |
result = cached |
| 128 |
} |
| 129 |
} else { |
| 130 |
// Invalidate mergerCache |
| 131 |
prevCount = count |
| 132 |
m.mergerCache = make(map[string]MatchResult) |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
if result.merger == nil { |
| 137 |
m.scanMutex.Lock() |
| 138 |
result = m.scan(request) |
| 139 |
m.scanMutex.Unlock() |
| 140 |
} |
| 141 |
|
| 142 |
if !result.cancelled { |
| 143 |
if result.cacheable() { |
| 144 |
m.mergerCache[patternString] = result |
| 145 |
} |
| 146 |
result.merger.final = request.final |
| 147 |
m.eventBox.Set(EvtSearchFin, result) |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
type partialResult struct { |
| 153 |
index int |
| 154 |
matches []Result |
| 155 |
} |
| 156 |
|
| 157 |
func (m *Matcher) scan(request MatchRequest) MatchResult { |
| 158 |
startedAt := time.Now() |
| 159 |
|
| 160 |
numChunks := len(request.chunks) |
| 161 |
if numChunks == 0 { |
| 162 |
m := EmptyMerger(request.revision) |
| 163 |
return MatchResult{m, m, false} |
| 164 |
} |
| 165 |
pattern := request.pattern |
| 166 |
passMerger := PassMerger(&request.chunks, m.tac, request.revision, pattern.startIndex) |
| 167 |
if pattern.IsEmpty() { |
| 168 |
return MatchResult{passMerger, passMerger, false} |
| 169 |
} |
| 170 |
|
| 171 |
minIndex := request.chunks[0].items[0].Index() |
| 172 |
maxIndex := request.chunks[numChunks-1].lastIndex(minIndex) |
| 173 |
cancelled := util.NewAtomicBool(false) |
| 174 |
|
| 175 |
numWorkers := min(m.partitions, numChunks) |
| 176 |
var nextChunk atomic.Int32 |
| 177 |
resultChan := make(chan partialResult, numWorkers) |
| 178 |
countChan := make(chan int, numChunks) |
| 179 |
waitGroup := sync.WaitGroup{} |
| 180 |
|
| 181 |
for idx := range numWorkers { |
| 182 |
waitGroup.Add(1) |
| 183 |
if m.slab[idx] == nil { |
| 184 |
m.slab[idx] = util.MakeSlab(slab16Size, slab32Size) |
| 185 |
} |
| 186 |
go func(idx int, slab *util.Slab) { |
| 187 |
defer waitGroup.Done() |
| 188 |
var matches []Result |
| 189 |
for { |
| 190 |
ci := int(nextChunk.Add(1)) - 1 |
| 191 |
if ci >= numChunks { |
| 192 |
break |
| 193 |
} |
| 194 |
chunkMatches := request.pattern.Match(request.chunks[ci], slab) |
| 195 |
matches = append(matches, chunkMatches...) |
| 196 |
if cancelled.Get() { |
| 197 |
return |
| 198 |
} |
| 199 |
countChan <- len(chunkMatches) |
| 200 |
} |
| 201 |
if m.sort && request.pattern.sortable { |
| 202 |
m.sortBuf[idx] = radixSortResults(matches, m.tac, m.sortBuf[idx]) |
| 203 |
} |
| 204 |
resultChan <- partialResult{idx, matches} |
| 205 |
}(idx, m.slab[idx]) |
| 206 |
} |
| 207 |
|
| 208 |
wait := func() bool { |
| 209 |
cancelled.Set(true) |
| 210 |
waitGroup.Wait() |
| 211 |
return true |
| 212 |
} |
| 213 |
|
| 214 |
count := 0 |
| 215 |
matchCount := 0 |
| 216 |
for matchesInChunk := range countChan { |
| 217 |
count++ |
| 218 |
matchCount += matchesInChunk |
| 219 |
|
| 220 |
if count == numChunks { |
| 221 |
break |
| 222 |
} |
| 223 |
|
| 224 |
if m.cancelScan.Get() || m.reqBox.Peek(reqReset) { |
| 225 |
return MatchResult{nil, nil, wait()} |
| 226 |
} |
| 227 |
|
| 228 |
if time.Since(startedAt) > progressMinDuration { |
| 229 |
m.eventBox.Set(EvtSearchProgress, float32(count)/float32(numChunks)) |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
partialResults := make([][]Result, numWorkers) |
| 234 |
for range numWorkers { |
| 235 |
partialResult := <-resultChan |
| 236 |
partialResults[partialResult.index] = partialResult.matches |
| 237 |
} |
| 238 |
merger := NewMerger(pattern, partialResults, m.sort && request.pattern.sortable, m.tac, request.revision, minIndex, maxIndex) |
| 239 |
return MatchResult{merger, passMerger, false} |
| 240 |
} |
| 241 |
|
| 242 |
// Reset is called to interrupt/signal the ongoing search |
| 243 |
func (m *Matcher) Reset(chunks []*Chunk, patternRunes []rune, cancel bool, final bool, sort bool, revision revision) { |
| 244 |
pattern := m.patternBuilder(patternRunes) |
| 245 |
|
| 246 |
var event util.EventType |
| 247 |
if cancel { |
| 248 |
event = reqReset |
| 249 |
} else { |
| 250 |
event = reqRetry |
| 251 |
} |
| 252 |
m.reqBox.Set(event, MatchRequest{chunks, pattern, final, sort, revision}) |
| 253 |
} |
| 254 |
|
| 255 |
// CancelScan cancels any in-flight scan, waits for it to finish, |
| 256 |
// and prevents new scans from starting until ResumeScan is called. |
| 257 |
// This is used to safely mutate shared items (e.g., during with-nth changes). |
| 258 |
func (m *Matcher) CancelScan() { |
| 259 |
m.cancelScan.Set(true) |
| 260 |
m.scanMutex.Lock() |
| 261 |
m.cancelScan.Set(false) |
| 262 |
} |
| 263 |
|
| 264 |
// ResumeScan allows scans to proceed again after CancelScan. |
| 265 |
func (m *Matcher) ResumeScan() { |
| 266 |
m.scanMutex.Unlock() |
| 267 |
} |
| 268 |
|
| 269 |
func (m *Matcher) Stop() { |
| 270 |
m.reqBox.Set(reqQuit, nil) |
| 271 |
} |