all repos — searchix @ 1aa991ae1f1a426424549c92060b204114c8b3c2

Search engine for NixOS, nix-darwin, home-manager and NUR users

refactor: deduce index path automatically from config.DataPath

Alan Pearce
commit

1aa991ae1f1a426424549c92060b204114c8b3c2

parent

48423548f4718886d32a87ddd65d5ee2620fd8c8

M import/main.goimport/main.go
@@ -22,7 +22,6 @@ ConfigFile string `conf:"short:c"`
LogLevel slog.Level `conf:"default:INFO"` Timeout time.Duration `conf:"default:30m,help:maximum time to wait for all fetchers and importers combined"` Replace bool `conf:"default:false,help:whether to remove existing database, if exists"` - IndexPath string `conf:"default:data/index.bleve"` } func main() {
@@ -51,7 +50,7 @@
return } - indexer, err := search.NewIndexer(runtimeConfig.IndexPath, runtimeConfig.Replace) + indexer, err := search.NewIndexer(cfg.DataPath, runtimeConfig.Replace) if err != nil { log.Fatalf("Failed to create indexer: %v", err) }
M internal/importer/channel.gointernal/importer/channel.go
@@ -20,7 +20,6 @@ DataPath string
Source *Source SourceFile string Logger *slog.Logger - indexPath string } func (i *ChannelImporter) FetchIfNeeded(parent context.Context) (bool, error) {
@@ -86,9 +85,8 @@ filename := path.Join(i.DataPath, i.SourceFile, i.Source.OutputPath)
i.Logger.Debug("preparing import run", "revision", i.Source.Repo.Revision, "filename", filename) return processOptions(parent, indexer, &importConfig{ - IndexPath: i.indexPath, - Source: i.Source, - Filename: filename, - Logger: i.Logger, + Source: i.Source, + Filename: filename, + Logger: i.Logger, }) }
M internal/importer/importer.gointernal/importer/importer.go
@@ -32,34 +32,29 @@ source *Source,
dataPath string, logger *slog.Logger, ) *NixpkgsChannelImporter { - indexPath := dataPath fullpath := path.Join(dataPath, source.Channel) return &NixpkgsChannelImporter{ - DataPath: fullpath, - Source: source, - Logger: logger, - indexPath: indexPath, + DataPath: fullpath, + Source: source, + Logger: logger, } } func NewChannelImporter(source *Source, dataPath string, logger *slog.Logger) *ChannelImporter { - indexPath := dataPath fullpath := path.Join(dataPath, source.Channel) return &ChannelImporter{ - DataPath: fullpath, - Source: source, - Logger: logger, - indexPath: indexPath, + DataPath: fullpath, + Source: source, + Logger: logger, } } type importConfig struct { - IndexPath string - Filename string - Source *Source - Logger *slog.Logger + Filename string + Source *Source + Logger *slog.Logger } func processOptions(
M internal/importer/nixpkgs-channel.gointernal/importer/nixpkgs-channel.go
@@ -14,10 +14,9 @@ "github.com/pkg/errors"
) type NixpkgsChannelImporter struct { - DataPath string - Source *Source - Logger *slog.Logger - indexPath string + DataPath string + Source *Source + Logger *slog.Logger } func makeChannelURL(channel string, subPath string) (string, error) {
@@ -77,9 +76,8 @@ i.Source.Repo.Revision = string(bytes.TrimSpace(bits))
i.Logger.Debug("preparing import run", "revision", i.Source.Repo.Revision, "filename", filename) return processOptions(parent, indexer, &importConfig{ - IndexPath: i.indexPath, - Source: i.Source, - Filename: filename, - Logger: i.Logger, + Source: i.Source, + Filename: filename, + Logger: i.Logger, }) }
M internal/search/indexer.gointernal/search/indexer.go
@@ -28,8 +28,6 @@ type WriteIndex struct {
index bleve.Index } -const ExpectedIndexExtension = ".bleve" - func createIndexMapping() (mapping.IndexMapping, error) { indexMapping := bleve.NewIndexMapping() indexMapping.StoreDynamic = false
@@ -125,9 +123,13 @@
return idx, nil } -func NewIndexer(indexPath string, force bool) (*WriteIndex, error) { +const indexBaseName = "index.bleve" + +func NewIndexer(dataRoot string, force bool) (*WriteIndex, error) { var err error bleve.SetLog(log.Default()) + + indexPath := path.Join(dataRoot, indexBaseName) exists, err := file.Exists(indexPath) if err != nil {
@@ -141,13 +143,6 @@
var idx bleve.Index if !exists || force { if force { - if path.Ext(indexPath) != ExpectedIndexExtension { - return nil, errors.Errorf( - "cowardly refusing to delete path %s (it doesn't end in '%s')", - indexPath, - ExpectedIndexExtension, - ) - } err := os.RemoveAll(indexPath) if err != nil { return nil, errors.WithMessagef(err, "could not remove index file %s", indexPath)
M internal/search/search.gointernal/search/search.go
@@ -4,6 +4,7 @@ import (
"bytes" "context" "encoding/gob" + "path" "searchix/internal/options" "github.com/blevesearch/bleve/v2"
@@ -27,7 +28,9 @@ type ReadIndex struct {
index bleve.Index } -func Open(indexPath string) (*ReadIndex, error) { +func Open(dataRoot string) (*ReadIndex, error) { + indexPath := path.Join(dataRoot, indexBaseName) + idx, err := bleve.Open(indexPath) if err != nil { return nil, errors.WithMessagef(err, "unable to open index at path %s", indexPath)
M internal/server/server.gointernal/server/server.go
@@ -40,7 +40,6 @@ Port string `conf:"default:3000,short:p"`
BaseURL cfg.URL `conf:"default:http://localhost:3000,short:b"` ConfigFile string `conf:"short:c"` LogLevel slog.Level `conf:"default:INFO"` - IndexPath string `conf:"default:data/index.bleve"` SentryDSN string }
@@ -87,7 +86,7 @@ return nil, errors.WithMessage(err, "error parsing configuration file")
} slog.Debug("loading index") - index, err := search.Open(runtimeConfig.IndexPath) + index, err := search.Open(config.DataPath) slog.Debug("loaded index") if err != nil { log.Fatalf("could not open search index, error: %#v", err)