-
Notifications
You must be signed in to change notification settings - Fork 0
/
csbackend.go
350 lines (315 loc) · 8.36 KB
/
csbackend.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
349
350
package cs
import (
"context"
"encoding/json"
"errors"
"io/fs"
"log"
"maps"
"os"
"path/filepath"
"regexp/syntax"
"runtime"
"sort"
"strings"
"time"
"golang.org/x/sync/errgroup"
"sgrankin.dev/cs/codesearch/index"
"sgrankin.dev/cs/codesearch/regexp"
)
type Version = string
/*
A search index is a directory that contains everything needed to build, update, and search several git repositories.
Layout:
/meta.json
Contains the repository names, versions, and the relative paths to the search index for searching them.
/indexes/{sha1}.csindex
The index for the given sha1 hash. The hash is lowercased git hash of the commit.
/git/
The (bare) git repository with refs corresponding to repo/tree names names.
*/
type searchIndex struct {
cfg IndexConfig
meta indexMeta
trees map[string]struct {
*indexSearcher
Version string
}
}
func NewSearchIndex(cfg IndexConfig) *searchIndex {
if cfg.Name == "" {
cfg.Name = filepath.Base(cfg.Path)
}
if err := os.MkdirAll(cfg.Path, 0o777); err != nil {
log.Panicf("creating %q: %v", cfg.Path, err)
}
si := &searchIndex{
cfg: cfg,
trees: map[string]struct {
*indexSearcher
Version string
}{},
}
meta, err := readIndexMeta(cfg.Path)
if err != nil {
log.Panic(err)
}
si.meta = *meta
for _, tree := range meta.Trees {
si.trees[tree.Name] = struct {
*indexSearcher
Version string
}{
indexSearcher: newIndexSearcher(index.Open(filepath.Join(cfg.Path, tree.IndexPath)), tree.Name, tree.Version),
Version: tree.Version,
}
}
return si
}
func (si *searchIndex) Reload() {
meta, err := readIndexMeta(si.cfg.Path)
if err != nil || meta.ModTime == si.meta.ModTime {
return
}
log.Printf("Reloading index %s at %s (%v -> %v)", si.cfg.Name, si.cfg.Path, meta.ModTime, si.meta.ModTime)
*si = *NewSearchIndex(si.cfg)
}
func (si *searchIndex) Name() string { return si.cfg.Name }
// Info implements SearchIndex.
func (si *searchIndex) Info() IndexInfo {
info := IndexInfo{
IndexTime: si.meta.ModTime,
}
for name, t := range si.trees {
info.Trees = append(info.Trees, Tree{
Name: name,
Version: t.Version,
})
}
return info
}
// Paths implements SearchIndex.
func (si *searchIndex) Paths(tree string, version string, pathPrefix string) []File {
return si.trees[tree].Paths(pathPrefix)
}
// Data implements SearchIndex.
func (si *searchIndex) Data(tree string, version string, path string) string {
return si.trees[tree].Data(path)
}
// Search implements SearchIndex.
func (si *searchIndex) Search(ctx context.Context, q Query) (*CodeSearchResult, error) {
// Use the query repo filters to scope down to the searchers that should handle the query.
// Then fan out the query to all searchers,
// - limiting parallelism
// - canceling the search once we have sufficient results.
repoFilter, err := newRegexpFilter(q.Repo, q.NotRepo)
if err != nil {
return nil, err
}
var fixedRepoFilter *setFilter
if q.RepoFilter != nil {
fixedRepoFilter = newSetFilter(q.RepoFilter)
}
var searchers []*indexSearcher
for name, t := range si.trees {
if fixedRepoFilter.Accept([]byte(name)) && repoFilter.Accept([]byte(name)) {
searchers = append(searchers, t.indexSearcher)
}
}
pat := q.Line
var flags syntax.Flags
if q.FoldCase {
flags |= syntax.FoldCase
}
re, err := regexp.Compile(pat, flags)
if err != nil {
return nil, err
}
fileFilter, err := newRegexpFilter(strings.Join(q.File, "|"), strings.Join(q.NotFile, "|"))
if err != nil {
return nil, err
}
fileSearchChan := make(chan []FileResult)
ctxFileSearch, cancelFileSearch := context.WithCancel(ctx)
go func(ctx context.Context) {
defer close(fileSearchChan)
wg, ctx := errgroup.WithContext(ctx)
wg.SetLimit(runtime.GOMAXPROCS(0))
for _, searcher := range searchers {
wg.Go(func() error {
r := searcher.SearchFileNames(ctx, re.Clone(), fileFilter.Clone(), q.MaxMatches)
if len(r) > 0 {
select {
case fileSearchChan <- r:
case <-ctx.Done():
}
}
return nil
})
}
wg.Wait()
}(ctxFileSearch)
var exitReason ExitReason = ExitReasonNone
var searchResults []SearchResult
if !q.FilenameOnly {
searchChan := make(chan SearchResult)
ctxSearch, cancelSearch := context.WithCancel(ctx)
go func(ctx context.Context) {
defer close(searchChan)
wg, ctx := errgroup.WithContext(ctx)
wg.SetLimit(runtime.GOMAXPROCS(0))
for _, searcher := range searchers {
searcher.SearchFiles(ctx, re.Clone(), fileFilter.Clone(), q.ContextLines, q.MaxMatches, func(f func() *SearchResult) {
wg.Go(func() error {
result := f()
if result != nil {
select {
case searchChan <- *result:
case <-ctx.Done():
}
}
return nil
})
})
}
wg.Wait()
}(ctxSearch)
nMatches := 0
for r := range searchChan {
searchResults = append(searchResults, r)
nMatches += len(r.Lines)
if nMatches > q.MaxMatches {
exitReason = ExitReasonMatchLimit
break
}
}
cancelSearch()
sort.Slice(searchResults, func(i, j int) bool { return searchResults[i].File.Less(searchResults[j].File) })
}
fileResults := []FileResult{}
for r := range fileSearchChan {
fileResults = append(fileResults, r...)
if len(fileResults) > q.MaxMatches {
if q.FilenameOnly {
exitReason = ExitReasonMatchLimit
}
break
}
}
cancelFileSearch()
sort.Slice(fileResults, func(i, j int) bool { return fileResults[i].File.Less(fileResults[j].File) })
return &CodeSearchResult{
Stats: SearchStats{ExitReason: exitReason},
Results: searchResults,
FileResults: fileResults,
}, nil
}
type treeMeta struct {
Name string
Version string
IndexPath string
}
type indexMeta struct {
ModTime time.Time
Trees []treeMeta
}
func readIndexMeta(indexPath string) (*indexMeta, error) {
path := filepath.Join(indexPath, "meta.json")
f, err := os.Open(path)
if errors.Is(err, fs.ErrNotExist) {
// If the index is new, the meta doesn't exist, but this is a well formed empty index.
return &indexMeta{}, nil
} else if err != nil {
return nil, err
}
defer f.Close()
stat, err := f.Stat()
if err != nil {
return nil, err
}
meta := indexMeta{ModTime: stat.ModTime()}
return &meta, json.NewDecoder(f).Decode(&meta.Trees)
}
func writeIndexMeta(indexPath string, trees []treeMeta) error {
path := filepath.Join(indexPath, "meta.json")
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
return json.NewEncoder(f).Encode(trees)
}
var _ SearchIndex = (*searchIndex)(nil)
func BuildSearchIndex(cfg IndexConfig, githubToken string) error {
if cfg.Name == "" {
cfg.Name = filepath.Base(cfg.Path)
}
if err := os.MkdirAll(cfg.Path, 0o777); err != nil {
log.Panicf("creating %q: %v", cfg.Path, err)
}
gitPath := filepath.Join(cfg.Path, "git")
repoSyncer := newRepoSyncer(gitPath, githubToken, cfg.Repos, cfg.RepoSources)
repos, err := repoSyncer.Refresh()
if err != nil {
log.Printf("Repo sync failed: %v", err)
return nil
}
repoMap := map[string]string{}
for _, r := range repos {
if r.Version() != "" {
repoMap[r.Name()] = r.Version()
}
}
meta, err := readIndexMeta(cfg.Path)
if err != nil {
return err
}
unusedIndexes := map[string]bool{}
metaMap := map[string]string{}
for _, t := range meta.Trees {
metaMap[t.Name] = t.Version
unusedIndexes[t.IndexPath] = true
}
if maps.Equal(repoMap, metaMap) {
return nil // Nothing to do.
}
ctx := context.Background()
wg, ctx := errgroup.WithContext(ctx)
wg.SetLimit(1)
wg.SetLimit(runtime.GOMAXPROCS(0))
metaTrees := []treeMeta{}
for _, repo := range repos {
if repo.Version() == "" {
continue
}
indexPath := filepath.Join("indexes", repo.Version())
metaTrees = append(metaTrees, treeMeta{
Name: repo.Name(),
Version: repo.Version(),
IndexPath: indexPath,
})
if metaMap[repo.Name()] == repo.Version() {
delete(unusedIndexes, indexPath)
continue // Already up to date.
}
indexPath = filepath.Join(cfg.Path, indexPath)
log.Printf("Building index for %s %s at %s", repo.Name(), repo.Version(), indexPath)
os.MkdirAll(filepath.Dir(indexPath), 0o777)
if err := BuildIndex(indexPath, []Repo{repo}); err != nil {
return err
}
}
if err := wg.Wait(); err != nil {
return err
}
if err := writeIndexMeta(cfg.Path, metaTrees); err != nil {
return err
}
// Now clean up the old indexes.
for path := range unusedIndexes {
if err := os.Remove(filepath.Join(cfg.Path, path)); err != nil {
return err
}
}
return nil
}