simplify timeline cleanup (#1932)

* simplify timeline cleanup

* fix test
This commit is contained in:
Konrad Pozniak 2020-09-14 07:52:54 +02:00 committed by Alibek Omarov
parent cfd99ed216
commit e2d2d89afb
5 changed files with 26 additions and 35 deletions

View File

@ -196,7 +196,7 @@ dependencies {
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0" testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"
androidTestImplementation "androidx.test.espresso:espresso-core:3.2.0" androidTestImplementation "androidx.test.espresso:espresso-core:3.2.0"
androidTestImplementation "android.arch.persistence.room:testing:1.1.1" androidTestImplementation "androidx.room:room-testing:$roomVersion"
androidTestImplementation "androidx.test.ext:junit:1.1.1" androidTestImplementation "androidx.test.ext:junit:1.1.1"
debugImplementation "im.dino:dbinspector:4.0.0@aar" debugImplementation "im.dino:dbinspector:4.0.0@aar"

View File

@ -78,46 +78,39 @@ class TimelineDAOTest {
fun cleanup() { fun cleanup() {
val now = System.currentTimeMillis() val now = System.currentTimeMillis()
val oldDate = now - TimelineRepository.CLEANUP_INTERVAL - 20_000 val oldDate = now - TimelineRepository.CLEANUP_INTERVAL - 20_000
val oldByThisAccount = makeStatus( val oldThisAccount = makeStatus(
statusId = 5, statusId = 5,
createdAt = oldDate createdAt = oldDate
) )
val oldByAnotherAccount = makeStatus( val oldAnotherAccount = makeStatus(
statusId = 10, statusId = 10,
createdAt = oldDate, createdAt = oldDate,
authorServerId = "100" accountId = 2
) )
val oldForAnotherAccount = makeStatus( val recentThisAccount = makeStatus(
accountId = 2,
statusId = 20,
authorServerId = "200",
createdAt = oldDate
)
val recentByThisAccount = makeStatus(
statusId = 30, statusId = 30,
createdAt = System.currentTimeMillis() createdAt = System.currentTimeMillis()
) )
val recentByAnotherAccount = makeStatus( val recentAnotherAccount = makeStatus(
statusId = 60, statusId = 60,
createdAt = System.currentTimeMillis(), createdAt = System.currentTimeMillis(),
authorServerId = "200" accountId = 2
) )
for ((status, author, reblogAuthor) in listOf(oldByThisAccount, oldByAnotherAccount, for ((status, author, reblogAuthor) in listOf(oldThisAccount, oldAnotherAccount, recentThisAccount, recentAnotherAccount)) {
oldForAnotherAccount, recentByThisAccount, recentByAnotherAccount)) {
timelineDao.insertInTransaction(status, author, reblogAuthor) timelineDao.insertInTransaction(status, author, reblogAuthor)
} }
timelineDao.cleanup(1, "20", now - TimelineRepository.CLEANUP_INTERVAL) timelineDao.cleanup(now - TimelineRepository.CLEANUP_INTERVAL)
assertEquals( assertEquals(
listOf(recentByAnotherAccount, recentByThisAccount, oldByThisAccount), listOf(recentThisAccount),
timelineDao.getStatusesForAccount(1, null, null, 100).blockingGet() timelineDao.getStatusesForAccount(1, null, null, 100).blockingGet()
.map { it.toTriple() } .map { it.toTriple() }
) )
assertEquals( assertEquals(
listOf(oldForAnotherAccount), listOf(recentAnotherAccount),
timelineDao.getStatusesForAccount(2, null, null, 100).blockingGet() timelineDao.getStatusesForAccount(2, null, null, 100).blockingGet()
.map { it.toTriple() } .map { it.toTriple() }
) )
@ -217,7 +210,8 @@ class TimelineDAOTest {
application = "application$accountId", application = "application$accountId",
reblogServerId = if (reblog) (statusId * 100).toString() else null, reblogServerId = if (reblog) (statusId * 100).toString() else null,
reblogAccountId = reblogAuthor?.serverId, reblogAccountId = reblogAuthor?.serverId,
poll = null poll = null,
muted = false
) )
return Triple(status, author, reblogAuthor) return Triple(status, author, reblogAuthor)
} }
@ -246,7 +240,8 @@ class TimelineDAOTest {
application = null, application = null,
reblogServerId = null, reblogServerId = null,
reblogAccountId = null, reblogAccountId = null,
poll = null poll = null,
muted = false
) )
} }

View File

