Skip to content

Commit

Permalink
Make OffsetQueryPagingSource only work with Int
Browse files Browse the repository at this point in the history
  • Loading branch information
veyndan committed Jul 19, 2022
1 parent 9c13481 commit 8f4a014
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
Expand Up @@ -22,26 +22,26 @@ import kotlinx.coroutines.withContext
import kotlin.coroutines.CoroutineContext

internal class OffsetQueryPagingSource<RowType : Any>(
private val queryProvider: (limit: Long, offset: Long) -> Query<RowType>,
private val countQuery: Query<Long>,
private val queryProvider: (limit: Int, offset: Int) -> Query<RowType>,
private val countQuery: Query<Int>,
private val transacter: Transacter,
private val context: CoroutineContext,
) : QueryPagingSource<Long, RowType>() {
) : QueryPagingSource<Int, RowType>() {

override val jumpingSupported get() = true

override suspend fun load(
params: LoadParams<Long>,
): LoadResult<Long, RowType> = withContext(context) {
params: LoadParams<Int>,
): LoadResult<Int, RowType> = withContext(context) {
try {
val key = params.key ?: 0L
val key = params.key ?: 0
transacter.transactionWithResult {
val count = countQuery.executeAsOne()
if (count != 0L && key >= count) throw IndexOutOfBoundsException()
if (count != 0 && key >= count) throw IndexOutOfBoundsException()

val loadSize = if (key < 0) params.loadSize + key else params.loadSize

val data = queryProvider(loadSize.toLong(), maxOf(0, key))
val data = queryProvider(loadSize, maxOf(0, key))
.also { currentQuery = it }
.executeAsList()

Expand All @@ -51,8 +51,8 @@ internal class OffsetQueryPagingSource<RowType : Any>(
// misaligned prepend queries to avoid duplicates.
prevKey = if (key <= 0L) null else key - params.loadSize,
nextKey = if (key + params.loadSize >= count) null else key + params.loadSize,
itemsBefore = maxOf(0L, key).toInt(),
itemsAfter = maxOf(0, (count - (key + params.loadSize))).toInt(),
itemsBefore = maxOf(0, key),
itemsAfter = maxOf(0, (count - (key + params.loadSize))),
)
}
} catch (e: Exception) {
Expand All @@ -61,5 +61,5 @@ internal class OffsetQueryPagingSource<RowType : Any>(
}
}

override fun getRefreshKey(state: PagingState<Long, RowType>) = state.anchorPosition?.toLong()
override fun getRefreshKey(state: PagingState<Int, RowType>) = state.anchorPosition
}
Expand Up @@ -59,11 +59,11 @@ internal abstract class QueryPagingSource<Key : Any, RowType : Any> :
*/
@Suppress("FunctionName")
fun <RowType : Any> QueryPagingSource(
countQuery: Query<Long>,
countQuery: Query<Int>,
transacter: Transacter,
context: CoroutineContext = Dispatchers.IO,
queryProvider: (limit: Long, offset: Long) -> Query<RowType>,
): PagingSource<Long, RowType> = OffsetQueryPagingSource(
queryProvider: (limit: Int, offset: Int) -> Query<RowType>,
): PagingSource<Int, RowType> = OffsetQueryPagingSource(
queryProvider,
countQuery,
transacter,
Expand Down
Expand Up @@ -72,7 +72,7 @@ class OffsetQueryPagingSourceTest {
val results = runBlocking { source.load(Refresh(null, 2, false)) }

assertNull((results as LoadResult.Page).prevKey)
assertEquals(2L, results.nextKey)
assertEquals(2, results.nextKey)
}

@Test fun `aligned last page gives correct prevKey and nextKey`() {
Expand All @@ -85,7 +85,7 @@ class OffsetQueryPagingSourceTest {

val results = runBlocking { source.load(Refresh(8, 2, false)) }

assertEquals(6L, (results as LoadResult.Page).prevKey)
assertEquals(6, (results as LoadResult.Page).prevKey)
assertNull(results.nextKey)
}

Expand All @@ -98,8 +98,8 @@ class OffsetQueryPagingSourceTest {
)

runBlocking {
val expected = (0L until 10L).chunked(2).iterator()
var nextKey: Long? = null
val expected = (0 until 10).chunked(2).iterator()
var nextKey: Int? = null
do {
val results = source.load(Refresh(nextKey, 2, false))
assertEquals(expected.next(), (results as LoadResult.Page).data)
Expand All @@ -118,7 +118,7 @@ class OffsetQueryPagingSourceTest {

val results = runBlocking { source.load(Refresh(9, 2, false)) }

assertEquals(7L, (results as LoadResult.Page).prevKey)
assertEquals(7, (results as LoadResult.Page).prevKey)
assertNull(results.nextKey)
}

Expand All @@ -130,10 +130,10 @@ class OffsetQueryPagingSourceTest {
EmptyCoroutineContext,
)

val results = runBlocking { source.load(Refresh(1L, 2, false)) }
val results = runBlocking { source.load(Refresh(1, 2, false)) }

assertEquals(-1L, (results as LoadResult.Page).prevKey)
assertEquals(3L, results.nextKey)
assertEquals(-1, (results as LoadResult.Page).prevKey)
assertEquals(3, results.nextKey)
}

@Test fun `initial page has correct itemsBefore and itemsAfter`() {
Expand Down Expand Up @@ -215,8 +215,8 @@ class OffsetQueryPagingSourceTest {
)

runBlocking {
val expected = listOf(listOf(1L, 2L), listOf(0L)).iterator()
var prevKey: Long? = 1L
val expected = listOf(listOf(1, 2), listOf(0)).iterator()
var prevKey: Int? = 1
do {
val results = source.load(Refresh(prevKey, 2, false))
assertEquals(expected.next(), (results as LoadResult.Page).data)
Expand Down Expand Up @@ -256,12 +256,12 @@ class OffsetQueryPagingSourceTest {
assertTrue(source.invalid)
}

private fun query(limit: Long, offset: Long) = object : Query<Long>(
{ cursor -> cursor.getLong(0)!! },
private fun query(limit: Int, offset: Int) = object : Query<Int>(
{ cursor -> cursor.getLong(0)!!.toInt() },
) {
override fun <R> execute(mapper: (SqlCursor) -> R) = driver.executeQuery(1, "SELECT value FROM testTable LIMIT ? OFFSET ?", mapper, 2) {
bindLong(0, limit)
bindLong(1, offset)
bindLong(0, limit.toLong())
bindLong(1, offset.toLong())
}

override fun addListener(listener: Listener) = driver.addListener(listener, arrayOf("testTable"))
Expand All @@ -275,7 +275,7 @@ class OffsetQueryPagingSourceTest {
"Test.sq",
"count",
"SELECT count(*) FROM testTable",
{ it.getLong(0)!! },
{ it.getLong(0)!!.toInt() },
)

private fun insert(value: Long, db: SqlDriver = driver) {
Expand Down

0 comments on commit 8f4a014

Please sign in to comment.