Skip to content

Commit

Permalink
Remove the "ancient PC" optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Borewit committed Oct 4, 2023
1 parent 2514d45 commit da15e81
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 40 deletions.
8 changes: 4 additions & 4 deletions src/main/java/listfix/model/playlists/Playlist.java
Original file line number Diff line number Diff line change
Expand Up @@ -693,10 +693,10 @@ public List<BatchMatchItem> findClosestMatches(List<PlaylistEntry> entries, Coll
{
final long start = System.currentTimeMillis();

ProgressAdapter<String> progress = ProgressAdapter.make(observer);
final ProgressAdapter<String> progress = ProgressAdapter.make(observer);
progress.setTotal(entries.size());

List<BatchMatchItem> fixed = new ArrayList<>();
final List<BatchMatchItem> needToBeFixed = new ArrayList<>();

entries.parallelStream().forEach(entry -> {
if (observer.getCancelled()) return;
Expand All @@ -705,7 +705,7 @@ public List<BatchMatchItem> findClosestMatches(List<PlaylistEntry> entries, Coll
List<PotentialPlaylistEntryMatch> matches = entry.findClosestMatches(libraryFiles, null, this.playListOptions);
if (!matches.isEmpty())
{
fixed.add(new BatchMatchItem(entry, matches));
needToBeFixed.add(new BatchMatchItem(entry, matches));
}
}
progress.stepCompleted();
Expand All @@ -714,7 +714,7 @@ public List<BatchMatchItem> findClosestMatches(List<PlaylistEntry> entries, Coll
long timeElapsed = System.currentTimeMillis() - start;
_logger.info("Resolved closest matches in " + timeElapsed + " ms.");

return fixed;
return needToBeFixed;
}

public List<BatchMatchItem> findClosestMatchesForSelectedEntries(List<Integer> rowList, Collection<String> libraryFiles, ProgressWorker<List<BatchMatchItem>, String> observer)
Expand Down
50 changes: 14 additions & 36 deletions src/main/java/listfix/model/playlists/PlaylistEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public abstract class PlaylistEntry implements Cloneable
{
Expand Down Expand Up @@ -101,51 +102,28 @@ public void setFixed(boolean fixed)

public abstract boolean isRelative();

private final MatchedPlaylistEntryComparator matchedPlaylistEntryComparator = new MatchedPlaylistEntryComparator();

public List<PotentialPlaylistEntryMatch> findClosestMatches(Collection<String> mediaFiles, IProgressObserver<String> observer, IPlaylistOptions playListOptions)
{
List<PotentialPlaylistEntryMatch> matches = new ArrayList<>();
ProgressAdapter<String> progress = new ProgressAdapter<>(observer);
progress.setTotal(mediaFiles.size());

// Remove apostrophes and addAt spaces between lowercase and capital letters so we can tokenize by camel case.
String entryName = CAMEL_CASE_PATTERN.matcher(APOS_PATTERN.matcher(getTrackFileName()).replaceAll("")).replaceAll("$2 $3").toLowerCase();
File mediaFile;
int score;
for (String mediaFilePath : mediaFiles)
{
if (observer == null || !observer.getCancelled())
{

return mediaFiles.stream().map(mediaFilePath -> {

progress.stepCompleted();

mediaFile = new File(mediaFilePath);

// Remove apostrophes and addAt spaces between lowercase and capital letters so we can tokenize by camel case.
score = new FileNameTokenizer(playListOptions).score(entryName, CAMEL_CASE_PATTERN.matcher(APOS_PATTERN.matcher(mediaFile.getName()).replaceAll("")).replaceAll("$2 $3").toLowerCase());
if (score > 0)
{
// Only keep the top X highest-rated matches (default is 20), anything more than that has a good chance of using too much memory
// on systems w/ huge media libraries, too little RAM, or when fixing excessively large playlists (the things you have to worry
// about when people run your software on ancient PCs in Africa =])
if (matches.size() < playListOptions.getMaxClosestResults())
{
matches.add(new PotentialPlaylistEntryMatch(mediaFile.toPath(), score));
}
else
{
if (matches.get(playListOptions.getMaxClosestResults() - 1).getScore() < score)
{
matches.set(playListOptions.getMaxClosestResults() - 1, new PotentialPlaylistEntryMatch(mediaFile.toPath(), score));
}
}
matches.sort(new MatchedPlaylistEntryComparator());
}
}
else
{
return null;
}
}
return matches;
final File mediaFile = new File(mediaFilePath);
final int score = new FileNameTokenizer(playListOptions).score(entryName, CAMEL_CASE_PATTERN.matcher(APOS_PATTERN.matcher(mediaFile.getName()).replaceAll("")).replaceAll("$2 $3").toLowerCase());

return new PotentialPlaylistEntryMatch(mediaFile.toPath(), score);
})
.sorted(matchedPlaylistEntryComparator)
.limit(playListOptions.getMaxClosestResults())
.collect(Collectors.toList());
}

@Override
Expand Down

0 comments on commit da15e81

Please sign in to comment.