2017-10-27 11:40:54 +05:30
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2019-12-10 18:59:40 +05:30
|
|
|
package code
|
2017-10-27 11:40:54 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2019-10-15 19:09:51 +05:30
|
|
|
"sync"
|
2017-10-27 11:40:54 +05:30
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
|
|
|
"github.com/blevesearch/bleve"
|
|
|
|
"github.com/blevesearch/bleve/analysis/analyzer/custom"
|
|
|
|
"github.com/blevesearch/bleve/analysis/token/lowercase"
|
|
|
|
"github.com/blevesearch/bleve/analysis/tokenizer/unicode"
|
2018-03-16 19:34:33 +05:30
|
|
|
"github.com/blevesearch/bleve/search/query"
|
2018-02-05 23:59:17 +05:30
|
|
|
"github.com/ethantkoenig/rupture"
|
2017-10-27 11:40:54 +05:30
|
|
|
)
|
|
|
|
|
2018-02-05 23:59:17 +05:30
|
|
|
const (
|
|
|
|
repoIndexerAnalyzer = "repoIndexerAnalyzer"
|
|
|
|
repoIndexerDocType = "repoIndexerDocType"
|
|
|
|
|
2019-08-15 21:08:55 +05:30
|
|
|
repoIndexerLatestVersion = 4
|
2018-02-05 23:59:17 +05:30
|
|
|
)
|
2017-10-27 11:40:54 +05:30
|
|
|
|
2019-10-15 19:09:51 +05:30
|
|
|
type bleveIndexerHolder struct {
|
|
|
|
index bleve.Index
|
|
|
|
mutex sync.RWMutex
|
|
|
|
cond *sync.Cond
|
|
|
|
}
|
|
|
|
|
|
|
|
func newBleveIndexerHolder() *bleveIndexerHolder {
|
|
|
|
b := &bleveIndexerHolder{}
|
|
|
|
b.cond = sync.NewCond(b.mutex.RLocker())
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *bleveIndexerHolder) set(index bleve.Index) {
|
|
|
|
r.mutex.Lock()
|
|
|
|
defer r.mutex.Unlock()
|
|
|
|
r.index = index
|
|
|
|
r.cond.Broadcast()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *bleveIndexerHolder) get() bleve.Index {
|
|
|
|
r.mutex.RLock()
|
|
|
|
defer r.mutex.RUnlock()
|
|
|
|
if r.index == nil {
|
|
|
|
r.cond.Wait()
|
|
|
|
}
|
|
|
|
return r.index
|
|
|
|
}
|
|
|
|
|
2017-10-27 11:40:54 +05:30
|
|
|
// repoIndexer (thread-safe) index for repository contents
|
2019-10-15 19:09:51 +05:30
|
|
|
var indexerHolder = newBleveIndexerHolder()
|
2017-10-27 11:40:54 +05:30
|
|
|
|
|
|
|
// RepoIndexerOp type of operation to perform on repo indexer
|
|
|
|
type RepoIndexerOp int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// RepoIndexerOpUpdate add/update a file's contents
|
|
|
|
RepoIndexerOpUpdate = iota
|
|
|
|
|
|
|
|
// RepoIndexerOpDelete delete a file
|
|
|
|
RepoIndexerOpDelete
|
|
|
|
)
|
|
|
|
|
|
|
|
// RepoIndexerData data stored in the repo indexer
|
|
|
|
type RepoIndexerData struct {
|
|
|
|
RepoID int64
|
|
|
|
Content string
|
|
|
|
}
|
|
|
|
|
2018-02-05 23:59:17 +05:30
|
|
|
// Type returns the document type, for bleve's mapping.Classifier interface.
|
|
|
|
func (d *RepoIndexerData) Type() string {
|
|
|
|
return repoIndexerDocType
|
|
|
|
}
|
|
|
|
|
2017-10-27 11:40:54 +05:30
|
|
|
// RepoIndexerUpdate an update to the repo indexer
|
|
|
|
type RepoIndexerUpdate struct {
|
|
|
|
Filepath string
|
|
|
|
Op RepoIndexerOp
|
|
|
|
Data *RepoIndexerData
|
|
|
|
}
|
|
|
|
|
2018-02-05 23:59:17 +05:30
|
|
|
// AddToFlushingBatch adds the update to the given flushing batch.
|
|
|
|
func (update RepoIndexerUpdate) AddToFlushingBatch(batch rupture.FlushingBatch) error {
|
2017-10-27 11:40:54 +05:30
|
|
|
id := filenameIndexerID(update.Data.RepoID, update.Filepath)
|
|
|
|
switch update.Op {
|
|
|
|
case RepoIndexerOpUpdate:
|
|
|
|
return batch.Index(id, update.Data)
|
|
|
|
case RepoIndexerOpDelete:
|
2018-02-05 23:59:17 +05:30
|
|
|
return batch.Delete(id)
|
2017-10-27 11:40:54 +05:30
|
|
|
default:
|
2019-04-02 13:18:31 +05:30
|
|
|
log.Error("Unrecognized repo indexer op: %d", update.Op)
|
2017-10-27 11:40:54 +05:30
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-10 18:59:40 +05:30
|
|
|
// initRepoIndexer initialize repo indexer
|
|
|
|
func initRepoIndexer(populateIndexer func() error) {
|
2019-10-15 19:09:51 +05:30
|
|
|
indexer, err := openIndexer(setting.Indexer.RepoPath, repoIndexerLatestVersion)
|
2017-10-27 11:40:54 +05:30
|
|
|
if err != nil {
|
2019-04-02 13:18:31 +05:30
|
|
|
log.Fatal("InitRepoIndexer: %v", err)
|
2018-02-05 23:59:17 +05:30
|
|
|
}
|
2019-10-15 19:09:51 +05:30
|
|
|
if indexer != nil {
|
|
|
|
indexerHolder.set(indexer)
|
2018-02-05 23:59:17 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-19 14:07:48 +05:30
|
|
|
if err = createRepoIndexer(setting.Indexer.RepoPath, repoIndexerLatestVersion); err != nil {
|
2019-04-02 13:18:31 +05:30
|
|
|
log.Fatal("CreateRepoIndexer: %v", err)
|
2018-02-05 23:59:17 +05:30
|
|
|
}
|
|
|
|
if err = populateIndexer(); err != nil {
|
2019-04-02 13:18:31 +05:30
|
|
|
log.Fatal("PopulateRepoIndex: %v", err)
|
2017-10-27 11:40:54 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// createRepoIndexer create a repo indexer if one does not already exist
|
2018-12-19 14:07:48 +05:30
|
|
|
func createRepoIndexer(path string, latestVersion int) error {
|
2017-10-27 11:40:54 +05:30
|
|
|
docMapping := bleve.NewDocumentMapping()
|
2018-02-05 23:59:17 +05:30
|
|
|
numericFieldMapping := bleve.NewNumericFieldMapping()
|
|
|
|
numericFieldMapping.IncludeInAll = false
|
|
|
|
docMapping.AddFieldMappingsAt("RepoID", numericFieldMapping)
|
2017-10-27 11:40:54 +05:30
|
|
|
|
|
|
|
textFieldMapping := bleve.NewTextFieldMapping()
|
2018-02-05 23:59:17 +05:30
|
|
|
textFieldMapping.IncludeInAll = false
|
2017-10-27 11:40:54 +05:30
|
|
|
docMapping.AddFieldMappingsAt("Content", textFieldMapping)
|
|
|
|
|
|
|
|
mapping := bleve.NewIndexMapping()
|
2019-10-15 19:09:51 +05:30
|
|
|
if err := addUnicodeNormalizeTokenFilter(mapping); err != nil {
|
2017-10-27 11:40:54 +05:30
|
|
|
return err
|
2019-10-15 19:09:51 +05:30
|
|
|
} else if err := mapping.AddCustomAnalyzer(repoIndexerAnalyzer, map[string]interface{}{
|
2017-10-27 11:40:54 +05:30
|
|
|
"type": custom.Name,
|
|
|
|
"char_filters": []string{},
|
|
|
|
"tokenizer": unicode.Name,
|
2019-08-15 21:08:55 +05:30
|
|
|
"token_filters": []string{unicodeNormalizeName, lowercase.Name},
|
2017-10-27 11:40:54 +05:30
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
mapping.DefaultAnalyzer = repoIndexerAnalyzer
|
2018-02-05 23:59:17 +05:30
|
|
|
mapping.AddDocumentMapping(repoIndexerDocType, docMapping)
|
|
|
|
mapping.AddDocumentMapping("_all", bleve.NewDocumentDisabledMapping())
|
|
|
|
|
2019-10-15 19:09:51 +05:30
|
|
|
indexer, err := bleve.New(path, mapping)
|
2018-12-19 14:07:48 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-15 19:09:51 +05:30
|
|
|
indexerHolder.set(indexer)
|
|
|
|
|
2018-12-19 14:07:48 +05:30
|
|
|
return rupture.WriteIndexMetadata(path, &rupture.IndexMetadata{
|
|
|
|
Version: latestVersion,
|
|
|
|
})
|
2017-10-27 11:40:54 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func filenameIndexerID(repoID int64, filename string) string {
|
|
|
|
return indexerID(repoID) + "_" + filename
|
|
|
|
}
|
|
|
|
|
|
|
|
func filenameOfIndexerID(indexerID string) string {
|
|
|
|
index := strings.IndexByte(indexerID, '_')
|
|
|
|
if index == -1 {
|
2019-04-02 13:18:31 +05:30
|
|
|
log.Error("Unexpected ID in repo indexer: %s", indexerID)
|
2017-10-27 11:40:54 +05:30
|
|
|
}
|
|
|
|
return indexerID[index+1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
// RepoIndexerBatch batch to add updates to
|
2018-02-05 23:59:17 +05:30
|
|
|
func RepoIndexerBatch() rupture.FlushingBatch {
|
2019-10-15 19:09:51 +05:30
|
|
|
return rupture.NewFlushingBatch(indexerHolder.get(), maxBatchSize)
|
2017-10-27 11:40:54 +05:30
|
|
|
}
|
|
|
|
|
2019-12-10 18:59:40 +05:30
|
|
|
// deleteRepoFromIndexer delete all of a repo's files from indexer
|
|
|
|
func deleteRepoFromIndexer(repoID int64) error {
|
2017-10-27 11:40:54 +05:30
|
|
|
query := numericEqualityQuery(repoID, "RepoID")
|
|
|
|
searchRequest := bleve.NewSearchRequestOptions(query, 2147483647, 0, false)
|
2019-10-15 19:09:51 +05:30
|
|
|
result, err := indexerHolder.get().Search(searchRequest)
|
2017-10-27 11:40:54 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
batch := RepoIndexerBatch()
|
|
|
|
for _, hit := range result.Hits {
|
2018-02-05 23:59:17 +05:30
|
|
|
if err = batch.Delete(hit.ID); err != nil {
|
2017-10-27 11:40:54 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return batch.Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
// RepoSearchResult result of performing a search in a repo
|
|
|
|
type RepoSearchResult struct {
|
2018-03-16 19:34:33 +05:30
|
|
|
RepoID int64
|
2017-10-27 11:40:54 +05:30
|
|
|
StartIndex int
|
|
|
|
EndIndex int
|
|
|
|
Filename string
|
|
|
|
Content string
|
|
|
|
}
|
|
|
|
|
|
|
|
// SearchRepoByKeyword searches for files in the specified repo.
|
|
|
|
// Returns the matching file-paths
|
2018-03-16 19:34:33 +05:30
|
|
|
func SearchRepoByKeyword(repoIDs []int64, keyword string, page, pageSize int) (int64, []*RepoSearchResult, error) {
|
2017-10-27 11:40:54 +05:30
|
|
|
phraseQuery := bleve.NewMatchPhraseQuery(keyword)
|
|
|
|
phraseQuery.FieldVal = "Content"
|
|
|
|
phraseQuery.Analyzer = repoIndexerAnalyzer
|
2018-03-16 19:34:33 +05:30
|
|
|
|
|
|
|
var indexerQuery query.Query
|
|
|
|
if len(repoIDs) > 0 {
|
|
|
|
var repoQueries = make([]query.Query, 0, len(repoIDs))
|
|
|
|
for _, repoID := range repoIDs {
|
|
|
|
repoQueries = append(repoQueries, numericEqualityQuery(repoID, "RepoID"))
|
|
|
|
}
|
|
|
|
|
|
|
|
indexerQuery = bleve.NewConjunctionQuery(
|
|
|
|
bleve.NewDisjunctionQuery(repoQueries...),
|
|
|
|
phraseQuery,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
indexerQuery = phraseQuery
|
|
|
|
}
|
|
|
|
|
2017-10-27 11:40:54 +05:30
|
|
|
from := (page - 1) * pageSize
|
|
|
|
searchRequest := bleve.NewSearchRequestOptions(indexerQuery, pageSize, from, false)
|
2018-03-16 19:34:33 +05:30
|
|
|
searchRequest.Fields = []string{"Content", "RepoID"}
|
2017-10-27 11:40:54 +05:30
|
|
|
searchRequest.IncludeLocations = true
|
|
|
|
|
2019-10-15 19:09:51 +05:30
|
|
|
result, err := indexerHolder.get().Search(searchRequest)
|
2017-10-27 11:40:54 +05:30
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
searchResults := make([]*RepoSearchResult, len(result.Hits))
|
|
|
|
for i, hit := range result.Hits {
|
|
|
|
var startIndex, endIndex int = -1, -1
|
|
|
|
for _, locations := range hit.Locations["Content"] {
|
|
|
|
location := locations[0]
|
|
|
|
locationStart := int(location.Start)
|
|
|
|
locationEnd := int(location.End)
|
|
|
|
if startIndex < 0 || locationStart < startIndex {
|
|
|
|
startIndex = locationStart
|
|
|
|
}
|
|
|
|
if endIndex < 0 || locationEnd > endIndex {
|
|
|
|
endIndex = locationEnd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
searchResults[i] = &RepoSearchResult{
|
2018-03-16 19:34:33 +05:30
|
|
|
RepoID: int64(hit.Fields["RepoID"].(float64)),
|
2017-10-27 11:40:54 +05:30
|
|
|
StartIndex: startIndex,
|
|
|
|
EndIndex: endIndex,
|
|
|
|
Filename: filenameOfIndexerID(hit.ID),
|
|
|
|
Content: hit.Fields["Content"].(string),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return int64(result.Total), searchResults, nil
|
|
|
|
}
|