Skip to content

Commit

Permalink
Revert "Remove the "ancient PC" optimization"
Browse files Browse the repository at this point in the history
This partially reverts commit da15e81.

- Restores the ancient PC optimization
- But prevent reconstructing the `MatchedPlaylistEntryComparator`
  • Loading branch information
Borewit committed Oct 6, 2023
1 parent a620bc8 commit a752070
Showing 1 changed file with 36 additions and 12 deletions.
48 changes: 36 additions & 12 deletions src/main/java/listfix/model/playlists/PlaylistEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
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 @@ -106,24 +105,49 @@ public void setFixed(boolean fixed)

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();

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

File mediaFile;
int score;
for (String mediaFilePath : mediaFiles)
{
if (observer == null || !observer.getCancelled())
{
progress.stepCompleted();

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());
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(matchedPlaylistEntryComparator);
}
}
else
{
return null;
}
}
return matches;
}

@Override
Expand Down

0 comments on commit a752070

Please sign in to comment.