Move code to get highest message UID from K9BackendFolder to MessageStore

This commit is contained in:
cketti 2021-04-07 20:04:48 +02:00
parent 2e6b5710be
commit 102fbc690f
5 changed files with 52 additions and 11 deletions

View file

@ -18,34 +18,30 @@ class K9BackendFolder(
) : BackendFolder {
private val database = localStore.database
private val databaseId: String
private val folderId: Long
private val localFolder = localStore.getFolder(folderServerId)
override val name: String
override val visibleLimit: Int
init {
data class Init(val databaseId: String, val name: String, val visibleLimit: Int)
data class Init(val folderId: Long, val name: String, val visibleLimit: Int)
val init = messageStore.getFolder(folderServerId) { folder ->
Init(
databaseId = folder.id.toString(),
folderId = folder.id,
name = folder.name,
visibleLimit = folder.visibleLimit
)
} ?: error("Couldn't find folder $folderServerId")
databaseId = init.databaseId
databaseId = init.folderId.toString()
folderId = init.folderId
name = init.name
visibleLimit = init.visibleLimit
}
override fun getLastUid(): Long? {
return database.rawQuery("SELECT MAX(uid) FROM messages WHERE folder_id = ?", databaseId) { cursor ->
if (cursor.moveToFirst()) {
cursor.getLongOrNull(0)
} else {
null
}
}
return messageStore.getLastUid(folderId)
}
override fun getMessageServerIds(): Set<String> {

View file

@ -56,6 +56,11 @@ interface MessageStore {
*/
fun getHeaders(folderId: Long, messageServerId: String): List<Header>
/**
* Get highest UID (message server ID)
*/
fun getLastUid(folderId: Long): Long?
/**
* Create folders.
*/

View file

@ -53,6 +53,10 @@ class K9MessageStore(
return retrieveMessageOperations.getHeaders(folderId, messageServerId)
}
override fun getLastUid(folderId: Long): Long? {
return retrieveMessageOperations.getLastUid(folderId)
}
override fun createFolders(folders: List<CreateFolderInfo>) {
createFolderOperations.createFolders(folders)
}

View file

@ -1,5 +1,6 @@
package com.fsck.k9.storage.messages
import androidx.core.database.getLongOrNull
import com.fsck.k9.mail.Header
import com.fsck.k9.mail.MessagingException
import com.fsck.k9.mail.internet.MimeHeader
@ -52,7 +53,6 @@ internal class RetrieveMessageOperations(private val lockableDatabase: LockableD
databaseIdToServerIdMapping[databaseId] = serverId
}
}
Unit
}
databaseIdToServerIdMapping
@ -80,4 +80,19 @@ internal class RetrieveMessageOperations(private val lockableDatabase: LockableD
}
}
}
fun getLastUid(folderId: Long): Long? {
return lockableDatabase.execute(false) { database ->
database.rawQuery(
"SELECT MAX(uid) FROM messages WHERE folder_id = ?",
arrayOf(folderId.toString())
).use { cursor ->
if (cursor.moveToFirst()) {
cursor.getLongOrNull(0)
} else {
null
}
}
}
}
}

View file

@ -73,4 +73,25 @@ class RetrieveMessageOperationsTest : RobolectricTest() {
)
)
}
@Test
fun `get highest message uid`() {
val folderId = sqliteDatabase.createFolder()
sqliteDatabase.createMessage(uid = "42", folderId = folderId)
sqliteDatabase.createMessage(uid = "23", folderId = folderId)
sqliteDatabase.createMessage(uid = "27", folderId = folderId)
val highestUid = retrieveMessageOperations.getLastUid(folderId)
assertThat(highestUid).isEqualTo(42)
}
@Test
fun `get highest message uid should return null if there are no messages`() {
val folderId = sqliteDatabase.createFolder()
val highestUid = retrieveMessageOperations.getLastUid(folderId)
assertThat(highestUid).isNull()
}
}