Skip to content

Commit

Permalink
Fix #1193 Don't throw an error if FTS is used before the end of a scan.
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Herger <[email protected]>
  • Loading branch information
michaelherger committed Jan 23, 2025
1 parent a3f975c commit 654e0fb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
1 change: 1 addition & 0 deletions Changelog9.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ <h2><a name="v9.0.2" id="v9.0.2"></a>Version 9.0.2</h2>
<li>Bug Fixes:</li>
<ul>
<li>Mitigate an <a href="https://forums.lyrion.org/forum/developer-forums/developers/1747258">issue with uppercase umlauts and other non-latin characters</a> in the full text search on Windows.</li>
<li><a href="https://github.com/LMS-Community/slimserver/issues/1193">#1193</a> - Don't throw error when fulltext search is being used before the end of a scan.</li>
<li><a href="https://github.com/LMS-Community/slimserver/issues/1288">#1288</a> - Update Carp::Assert to latest to fix compatibility with recent Perl versions.</li>
<li><a href="https://github.com/LMS-Community/slimserver/pull/1303">#1303</a> - Fix an issue where browsing releases would sometimes create thousands of parameters (and more - thanks @darrel-k!).</li>
<li></li>
Expand Down
54 changes: 32 additions & 22 deletions Slim/Plugin/FullTextSearch/Plugin.pm
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ sub _rebuildIndex {

$progress && $progress->update(string('DBOPTIMIZE_PROGRESS'));
Slim::Schema->forceCommit if main::SCANNER;
main::idleStreams() unless main::SCANNER;

$dbh->do("DROP TABLE IF EXISTS fulltext_terms;") or $scanlog->error($dbh->errstr);
$dbh->do("CREATE VIRTUAL TABLE fulltext_terms USING fts4aux(fulltext);") or $scanlog->error($dbh->errstr);
Expand Down Expand Up @@ -628,34 +629,43 @@ sub _initPopularTerms {

main::DEBUGLOG && $log->is_debug && $log->debug("Analyzing most popular tokens");

if (!_ftExists() && !$scanDone) {
$scanlog->error("Fulltext index missing or outdated - re-building");

$prefs->remove('popularTerms');
_rebuildIndex();
}

if (_ftExists()) {
# get a list of terms which occur more than LARGE_RESULTSET times in our database
my $terms = Slim::Schema->dbh->selectcol_arrayref( sprintf(qq{
SELECT term FROM (
SELECT term, SUM(documents) d
FROM fulltext_terms
WHERE NOT col IN ('*', 1, 0) AND LENGTH(term) > 1
GROUP BY term
)
WHERE d > %i
}, LARGE_RESULTSET) );

$prefs->set('popularTerms', $terms);
$popularTerms = join('|', @{$prefs->get('popularTerms')});

main::DEBUGLOG && $log->is_debug && $log->debug(sprintf("Found %s popular tokens", scalar @$terms));
}
else {
$log->warn("Fulltext index missing - can't analyze popular terms");
}
}

sub _ftExists {
my $dbh = Slim::Schema->dbh;

my ($ftExists) = $dbh->selectrow_array( qq{ SELECT name FROM sqlite_master WHERE type='table' AND name='fulltext' } );
($ftExists) = $dbh->selectrow_array( qq{ SELECT name FROM sqlite_master WHERE type='table' AND name='fulltext_terms' } ) if $ftExists;
($ftExists) = $dbh->selectrow_array( qq{ SELECT id FROM fulltext WHERE fulltext.id MATCH 'YXLALBUM*' } ) if $ftExists; # 8.3: IDs must be prefixed to make them "non searchable"

if (!$ftExists) {
$scanlog->error("Fulltext index missing or outdated - re-building");

$prefs->remove('popularTerms');
_rebuildIndex() unless $scanDone;
}

# get a list of terms which occur more than LARGE_RESULTSET times in our database
my $terms = $dbh->selectcol_arrayref( sprintf(qq{
SELECT term FROM (
SELECT term, SUM(documents) d
FROM fulltext_terms
WHERE NOT col IN ('*', 1, 0) AND LENGTH(term) > 1
GROUP BY term
)
WHERE d > %i
}, LARGE_RESULTSET) );

$prefs->set('popularTerms', $terms);
$popularTerms = join('|', @{$prefs->get('popularTerms')});

main::DEBUGLOG && $log->is_debug && $log->debug(sprintf("Found %s popular tokens", scalar @$terms));
return $ftExists;
}

sub postDBConnect {
Expand Down

0 comments on commit 654e0fb

Please sign in to comment.