From 90c386275c495752de81477413d5eda27e2c28bc Mon Sep 17 00:00:00 2001 From: CodingPF Date: Sun, 5 Nov 2023 21:00:19 +0100 Subject: [PATCH] improve merge performance --- .../java/de/mediathekview/mlib/daten/Film.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/mediathekview/mlib/daten/Film.java b/src/main/java/de/mediathekview/mlib/daten/Film.java index 238e5963..acb4e49f 100644 --- a/src/main/java/de/mediathekview/mlib/daten/Film.java +++ b/src/main/java/de/mediathekview/mlib/daten/Film.java @@ -6,6 +6,7 @@ import java.util.*; import java.util.stream.Collectors; + /** Represents a found film. */ public class Film extends Podcast { private static final long serialVersionUID = -7834270191129532291L; @@ -157,21 +158,23 @@ public static Filmlist mergeTwoFilmlists(final Filmlist aThis, final Filmlist aF final Filmlist toBeAdded = new Filmlist(UUID.randomUUID(), LocalDateTime.now()); final Filmlist diff = new Filmlist(UUID.randomUUID(), LocalDateTime.now()); // add all from old list not in the new list + Map indexTTSD = buildIndexTitelThemaSenderDuration(aThis); aFilmlist.getFilms().entrySet().stream() - .filter(e -> !containsFilm(aThis, e.getValue())) + .filter(e -> !indexTTSD.containsKey(Objects.hash(e.getValue().getSenderName(),e.getValue().getTitel(), e.getValue().getThema(), e.getValue().getDuration().toString()))) .forEachOrdered(e -> toBeAdded.getFilms().put(e.getKey(), e.getValue())); // the diff list contains all new entries (fresh list) which are not already in the old list + Map indexaFilmlist = buildIndexTitelThemaSenderDuration(aFilmlist); aThis.getFilms().entrySet().stream() - .filter(e -> !containsFilm(aFilmlist,e.getValue())) + .filter(e -> !indexaFilmlist.containsKey(Objects.hash(e.getValue().getSenderName(),e.getValue().getTitel(), e.getValue().getThema(), e.getValue().getDuration().toString()))) .forEachOrdered(e -> diff.getFilms().put(e.getKey(), e.getValue())); // add the history to the current list aThis.getFilms().putAll(toBeAdded.getFilms()); // // the same for podcast - aFilmlist.getPodcasts().entrySet().stream() + aFilmlist.getPodcasts().entrySet().stream().parallel() .filter(e -> !containsPodcast(aThis,e.getValue())) .forEachOrdered(e -> toBeAdded.getPodcasts().put(e.getKey(), e.getValue())); - aThis.getPodcasts().entrySet().stream() + aThis.getPodcasts().entrySet().stream().parallel() .filter(e -> !containsPodcast(aFilmlist,e.getValue())) .forEachOrdered(e -> diff.getPodcasts().put(e.getKey(), e.getValue())); aThis.getPodcasts().putAll(toBeAdded.getPodcasts()); @@ -179,6 +182,13 @@ public static Filmlist mergeTwoFilmlists(final Filmlist aThis, final Filmlist aF return diff; } + private static Map buildIndexTitelThemaSenderDuration(Filmlist aList) { + Map index = new HashMap(aList.getFilms().size()); + aList.getFilms().values().forEach( entry -> { + index.put(Objects.hash(entry.getSenderName(),entry.getTitel(), entry.getThema(), entry.getDuration().toString()), entry); + }); + return index; + } public static boolean containsFilm(Filmlist athis, Film film) { Optional check = athis.getFilms().entrySet().stream() .filter(entry ->