forked from google/zoekt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
contentprovider.go
348 lines (295 loc) · 8.6 KB
/
contentprovider.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Copyright 2016 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package zoekt
import (
"bytes"
"log"
"sort"
"unicode/utf8"
)
var _ = log.Println
// contentProvider is an abstraction to treat matches for names and
// content with the same code.
type contentProvider struct {
id *indexData
stats *Stats
// mutable
err error
idx uint32
_data []byte
_nl []uint32
_nlBuf []uint32
_sects []DocumentSection
_sectBuf []DocumentSection
fileSize uint32
}
// setDocument skips to the given document.
func (p *contentProvider) setDocument(docID uint32) {
fileStart := p.id.boundaries[docID]
p.idx = docID
p.fileSize = p.id.boundaries[docID+1] - fileStart
p._nl = nil
p._sects = nil
p._data = nil
}
func (p *contentProvider) docSections() []DocumentSection {
if p._sects == nil {
var sz uint32
p._sects, sz, p.err = p.id.readDocSections(p.idx, p._sectBuf)
p.stats.ContentBytesLoaded += int64(sz)
p._sectBuf = p._sects
}
return p._sects
}
func (p *contentProvider) newlines() []uint32 {
if p._nl == nil {
var sz uint32
p._nl, sz, p.err = p.id.readNewlines(p.idx, p._nlBuf)
p._nlBuf = p._nl
p.stats.ContentBytesLoaded += int64(sz)
}
return p._nl
}
func (p *contentProvider) data(fileName bool) []byte {
if fileName {
return p.id.fileNameContent[p.id.fileNameIndex[p.idx]:p.id.fileNameIndex[p.idx+1]]
}
if p._data == nil {
p._data, p.err = p.id.readContents(p.idx)
p.stats.FilesLoaded++
p.stats.ContentBytesLoaded += int64(len(p._data))
}
return p._data
}
// Find offset in bytes (relative to corpus start) for an offset in
// runes (relative to document start). If filename is set, the corpus
// is the set of filenames, with the document being the name itself.
func (p *contentProvider) findOffset(filename bool, r uint32) uint32 {
if p.id.metaData.PlainASCII {
return r
}
sample := p.id.runeOffsets
runeEnds := p.id.fileEndRunes
fileStartByte := p.id.boundaries[p.idx]
if filename {
sample = p.id.fileNameRuneOffsets
runeEnds = p.id.fileNameEndRunes
fileStartByte = p.id.fileNameIndex[p.idx]
}
absR := r
if p.idx > 0 {
absR += runeEnds[p.idx-1]
}
byteOff, left := sample.lookup(absR)
var data []byte
if filename {
data = p.id.fileNameContent[byteOff:]
} else {
data, p.err = p.id.readContentSlice(byteOff, 3*runeOffsetFrequency)
if p.err != nil {
return 0
}
}
for left > 0 {
_, sz := utf8.DecodeRune(data)
byteOff += uint32(sz)
data = data[sz:]
left--
}
byteOff -= fileStartByte
return byteOff
}
func (p *contentProvider) fillMatches(ms []*candidateMatch, numContextLines int) []LineMatch {
var result []LineMatch
if ms[0].fileName {
// There is only "line" in a filename.
res := LineMatch{
Line: p.id.fileName(p.idx),
FileName: true,
}
for _, m := range ms {
res.LineFragments = append(res.LineFragments, LineFragmentMatch{
LineOffset: int(m.byteOffset),
MatchLength: int(m.byteMatchSz),
Offset: m.byteOffset,
})
result = []LineMatch{res}
}
} else {
ms = breakMatchesOnNewlines(ms, p.data(false))
result = p.fillContentMatches(ms, numContextLines)
}
sects := p.docSections()
for i, m := range result {
result[i].Score = matchScore(sects, &m)
}
return result
}
func (p *contentProvider) fillContentMatches(ms []*candidateMatch, numContextLines int) []LineMatch {
var result []LineMatch
for len(ms) > 0 {
m := ms[0]
num, lineStart, lineEnd := m.line(p.newlines(), p.fileSize)
var lineCands []*candidateMatch
endMatch := m.byteOffset + m.byteMatchSz
for len(ms) > 0 {
m := ms[0]
if int(m.byteOffset) <= lineEnd {
endMatch = m.byteOffset + m.byteMatchSz
lineCands = append(lineCands, m)
ms = ms[1:]
} else {
break
}
}
if len(lineCands) == 0 {
log.Panicf(
"%s %v infinite loop: num %d start,end %d,%d, offset %d",
p.id.fileName(p.idx), p.id.metaData,
num, lineStart, lineEnd,
m.byteOffset)
}
data := p.data(false)
// Due to merging matches, we may have a match that
// crosses a line boundary. Prevent confusion by
// taking lines until we pass the last match
for lineEnd < len(data) && endMatch > uint32(lineEnd) {
next := bytes.IndexByte(data[lineEnd+1:], '\n')
if next == -1 {
lineEnd = len(data)
} else {
// TODO(hanwen): test that checks "+1" part here.
lineEnd += next + 1
}
}
finalMatch := LineMatch{
LineStart: lineStart,
LineEnd: lineEnd,
LineNumber: num,
}
finalMatch.Line = data[lineStart:lineEnd]
if numContextLines > 0 {
finalMatch.Before = getLines(data, p.newlines(), num-numContextLines, num)
finalMatch.After = getLines(data, p.newlines(), num+1, num+1+numContextLines)
}
for _, m := range lineCands {
fragment := LineFragmentMatch{
Offset: m.byteOffset,
LineOffset: int(m.byteOffset) - lineStart,
MatchLength: int(m.byteMatchSz),
}
if m.symbol {
start := p.id.fileEndSymbol[p.idx]
fragment.SymbolInfo = p.id.symbols.data(start + m.symbolIdx)
if fragment.SymbolInfo != nil {
sec := p.docSections()[m.symbolIdx]
fragment.SymbolInfo.Sym = string(data[sec.Start:sec.End])
}
}
finalMatch.LineFragments = append(finalMatch.LineFragments, fragment)
}
result = append(result, finalMatch)
}
return result
}
// getLines returns a slice of data containing the lines [low, high).
// low is 1-based and inclusive. high is exclusive.
func getLines(data []byte, newLines []uint32, low, high int) []byte {
// newlines[0] is the start of the 2nd line in data.
// So adjust low and high to be based on newLines.
low -= 2
high -= 2
if low >= high || high < 0 || low >= len(newLines) || len(newLines) == 0 {
return nil
}
var startIndex uint32
if low < 0 {
startIndex = 0
} else {
startIndex = newLines[low] + 1
}
if high >= len(newLines) {
return data[startIndex:]
}
return data[startIndex:newLines[high]]
}
const (
// TODO - how to scale this relative to rank?
scorePartialWordMatch = 50.0
scoreWordMatch = 500.0
scoreImportantThreshold = 2000.0
scorePartialSymbol = 4000.0
scoreSymbol = 7000.0
scoreFactorAtomMatch = 400.0
scoreShardRankFactor = 20.0
scoreFileOrderFactor = 10.0
scoreLineOrderFactor = 1.0
)
func findSection(secs []DocumentSection, off, sz uint32) *DocumentSection {
j := sort.Search(len(secs), func(i int) bool {
return secs[i].End >= off+sz
})
if j == len(secs) {
return nil
}
if secs[j].Start <= off && off+sz <= secs[j].End {
return &secs[j]
}
return nil
}
func matchScore(secs []DocumentSection, m *LineMatch) float64 {
var maxScore float64
for _, f := range m.LineFragments {
startBoundary := f.LineOffset < len(m.Line) && (f.LineOffset == 0 || byteClass(m.Line[f.LineOffset-1]) != byteClass(m.Line[f.LineOffset]))
end := int(f.LineOffset) + f.MatchLength
endBoundary := end > 0 && (end == len(m.Line) || byteClass(m.Line[end-1]) != byteClass(m.Line[end]))
score := 0.0
if startBoundary && endBoundary {
score = scoreWordMatch
} else if startBoundary || endBoundary {
score = scorePartialWordMatch
}
sec := findSection(secs, f.Offset, uint32(f.MatchLength))
if sec != nil {
startMatch := sec.Start == f.Offset
endMatch := sec.End == f.Offset+uint32(f.MatchLength)
if startMatch && endMatch {
score += scoreSymbol
} else if startMatch || endMatch {
score += (scoreSymbol + scorePartialSymbol) / 2
} else {
score += scorePartialSymbol
}
}
if score > maxScore {
maxScore = score
}
}
return maxScore
}
type matchScoreSlice []LineMatch
func (m matchScoreSlice) Len() int { return len(m) }
func (m matchScoreSlice) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
func (m matchScoreSlice) Less(i, j int) bool { return m[i].Score > m[j].Score }
type fileMatchSlice []FileMatch
func (m fileMatchSlice) Len() int { return len(m) }
func (m fileMatchSlice) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
func (m fileMatchSlice) Less(i, j int) bool { return m[i].Score > m[j].Score }
func sortMatchesByScore(ms []LineMatch) {
sort.Sort(matchScoreSlice(ms))
}
// Sort a slice of results.
func SortFilesByScore(ms []FileMatch) {
sort.Sort(fileMatchSlice(ms))
}