From a99c0d32caace90178b4e92bad71dd6621bce308 Mon Sep 17 00:00:00 2001 From: Synced Synapse Date: Wed, 4 Feb 2015 23:24:29 +0000 Subject: [PATCH] Changed music sync to insert partial results in DB as they are obtained --- .../kore2/service/LibrarySyncService.java | 163 ++++++++---------- 1 file changed, 75 insertions(+), 88 deletions(-) diff --git a/app/src/main/java/com/syncedsynapse/kore2/service/LibrarySyncService.java b/app/src/main/java/com/syncedsynapse/kore2/service/LibrarySyncService.java index 4d6eb90..28ff051 100644 --- a/app/src/main/java/com/syncedsynapse/kore2/service/LibrarySyncService.java +++ b/app/src/main/java/com/syncedsynapse/kore2/service/LibrarySyncService.java @@ -605,8 +605,7 @@ public class LibrarySyncService extends Service { @Override public void onSucess(VideoType.DetailsTVShow result) { deleteTVShows(contentResolver, hostId, tvshowId); - List tvShows = - new ArrayList(1); + List tvShows = new ArrayList<>(1); tvShows.add(result); insertTVShowsAndGetDetails(orchestrator, hostConnection, callbackHandler, contentResolver, tvShows); @@ -908,8 +907,7 @@ public class LibrarySyncService extends Service { final HostConnection hostConnection, final Handler callbackHandler, final ContentResolver contentResolver) { - chainCallSyncArtists(orchestrator, hostConnection, callbackHandler, contentResolver, - 0, new ArrayList()); + chainCallSyncArtists(orchestrator, hostConnection, callbackHandler, contentResolver, 0); } private final static String getArtistsProperties[] = { @@ -932,8 +930,7 @@ public class LibrarySyncService extends Service { final HostConnection hostConnection, final Handler callbackHandler, final ContentResolver contentResolver, - final int startIdx, - final List allResults) { + final int startIdx) { // Artists->Genres->Albums->Songs // Only gets album artists (first parameter) ListType.Limits limits = new ListType.Limits(startIdx, startIdx + LIMIT_SYNC_ARTISTS); @@ -941,26 +938,27 @@ public class LibrarySyncService extends Service { action.execute(hostConnection, new ApiCallback>() { @Override public void onSucess(List result) { - allResults.addAll(result); + if (result == null) result = new ArrayList<>(0); // Safeguard + // First delete all music info + if (startIdx == 0) deleteMusicInfo(contentResolver, hostId); + + // Insert artists + ContentValues artistValuesBatch[] = new ContentValues[result.size()]; + for (int i = 0; i < result.size(); i++) { + AudioType.DetailsArtist artist = result.get(i); + artistValuesBatch[i] = SyncUtils.contentValuesFromArtist(hostId, artist); + } + contentResolver.bulkInsert(MediaContract.Artists.CONTENT_URI, artistValuesBatch); + if (result.size() == LIMIT_SYNC_ARTISTS) { // Max limit returned, there may be some more LogUtils.LOGD(TAG, "chainCallSyncArtists: More results on media center, recursing."); + result = null; // Help the GC? chainCallSyncArtists(orchestrator, hostConnection, callbackHandler, contentResolver, - startIdx + LIMIT_SYNC_ARTISTS, allResults); + startIdx + LIMIT_SYNC_ARTISTS); } else { - // Ok, we have all the shows, insert them + // Ok, we have all the artists, proceed LogUtils.LOGD(TAG, "chainCallSyncArtists: Got all results, continuing"); - - // First delete all music info - deleteMusicInfo(contentResolver, hostId); - - ContentValues artistValuesBatch[] = new ContentValues[allResults.size()]; - for (int i = 0; i < allResults.size(); i++) { - AudioType.DetailsArtist artist = allResults.get(i); - artistValuesBatch[i] = SyncUtils.contentValuesFromArtist(hostId, artist); - } - // Insert the artists and continue the syncing - contentResolver.bulkInsert(MediaContract.Artists.CONTENT_URI, artistValuesBatch); chainCallSyncGenres(orchestrator, hostConnection, callbackHandler, contentResolver); } } @@ -1008,6 +1006,7 @@ public class LibrarySyncService extends Service { action.execute(hostConnection, new ApiCallback>() { @Override public void onSucess(List result) { + if (result == null) result = new ArrayList<>(0); // Safeguard ContentValues genresValuesBatch[] = new ContentValues[result.size()]; for (int i = 0; i < result.size(); i++) { @@ -1015,11 +1014,9 @@ public class LibrarySyncService extends Service { genresValuesBatch[i] = SyncUtils.contentValuesFromAudioGenre(hostId, genre); } - // Insert the genres + // Insert the genres and proceed to albums contentResolver.bulkInsert(MediaContract.AudioGenres.CONTENT_URI, genresValuesBatch); - - chainCallSyncAlbums(orchestrator, hostConnection, callbackHandler, contentResolver, - 0, new ArrayList()); + chainCallSyncAlbums(orchestrator, hostConnection, callbackHandler, contentResolver, 0); } @Override @@ -1052,8 +1049,7 @@ public class LibrarySyncService extends Service { final HostConnection hostConnection, final Handler callbackHandler, final ContentResolver contentResolver, - final int startIdx, - final List allResults) { + final int startIdx) { final long albumSyncStartTime = System.currentTimeMillis(); // Albums->Songs ListType.Limits limits = new ListType.Limits(startIdx, startIdx + LIMIT_SYNC_ALBUMS); @@ -1061,68 +1057,60 @@ public class LibrarySyncService extends Service { action.execute(hostConnection, new ApiCallback>() { @Override public void onSucess(List result) { - allResults.addAll(result); + if (result == null) result = new ArrayList<>(0); // Safeguard + // Insert the partial results + ContentValues albumValuesBatch[] = new ContentValues[result.size()]; + int artistsCount = 0, genresCount = 0; + for (int i = 0; i < result.size(); i++) { + AudioType.DetailsAlbum album = result.get(i); + albumValuesBatch[i] = SyncUtils.contentValuesFromAlbum(hostId, album); + + artistsCount += album.artistid.size(); + genresCount += album.genreid.size(); + } + contentResolver.bulkInsert(MediaContract.Albums.CONTENT_URI, albumValuesBatch); + + LogUtils.LOGD(TAG, "Finished inserting albums in: " + + (System.currentTimeMillis() - albumSyncStartTime)); + + // Iterate on each album, collect the artists and the genres and insert them + ContentValues albumArtistsValuesBatch[] = new ContentValues[artistsCount]; + ContentValues albumGenresValuesBatch[] = new ContentValues[genresCount]; + int artistCount = 0, genreCount = 0; + for (AudioType.DetailsAlbum album : result) { + for (int artistId : album.artistid) { + albumArtistsValuesBatch[artistCount] = new ContentValues(); + albumArtistsValuesBatch[artistCount].put(MediaContract.AlbumArtists.HOST_ID, hostId); + albumArtistsValuesBatch[artistCount].put(MediaContract.AlbumArtists.ALBUMID, album.albumid); + albumArtistsValuesBatch[artistCount].put(MediaContract.AlbumArtists.ARTISTID, artistId); + artistCount++; + } + + for (int genreId : album.genreid) { + albumGenresValuesBatch[genreCount] = new ContentValues(); + albumGenresValuesBatch[genreCount].put(MediaContract.AlbumGenres.HOST_ID, hostId); + albumGenresValuesBatch[genreCount].put(MediaContract.AlbumGenres.ALBUMID, album.albumid); + albumGenresValuesBatch[genreCount].put(MediaContract.AlbumGenres.GENREID, genreId); + genreCount++; + } + } + + contentResolver.bulkInsert(MediaContract.AlbumArtists.CONTENT_URI, albumArtistsValuesBatch); + contentResolver.bulkInsert(MediaContract.AlbumGenres.CONTENT_URI, albumGenresValuesBatch); + + LogUtils.LOGD(TAG, "Finished inserting artists and genres in: " + + (System.currentTimeMillis() - albumSyncStartTime)); + if (result.size() == LIMIT_SYNC_ALBUMS) { // Max limit returned, there may be some more LogUtils.LOGD(TAG, "chainCallSyncAlbums: More results on media center, recursing."); + result = null; // Help the GC? chainCallSyncAlbums(orchestrator, hostConnection, callbackHandler, contentResolver, - startIdx + LIMIT_SYNC_ALBUMS, allResults); + startIdx + LIMIT_SYNC_ALBUMS); } else { - // Ok, we have all the shows, insert them + // Ok, we have all the albums, proceed to songs LogUtils.LOGD(TAG, "chainCallSyncAlbums: Got all results, continuing"); - - ContentValues albumValuesBatch[] = new ContentValues[allResults.size()]; - int artistsCount = 0; - int genresCount = 0; - for (int i = 0; i < allResults.size(); i++) { - AudioType.DetailsAlbum album = allResults.get(i); - albumValuesBatch[i] = SyncUtils.contentValuesFromAlbum(hostId, album); - - artistsCount += album.artistid.size(); - genresCount += album.genreid.size(); - } - - LogUtils.LOGD(TAG, "Finished parsing albums in: " + - (System.currentTimeMillis() - albumSyncStartTime)); - - // Insert the albums - contentResolver.bulkInsert(MediaContract.Albums.CONTENT_URI, albumValuesBatch); - - LogUtils.LOGD(TAG, "Finished inserting albums in: " + - (System.currentTimeMillis() - albumSyncStartTime)); - - // Iterate on each album, collect the artists and the genres and insert them - ContentValues albumArtistsValuesBatch[] = new ContentValues[artistsCount]; - ContentValues albumGenresValuesBatch[] = new ContentValues[genresCount]; - int artistCount = 0, genreCount = 0; - for (AudioType.DetailsAlbum album : allResults) { - for (int artistId : album.artistid) { - albumArtistsValuesBatch[artistCount] = new ContentValues(); - albumArtistsValuesBatch[artistCount].put(MediaContract.AlbumArtists.HOST_ID, hostId); - albumArtistsValuesBatch[artistCount].put(MediaContract.AlbumArtists.ALBUMID, album.albumid); - albumArtistsValuesBatch[artistCount].put(MediaContract.AlbumArtists.ARTISTID, artistId); - artistCount++; - } - - for (int genreId : album.genreid) { - albumGenresValuesBatch[genreCount] = new ContentValues(); - albumGenresValuesBatch[genreCount].put(MediaContract.AlbumGenres.HOST_ID, hostId); - albumGenresValuesBatch[genreCount].put(MediaContract.AlbumGenres.ALBUMID, album.albumid); - albumGenresValuesBatch[genreCount].put(MediaContract.AlbumGenres.GENREID, genreId); - genreCount++; - } - } - - LogUtils.LOGD(TAG, "Finished parsing artists and genres in: " + - (System.currentTimeMillis() - albumSyncStartTime)); - - contentResolver.bulkInsert(MediaContract.AlbumArtists.CONTENT_URI, albumArtistsValuesBatch); - contentResolver.bulkInsert(MediaContract.AlbumGenres.CONTENT_URI, albumGenresValuesBatch); - - LogUtils.LOGD(TAG, "Finished inserting artists and genres in: " + - (System.currentTimeMillis() - albumSyncStartTime)); - chainCallSyncSongs(orchestrator, hostConnection, callbackHandler, contentResolver, - 0, new ArrayList()); + chainCallSyncSongs(orchestrator, hostConnection, callbackHandler, contentResolver, 0); } } @@ -1160,29 +1148,28 @@ public class LibrarySyncService extends Service { final HostConnection hostConnection, final Handler callbackHandler, final ContentResolver contentResolver, - final int startIdx, - final List allResults) { + final int startIdx) { // Songs ListType.Limits limits = new ListType.Limits(startIdx, startIdx + LIMIT_SYNC_SONGS); AudioLibrary.GetSongs action = new AudioLibrary.GetSongs(limits, getSongsProperties); action.execute(hostConnection, new ApiCallback>() { @Override public void onSucess(List result) { + if (result == null) result = new ArrayList<>(0); // Safeguard + // Save partial results to DB ContentValues songValuesBatch[] = new ContentValues[result.size()]; - for (int i = 0; i < result.size(); i++) { AudioType.DetailsSong song = result.get(i); songValuesBatch[i] = SyncUtils.contentValuesFromSong(hostId, song); } - - // Insert the songs contentResolver.bulkInsert(MediaContract.Songs.CONTENT_URI, songValuesBatch); if (result.size() == LIMIT_SYNC_SONGS) { // Max limit returned, there may be some more LogUtils.LOGD(TAG, "chainCallSyncSongs: More results on media center, recursing."); + result = null; // Help the GC? chainCallSyncSongs(orchestrator, hostConnection, callbackHandler, contentResolver, - startIdx + LIMIT_SYNC_SONGS, allResults); + startIdx + LIMIT_SYNC_SONGS); } else { // Ok, we have all the songs, insert them LogUtils.LOGD(TAG, "chainCallSyncSongs: Got all results, continuing");