diff options
author | Scott Jackson <daneren2005@gmail.com> | 2014-09-12 15:47:07 -0700 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2014-09-12 15:47:07 -0700 |
commit | f7aa1845c435e1e038794f95ab196ec331fe806a (patch) | |
tree | 6309e16797f409f301b6726585bb7e2fcb85ac8c /src/github/daneren2005 | |
parent | e0dcbfde8ed0bcc7fefcbdbab240f442713419e0 (diff) | |
download | dsub-f7aa1845c435e1e038794f95ab196ec331fe806a.tar.gz dsub-f7aa1845c435e1e038794f95ab196ec331fe806a.tar.bz2 dsub-f7aa1845c435e1e038794f95ab196ec331fe806a.zip |
#401 Cache child lookups to speed things up
Diffstat (limited to 'src/github/daneren2005')
-rw-r--r-- | src/github/daneren2005/dsub/util/FileUtil.java | 34 |
1 files changed, 29 insertions, 5 deletions
diff --git a/src/github/daneren2005/dsub/util/FileUtil.java b/src/github/daneren2005/dsub/util/FileUtil.java index 8dc71971..7a8eb772 100644 --- a/src/github/daneren2005/dsub/util/FileUtil.java +++ b/src/github/daneren2005/dsub/util/FileUtil.java @@ -74,6 +74,7 @@ public class FileUtil { private static final List<String> PLAYLIST_FILE_EXTENSIONS = Arrays.asList("m3u"); private static File DEFAULT_MUSIC_DIR; private static final Kryo kryo = new Kryo(); + private static HashMap<String, MusicDirectory.Entry> entryLookup; static { kryo.register(MusicDirectory.Entry.class); @@ -286,10 +287,13 @@ public class FileUtil { File f = new File(fileSystemSafeDir(entry.getPath())); dir = new File(getMusicDirectory(context).getPath() + "/" + (entry.isDirectory() ? f.getPath() : f.getParent())); } else { - MusicDirectory.Entry firstSong = lookupChild(context, entry, false); - if(firstSong != null) { - File songFile = FileUtil.getSongFile(context, firstSong); - dir = songFile.getParentFile(); + MusicDirectory.Entry firstSong; + if(!Util.isOffline(context)) { + firstSong = lookupChild(context, entry, false); + if(firstSong != null) { + File songFile = FileUtil.getSongFile(context, firstSong); + dir = songFile.getParentFile(); + } } if(dir == null) { @@ -305,6 +309,23 @@ public class FileUtil { } public static MusicDirectory.Entry lookupChild(Context context, MusicDirectory.Entry entry, boolean allowDir) { + // Initialize lookupMap if first time called + String cacheName = Util.getCacheName(context, "entryLookup"); + if(entryLookup == null) { + entryLookup = deserialize(context, cacheName, HashMap.class); + + // Create it if + if(entryLookup == null) { + entryLookup = new HashMap<String, MusicDirectory.Entry>(); + } + } + + // Check if this lookup has already been done before + MusicDirectory.Entry child = entryLookup.get(entry.getId()); + if(child != null) { + return child; + } + // Do a special lookup since 4.7+ doesn't match artist/album to entry.getPath String s = Util.getRestUrl(context, null, false) + entry.getId(); String cacheName = (Util.isTagBrowsing(context) ? "album-" : "directory-") + s.hashCode() + ".ser"; @@ -313,7 +334,10 @@ public class FileUtil { if(entryDir != null) { List<MusicDirectory.Entry> songs = entryDir.getChildren(allowDir, true); if(songs.size() > 0) { - return songs.get(0); + child = songs.get(0); + entryLookup.put(entry.getId(), child); + serialize(context, entryLookup, cacheName); + return child; } } |