Changed music sync to insert partial results in DB as they are obtained

This commit is contained in:
Synced Synapse 2015-02-04 23:24:29 +00:00
parent ca31a0ebb1
commit a99c0d32ca
1 changed files with 75 additions and 88 deletions

View File

@ -605,8 +605,7 @@ public class LibrarySyncService extends Service {
@Override
public void onSucess(VideoType.DetailsTVShow result) {
deleteTVShows(contentResolver, hostId, tvshowId);
List<VideoType.DetailsTVShow> tvShows =
new ArrayList<VideoType.DetailsTVShow>(1);
List<VideoType.DetailsTVShow> 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<AudioType.DetailsArtist>());
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<AudioType.DetailsArtist> 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<List<AudioType.DetailsArtist>>() {
@Override
public void onSucess(List<AudioType.DetailsArtist> 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<List<LibraryType.DetailsGenre>>() {
@Override
public void onSucess(List<LibraryType.DetailsGenre> 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<AudioType.DetailsAlbum>());
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<AudioType.DetailsAlbum> 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<List<AudioType.DetailsAlbum>>() {
@Override
public void onSucess(List<AudioType.DetailsAlbum> 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<AudioType.DetailsSong>());
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<AudioType.DetailsSong> 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<List<AudioType.DetailsSong>>() {
@Override
public void onSucess(List<AudioType.DetailsSong> 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");