use hash63 also for playlist names

Unlike eg. MySQL, SQLite3 tends to re-use INDEX columns, so creating & deleting a playlist would cause the next one to re-use the same ID, which confuses our in-memory cache (which assumes stable / unique IDs)

Hashing by name solves this and also ensures that we do not create duplicate playlists in any code path
This commit is contained in:
Adrian Ulrich 2016-11-21 20:42:23 +01:00
parent 6f15dd5989
commit db39ae5819
2 changed files with 24 additions and 22 deletions

View File

@ -109,6 +109,7 @@ public class MediaLibrary {
*/
public static long createPlaylist(Context context, String name) {
ContentValues v = new ContentValues();
v.put(MediaLibrary.PlaylistColumns._ID, hash63(name));
v.put(MediaLibrary.PlaylistColumns.NAME, name);
return getBackend(context).insert(MediaLibrary.TABLE_PLAYLISTS, null, v);
}
@ -201,6 +202,24 @@ public class MediaLibrary {
return MediaStore.Audio.keyFor(name);
}
/**
* Simple 63 bit hash function for strings
*
* @param str the string to hash
* @return a positive long
*/
public static long hash63(String str) {
if (str == null)
return 0;
long hash = 0;
int len = str.length();
for (int i = 0; i < len ; i++) {
hash = 31*hash + str.charAt(i);
}
return (hash < 0 ? hash*-1 : hash);
}
// Columns of Song entries
public interface SongColumns {
/**

View File

@ -103,7 +103,7 @@ public class MediaScanner implements Handler.Callback {
private void scanFile(File file) {
String path = file.getAbsolutePath();
long songId = hash63(path);
long songId = MediaLibrary.hash63(path);
HashMap tags = (new Bastp()).getTags(path);
if (tags.containsKey("type") == false)
@ -140,9 +140,9 @@ Log.v("VanillaMusic", "> Found mime "+((String)tags.get("type")));
String songnum = data.extractMetadata(MediaMetadataRetriever.METADATA_KEY_CD_TRACK_NUMBER);
String composer = data.extractMetadata(MediaMetadataRetriever.METADATA_KEY_COMPOSER);
long albumId = hash63(album);
long artistId = hash63(artist);
long composerId = hash63(composer);
long albumId = MediaLibrary.hash63(album);
long artistId = MediaLibrary.hash63(artist);
long composerId = MediaLibrary.hash63(composer);
ContentValues v = new ContentValues();
v.put(MediaLibrary.SongColumns._ID, songId);
@ -190,7 +190,7 @@ Log.v("VanillaMusic", "> Found mime "+((String)tags.get("type")));
if (tags.containsKey("GENRE")) {
Vector<String> genres = (Vector)tags.get("GENRE");
for (String genre : genres) {
long genreId = hash63(genre);
long genreId = MediaLibrary.hash63(genre);
v.clear();
v.put(MediaLibrary.GenreColumns._ID, genreId);
v.put(MediaLibrary.GenreColumns._GENRE, genre);
@ -207,22 +207,5 @@ Log.v("VanillaMusic", "> Found mime "+((String)tags.get("type")));
Log.v("VanillaMusic", "MediaScanner: inserted "+path);
}
/**
* Simple 63 bit hash function for strings
*/
private long hash63(String str) {
if (str == null)
return 0;
long hash = 0;
int len = str.length();
for (int i = 0; i < len ; i++) {
hash = 31*hash + str.charAt(i);
}
return (hash < 0 ? hash*-1 : hash);
}
}