2017-01-24 21:43:02 -05:00
|
|
|
// Copyright 2016 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 08:29:40 -05:00
|
|
|
package code
|
2017-01-24 21:43:02 -05:00
|
|
|
|
|
|
|
import (
|
2019-12-23 07:31:16 -05:00
|
|
|
"time"
|
2017-09-16 16:16:21 -04:00
|
|
|
|
2019-12-23 07:31:16 -05:00
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2018-02-05 13:29:17 -05:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-12-23 07:31:16 -05:00
|
|
|
)
|
2018-02-05 13:29:17 -05:00
|
|
|
|
2019-12-23 07:31:16 -05:00
|
|
|
var (
|
|
|
|
indexer Indexer
|
2017-01-24 21:43:02 -05:00
|
|
|
)
|
|
|
|
|
2019-12-23 07:31:16 -05:00
|
|
|
// SearchResult result of performing a search in a repo
|
|
|
|
type SearchResult struct {
|
|
|
|
RepoID int64
|
|
|
|
StartIndex int
|
|
|
|
EndIndex int
|
|
|
|
Filename string
|
|
|
|
Content string
|
2017-09-16 16:16:21 -04:00
|
|
|
}
|
|
|
|
|
2019-12-23 07:31:16 -05:00
|
|
|
// Indexer defines an interface to indexer issues contents
|
|
|
|
type Indexer interface {
|
|
|
|
Index(repoID int64) error
|
|
|
|
Delete(repoID int64) error
|
|
|
|
Search(repoIDs []int64, keyword string, page, pageSize int) (int64, []*SearchResult, error)
|
|
|
|
Close()
|
2017-09-16 16:16:21 -04:00
|
|
|
}
|
|
|
|
|
2019-12-23 07:31:16 -05:00
|
|
|
// Init initialize the repo indexer
|
|
|
|
func Init() {
|
|
|
|
if !setting.Indexer.RepoIndexerEnabled {
|
|
|
|
return
|
|
|
|
}
|
2017-09-24 20:08:48 -04:00
|
|
|
|
2019-12-23 07:31:16 -05:00
|
|
|
waitChannel := make(chan time.Duration)
|
|
|
|
go func() {
|
|
|
|
start := time.Now()
|
|
|
|
log.Info("Initializing Repository Indexer")
|
|
|
|
var created bool
|
|
|
|
var err error
|
|
|
|
indexer, created, err = NewBleveIndexer(setting.Indexer.RepoPath)
|
|
|
|
if err != nil {
|
|
|
|
indexer.Close()
|
|
|
|
log.Fatal("indexer.Init: %v", err)
|
|
|
|
}
|
2017-09-24 20:08:48 -04:00
|
|
|
|
2019-12-23 07:31:16 -05:00
|
|
|
go processRepoIndexerOperationQueue(indexer)
|
2017-09-24 20:08:48 -04:00
|
|
|
|
2019-12-23 07:31:16 -05:00
|
|
|
if created {
|
|
|
|
go populateRepoIndexer()
|
|
|
|
}
|
2017-09-24 20:08:48 -04:00
|
|
|
|
2019-12-23 07:31:16 -05:00
|
|
|
waitChannel <- time.Since(start)
|
|
|
|
}()
|
2017-09-24 20:08:48 -04:00
|
|
|
|
2019-12-23 07:31:16 -05:00
|
|
|
if setting.Indexer.StartupTimeout > 0 {
|
|
|
|
go func() {
|
|
|
|
timeout := setting.Indexer.StartupTimeout
|
|
|
|
if graceful.GetManager().IsChild() && setting.GracefulHammerTime > 0 {
|
|
|
|
timeout += setting.GracefulHammerTime
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case duration := <-waitChannel:
|
|
|
|
log.Info("Repository Indexer Initialization took %v", duration)
|
|
|
|
case <-time.After(timeout):
|
|
|
|
log.Fatal("Repository Indexer Initialization Timed-Out after: %v", timeout)
|
|
|
|
}
|
|
|
|
}()
|
2017-09-24 20:08:48 -04:00
|
|
|
}
|
|
|
|
}
|