improve full rescans to include skip and playcounts, also makes things nicer to read.

This commit is contained in:
Adrian Ulrich committed 2017-04-22 18:13:21 +02:00
1 parent 4f509303de
commit 46a8d0b602
3 files changed
+40 -22

No files matched your search

@@ -212,7 +212,6 @@ public class MediaLibrary {
MediaLibraryBackend backend = getBackend(context); // also initialized sScanner
if (drop) {
sScanner.flushDatabase();
// fixme: should clean orphaned AFTER scan finished
}
if (forceFull) {
@@ -291,7 +290,7 @@ public class MediaLibrary {
int rows = getBackend(context).delete(TABLE_SONGS, SongColumns._ID+"="+id, null);
if (rows > 0) {
getBackend(context).cleanOrphanedEntries(true);
getBackend(context).cleanOrphanedEntries();
notifyObserver();
}
return rows;
@@ -40,6 +40,11 @@ public class MediaLibraryBackend extends SQLiteOpenHelper {
* on-disk file to store the database
*/
private static final String DATABASE_NAME = "media-library.db";
/**
* The magic mtime to use for songs which are in PENDING_DELETION state.
* This is NOT 0 as the mtime is always expected to be > 0 for existing rows
*/
private static final int PENDING_DELETION_MTIME = 1;
/**
* Regexp to detect genre queries which we can optimize
*/
@@ -159,22 +164,29 @@ public class MediaLibraryBackend extends SQLiteOpenHelper {
return result;
}
/**
* Marks all songs as 'deleteable' - but doesn't delete them yet.
* Calling cleanOrphanedEntries() would take care of the actual deletion.
* This is a hacky way to get rid of all songs that a scan didn't touch.
*/
void setPendingDeletion() {
SQLiteDatabase dbh = getWritableDatabase();
dbh.execSQL("UPDATE "+MediaLibrary.TABLE_SONGS+" SET "+MediaLibrary.SongColumns.MTIME+"="+PENDING_DELETION_MTIME);
}
/**
* Purges orphaned entries from the media library
*
* @param purgeUserData also delete user data, such as playlists if true
*/
void cleanOrphanedEntries(boolean purgeUserData) {
void cleanOrphanedEntries() {
SQLiteDatabase dbh = getWritableDatabase();
dbh.execSQL("DELETE FROM "+MediaLibrary.TABLE_SONGS+" WHERE "+MediaLibrary.SongColumns.MTIME+"="+PENDING_DELETION_MTIME);
dbh.execSQL("DELETE FROM "+MediaLibrary.TABLE_ALBUMS+" WHERE "+MediaLibrary.AlbumColumns._ID+" NOT IN (SELECT "+MediaLibrary.SongColumns.ALBUM_ID+" FROM "+MediaLibrary.TABLE_SONGS+");");
dbh.execSQL("DELETE FROM "+MediaLibrary.TABLE_GENRES_SONGS+" WHERE "+MediaLibrary.GenreSongColumns.SONG_ID+" NOT IN (SELECT "+MediaLibrary.SongColumns._ID+" FROM "+MediaLibrary.TABLE_SONGS+");");
dbh.execSQL("DELETE FROM "+MediaLibrary.TABLE_GENRES+" WHERE "+MediaLibrary.GenreColumns._ID+" NOT IN (SELECT "+MediaLibrary.GenreSongColumns._GENRE_ID+" FROM "+MediaLibrary.TABLE_GENRES_SONGS+");");
dbh.execSQL("DELETE FROM "+MediaLibrary.TABLE_CONTRIBUTORS_SONGS+" WHERE "+MediaLibrary.ContributorSongColumns.SONG_ID+" NOT IN (SELECT "+MediaLibrary.SongColumns._ID+" FROM "+MediaLibrary.TABLE_SONGS+");");
dbh.execSQL("DELETE FROM "+MediaLibrary.TABLE_CONTRIBUTORS+" WHERE "+MediaLibrary.ContributorColumns._ID+" NOT IN (SELECT "+MediaLibrary.ContributorSongColumns._CONTRIBUTOR_ID+" FROM "+MediaLibrary.TABLE_CONTRIBUTORS_SONGS+");");
if (purgeUserData) {
dbh.execSQL("DELETE FROM "+MediaLibrary.TABLE_PLAYLISTS_SONGS+" WHERE "+MediaLibrary.PlaylistSongColumns.SONG_ID+" NOT IN (SELECT "+MediaLibrary.SongColumns._ID+" FROM "+MediaLibrary.TABLE_SONGS+");");
}
dbh.execSQL("DELETE FROM "+MediaLibrary.TABLE_PLAYLISTS_SONGS+" WHERE "+MediaLibrary.PlaylistSongColumns.SONG_ID+" NOT IN (SELECT "+MediaLibrary.SongColumns._ID+" FROM "+MediaLibrary.TABLE_SONGS+");");
}
/**
@@ -66,6 +66,10 @@ public class MediaScanner implements Handler.Callback {
* Set by KICKSTART rpc
*/
private boolean mIsInitialScan;
/**
* True if we must cleanup orphaned entries after the scan finished.
*/
private boolean mPendingCleanup;
/**
* Timestamp in half-seconds since last notification
*/
@@ -139,11 +143,11 @@ public class MediaScanner implements Handler.Callback {
}
/**
* Drops the media library
* Prepares a flush of the databse.
*/
public void flushDatabase() {
mBackend.delete(MediaLibrary.TABLE_SONGS, null, null);
mBackend.cleanOrphanedEntries(false); // -> keep playlists
mBackend.setPendingDeletion();
mPendingCleanup = true;
MediaLibrary.Preferences prefs = MediaLibrary.getPreferences(mContext);
prefs._nativeLastMtime = 0;
@@ -193,6 +197,13 @@ public class MediaScanner implements Handler.Callback {
mIsInitialScan = false;
PlaylistBridge.importAndroidPlaylists(mContext);
}
if (mPendingCleanup) {
mPendingCleanup = false;
mBackend.cleanOrphanedEntries();
}
// make sure to notify about changes which cleanOrphanedEntries
// might have caused
mHandler.sendEmptyMessage(MSG_NOTIFY_CHANGE);
updateNotification(false);
break;
}
@@ -239,7 +250,7 @@ public class MediaScanner implements Handler.Callback {
if (step == null) {
mHandler.sendEmptyMessage(MSG_SCAN_FINISHED);
} else {
Log.v("VanillaMusic", "--- starting scan of type "+step.msg);
Log.v("VanillaMusic", "xxx --- starting scan of type "+step.msg);
mHandler.sendMessage(mHandler.obtainMessage(MSG_SCAN_RPC, step.msg, 0, step.arg));
}
}
@@ -421,7 +432,7 @@ public class MediaScanner implements Handler.Callback {
long playCount = 0;
long skipCount = 0;
boolean hasChanged = false;
boolean mustInsert = true;
boolean mustInsert = false;
if (fileMtime > 0 && dbEntryMtime >= fileMtime) {
return false; // on-disk mtime is older than db mtime and it still exists -> nothing to do
@@ -433,18 +444,14 @@ public class MediaScanner implements Handler.Callback {
playCount = mBackend.getColumnFromSongId(MediaLibrary.SongColumns.PLAYCOUNT, songId);
skipCount = mBackend.getColumnFromSongId(MediaLibrary.SongColumns.SKIPCOUNT, songId);
mBackend.delete(MediaLibrary.TABLE_SONGS, MediaLibrary.SongColumns._ID+"="+songId, null);
hasChanged = true;
mPendingCleanup = true; // we deleted an entry, so we must also cleanup playlists, artists, etc...
hasChanged = true; // notify caller about change even if we are not going to re-insert this file.
}
// Check if we are willing to insert this file
// This is the case if we consider it to be playable on this device.
MediaMetadataExtractor tags = new MediaMetadataExtractor(path, prefs.forceBastp);
if (!tags.isMediaFile()) {
mustInsert = false; // does not have any useable metadata: won't insert even if it is a playable file
}
if (hasChanged) {
boolean purgeUserData = (mustInsert ? false : true);
mBackend.cleanOrphanedEntries(purgeUserData);
}
mustInsert = tags.isMediaFile();
if (mustInsert) {
hasChanged = true;