From 891fb0b4794adc233273ce2286012decd646523f Mon Sep 17 00:00:00 2001 From: Syer10 Date: Sat, 8 Apr 2023 15:27:18 -0400 Subject: [PATCH] Simplify keyset pagination --- .../graphql/queries/CategoryQuery.kt | 29 +++---------- .../tachidesk/graphql/queries/MangaQuery.kt | 41 ++++--------------- .../graphql/server/primitives/OrderBy.kt | 18 ++++++++ .../tachidesk/graphql/types/MangaType.kt | 4 +- 4 files changed, 34 insertions(+), 58 deletions(-) diff --git a/server/src/main/kotlin/suwayomi/tachidesk/graphql/queries/CategoryQuery.kt b/server/src/main/kotlin/suwayomi/tachidesk/graphql/queries/CategoryQuery.kt index 3fcb6057..94d5cfd7 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/graphql/queries/CategoryQuery.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/graphql/queries/CategoryQuery.kt @@ -12,12 +12,9 @@ import graphql.schema.DataFetchingEnvironment import org.jetbrains.exposed.sql.Column import org.jetbrains.exposed.sql.Op import org.jetbrains.exposed.sql.SortOrder -import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.SqlExpressionBuilder.greater import org.jetbrains.exposed.sql.SqlExpressionBuilder.less -import org.jetbrains.exposed.sql.and import org.jetbrains.exposed.sql.andWhere -import org.jetbrains.exposed.sql.or import org.jetbrains.exposed.sql.selectAll import org.jetbrains.exposed.sql.transactions.transaction import suwayomi.tachidesk.graphql.queries.filter.BooleanFilter @@ -34,6 +31,8 @@ import suwayomi.tachidesk.graphql.server.primitives.Cursor import suwayomi.tachidesk.graphql.server.primitives.OrderBy import suwayomi.tachidesk.graphql.server.primitives.PageInfo import suwayomi.tachidesk.graphql.server.primitives.QueryResults +import suwayomi.tachidesk.graphql.server.primitives.greaterNotUnique +import suwayomi.tachidesk.graphql.server.primitives.lessNotUnique import suwayomi.tachidesk.graphql.server.primitives.maybeSwap import suwayomi.tachidesk.graphql.types.CategoryNodeList import suwayomi.tachidesk.graphql.types.CategoryType @@ -65,32 +64,16 @@ class CategoryQuery { override fun greater(cursor: Cursor): Op { return when (this) { ID -> CategoryTable.id greater cursor.value.toInt() - NAME -> { - val id = cursor.value.substringBefore('-').toInt() - val value = cursor.value.substringAfter('-') - (CategoryTable.name greater value) or ((CategoryTable.name eq value) and (CategoryTable.id greater id)) - } - ORDER -> { - val id = cursor.value.substringBefore('-').toInt() - val value = cursor.value.substringAfter('-').toInt() - (CategoryTable.order greater value) or ((CategoryTable.order eq value) and (CategoryTable.id greater id)) - } + NAME -> greaterNotUnique(CategoryTable.name, CategoryTable.id, cursor, String::toString) + ORDER -> greaterNotUnique(CategoryTable.order, CategoryTable.id, cursor, String::toInt) } } override fun less(cursor: Cursor): Op { return when (this) { ID -> CategoryTable.id less cursor.value.toInt() - NAME -> { - val id = cursor.value.substringBefore('-').toInt() - val value = cursor.value.substringAfter('-') - (CategoryTable.name less value) or ((CategoryTable.name eq value) and (CategoryTable.id less id)) - } - ORDER -> { - val id = cursor.value.substringBefore('-').toInt() - val value = cursor.value.substringAfter('-').toInt() - (CategoryTable.order less value) or ((CategoryTable.order eq value) and (CategoryTable.id less id)) - } + NAME -> lessNotUnique(CategoryTable.name, CategoryTable.id, cursor, String::toString) + ORDER -> lessNotUnique(CategoryTable.order, CategoryTable.id, cursor, String::toInt) } } diff --git a/server/src/main/kotlin/suwayomi/tachidesk/graphql/queries/MangaQuery.kt b/server/src/main/kotlin/suwayomi/tachidesk/graphql/queries/MangaQuery.kt index 666936ee..95a6f48c 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/graphql/queries/MangaQuery.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/graphql/queries/MangaQuery.kt @@ -12,12 +12,9 @@ import graphql.schema.DataFetchingEnvironment import org.jetbrains.exposed.sql.Column import org.jetbrains.exposed.sql.Op import org.jetbrains.exposed.sql.SortOrder -import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.SqlExpressionBuilder.greater import org.jetbrains.exposed.sql.SqlExpressionBuilder.less -import org.jetbrains.exposed.sql.and import org.jetbrains.exposed.sql.andWhere -import org.jetbrains.exposed.sql.or import org.jetbrains.exposed.sql.select import org.jetbrains.exposed.sql.selectAll import org.jetbrains.exposed.sql.transactions.transaction @@ -37,6 +34,8 @@ import suwayomi.tachidesk.graphql.server.primitives.Cursor import suwayomi.tachidesk.graphql.server.primitives.OrderBy import suwayomi.tachidesk.graphql.server.primitives.PageInfo import suwayomi.tachidesk.graphql.server.primitives.QueryResults +import suwayomi.tachidesk.graphql.server.primitives.greaterNotUnique +import suwayomi.tachidesk.graphql.server.primitives.lessNotUnique import suwayomi.tachidesk.graphql.server.primitives.maybeSwap import suwayomi.tachidesk.graphql.types.MangaNodeList import suwayomi.tachidesk.graphql.types.MangaType @@ -73,42 +72,18 @@ class MangaQuery { override fun greater(cursor: Cursor): Op { return when (this) { ID -> MangaTable.id greater cursor.value.toInt() - TITLE -> { - val id = cursor.value.substringBefore('-').toInt() - val value = cursor.value.substringAfter('-') - (MangaTable.title greater value) or ((MangaTable.title eq value) and (MangaTable.id greater id)) - } - IN_LIBRARY_AT -> { - val id = cursor.value.substringBefore('-').toInt() - val value = cursor.value.substringAfter('-').toLong() - (MangaTable.inLibraryAt greater value) or ((MangaTable.inLibraryAt eq value) and (MangaTable.id greater id)) - } - LAST_FETCHED_AT -> { - val id = cursor.value.substringBefore('-').toInt() - val value = cursor.value.substringAfter('-').toLong() - (MangaTable.lastFetchedAt greater value) or ((MangaTable.lastFetchedAt eq value) and (MangaTable.id greater id)) - } + TITLE -> greaterNotUnique(MangaTable.title, MangaTable.id, cursor, String::toString) + IN_LIBRARY_AT -> greaterNotUnique(MangaTable.inLibraryAt, MangaTable.id, cursor, String::toLong) + LAST_FETCHED_AT -> greaterNotUnique(MangaTable.lastFetchedAt, MangaTable.id, cursor, String::toLong) } } override fun less(cursor: Cursor): Op { return when (this) { ID -> MangaTable.id less cursor.value.toInt() - TITLE -> { - val id = cursor.value.substringBefore('-').toInt() - val value = cursor.value.substringAfter('-') - (MangaTable.title less value) or ((MangaTable.title eq value) and (MangaTable.id less id)) - } - IN_LIBRARY_AT -> { - val id = cursor.value.substringBefore('-').toInt() - val value = cursor.value.substringAfter('-').toLong() - (MangaTable.inLibraryAt less value) or ((MangaTable.inLibraryAt eq value) and (MangaTable.id less id)) - } - LAST_FETCHED_AT -> { - val id = cursor.value.substringBefore('-').toInt() - val value = cursor.value.substringAfter('-').toLong() - (MangaTable.lastFetchedAt less value) or ((MangaTable.lastFetchedAt eq value) and (MangaTable.id less id)) - } + TITLE -> lessNotUnique(MangaTable.title, MangaTable.id, cursor, String::toString) + IN_LIBRARY_AT -> lessNotUnique(MangaTable.inLibraryAt, MangaTable.id, cursor, String::toLong) + LAST_FETCHED_AT -> lessNotUnique(MangaTable.lastFetchedAt, MangaTable.id, cursor, String::toLong) } } diff --git a/server/src/main/kotlin/suwayomi/tachidesk/graphql/server/primitives/OrderBy.kt b/server/src/main/kotlin/suwayomi/tachidesk/graphql/server/primitives/OrderBy.kt index 4f6733c4..0a7bb7bc 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/graphql/server/primitives/OrderBy.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/graphql/server/primitives/OrderBy.kt @@ -1,8 +1,14 @@ package suwayomi.tachidesk.graphql.server.primitives +import org.jetbrains.exposed.dao.id.EntityID import org.jetbrains.exposed.sql.Column import org.jetbrains.exposed.sql.Op import org.jetbrains.exposed.sql.SortOrder +import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq +import org.jetbrains.exposed.sql.SqlExpressionBuilder.greater +import org.jetbrains.exposed.sql.SqlExpressionBuilder.less +import org.jetbrains.exposed.sql.and +import org.jetbrains.exposed.sql.or interface OrderBy { val column: Column> @@ -29,3 +35,15 @@ fun SortOrder?.maybeSwap(value: Any?): SortOrder { this ?: SortOrder.ASC } } + +fun > greaterNotUnique(column: Column, idColumn: Column>, cursor: Cursor, toValue: (String) -> T): Op { + val id = cursor.value.substringBefore('-').toInt() + val value = toValue(cursor.value.substringAfter('-')) + return (column greater value) or ((column eq value) and (idColumn greater id)) +} + +fun > lessNotUnique(column: Column, idColumn: Column>, cursor: Cursor, toValue: (String) -> T): Op { + val id = cursor.value.substringBefore('-').toInt() + val value = toValue(cursor.value.substringAfter('-')) + return (column less value) or ((column eq value) and (idColumn less id)) +} \ No newline at end of file diff --git a/server/src/main/kotlin/suwayomi/tachidesk/graphql/types/MangaType.kt b/server/src/main/kotlin/suwayomi/tachidesk/graphql/types/MangaType.kt index 4abe5b27..979f0dfc 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/graphql/types/MangaType.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/graphql/types/MangaType.kt @@ -37,8 +37,8 @@ class MangaType( val inLibrary: Boolean, val inLibraryAt: Long, val realUrl: String?, - var lastFetchedAt: Long?, - var chaptersLastFetchedAt: Long? + var lastFetchedAt: Long?, //todo + var chaptersLastFetchedAt: Long? //todo ) : Node { constructor(row: ResultRow) : this( row[MangaTable.id].value,