From e376bc4abd109cbc9c15db2a0dbaf3341ec77871 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Tue, 19 Aug 2014 16:26:55 -0700 Subject: Optimizations to reduce the number of needless re-serializations In a single user environment, this will reduce most of the needless re-serialization. Previously, if the user starred something, then went into the starred list, it would re-write the serialization for the directory even though it was already update from when they called setStarred in the first place. This code is really only needed for the case where bookmarks/stars are added from a separate device. --- .../dsub/service/CachedMusicService.java | 128 +++++++++++++-------- 1 file changed, 78 insertions(+), 50 deletions(-) (limited to 'src') diff --git a/src/github/daneren2005/dsub/service/CachedMusicService.java b/src/github/daneren2005/dsub/service/CachedMusicService.java index d919775b..129fd564 100644 --- a/src/github/daneren2005/dsub/service/CachedMusicService.java +++ b/src/github/daneren2005/dsub/service/CachedMusicService.java @@ -372,31 +372,47 @@ public class CachedMusicService implements MusicService { // Left overs in oldList need to be unstarred updateStarredList(context, oldList, false, isTagBrowsing); - new PlaylistDirectoryUpdater(context) { - @Override - public boolean checkResult(Entry check) { - for(Entry entry: oldList) { - if(check.getId().equals(entry.getId())) { - check.setStarred(false); - return true; + + // Remove non-songs from lists before updating playlists + for(Iterator it = oldList.iterator(); it.hasNext(); ) { + if(it.next().isDirectory()) { + it.remove(); + } + } + for(Iterator it = newList.iterator(); it.hasNext(); ) { + if(it.next().isDirectory()) { + it.remove(); + } + } + + // Only try to update playlists if there was at least one song in new or old set + if(newList.size() > 0 || oldList.size() > 0) { + new PlaylistDirectoryUpdater(context) { + @Override + public boolean checkResult(Entry check) { + for(Entry entry: oldList) { + if(check.getId().equals(entry.getId()) && check.isStarred() != false) { + check.setStarred(false); + return true; + } } - } - - for(Entry entry: newList) { - if(check.getId().equals(entry.getId())) { - check.setStarred(true); - return true; + + for(Entry entry: newList) { + if(check.getId().equals(entry.getId()) && check.isStarred() != true) { + check.setStarred(true); + return true; + } } + + return false; } - return false; - } - - @Override - public void updateResult(Entry result) { - - } - }.execute(); + @Override + public void updateResult(Entry result) { + + } + }.execute(); + } } FileUtil.serialize(context, dir, "starred"); @@ -405,11 +421,6 @@ public class CachedMusicService implements MusicService { private void updateStarredList(Context context, List list, final boolean starred, final boolean isTagBrowsing) { for(final Entry entry: list) { - // Don't waste time when status is the same - if(entry.isStarred() == starred) { - continue; - } - String cacheName, parent = null; boolean isArtist = false; if(isTagBrowsing) { @@ -439,7 +450,7 @@ public class CachedMusicService implements MusicService { new IndexesUpdater(context, isTagBrowsing ? "artists" : "indexes") { @Override public boolean checkResult(Artist check) { - if(entry.getId().equals(check.getId())) { + if(entry.getId().equals(check.getId()) && check.isStarred() != starred) { return true; } @@ -455,7 +466,7 @@ public class CachedMusicService implements MusicService { new MusicDirectoryUpdater(context, cacheName, parent) { @Override public boolean checkResult(Entry check) { - if (entry.getId().equals(check.getId())) { + if (entry.getId().equals(check.getId()) && entry.isStarred() != starred) { return true; } @@ -801,30 +812,35 @@ public class CachedMusicService implements MusicService { // Add new bookmarks for things in new list setBookmarkCache(context, newList, false); - new PlaylistDirectoryUpdater(context) { - @Override - public boolean checkResult(Entry check) { - for(Entry entry: oldList) { - if(entry.getId().equals(check.getId())) { - setBookmarkCache(check, -1); - return true; + if(oldList.size() > 0 || nextList.size() > 0) { + new PlaylistDirectoryUpdater(context) { + @Override + public boolean checkResult(Entry check) { + for(Entry entry: oldList) { + if(entry.getId().equals(check.getId()) && check.getBookmark() != null) { + check.setBookmark(null); + return true; + } } - } - for(Entry entry: newList) { - if(entry.getId().equals(check.getId())) { - setBookmarkCache(check, entry.getBookmark().getPosition()); - return true; + for(Entry entry: newList) { + if(entry.getId().equals(check.getId())) { + int newPosition = entry.getBookmark().getPosition(); + if(check.getBookmark() == null || check.getBookmark().getPosition() != newPosition) { + setBookmarkCache(check, newPosition); + return true; + } + } } + + return false; } - return false; - } - - @Override - public void updateResult(Entry result) { - - } - }.execute(); + @Override + public void updateResult(Entry result) { + + } + }.execute(); + } } FileUtil.serialize(context, bookmarks, "bookmarks"); @@ -863,7 +879,7 @@ public class CachedMusicService implements MusicService { new MusicDirectoryUpdater(context, "directory", parent) { @Override public boolean checkResult(Entry check) { - return id.equals(check.getId()); + return shouldBookmarkUpdate(check, id, position); } @Override @@ -877,7 +893,7 @@ public class CachedMusicService implements MusicService { new PlaylistDirectoryUpdater(context) { @Override public boolean checkResult(Entry check) { - return id.equals(check.getId()); + return shouldBookmarkUpdate(check, id, position); } @Override @@ -887,6 +903,18 @@ public class CachedMusicService implements MusicService { }.execute(); } + private boolean shouldBookmarkUpdate(Entry check, String id, int position) { + if(id.equals(check.getId()) { + if(position == -1 && check.getBookmark() != null) { + return true; + } else if(position >= 0 && (check.getBookmark() == null || check.getBookmark().getPosition() != position)) { + return true; + } + } + + return false; + } + private void setBookmarkCache(Entry result, int position) { // If position == -1, then it is a delete if(result.getBookmark() != null && position == -1) { -- cgit v1.2.3