Skip to content

Commit 0e1dfc2

Browse files
Merge pull request #1507 from darrell-k/albumScanFullText
FTS single track scan
2 parents fbb39ee + e48069e commit 0e1dfc2

File tree

2 files changed

+56
-23
lines changed

2 files changed

+56
-23
lines changed

Slim/Plugin/FullTextSearch/Plugin.pm

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use constant LARGE_RESULTSET => 500;
2828
=cut
2929

3030
use constant SQL_CREATE_TRACK_ITEM => q{
31-
INSERT %s INTO fulltext (id, type, w10, w5, w3, w1)
31+
INSERT INTO fulltext (id, type, w10, w5, w3, w1)
3232
SELECT tracks.urlmd5 || tracks.id, 'track',
3333
-- weight 10
3434
UNIQUE_TOKENS(LOWER(IFNULL(tracks.title, '')) || ' ' || IFNULL(tracks.titlesearch, '') || ' ' || IFNULL(tracks.customsearch, '')),
@@ -57,19 +57,27 @@ use constant SQL_CREATE_TRACK_ITEM => q{
5757
GROUP BY tracks.id;
5858
};
5959

60-
use constant SQL_DROP_WORKTEMP => q{
61-
DROP TABLE IF EXISTS worktemp;
60+
use constant SQL_CREATE_WORKTEMP => q{
61+
CREATE TEMPORARY TABLE IF NOT EXISTS worktemp (
62+
id integer,
63+
w3 text
64+
)
6265
};
6366

64-
use constant SQL_CREATE_WORKTEMP => q{
65-
CREATE TEMPORARY TABLE worktemp AS
67+
use constant SQL_CLEAR_WORKTEMP => q{
68+
DELETE FROM worktemp;
69+
};
70+
71+
use constant SQL_POPULATE_WORKTEMP => q{
72+
INSERT INTO worktemp (id, w3)
6673
SELECT tracks.id AS id, UNIQUE_TOKENS(CONCAT_CONTRIBUTOR_ROLE(tracks.id, GROUP_CONCAT(contributor_track.contributor, ','), 'contributor_track')) AS w3
6774
FROM tracks JOIN contributor_track ON contributor_track.track = tracks.id
75+
%s
6876
GROUP BY tracks.id;
6977
};
7078

7179
use constant SQL_CREATE_WORK_ITEM => q{
72-
INSERT %s INTO fulltext (id, type, w10, w5, w3, w1)
80+
INSERT INTO fulltext (id, type, w10, w5, w3, w1)
7381
7482
SELECT 'YXLWORKSYYYYYYYYYYYYYYYYYYYYYYYY' || works.id, 'work',
7583
-- weight 10
@@ -92,7 +100,7 @@ use constant SQL_CREATE_WORK_ITEM => q{
92100
};
93101

94102
use constant SQL_CREATE_ALBUM_ITEM => q{
95-
INSERT %s INTO fulltext (id, type, w10, w5, w3, w1)
103+
INSERT INTO fulltext (id, type, w10, w5, w3, w1)
96104
SELECT 'YXLALBUMSYYYYYYYYYYYYYYYYYYYYYYY' || albums.id, 'album',
97105
-- weight 10
98106
UNIQUE_TOKENS(LOWER(IFNULL(albums.title, '')) || ' ' || IFNULL(albums.titlesearch, '') || ' ' || IFNULL(albums.customsearch, '') || ' '
@@ -115,7 +123,7 @@ use constant SQL_CREATE_ALBUM_ITEM => q{
115123
};
116124

117125
use constant SQL_CREATE_CONTRIBUTOR_ITEM => q{
118-
INSERT %s INTO fulltext (id, type, w10, w5, w3, w1)
126+
INSERT INTO fulltext (id, type, w10, w5, w3, w1)
119127
SELECT 'YXLCONTRIBUTORSYYYYYYYYYYYYYYYYY' || contributors.id, 'contributor',
120128
-- weight 10
121129
UNIQUE_TOKENS(LOWER(IFNULL(contributors.name, '')) || ' ' || IFNULL(contributors.namesearch, '') || ' ' || IFNULL(contributors.customsearch, '')),
@@ -146,6 +154,15 @@ use constant SQL_CREATE_PLAYLIST_ITEM => CAN_FTS4
146154
WHERE tracks.id = ?
147155
};
148156

157+
use constant SQL_DELETE_FOR_TRACK => q{
158+
DELETE FROM fulltext
159+
WHERE id = ? || ?
160+
OR ( fulltext.type = 'contributor' AND REPLACE(fulltext.id, 'YXLCONTRIBUTORSYYYYYYYYYYYYYYYYY', '') IN (SELECT contributor FROM contributor_track WHERE track=?) )
161+
OR ( fulltext.type = 'contributor' AND fulltext.w10 <> ? AND NOT EXISTS (SELECT * FROM contributor_track WHERE contributor = REPLACE(fulltext.id, 'YXLCONTRIBUTORSYYYYYYYYYYYYYYYYY', '')) )
162+
OR ( fulltext.type = 'work' AND REPLACE(fulltext.id, 'YXLWORKSYYYYYYYYYYYYYYYYYYYYYYYY', '') IN (SELECT work FROM tracks WHERE id=?) )
163+
OR ( fulltext.type = 'work' AND NOT EXISTS (SELECT * FROM works WHERE id = REPLACE(fulltext.id, 'YXLWORKSYYYYYYYYYYYYYYYYYYYYYYYY', '')) )
164+
};
165+
149166
my $log = Slim::Utils::Log->addLogCategory({
150167
'category' => 'plugin.fulltext',
151168
'defaultLevel' => 'WARN',
@@ -233,9 +250,25 @@ sub checkSingleTrack {
233250

234251
my $dbh = Slim::Schema->dbh;
235252

236-
$dbh->do( sprintf(SQL_CREATE_TRACK_ITEM, 'OR REPLACE', 'WHERE tracks.id=?'), undef, $trackObj->id );
237-
$dbh->do( sprintf(SQL_CREATE_ALBUM_ITEM, 'OR REPLACE', 'WHERE albums.id=?'), undef, $trackObj->albumid ) if $trackObj->albumid;
238-
$dbh->do( sprintf(SQL_CREATE_CONTRIBUTOR_ITEM, 'OR REPLACE', 'WHERE contributors.id=?'), undef, $trackObj->artistid ) if $trackObj->artistid;
253+
my $deletionSql = SQL_DELETE_FOR_TRACK;
254+
my @params = ($trackObj->urlmd5, $trackObj->id, $trackObj->id, uc(Slim::Music::Info::variousArtistString()), $trackObj->id);
255+
256+
if ($trackObj->albumid) {
257+
$deletionSql .= q{ OR id = 'YXLALBUMSYYYYYYYYYYYYYYYYYYYYYYY' || ?};
258+
push @params, $trackObj->albumid;
259+
}
260+
261+
$dbh->do($deletionSql, undef, @params);
262+
263+
$dbh->do( sprintf(SQL_CREATE_TRACK_ITEM, 'WHERE tracks.id=?'), undef, $trackObj->id );
264+
$dbh->do( sprintf(SQL_CREATE_ALBUM_ITEM, 'WHERE albums.id=?'), undef, $trackObj->albumid ) if $trackObj->albumid;
265+
$dbh->do( sprintf(SQL_CREATE_CONTRIBUTOR_ITEM, 'WHERE contributors.id in (SELECT contributor FROM contributor_track WHERE track=?)'), undef, $trackObj->id );
266+
if ($trackObj->work) {
267+
$dbh->do( SQL_CREATE_WORKTEMP );
268+
$dbh->do( sprintf(SQL_POPULATE_WORKTEMP, 'WHERE tracks.work=?'), undef, $trackObj->workid);
269+
$dbh->do( sprintf(SQL_CREATE_WORK_ITEM, 'WHERE tracks.work=?'), undef, $trackObj->workid);
270+
$dbh->do( SQL_CLEAR_WORKTEMP );
271+
}
239272
}
240273

241274
sub checkPlaylist {
@@ -326,7 +359,7 @@ sub parseSearchTerm {
326359
# Check if the search string contains CJK (Chinese/Japanese/Korean) characters
327360
# \p{Han} matches Chinese Han characters (covers both simplified and traditional Chinese, and Kanji)
328361
# \p{Hiragana} matches Japanese Hiragana syllabary
329-
# \p{Katakana} matches Japanese Katakana syllabary
362+
# \p{Katakana} matches Japanese Katakana syllabary
330363
# \p{Hangul} matches Korean Hangul syllables
331364
my $isCJK = ( $search =~ /\p{Han}|\p{Hiragana}|\p{Katakana}|\p{Hangul}/ );
332365

@@ -335,10 +368,10 @@ sub parseSearchTerm {
335368
while ($search =~ s/"(.+?)"//) {
336369
my $quoted = $1;
337370
if ( $isCJK ) {
338-
# Since SQLite's full-text tokenizer does not use CJK punctuation for word segmentation,
371+
# Since SQLite's full-text tokenizer does not use CJK punctuation for word segmentation,
339372
# we replace ASCII punctuation while preserving CJK punctuation.
340373
#
341-
# Punctuation characters; in the ‘C’ locale and ASCII character encoding,
374+
# Punctuation characters; in the ‘C’ locale and ASCII character encoding,
342375
# this is ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~.
343376
# https://www.gnu.org/software/grep/manual/html_node/Character-Classes-and-Bracket-Expressions.html
344377
$quoted =~ s/[!"#\$%&'()*+,\-.\/:;<=>?@\[\\\]^_`{|}~]/ /g;
@@ -568,7 +601,7 @@ sub _rebuildIndex {
568601
$progress && $progress->update(string('SONGS'));
569602
Slim::Schema->forceCommit if main::SCANNER;
570603

571-
my $sql = sprintf(SQL_CREATE_TRACK_ITEM, '', '');
604+
my $sql = sprintf(SQL_CREATE_TRACK_ITEM, '');
572605

573606
# main::DEBUGLOG && $scanlog->is_debug && $scanlog->debug($sql);
574607
$dbh->do($sql) or $scanlog->error($dbh->errstr);
@@ -577,7 +610,7 @@ sub _rebuildIndex {
577610
$scanlog->error("Create fulltext index for albums");
578611
$progress && $progress->update(string('ALBUMS'));
579612
Slim::Schema->forceCommit if main::SCANNER;
580-
$sql = sprintf(SQL_CREATE_ALBUM_ITEM, '', '');
613+
$sql = sprintf(SQL_CREATE_ALBUM_ITEM, '');
581614

582615
# main::DEBUGLOG && $scanlog->is_debug && $scanlog->debug($sql);
583616
$dbh->do($sql) or $scanlog->error($dbh->errstr);
@@ -587,7 +620,7 @@ sub _rebuildIndex {
587620
$progress && $progress->update(string('ARTISTS'));
588621
Slim::Schema->forceCommit if main::SCANNER;
589622

590-
$sql = sprintf(SQL_CREATE_CONTRIBUTOR_ITEM, '', '');
623+
$sql = sprintf(SQL_CREATE_CONTRIBUTOR_ITEM, '');
591624

592625
# main::DEBUGLOG && $scanlog->is_debug && $scanlog->debug($sql);
593626
$dbh->do($sql) or $scanlog->error($dbh->errstr);
@@ -596,9 +629,11 @@ sub _rebuildIndex {
596629
$scanlog->error("Create fulltext index for works");
597630
$progress && $progress->update(string('WORKS'));
598631
Slim::Schema->forceCommit if main::SCANNER;
599-
$dbh->do(SQL_DROP_WORKTEMP) or $scanlog->error($dbh->errstr);
600632
$dbh->do(SQL_CREATE_WORKTEMP) or $scanlog->error($dbh->errstr);
601-
$sql = sprintf(SQL_CREATE_WORK_ITEM, '', '');
633+
$dbh->do(SQL_CLEAR_WORKTEMP) or $scanlog->error($dbh->errstr);
634+
$sql = sprintf(SQL_POPULATE_WORKTEMP, '');
635+
$dbh->do($sql) or $scanlog->error($dbh->errstr);
636+
$sql = sprintf(SQL_CREATE_WORK_ITEM, '');
602637
$dbh->do($sql) or $scanlog->error($dbh->errstr);
603638
main::idleStreams() unless main::SCANNER;
604639

Slim/Schema.pm

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ use constant SCAN_WORKS_FOR_MY_CLASSICAL_GENRES => 2;
5959
my $log = logger('database.info');
6060

6161
my $prefs = preferences('server');
62-
my $scanWorks = $prefs->get('worksScan') if main::SCANNER;
6362

6463
# Singleton objects for Unknowns
6564
our ($_unknownArtist, $_unknownGenre, $_unknownAlbumId) = ('', '', undef);
@@ -3372,11 +3371,10 @@ sub canFulltextSearch {
33723371

33733372
sub _workRequired {
33743373
my $genres = shift;
3375-
my $wantWorks = defined $scanWorks ? $scanWorks : $prefs->get('worksScan');
33763374

3377-
return $wantWorks == SCAN_WORKS_FOR_MY_CLASSICAL_GENRES
3375+
return $prefs->get('worksScan') == SCAN_WORKS_FOR_MY_CLASSICAL_GENRES
33783376
? Slim::Schema::Genre->isMyClassicalGenre($genres)
3379-
: $scanWorks;
3377+
: SCAN_WORKS_FOR_MY_CLASSICAL_GENRES;
33803378
}
33813379

33823380
=head1 SEE ALSO

0 commit comments

Comments
 (0)