@ -98,9 +98,8 @@ WHERE timelineUserId = :accountId AND (serverId = :statusId OR reblogServerId =
AND serverId = :statusId""") AND serverId = :statusId""")
abstract fun delete(accountId: Long, statusId: String) abstract fun delete(accountId: Long, statusId: String)
@Query("""DELETE FROM TimelineStatusEntity WHERE timelineUserId = :accountId @Query("""DELETE FROM TimelineStatusEntity WHERE createdAt < :olderThan""")
AND authorServerId != :accountServerId AND createdAt < :olderThan""") abstract fun cleanup(olderThan: Long)
abstract fun cleanup(accountId: Long, accountServerId: String, olderThan: Long)
@Query("""UPDATE TimelineStatusEntity SET poll = :poll @Query("""UPDATE TimelineStatusEntity SET poll = :poll
WHERE timelineUserId = :accountId AND (serverId = :statusId OR reblogServerId = :statusId)""") WHERE timelineUserId = :accountId AND (serverId = :statusId OR reblogServerId = :statusId)""")

View File

@ -1,6 +1,5 @@
package com.keylesspalace.tusky.repository package com.keylesspalace.tusky.repository
import android.text.Spanned
import android.text.SpannedString import android.text.SpannedString
import androidx.core.text.parseAsHtml import androidx.core.text.parseAsHtml
import androidx.core.text.toHtml import androidx.core.text.toHtml
@ -185,14 +184,10 @@ class TimelineRepositoryImpl(
} }
private fun cleanup() { private fun cleanup() {
Single.fromCallable { Schedulers.io().scheduleDirect {
val olderThan = System.currentTimeMillis() - TimelineRepository.CLEANUP_INTERVAL val olderThan = System.currentTimeMillis() - TimelineRepository.CLEANUP_INTERVAL
for (account in accountManager.getAllAccountsOrderedByActive()) { timelineDao.cleanup(olderThan)
timelineDao.cleanup(account.id, account.accountId, olderThan)
}
} }
.subscribeOn(Schedulers.io())
.subscribe()
} }
private fun TimelineStatusWithAccount.toStatus(): TimelineStatus { private fun TimelineStatusWithAccount.toStatus(): TimelineStatus {

View File

@ -1,10 +1,8 @@
package com.keylesspalace.tusky.fragment package com.keylesspalace.tusky.fragment
import android.text.SpannableString import android.text.SpannableString
import android.text.SpannedString
import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.gson.Gson import com.google.gson.Gson
import com.keylesspalace.tusky.SpanUtilsTest
import com.keylesspalace.tusky.db.AccountEntity import com.keylesspalace.tusky.db.AccountEntity
import com.keylesspalace.tusky.db.AccountManager import com.keylesspalace.tusky.db.AccountManager
import com.keylesspalace.tusky.db.TimelineDao import com.keylesspalace.tusky.db.TimelineDao
@ -26,8 +24,7 @@ import org.junit.Assert.assertEquals
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.any import org.mockito.ArgumentMatchers.*
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.Mock import org.mockito.Mock
import org.mockito.MockitoAnnotations import org.mockito.MockitoAnnotations
import org.robolectric.annotation.Config import org.robolectric.annotation.Config
@ -97,6 +94,7 @@ class TimelineRepositoryTest {
null null
) )
} }
verify(timelineDao).cleanup(anyLong())
verifyNoMoreInteractions(timelineDao) verifyNoMoreInteractions(timelineDao)
} }
@ -131,6 +129,7 @@ class TimelineRepositoryTest {
} }
verify(timelineDao).removeAllPlaceholdersBetween(account.id, response.first().id, verify(timelineDao).removeAllPlaceholdersBetween(account.id, response.first().id,
response.last().id) response.last().id)
verify(timelineDao).cleanup(anyLong())
verifyNoMoreInteractions(timelineDao) verifyNoMoreInteractions(timelineDao)
} }
@ -160,6 +159,7 @@ class TimelineRepositoryTest {
) )
} }
verify(timelineDao).insertStatusIfNotThere(placeholder.toEntity(account.id)) verify(timelineDao).insertStatusIfNotThere(placeholder.toEntity(account.id))
verify(timelineDao).cleanup(anyLong())
verifyNoMoreInteractions(timelineDao) verifyNoMoreInteractions(timelineDao)
} }
@ -203,6 +203,7 @@ class TimelineRepositoryTest {
} }
verify(timelineDao).removeAllPlaceholdersBetween(account.id, response.first().id, verify(timelineDao).removeAllPlaceholdersBetween(account.id, response.first().id,
response.last().id) response.last().id)
verify(timelineDao).cleanup(anyLong())
verifyNoMoreInteractions(timelineDao) verifyNoMoreInteractions(timelineDao)
} }
@ -249,6 +250,7 @@ class TimelineRepositoryTest {
verify(timelineDao).removeAllPlaceholdersBetween(account.id, response.first().id, verify(timelineDao).removeAllPlaceholdersBetween(account.id, response.first().id,
response.last().id) response.last().id)
verify(timelineDao).insertStatusIfNotThere(placeholder.toEntity(account.id)) verify(timelineDao).insertStatusIfNotThere(placeholder.toEntity(account.id))
verify(timelineDao).cleanup(anyLong())
verifyNoMoreInteractions(timelineDao) verifyNoMoreInteractions(timelineDao)
} }