Skip to content

Commit

Permalink
added migrate db
Browse files Browse the repository at this point in the history
  • Loading branch information
sunmisc committed Jul 18, 2024
1 parent 1a8528b commit 8e53d01
Show file tree
Hide file tree
Showing 9 changed files with 401 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public static boolean injectOnClick(int actionId, Playlist playlist) { // playli
}

if (actionId == R.id.remove_from_cache) {
PlaylistCacheDbDelegate.deletePlaylist(playlist.v1());
PlaylistCacheDbDelegate.deletePlaylist(playlist.a, playlist.b);
return true;
} else if (actionId == R.id.add_to_cache) {
AudioDownloader.cachePlaylist(playlist);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


public interface Constants {
int DV_VERSION = 3;
int DV_VERSION = 4;
String DB_NAME = "vt_lite_cache_playlists.db";

// tables
Expand Down
66 changes: 62 additions & 4 deletions app/src/main/java/ru/vtosters/lite/music/cache/db/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@

import androidx.annotation.Nullable;

import com.vk.dto.music.MusicTrack;
import com.vk.dto.music.Playlist;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ru.vtosters.lite.music.cache.db.old.OldMusicCacheDb;
import ru.vtosters.lite.music.cache.db.old.OldPlaylistCacheDb;
import ru.vtosters.lite.utils.AndroidUtils;

public final class Database extends SQLiteOpenHelper implements AutoCloseable {
Expand All @@ -27,10 +36,59 @@ public void onCreate(SQLiteDatabase db) {
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(Constants.DROP_MUSICS);
db.execSQL(Constants.DROP_PLAYLIST);
onCreate(db);
public void onUpgrade(SQLiteDatabase db, int prev, int current) {
if (prev < current) {

migrateMusics(db);

migratePlaylists(db);

}
}
private void migrateMusics(SQLiteDatabase db) {

try (OldMusicCacheDb old = new OldMusicCacheDb(AndroidUtils.getGlobalContext())) {
MusicCacheDb musics = new MusicCacheDb(this);

List<MusicTrack> tracks = old.getAllTracks();

old.getWritableDatabase().execSQL(OldMusicCacheDb.Constants.DROP_QUERY);
db.execSQL(Constants.CREATE_TABLE_MUSICS);

tracks.forEach(musics::addTrack);


}

}
private void migratePlaylists(SQLiteDatabase db) {

try (OldPlaylistCacheDb old = new OldPlaylistCacheDb(AndroidUtils.getGlobalContext())) {


Map<Playlist, List<MusicTrack>> map = new HashMap<>();

old.getAllPlaylists().forEach(playlist -> {
map.put(playlist, old.getTracksInPlaylist(playlist.v1()));
});
old.getWritableDatabase().execSQL(OldMusicCacheDb.Constants.DROP_QUERY);

SqlPlaylists playlists = new SqlPlaylists(this);

db.execSQL(Constants.CREATE_TABLE_PLAYLISTS);
db.execSQL(Constants.CREATE_TABLE_PLAYLIST_TRACKS);

map.forEach((playlist, tracks) -> {

playlists.addPlaylist(playlist);

playlists.playlist(playlist.b, playlist.a).ifPresent(newPlaylist -> {

tracks.forEach(track -> newPlaylist.addTrack(track.y1()));
});
});

}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public Optional<IPlaylist> playlist(int ownerId, int id) {
Integer.toString(ownerId),
Integer.toString(id)
},
null, null, null)) {
null, null, null, "1")) {
return cur.moveToFirst()
? Optional.of(
new SqlPlaylist(
Expand Down Expand Up @@ -133,8 +133,7 @@ public boolean isEmpty() {
}



public static JSONObject generatePhotoJSON(Playlist playlist) {
private static JSONObject generatePhotoJSON(Playlist playlist) {
if (PlaylistUtils.getThumb(playlist) != null) {
JSONObject photo = new JSONObject();
String playlistId = playlist.v1();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package ru.vtosters.lite.music.cache.db.old;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;

import com.vk.dto.music.AlbumLink;
import com.vk.dto.music.MusicTrack;
import com.vk.dto.music.Thumb;

import org.json.JSONObject;

import java.io.File;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import ru.vtosters.lite.utils.music.MusicCacheStorageUtils;

/**
* <p class="note"><strong>Note:</strong> the {@link AutoCloseable} interface was
* first added in the {@link android.os.Build.VERSION_CODES#Q} release for the {@link SQLiteOpenHelper} </p>
*/
@SuppressWarnings("forRemoval")
public class OldMusicCacheDb extends SQLiteOpenHelper {
public OldMusicCacheDb(Context context) {
super(context, Constants.DB_NAME, null, Constants.DV_VERSION);
}

private static MusicTrack fromCursor(Cursor cur) {
try {
int trackIdIndex = cur.getColumnIndex(Constants.COLUMN_TRACK_ID);
int titleIndex = cur.getColumnIndex(Constants.COLUMN_TITLE);
int subtitleIndex = cur.getColumnIndex(Constants.COLUMN_SUBTITLE);
int artistIndex = cur.getColumnIndex(Constants.COLUMN_ARTIST);
int durationIndex = cur.getColumnIndex(Constants.COLUMN_DURATION);
int explicitIndex = cur.getColumnIndex(Constants.COLUMN_EXPLICIT);
int albumIdIndex = cur.getColumnIndex(Constants.COLUMN_ALBUM_ID);
int albumTitleIndex = cur.getColumnIndex(Constants.COLUMN_ALBUM_TITLE);

if (trackIdIndex == -1 || titleIndex == -1 || subtitleIndex == -1 || artistIndex == -1 || durationIndex == -1 || explicitIndex == -1 || albumIdIndex == -1) {
return null;
}

String trackId = cur.getString(trackIdIndex);
String[] splits = trackId.split("_");

if (splits.length != 2) {
return null;
}

MusicTrack res = new MusicTrack();
res.J = Integer.MAX_VALUE + ""; //access_key
res.D = "https://vtosters.app"; //url
res.d = Integer.parseInt(splits[1]); //id
res.e = Integer.parseInt(splits[0]); //owner_id
res.f = URLDecoder.decode(cur.getString(titleIndex), "UTF-8"); //title
res.g = URLDecoder.decode(cur.getString(subtitleIndex), "UTF-8"); //subtitle
res.C = URLDecoder.decode(cur.getString(artistIndex), "UTF-8"); //artist
res.h = cur.getInt(durationIndex); //duration
res.K = cur.getInt(explicitIndex) > 0; //is_explicit

int albumId = Integer.parseInt(cur.getString(albumIdIndex));
String albumTitle = albumTitleIndex != -1 ? cur.getString(albumTitleIndex) : null;

JSONObject node = new JSONObject();

File f300 = MusicCacheStorageUtils.getTrackThumb(trackId, 300);
File f600 = MusicCacheStorageUtils.getTrackThumb(trackId, 600);
File f1200 = MusicCacheStorageUtils.getTrackThumb(trackId, 1200);

if (f300.exists()) node.putOpt("photo_300", Uri.fromFile(f300).toString());
if (f600.exists()) node.putOpt("photo_600", Uri.fromFile(f600).toString());
if (f1200.exists()) node.putOpt("photo_1200", Uri.fromFile(f1200).toString());

res.I = new AlbumLink(
albumId,
res.e,
res.J,
albumTitle,
node.length() > 0
? new Thumb(node.put("width", 600)
.put("height", 600))
: null
); //album

return res;
} catch (Throwable ignored) {
// ignored
}
return null;
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(Constants.CREATE_QUERY);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}


public List<MusicTrack> getAllTracks() {
return getTracksWithCursor(getReadableDatabase().query(Constants.TABLE_NAME, null, null, null, null, null, null));
}

public List<MusicTrack> getPlaylist() {
return getTracksWithCursor(getWritableDatabase().query(
Constants.TABLE_NAME,
null,
Constants.COLUMN_ALBUM_ID + " > -1",
null,
Constants.COLUMN_ALBUM_ID,
"count(*) > 1",
Constants.COLUMN_ID + " desc"));
}

private List<MusicTrack> getTracksWithCursor(Cursor cur) {
List<MusicTrack> res = new ArrayList<>();
if (cur != null && cur.moveToFirst()) {
try (cur) {
do {
MusicTrack track = fromCursor(cur);
if (track != null) res.add(track);
} while (cur.moveToNext());
}
}
return res;
}

@Retention(RetentionPolicy.SOURCE)
public @interface Constants {
int DV_VERSION = 0x3;
String DB_NAME = "vt_lite_cache.db";
String TABLE_NAME = "tracks";
//columns
String COLUMN_ID = "id";
String COLUMN_TRACK_ID = "trackId";
String COLUMN_ALBUM_ID = "albumId";
String COLUMN_TITLE = "title";
String COLUMN_SUBTITLE = "subtitle";
String COLUMN_ARTIST = "artist";
String COLUMN_ALBUM_TITLE = "albumTitle";
String COLUMN_EXPLICIT = "explicit";
String COLUMN_DURATION = "duration";
String COLUMN_HAS_ARTWORK = "hasArtwork";
//queries
String CREATE_QUERY = "create table " + TABLE_NAME + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY autoincrement,"
+ COLUMN_TRACK_ID + " text not null,"
+ COLUMN_ALBUM_ID + " text not null,"
+ COLUMN_TITLE + " text not null,"
+ COLUMN_SUBTITLE + " text not null,"
+ COLUMN_ARTIST + " text not null,"
+ COLUMN_ALBUM_TITLE + " text not null,"
+ COLUMN_EXPLICIT + " int not null,"
+ COLUMN_DURATION + " int not null,"
+ COLUMN_HAS_ARTWORK + " int not null"
+ ")";
String DROP_QUERY = "drop table if exists " + TABLE_NAME;
}
}
Loading

0 comments on commit 8e53d01

Please sign in to comment.