Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image class and DB #3190

Closed
wb9688 opened this issue Mar 5, 2020 · 2 comments · Fixed by #10062
Closed

Image class and DB #3190

wb9688 opened this issue Mar 5, 2020 · 2 comments · Fixed by #10062
Labels
database Issue is related to database operations feature request Issue is related to a feature in the app

Comments

@wb9688
Copy link
Contributor

wb9688 commented Mar 5, 2020

@TobiGr @mauriciocolli @Stypox: I was working on having NewPipeExtractor return all possible images together with their resolution (if known) in TeamNewPipe/NewPipeExtractor#268. This will (obviously) need some changes in NewPipe. Currently NewPipe just saves the URL of the images in the DB. With that PR, there won't just be one URL, so we'll need a separate table for the images, so we could store multiple images (together with their resolutions) in the DB. As I'm not familiar with Room, I was wondering whether one of you could work on it. I also know that the DB will change a bit in #2309.

@Stypox
Copy link
Member

Stypox commented Mar 5, 2020

I never used databases, and I would like to learn. If nobody else takes this up I would be willing to, but it would be my first real experience with databases (and I would be looking forward to it ;-) )

@Stypox Stypox added the feature request Issue is related to a feature in the app label Mar 8, 2020
@Stypox Stypox self-assigned this Mar 8, 2020
@Stypox
Copy link
Member

Stypox commented Mar 14, 2020

I give up. I tried for a week to implement this the correct way, but there are just too many things to consider that I don't know enough about. I was able to create the migration code (it was rather simple since I more-or-less know the basics of databases), but I wasn't able to come up with consistent and clear-enough ways of accessing data via Room.
I tried to copy some code over from the classes that handle the "playlist_stream_join" table, but the design is different there since it is normal to ask for playlists without their streams, while it does not make sense to ask for a stream without the thumbnails.
Also, I saw indexes are used to improve performance, but I have no idea how they should be created and how to tell Room I want it to use them.

This is the migration code I came up with. It works (tested on an exported database), but does not create indexes.

// create new tables: images and stream_image_join
database.execSQL("CREATE TABLE IF NOT EXISTS images " +
        "(uid INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, url TEXT UNIQUE NOT NULL, " +
        "width INTEGER NOT NULL, height INTEGER NOT NULL)");

database.execSQL("CREATE TABLE IF NOT EXISTS stream_image_join " +
        "(image_id INTEGER PRIMARY KEY NOT NULL, stream_id INTEGER NOT NULL, " +
        "FOREIGN KEY(stream_id) REFERENCES streams(uid) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED, " +
        "FOREIGN KEY(image_id) REFERENCES images(uid) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED)");


// move stream thumbnail urls into images and link those with stream_image_join
database.execSQL("INSERT OR IGNORE INTO images (url, width, height) " +
        "SELECT thumbnail_url, -1, -1 FROM streams");

database.execSQL("INSERT INTO stream_image_join (stream_id, image_id) " +
        "SELECT streams.uid, images.uid " +
        "FROM streams INNER JOIN images " +
        "ON streams.thumbnail_url == images.thumbnail_url");


// drop column thumbnail_url in table streams
database.execSQL("CREATE TABLE streams_temp " +
        "(uid INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, service_id INTEGER NOT NULL, " +
        "url TEXT, title TEXT, stream_type TEXT, duration INTEGER, uploader TEXT)");

database.execSQL("INSERT INTO streams_temp (uid, service_id, url, title, stream_type, duration, uploader) " +
        "SELECT uid, service_id, url, title, stream_type, duration, uploader FROM streams");

database.execSQL("DROP TABLE streams");
database.execSQL("ALTER TABLE streams_temp RENAME TO streams");

@Stypox Stypox removed their assignment Jul 20, 2021
@SameenAhnaf SameenAhnaf added the database Issue is related to database operations label Feb 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
database Issue is related to database operations feature request Issue is related to a feature in the app
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants