Skip to content

Commit 88b465b

Browse files
committed
2 parents eb78634 + 6d191a4 commit 88b465b

File tree

1 file changed

+35
-23
lines changed

1 file changed

+35
-23
lines changed

SimpleLibraryViews/Plugin.pm

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use Slim::Menu::BrowseLibrary;
2626
use Slim::Utils::Log;
2727
use Slim::Utils::Misc;
2828
use File::Basename;
29+
use File::Spec::Functions qw(catfile);
2930
use Slim::Utils::Prefs;
3031
use Slim::Control::Request;
3132

@@ -123,34 +124,45 @@ sub createLibrary {
123124

124125
$log->info("Building library id " . $id . " for name " . $libName);
125126

126-
my $rs = Slim::Schema->resultset('Track')->search;
127-
my $obj;
128-
129-
do {
130-
$obj = $rs->next;
131-
if ($obj) {
132-
my $trackid = $obj->get_column("id");
133-
my $url = $obj->get_column("url");
127+
my $dbh = Slim::Schema->dbh;
128+
129+
# prepare the insert SQL statement - no need to re-initialize for every track
130+
my $sth_insert = $dbh->prepare('INSERT OR IGNORE INTO library_track (library, track) values (?, ?)');
131+
132+
# get track ID and URL for every single audio track in our library
133+
my $sth = $dbh->prepare('SELECT id, url FROM tracks WHERE content_type NOT IN ("cpl", "src", "ssp", "dir") ORDER BY url');
134+
$sth->execute();
135+
136+
# use a hash to cache results of the library file checks
137+
my %knownDirs;
138+
139+
# iterate over all tracks in our library
140+
while ( my ($trackid, $url) = $sth->fetchrow_array ) {
141+
# dirname would return the directory part of the file URL
142+
# good enough to be used as the cache key
143+
my $key = dirname($url);
144+
145+
# check the list of dirs we've seen before first
146+
my $hasLibFile = $knownDirs{$key};
147+
148+
# only check the library files if we don't have a defined result for this folder
149+
if (!defined $hasLibFile) {
134150
my $dir = dirname(Slim::Utils::Misc::pathFromFileURL($url));
135151

136-
$log->debug("ID: " . $trackid . ", URL: " . $url . ", path: " . $dir);
137-
138-
my $libFile = $dir . "/simple-library-views-" . $libName;
139-
my $newLibFile = $dir . "/.simple-library-views-" . $libName;
152+
my $libFile = catfile($dir, "simple-library-views-$libName");
153+
my $newLibFile = catfile($dir, ".simple-library-views-$libName");
154+
155+
$hasLibFile = $knownDirs{$key} = (-f $libFile || -f $newLibFile) ? 1 : 0;
156+
}
140157

141-
if (-f $libFile || -f $newLibFile) {
142-
$log->debug("Adding " . $url . " to library " . $libName);
158+
main::DEBUGLOG && $log->is_debug && $log->debug("ID: " . $trackid . ", URL: " . $url . ", path: " . $url);
143159

144-
my $dbh = Slim::Schema->dbh;
145-
$dbh->do(
146-
sprintf(
147-
q{INSERT OR IGNORE INTO library_track (library, track) values ('%s','%s')},
148-
$id, $trackid
149-
)
150-
);
151-
}
160+
if ($hasLibFile) {
161+
$log->debug("Adding " . $url . " to library " . $libName);
162+
163+
$sth_insert->execute($id, $trackid);
152164
}
153-
} while ($obj);
165+
}
154166
}
155167

156168
1;

0 commit comments

Comments
 (0)