about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAlan Pearce2024-05-11 14:34:15 +0200
committerAlan Pearce2024-05-11 14:37:30 +0200
commit1aa991ae1f1a426424549c92060b204114c8b3c2 (patch)
tree59dc9e67d6b0c5604d9a70f52260ae7d85411e6f
parent48423548f4718886d32a87ddd65d5ee2620fd8c8 (diff)
downloadsearchix-1aa991ae1f1a426424549c92060b204114c8b3c2.tar.lz
searchix-1aa991ae1f1a426424549c92060b204114c8b3c2.tar.zst
searchix-1aa991ae1f1a426424549c92060b204114c8b3c2.zip
refactor: deduce index path automatically from config.DataPath
-rw-r--r--import/main.go3
-rw-r--r--internal/importer/channel.go8
-rw-r--r--internal/importer/importer.go23
-rw-r--r--internal/importer/nixpkgs-channel.go14
-rw-r--r--internal/search/indexer.go15
-rw-r--r--internal/search/search.go5
-rw-r--r--internal/server/server.go3
7 files changed, 29 insertions, 42 deletions
diff --git a/import/main.go b/import/main.go
index db899e2..90f2b74 100644
--- a/import/main.go
+++ b/import/main.go
@@ -22,7 +22,6 @@ type Config struct {
 	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 @@ func main() {
 		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)
 	}
diff --git a/internal/importer/channel.go b/internal/importer/channel.go
index 3c199cc..97f0a27 100644
--- a/internal/importer/channel.go
+++ b/internal/importer/channel.go
@@ -20,7 +20,6 @@ type ChannelImporter struct {
 	Source     *Source
 	SourceFile string
 	Logger     *slog.Logger
-	indexPath  string
 }
 
 func (i *ChannelImporter) FetchIfNeeded(parent context.Context) (bool, error) {
@@ -86,9 +85,8 @@ func (i *ChannelImporter) Import(parent context.Context, indexer *search.WriteIn
 	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,
 	})
 }
diff --git a/internal/importer/importer.go b/internal/importer/importer.go
index 1338615..8a18c60 100644
--- a/internal/importer/importer.go
+++ b/internal/importer/importer.go
@@ -32,34 +32,29 @@ func NewNixpkgsChannelImporter(
 	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(
diff --git a/internal/importer/nixpkgs-channel.go b/internal/importer/nixpkgs-channel.go
index b78844b..2ee6027 100644
--- a/internal/importer/nixpkgs-channel.go
+++ b/internal/importer/nixpkgs-channel.go
@@ -14,10 +14,9 @@ import (
 )
 
 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 @@ func (i *NixpkgsChannelImporter) Import(
 	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,
 	})
 }
diff --git a/internal/search/indexer.go b/internal/search/indexer.go
index c00c358..87cb12f 100644
--- a/internal/search/indexer.go
+++ b/internal/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,10 +123,14 @@ func createIndex(indexPath string) (bleve.Index, error) {
 	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 {
 		return nil, errors.WithMessagef(
@@ -141,13 +143,6 @@ func NewIndexer(indexPath string, force bool) (*WriteIndex, error) {
 	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)
diff --git a/internal/search/search.go b/internal/search/search.go
index bc77cea..357698c 100644
--- a/internal/search/search.go
+++ b/internal/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)
diff --git a/internal/server/server.go b/internal/server/server.go
index 0474e39..28b74b2 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -40,7 +40,6 @@ type Config struct {
 	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 @@ func New(runtimeConfig *Config) (*Server, error) {
 	}
 
 	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)