Skip to content

Commit

Permalink
feat: experimentally allow client sort in EntitySequence.export
Browse files Browse the repository at this point in the history
  • Loading branch information
d1snin committed Nov 20, 2023
1 parent 4407d95 commit ede46fa
Showing 1 changed file with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,25 @@ public data class ExportedSequence<E : Entity<E>>(
*
* @see ExportedSequence
*/
public fun <E : Entity<E>, T : Table<E>> EntitySequence<E, T>.export(
public fun <E, T, S> EntitySequence<E, T>.export(
limit: Int = DEFAULT_LIMIT,
offset: Int = DEFAULT_OFFSET,
sort: ((T) -> OrderByExpression)? = null
): ExportedSequence<E> {
sort: ((T) -> OrderByExpression)? = null,
clientSort: ((E) -> S?)? = null
): ExportedSequence<E> where E : Entity<E>, T : Table<E>, S : Comparable<S> {
val totalCount = count()

val sortedElements = sort?.let { selector ->
sortedBy(selector)
} ?: this

val limitedElements = sortedElements.take(limit).toList().drop(offset)
val limitedElements = clientSort?.let {
sortedElements.toList().sortedBy(clientSort).take(limit)
} ?: sortedElements.take(limit).toList()

val elements = limitedElements.toMutableList()
val elementsWithoutOffset = limitedElements.drop(offset)

val elements = elementsWithoutOffset.toMutableList()

if (limit == 0) {
elements.clear()
Expand Down

0 comments on commit ede46fa

Please sign in to comment.