github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/setting/indexer.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package setting
     7  
     8  import (
     9  	"path/filepath"
    10  	"strings"
    11  	"time"
    12  
    13  	"github.com/gitbundle/modules/log"
    14  
    15  	"github.com/gobwas/glob"
    16  )
    17  
    18  // Indexer settings
    19  var Indexer = struct {
    20  	IssueType        string
    21  	IssuePath        string
    22  	IssueConnStr     string
    23  	IssueIndexerName string
    24  	StartupTimeout   time.Duration
    25  
    26  	RepoIndexerEnabled bool
    27  	RepoType           string
    28  	RepoPath           string
    29  	RepoConnStr        string
    30  	RepoIndexerName    string
    31  	MaxIndexerFileSize int64
    32  	IncludePatterns    []glob.Glob
    33  	ExcludePatterns    []glob.Glob
    34  	ExcludeVendored    bool
    35  }{
    36  	IssueType:        "bleve",
    37  	IssuePath:        "indexers/issues.bleve",
    38  	IssueConnStr:     "",
    39  	IssueIndexerName: "gitea_issues",
    40  
    41  	RepoIndexerEnabled: false,
    42  	RepoType:           "bleve",
    43  	RepoPath:           "indexers/repos.bleve",
    44  	RepoConnStr:        "",
    45  	RepoIndexerName:    "gitea_codes",
    46  	MaxIndexerFileSize: 1024 * 1024,
    47  	ExcludeVendored:    true,
    48  }
    49  
    50  func newIndexerService() {
    51  	sec := Cfg.Section("indexer")
    52  	Indexer.IssueType = sec.Key("ISSUE_INDEXER_TYPE").MustString("bleve")
    53  	Indexer.IssuePath = filepath.ToSlash(sec.Key("ISSUE_INDEXER_PATH").MustString(filepath.ToSlash(filepath.Join(AppDataPath, "indexers/issues.bleve"))))
    54  	if !filepath.IsAbs(Indexer.IssuePath) {
    55  		Indexer.IssuePath = filepath.ToSlash(filepath.Join(AppWorkPath, Indexer.IssuePath))
    56  	}
    57  	Indexer.IssueConnStr = sec.Key("ISSUE_INDEXER_CONN_STR").MustString(Indexer.IssueConnStr)
    58  	Indexer.IssueIndexerName = sec.Key("ISSUE_INDEXER_NAME").MustString(Indexer.IssueIndexerName)
    59  
    60  	// The following settings are deprecated and can be overridden by settings in [queue] or [queue.issue_indexer]
    61  	// FIXME: DEPRECATED to be removed in v1.18.0
    62  	deprecatedSetting("indexer", "ISSUE_INDEXER_QUEUE_TYPE", "queue.issue_indexer", "TYPE")
    63  	deprecatedSetting("indexer", "ISSUE_INDEXER_QUEUE_DIR", "queue.issue_indexer", "DATADIR")
    64  	deprecatedSetting("indexer", "ISSUE_INDEXER_QUEUE_CONN_STR", "queue.issue_indexer", "CONN_STR")
    65  	deprecatedSetting("indexer", "ISSUE_INDEXER_QUEUE_BATCH_NUMBER", "queue.issue_indexer", "BATCH_LENGTH")
    66  	deprecatedSetting("indexer", "UPDATE_BUFFER_LEN", "queue.issue_indexer", "LENGTH")
    67  
    68  	Indexer.RepoIndexerEnabled = sec.Key("REPO_INDEXER_ENABLED").MustBool(false)
    69  	Indexer.RepoType = sec.Key("REPO_INDEXER_TYPE").MustString("bleve")
    70  	Indexer.RepoPath = filepath.ToSlash(sec.Key("REPO_INDEXER_PATH").MustString(filepath.ToSlash(filepath.Join(AppDataPath, "indexers/repos.bleve"))))
    71  	if !filepath.IsAbs(Indexer.RepoPath) {
    72  		Indexer.RepoPath = filepath.ToSlash(filepath.Join(AppWorkPath, Indexer.RepoPath))
    73  	}
    74  	Indexer.RepoConnStr = sec.Key("REPO_INDEXER_CONN_STR").MustString("")
    75  	Indexer.RepoIndexerName = sec.Key("REPO_INDEXER_NAME").MustString("gitea_codes")
    76  
    77  	Indexer.IncludePatterns = IndexerGlobFromString(sec.Key("REPO_INDEXER_INCLUDE").MustString(""))
    78  	Indexer.ExcludePatterns = IndexerGlobFromString(sec.Key("REPO_INDEXER_EXCLUDE").MustString(""))
    79  	Indexer.ExcludeVendored = sec.Key("REPO_INDEXER_EXCLUDE_VENDORED").MustBool(true)
    80  	Indexer.MaxIndexerFileSize = sec.Key("MAX_FILE_SIZE").MustInt64(1024 * 1024)
    81  	Indexer.StartupTimeout = sec.Key("STARTUP_TIMEOUT").MustDuration(30 * time.Second)
    82  }
    83  
    84  // IndexerGlobFromString parses a comma separated list of patterns and returns a glob.Glob slice suited for repo indexing
    85  func IndexerGlobFromString(globstr string) []glob.Glob {
    86  	extarr := make([]glob.Glob, 0, 10)
    87  	for _, expr := range strings.Split(strings.ToLower(globstr), ",") {
    88  		expr = strings.TrimSpace(expr)
    89  		if expr != "" {
    90  			if g, err := glob.Compile(expr, '.', '/'); err != nil {
    91  				log.Info("Invalid glob expression '%s' (skipped): %v", expr, err)
    92  			} else {
    93  				extarr = append(extarr, g)
    94  			}
    95  		}
    96  	}
    97  	return extarr
    98  }