2017-01-25 05:43:02 +03: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 16:29:40 +03:00
|
|
|
package code
|
2017-01-25 05:43:02 +03:00
|
|
|
|
|
|
|
import (
|
2019-12-23 15:31:16 +03:00
|
|
|
"time"
|
2017-09-16 23:16:21 +03:00
|
|
|
|
2019-12-23 15:31:16 +03:00
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2018-02-05 21:29:17 +03:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-12-23 15:31:16 +03:00
|
|
|
)
|
2018-02-05 21:29:17 +03:00
|
|
|
|
2019-12-23 15:31:16 +03:00
|
|
|
var (
|
|
|
|
indexer Indexer
|
2017-01-25 05:43:02 +03:00
|
|
|
)
|
|
|
|
|
2019-12-23 15:31:16 +03: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 23:16:21 +03:00
|
|
|
}
|
|
|
|
|
2019-12-23 15:31:16 +03: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 23:16:21 +03:00
|
|
|
}
|
|
|
|
|
2019-12-23 15:31:16 +03:00
|
|
|
// Init initialize the repo indexer
|
|
|
|
func Init() {
|
|
|
|
if !setting.Indexer.RepoIndexerEnabled {
|
|
|
|
return
|
|
|
|
}
|
2017-09-25 03:08:48 +03:00
|
|
|
|
2019-12-23 15:31:16 +03: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-25 03:08:48 +03:00
|
|
|
|
2019-12-23 15:31:16 +03:00
|
|
|
go processRepoIndexerOperationQueue(indexer)
|
2017-09-25 03:08:48 +03:00
|
|
|
|
2019-12-23 15:31:16 +03:00
|
|
|
if created {
|
|
|
|
go populateRepoIndexer()
|
|
|
|
}
|
2017-09-25 03:08:48 +03:00
|
|
|
|
2019-12-23 15:31:16 +03:00
|
|
|
waitChannel <- time.Since(start)
|
|
|
|
}()
|
2017-09-25 03:08:48 +03:00
|
|
|
|
2019-12-23 15:31:16 +03: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-25 03:08:48 +03:00
|
|
|
}
|
|
|
|
}
|