fix: mark search index ready at service creation, not just during build
The search index ready flag is an in-memory bool that resets to false on every app restart. It was only set to true inside build(), which runs in a goroutine after SoftScanAllLibraries completes. If the user searched before the build goroutine started, IsReady() returned false and the search took the slow path (LB popularity + cross-ref: ~2.3s) even though the SQLite index had all the data from the previous build. Now MarkReadyIfPopulated() is called eagerly in NewExploreService — the index is queryable as soon as the service is constructed, before any goroutines launch. If the explore_index table has rows, ready=true immediately.
This commit is contained in:
@@ -48,6 +48,7 @@ func NewExploreService(logger *slog.Logger, db *database.DB) *Service {
|
|||||||
db, cache, mbBackgroundLimiter, logger.WithGroup("artist-image"),
|
db, cache, mbBackgroundLimiter, logger.WithGroup("artist-image"),
|
||||||
)
|
)
|
||||||
index := NewSearchIndex(db, lb, artistImg, logger.WithGroup("search-index"))
|
index := NewSearchIndex(db, lb, artistImg, logger.WithGroup("search-index"))
|
||||||
|
index.MarkReadyIfPopulated() // make index queryable immediately if data exists
|
||||||
libMBID := NewLibraryMBIDIndex(db)
|
libMBID := NewLibraryMBIDIndex(db)
|
||||||
|
|
||||||
logger.Info("explore service created")
|
logger.Info("explore service created")
|
||||||
|
|||||||
@@ -508,7 +508,7 @@ func (si *SearchIndex) build(ctx context.Context) {
|
|||||||
si.logger.Info("search index build starting")
|
si.logger.Info("search index build starting")
|
||||||
|
|
||||||
// Mark ready from existing rows so search works during the build.
|
// Mark ready from existing rows so search works during the build.
|
||||||
si.markReadyIfPopulated()
|
si.MarkReadyIfPopulated()
|
||||||
|
|
||||||
indexLimiter := NewRateLimiterN(indexerRate)
|
indexLimiter := NewRateLimiterN(indexerRate)
|
||||||
indexLB := NewListenBrainzClient(indexLimiter, si.lb.cache, si.logger.WithGroup("indexer"))
|
indexLB := NewListenBrainzClient(indexLimiter, si.lb.cache, si.logger.WithGroup("indexer"))
|
||||||
@@ -1609,7 +1609,10 @@ func (si *SearchIndex) setMeta(key, value string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (si *SearchIndex) markReadyIfPopulated() {
|
// MarkReadyIfPopulated sets the index as ready for querying if it
|
||||||
|
// already contains data from a previous build. Called eagerly at
|
||||||
|
// service creation so the index is queryable before StartBuild runs.
|
||||||
|
func (si *SearchIndex) MarkReadyIfPopulated() {
|
||||||
rows, err := si.db.QueryContext("SELECT COUNT(*) FROM explore_index")
|
rows, err := si.db.QueryContext("SELECT COUNT(*) FROM explore_index")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user