11package org.schabi.newpipe.database.stream.dao
22
3- import androidx.room.ColumnInfo
43import androidx.room.Dao
54import androidx.room.Insert
65import androidx.room.OnConflictStrategy
76import androidx.room.Query
87import androidx.room.Transaction
9- import io.reactivex.rxjava3.core.Completable
8+ import androidx.room.Update
109import io.reactivex.rxjava3.core.Flowable
11- import io.reactivex.rxjava3.core.Maybe
12- import org.schabi.newpipe.database.BasicDAO
10+ import kotlinx.coroutines.Dispatchers
11+ import kotlinx.coroutines.runBlocking
12+ import kotlinx.coroutines.rx3.rxMaybe
1313import org.schabi.newpipe.database.stream.model.StreamEntity
14- import org.schabi.newpipe.database.stream.model.StreamEntity.Companion.STREAM_ID
15- import org.schabi.newpipe.extractor.stream.StreamType
1614import org.schabi.newpipe.util.StreamTypeUtil
17- import java.time.OffsetDateTime
1815
1916@Dao
20- abstract class StreamDAO : BasicDAO <StreamEntity > {
21- @Query(" SELECT * FROM streams" )
22- abstract override fun getAll (): Flowable <List <StreamEntity >>
23-
24- @Query(" DELETE FROM streams" )
25- abstract override fun deleteAll (): Int
26-
27- @Query(" SELECT * FROM streams WHERE service_id = :serviceId" )
28- abstract override fun listByService (serviceId : Int ): Flowable <List <StreamEntity >>
29-
30- @Query(" SELECT * FROM streams WHERE url = :url AND service_id = :serviceId" )
31- abstract fun getStream (serviceId : Long , url : String ): Maybe <StreamEntity >
17+ interface StreamDAO {
18+ @Insert(onConflict = OnConflictStrategy .IGNORE )
19+ suspend fun insert (entity : StreamEntity ): Long
3220
33- @Query( " UPDATE streams SET uploader_url = :uploaderUrl WHERE url = :url AND service_id = :serviceId " )
34- abstract fun setUploaderUrl ( serviceId : Long , url : String , uploaderUrl : String ): Completable
21+ @Insert
22+ suspend fun insertAll ( entities : List < StreamEntity >)
3523
36- @Insert(onConflict = OnConflictStrategy . IGNORE )
37- internal abstract fun silentInsertInternal ( stream : StreamEntity ): Long
24+ @Update
25+ suspend fun update ( entity : StreamEntity )
3826
39- @Insert(onConflict = OnConflictStrategy . IGNORE )
40- internal abstract fun silentInsertAllInternal ( streams : List <StreamEntity >): List < Long >
27+ @Query( " SELECT * FROM streams " )
28+ fun getAll (): Flowable < List <StreamEntity >>
4129
42- @Query(" SELECT COUNT(*) != 0 FROM streams WHERE url = :url AND service_id = :serviceId" )
43- internal abstract fun exists (serviceId : Int , url : String ): Boolean
30+ @Query(" SELECT * FROM streams WHERE url = :url AND service_id = :serviceId" )
31+ suspend fun getStream (serviceId : Int , url : String ): StreamEntity ?
4432
45- @Query(
46- """
47- SELECT uid, stream_type, textual_upload_date, upload_date, is_upload_date_approximation, duration
48- FROM streams WHERE url = :url AND service_id = :serviceId
49- """
50- )
51- internal abstract fun getMinimalStreamForCompare (serviceId : Int , url : String ): StreamCompareFeed ?
33+ fun getStreamAsMaybe (serviceId : Int , url : String ) = rxMaybe(Dispatchers .IO ) { getStream(serviceId, url) }
5234
5335 @Transaction
54- open fun upsert (newerStream : StreamEntity ): Long {
55- val uid = silentInsertInternal (newerStream)
36+ suspend fun upsert (newerStream : StreamEntity ): Long {
37+ val uid = insert (newerStream)
5638
5739 if (uid != - 1L ) {
5840 newerStream.uid = uid
@@ -65,46 +47,31 @@ abstract class StreamDAO : BasicDAO<StreamEntity> {
6547 return newerStream.uid
6648 }
6749
50+ fun upsertBlocking (entity : StreamEntity ) = runBlocking(Dispatchers .IO ) { upsert(entity) }
51+
6852 @Transaction
69- open fun upsertAll (streams : List <StreamEntity >): List <Long > {
70- val insertUidList = silentInsertAllInternal(streams)
71-
72- val streamIds = ArrayList <Long >(streams.size)
73- for ((index, uid) in insertUidList.withIndex()) {
74- val newerStream = streams[index]
75- if (uid != - 1L ) {
76- streamIds.add(uid)
77- newerStream.uid = uid
78- continue
79- }
53+ suspend fun upsertAll (streams : List <StreamEntity >) = streams.map { upsert(it) }
8054
81- compareAndUpdateStream(newerStream)
82- streamIds.add(newerStream.uid)
83- }
55+ fun upsertAllBlocking (streams : List <StreamEntity >) = runBlocking(Dispatchers .IO ) { upsertAll(streams) }
8456
85- update(streams)
86- return streamIds
87- }
88-
89- private fun compareAndUpdateStream (newerStream : StreamEntity ) {
90- val existentMinimalStream = getMinimalStreamForCompare(newerStream.serviceId, newerStream.url)
57+ private suspend fun compareAndUpdateStream (newerStream : StreamEntity ) {
58+ val existingStream = getStream(newerStream.serviceId, newerStream.url)
9159 ? : throw IllegalStateException (" Stream cannot be null just after insertion." )
92- newerStream.uid = existentMinimalStream .uid
60+ newerStream.uid = existingStream .uid
9361
9462 if (! StreamTypeUtil .isLiveStream(newerStream.streamType)) {
95-
9663 // Use the existent upload date if the newer stream does not have a better precision
9764 // (i.e. is an approximation). This is done to prevent unnecessary changes.
9865 val hasBetterPrecision =
9966 newerStream.uploadDate != null && newerStream.isUploadDateApproximation != true
100- if (existentMinimalStream .uploadDate != null && ! hasBetterPrecision) {
101- newerStream.uploadDate = existentMinimalStream .uploadDate
102- newerStream.textualUploadDate = existentMinimalStream .textualUploadDate
103- newerStream.isUploadDateApproximation = existentMinimalStream .isUploadDateApproximation
67+ if (existingStream .uploadDate != null && ! hasBetterPrecision) {
68+ newerStream.uploadDate = existingStream .uploadDate
69+ newerStream.textualUploadDate = existingStream .textualUploadDate
70+ newerStream.isUploadDateApproximation = existingStream .isUploadDateApproximation
10471 }
10572
106- if (existentMinimalStream .duration > 0 && newerStream.duration < 0 ) {
107- newerStream.duration = existentMinimalStream .duration
73+ if (existingStream .duration > 0 && newerStream.duration < 0 ) {
74+ newerStream.duration = existingStream .duration
10875 }
10976 }
11077 }
@@ -123,28 +90,5 @@ abstract class StreamDAO : BasicDAO<StreamEntity> {
12390 WHERE f.stream_id = streams.uid)
12491 """
12592 )
126- abstract fun deleteOrphans (): Int
127-
128- /* *
129- * Minimal entry class used when comparing/updating an existent stream.
130- */
131- internal data class StreamCompareFeed (
132- @ColumnInfo(name = STREAM_ID )
133- var uid : Long = 0 ,
134-
135- @ColumnInfo(name = StreamEntity .STREAM_TYPE )
136- var streamType : StreamType ,
137-
138- @ColumnInfo(name = StreamEntity .STREAM_TEXTUAL_UPLOAD_DATE )
139- var textualUploadDate : String? = null ,
140-
141- @ColumnInfo(name = StreamEntity .STREAM_UPLOAD_DATE )
142- var uploadDate : OffsetDateTime ? = null ,
143-
144- @ColumnInfo(name = StreamEntity .STREAM_IS_UPLOAD_DATE_APPROXIMATION )
145- var isUploadDateApproximation : Boolean? = null ,
146-
147- @ColumnInfo(name = StreamEntity .STREAM_DURATION )
148- var duration : Long
149- )
93+ fun deleteOrphans (): Int
15094}
0 commit comments