forked from google/zoekt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexdata.go
313 lines (256 loc) · 6.73 KB
/
indexdata.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
// 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 (
"fmt"
"hash/crc64"
"unicode/utf8"
"github.com/google/zoekt/query"
)
// indexData holds the pattern-independent data that we have to have
// in memory to search. Most of the memory is taken up by the ngram =>
// offset index.
type indexData struct {
file IndexFile
ngrams map[ngram]simpleSection
newlinesStart uint32
newlinesIndex []uint32
docSectionsStart uint32
docSectionsIndex []uint32
// rune offset=>byte offset mapping, relative to the start of the content corpus
runeOffsets []uint32
// offsets of file contents; includes end of last file
boundariesStart uint32
boundaries []uint32
// rune offsets for the file content boundaries
fileEndRunes []uint32
fileNameContent []byte
fileNameIndex []uint32
fileNameNgrams map[ngram][]uint32
// rune offset=>byte offset mapping, relative to the start of the filename corpus
fileNameRuneOffsets []uint32
// rune offsets for the file name boundaries
fileNameEndRunes []uint32
fileBranchMasks []uint64
// mask (power of 2) => name
branchNames map[uint]string
// name => mask (power of 2)
branchIDs map[string]uint
metaData IndexMetadata
repoMetaData Repository
subRepos []uint32
subRepoPaths []string
// Checksums for all the files, at 8-byte intervals
checksums []byte
// languages for all the files.
languages []byte
// inverse of LanguageMap in metaData
languageMap map[byte]string
repoListEntry RepoListEntry
}
func (d *indexData) getChecksum(idx uint32) []byte {
start := crc64.Size * idx
return d.checksums[start : start+crc64.Size]
}
func (d *indexData) calculateStats() {
var last uint32
if len(d.boundaries) > 0 {
last += d.boundaries[len(d.boundaries)-1]
}
lastFN := last
if len(d.fileNameIndex) > 0 {
lastFN = d.fileNameIndex[len(d.fileNameIndex)-1]
}
stats := RepoStats{
IndexBytes: int64(d.memoryUse()),
ContentBytes: int64(int(last) + int(lastFN)),
Documents: len(d.newlinesIndex) - 1,
}
d.repoListEntry = RepoListEntry{
Repository: d.repoMetaData,
IndexMetadata: d.metaData,
Stats: stats,
}
}
func (d *indexData) String() string {
return fmt.Sprintf("shard(%s)", d.file.Name())
}
func (d *indexData) memoryUse() int {
sz := 0
for _, a := range [][]uint32{
d.newlinesIndex, d.docSectionsIndex,
d.boundaries, d.fileNameIndex,
d.runeOffsets, d.fileNameRuneOffsets,
d.fileEndRunes, d.fileNameEndRunes,
} {
sz += 4 * len(a)
}
sz += 8 * len(d.fileBranchMasks)
sz += 12 * len(d.ngrams)
for _, v := range d.fileNameNgrams {
sz += 4*len(v) + 4
}
return sz
}
const maxUInt32 = 0xffffffff
func minarg(xs []uint32) uint32 {
m := uint32(maxUInt32)
j := len(xs)
for i, x := range xs {
if x <= m {
m = x
j = i
}
}
return uint32(j)
}
func (data *indexData) ngramFrequency(ng ngram, filename bool) uint32 {
if filename {
return uint32(len(data.fileNameNgrams[ng]))
}
return data.ngrams[ng].sz
}
type ngramIterationResults struct {
cands []*candidateMatch
// The ngram matches cover the pattern, so no need to check
// contents.
coversContent bool
// The number of posting bytes
bytesRead uint32
}
func (data *indexData) iterateNgrams(query *query.Substring) (*ngramIterationResults, error) {
iter := &ngramDocIterator{
query: query,
}
if query.FileName {
iter.ends = data.fileNameEndRunes
} else {
iter.ends = data.fileEndRunes
}
str := query.Pattern
// Find the 2 least common ngrams from the string.
ngramOffs := splitNGrams([]byte(query.Pattern))
frequencies := make([]uint32, 0, len(ngramOffs))
for _, o := range ngramOffs {
var freq uint32
if query.CaseSensitive {
freq = data.ngramFrequency(o.ngram, query.FileName)
} else {
for _, v := range generateCaseNgrams(o.ngram) {
freq += data.ngramFrequency(v, query.FileName)
}
}
if freq == 0 {
return &ngramIterationResults{}, nil
}
frequencies = append(frequencies, freq)
}
firstI := minarg(frequencies)
frequencies[firstI] = maxUInt32
lastI := minarg(frequencies)
if firstI > lastI {
lastI, firstI = firstI, lastI
}
firstNG := ngramOffs[firstI].ngram
lastNG := ngramOffs[lastI].ngram
iter.distance = lastI - firstI
iter.leftPad = firstI
iter.rightPad = uint32(utf8.RuneCountInString(str)-ngramSize) - lastI
postings, bytesRead, err := data.readPostings(firstNG, query.CaseSensitive, query.FileName)
if err != nil {
return nil, err
}
iter.first = postings
if firstI != lastI {
postings, sz, err := data.readPostings(lastNG, query.CaseSensitive, query.FileName)
bytesRead += sz
if err != nil {
return nil, err
}
iter.last = postings
} else {
// TODO - we could be a little faster and skip the
// list intersection
iter.last = iter.first
}
result := ngramIterationResults{
bytesRead: bytesRead,
cands: iter.candidates(),
coversContent: lastI-firstI <= ngramSize && iter.leftPad == 0 && iter.rightPad == 0,
}
return &result, nil
}
func (d *indexData) readPostings(ng ngram, caseSensitive, fileName bool) ([]uint32, uint32, error) {
variants := []ngram{ng}
if !caseSensitive {
variants = generateCaseNgrams(ng)
}
// TODO - generate less garbage.
var sz uint32
postings := make([][]uint32, 0, len(variants))
for _, v := range variants {
if fileName {
postings = append(postings, d.fileNameNgrams[v])
continue
}
sec := d.ngrams[v]
blob, err := d.readSectionBlob(sec)
if err != nil {
return nil, 0, err
}
sz += sec.sz
ps := fromDeltas(blob, nil)
if len(ps) > 0 {
postings = append(postings, ps)
}
}
result := mergeUint32(postings)
return result, sz, nil
}
func mergeUint32(in [][]uint32) []uint32 {
sz := 0
for _, i := range in {
sz += len(i)
}
out := make([]uint32, 0, sz)
for len(in) > 0 {
minVal := uint32(maxUInt32)
for _, n := range in {
if len(n) > 0 && n[0] < minVal {
minVal = n[0]
}
}
next := in[:0]
for _, n := range in {
if len(n) == 0 {
continue
}
if n[0] == minVal {
out = append(out, minVal)
n = n[1:]
}
if len(n) > 0 {
next = append(next, n)
}
}
in = next
}
return out
}
func (d *indexData) fileName(i uint32) []byte {
return d.fileNameContent[d.fileNameIndex[i]:d.fileNameIndex[i+1]]
}
func (s *indexData) Close() {
s.file.Close()
}