2019-12-24 10:26:34 +03:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 21:20:29 +03:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-12-24 10:26:34 +03:00
|
|
|
|
|
|
|
package code
|
|
|
|
|
|
|
|
import (
|
2022-01-20 02:26:57 +03:00
|
|
|
"context"
|
2019-12-24 10:26:34 +03:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
2020-08-30 19:08:01 +03:00
|
|
|
|
2021-12-10 04:27:50 +03:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2022-01-27 11:30:51 +03:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2019-12-24 10:26:34 +03:00
|
|
|
)
|
|
|
|
|
2022-01-20 20:46:10 +03:00
|
|
|
var indexer = newWrappedIndexer()
|
2019-12-24 10:26:34 +03:00
|
|
|
|
|
|
|
// ErrWrappedIndexerClosed is the error returned if the indexer was closed before it was ready
|
|
|
|
var ErrWrappedIndexerClosed = fmt.Errorf("Indexer closed before ready")
|
|
|
|
|
|
|
|
type wrappedIndexer struct {
|
|
|
|
internal Indexer
|
|
|
|
lock sync.RWMutex
|
|
|
|
cond *sync.Cond
|
|
|
|
closed bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func newWrappedIndexer() *wrappedIndexer {
|
|
|
|
w := &wrappedIndexer{}
|
|
|
|
w.cond = sync.NewCond(w.lock.RLocker())
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrappedIndexer) set(indexer Indexer) {
|
|
|
|
w.lock.Lock()
|
|
|
|
defer w.lock.Unlock()
|
|
|
|
if w.closed {
|
|
|
|
// Too late!
|
|
|
|
indexer.Close()
|
|
|
|
}
|
|
|
|
w.internal = indexer
|
|
|
|
w.cond.Broadcast()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrappedIndexer) get() (Indexer, error) {
|
|
|
|
w.lock.RLock()
|
|
|
|
defer w.lock.RUnlock()
|
|
|
|
if w.internal == nil {
|
|
|
|
if w.closed {
|
|
|
|
return nil, ErrWrappedIndexerClosed
|
|
|
|
}
|
|
|
|
w.cond.Wait()
|
|
|
|
if w.closed {
|
|
|
|
return nil, ErrWrappedIndexerClosed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return w.internal, nil
|
|
|
|
}
|
|
|
|
|
2022-01-27 11:30:51 +03:00
|
|
|
// Ping checks if elastic is available
|
|
|
|
func (w *wrappedIndexer) Ping() bool {
|
|
|
|
indexer, err := w.get()
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("Failed to get indexer: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return indexer.Ping()
|
|
|
|
}
|
|
|
|
|
2022-01-20 02:26:57 +03:00
|
|
|
func (w *wrappedIndexer) Index(ctx context.Context, repo *repo_model.Repository, sha string, changes *repoChanges) error {
|
2019-12-24 10:26:34 +03:00
|
|
|
indexer, err := w.get()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-20 02:26:57 +03:00
|
|
|
return indexer.Index(ctx, repo, sha, changes)
|
2019-12-24 10:26:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrappedIndexer) Delete(repoID int64) error {
|
|
|
|
indexer, err := w.get()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return indexer.Delete(repoID)
|
|
|
|
}
|
|
|
|
|
2022-01-27 11:30:51 +03:00
|
|
|
func (w *wrappedIndexer) Search(ctx context.Context, repoIDs []int64, language, keyword string, page, pageSize int, isMatch bool) (int64, []*SearchResult, []*SearchResultLanguages, error) {
|
2019-12-24 10:26:34 +03:00
|
|
|
indexer, err := w.get()
|
|
|
|
if err != nil {
|
2020-02-20 22:53:55 +03:00
|
|
|
return 0, nil, nil, err
|
2019-12-24 10:26:34 +03:00
|
|
|
}
|
2022-01-27 11:30:51 +03:00
|
|
|
return indexer.Search(ctx, repoIDs, language, keyword, page, pageSize, isMatch)
|
2019-12-24 10:26:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrappedIndexer) Close() {
|
|
|
|
w.lock.Lock()
|
|
|
|
defer w.lock.Unlock()
|
|
|
|
if w.closed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.closed = true
|
|
|
|
w.cond.Broadcast()
|
|
|
|
if w.internal != nil {
|
|
|
|
w.internal.Close()
|
|
|
|
}
|
|
|
|
}
|