Skip to content

Commit f270b15

Browse files
authored
Merge pull request #259 from modelix/MODELIX-551
MODELIX-551 Support pagination
2 parents f6444ce + c371cca commit f270b15

File tree

4 files changed

+115
-1
lines changed

4 files changed

+115
-1
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package org.modelix.modelql.core
15+
16+
import kotlinx.coroutines.flow.drop
17+
import kotlinx.serialization.KSerializer
18+
import kotlinx.serialization.SerialName
19+
import kotlinx.serialization.Serializable
20+
21+
class DropStep<E>(val count: Int) : TransformingStep<E, E>(), IMonoStep<E>, IFluxStep<E> {
22+
23+
override fun canBeEmpty(): Boolean = true
24+
25+
override fun canBeMultiple(): Boolean = getProducer().canBeMultiple()
26+
27+
override fun createFlow(input: StepFlow<E>, context: IFlowInstantiationContext): StepFlow<E> {
28+
return input.drop(count)
29+
}
30+
31+
override fun getOutputSerializer(serializationContext: SerializationContext): KSerializer<out IStepOutput<E>> {
32+
return getProducer().getOutputSerializer(serializationContext)
33+
}
34+
35+
override fun toString(): String {
36+
return """${getProducers().single()}.drop($count)"""
37+
}
38+
39+
override fun createDescriptor(context: QueryGraphDescriptorBuilder) = Descriptor(count)
40+
41+
@Serializable
42+
@SerialName("drop")
43+
class Descriptor(val count: Int) : CoreStepDescriptor() {
44+
override fun createStep(context: QueryDeserializationContext): IStep {
45+
return DropStep<Any?>(count)
46+
}
47+
}
48+
}
49+
50+
fun <T> IFluxStep<T>.drop(count: Int): IFluxStep<T> {
51+
return DropStep<T>(count).also { connect(it) }
52+
}

modelql-core/src/commonMain/kotlin/org/modelix/modelql/core/Query.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,10 @@ abstract class UnboundQuery<In, AggregationOut, ElementOut>(
401401
subclass(org.modelix.modelql.core.AllowEmptyStep.Descriptor::class)
402402
subclass(org.modelix.modelql.core.AndOperatorStep.Descriptor::class)
403403
subclass(org.modelix.modelql.core.AssertNotEmptyStep.Descriptor::class)
404+
subclass(org.modelix.modelql.core.CollectionSizeStep.Descriptor::class)
404405
subclass(org.modelix.modelql.core.ConstantSourceStep.Descriptor::class, ConstantSourceStep.Descriptor.Serializer())
405406
subclass(org.modelix.modelql.core.CountingStep.CountDescriptor::class)
406-
subclass(org.modelix.modelql.core.CollectionSizeStep.Descriptor::class)
407+
subclass(org.modelix.modelql.core.DropStep.Descriptor::class)
407408
subclass(org.modelix.modelql.core.EmptyStringIfNullStep.Descriptor::class)
408409
subclass(org.modelix.modelql.core.EqualsOperatorStep.Descriptor::class)
409410
subclass(org.modelix.modelql.core.FilteringStep.Descriptor::class)
@@ -435,6 +436,7 @@ abstract class UnboundQuery<In, AggregationOut, ElementOut>(
435436
subclass(org.modelix.modelql.core.StringContainsPredicate.StringContainsDescriptor::class)
436437
subclass(org.modelix.modelql.core.StringToBooleanStep.Descriptor::class)
437438
subclass(org.modelix.modelql.core.StringToIntStep.Descriptor::class)
439+
subclass(org.modelix.modelql.core.TakeStep.Descriptor::class)
438440
subclass(org.modelix.modelql.core.ToStringStep.Descriptor::class)
439441
subclass(org.modelix.modelql.core.WhenStep.Descriptor::class)
440442
subclass(org.modelix.modelql.core.WithIndexStep.Descriptor::class)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package org.modelix.modelql.core
15+
16+
import kotlinx.coroutines.flow.take
17+
import kotlinx.serialization.KSerializer
18+
import kotlinx.serialization.SerialName
19+
import kotlinx.serialization.Serializable
20+
21+
class TakeStep<E>(val count: Int) : TransformingStep<E, E>(), IMonoStep<E>, IFluxStep<E> {
22+
23+
override fun canBeEmpty(): Boolean = true
24+
25+
override fun canBeMultiple(): Boolean = getProducer().canBeMultiple()
26+
27+
override fun createFlow(input: StepFlow<E>, context: IFlowInstantiationContext): StepFlow<E> {
28+
return input.take(count)
29+
}
30+
31+
override fun getOutputSerializer(serializationContext: SerializationContext): KSerializer<out IStepOutput<E>> {
32+
return getProducer().getOutputSerializer(serializationContext)
33+
}
34+
35+
override fun toString(): String {
36+
return """${getProducers().single()}.take($count)"""
37+
}
38+
39+
override fun createDescriptor(context: QueryGraphDescriptorBuilder) = Descriptor(count)
40+
41+
@Serializable
42+
@SerialName("take")
43+
class Descriptor(val count: Int) : CoreStepDescriptor() {
44+
override fun createStep(context: QueryDeserializationContext): IStep {
45+
return TakeStep<Any?>(count)
46+
}
47+
}
48+
}
49+
50+
fun <T> IFluxStep<T>.take(count: Int): IFluxStep<T> {
51+
return TakeStep<T>(count).also { connect(it) }
52+
}

modelql-core/src/commonTest/kotlin/org/modelix/modelql/core/ModelQLTest.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,14 @@ class ModelQLTest {
359359
assertEquals(testDatabase.products.map { it.title to it.title }, result.map { it.first to it.second })
360360
}
361361

362+
@Test
363+
fun paginationTest() = runTestWithTimeout {
364+
val result = remoteProductDatabaseQuery { db ->
365+
db.products.drop(3).take(5).map { it.title }.toList()
366+
}
367+
assertEquals(testDatabase.products.drop(3).take(5).map { it.title }, result)
368+
}
369+
362370
// @Test
363371
// fun testIndexLookup() {
364372
// val result = remoteProductDatabaseQuery { db ->

0 commit comments

Comments
 (0)