Skip to content

Commit bbe85f4

Browse files
committed
Sort tests
1 parent b1f4006 commit bbe85f4

File tree

10 files changed

+898
-59
lines changed

10 files changed

+898
-59
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/DocumentReference.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public final class DocumentReference {
7171
}
7272

7373
/** @hide */
74-
static DocumentReference forPath(ResourcePath path, FirebaseFirestore firestore) {
74+
public static DocumentReference forPath(ResourcePath path, FirebaseFirestore firestore) {
7575
if (path.length() % 2 != 0) {
7676
throw new IllegalArgumentException(
7777
"Invalid document reference. Document references must have an even number "

firebase-firestore/src/main/java/com/google/firebase/firestore/Pipeline.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,9 @@ internal constructor(
786786
fun select(fieldName: String, vararg additionalSelections: Any): RealtimePipeline =
787787
append(SelectStage.of(fieldName, *additionalSelections))
788788

789+
fun sort(order: Ordering, vararg additionalOrders: Ordering): RealtimePipeline =
790+
append(SortStage(arrayOf(order, *additionalOrders)))
791+
789792
fun where(condition: BooleanExpr): RealtimePipeline = append(WhereStage(condition))
790793
}
791794

firebase-firestore/src/main/java/com/google/firebase/firestore/pipeline/evaluation.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import com.google.common.math.LongMath
2020
import com.google.common.math.LongMath.checkedAdd
2121
import com.google.common.math.LongMath.checkedMultiply
2222
import com.google.common.math.LongMath.checkedSubtract
23+
import com.google.firebase.firestore.FirebaseFirestore
2324
import com.google.firebase.firestore.UserDataReader
2425
import com.google.firebase.firestore.model.MutableDocument
2526
import com.google.firebase.firestore.model.Values
@@ -42,7 +43,10 @@ import kotlin.math.log10
4243
import kotlin.math.pow
4344
import kotlin.math.sqrt
4445

45-
internal class EvaluationContext(val userDataReader: UserDataReader)
46+
internal class EvaluationContext(
47+
val db: FirebaseFirestore,
48+
val userDataReader: UserDataReader
49+
)
4650

4751
internal typealias EvaluateDocument = (input: MutableDocument) -> EvaluateResult
4852

firebase-firestore/src/main/java/com/google/firebase/firestore/pipeline/expressions.kt

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ import com.google.firebase.firestore.Pipeline
2323
import com.google.firebase.firestore.UserDataReader
2424
import com.google.firebase.firestore.VectorValue
2525
import com.google.firebase.firestore.model.DocumentKey
26+
import com.google.firebase.firestore.model.FieldPath.CREATE_TIME_PATH
27+
import com.google.firebase.firestore.model.FieldPath.KEY_PATH
28+
import com.google.firebase.firestore.model.FieldPath.UPDATE_TIME_PATH
2629
import com.google.firebase.firestore.model.FieldPath as ModelFieldPath
2730
import com.google.firebase.firestore.model.MutableDocument
2831
import com.google.firebase.firestore.model.Values
@@ -295,10 +298,12 @@ abstract class Expr internal constructor() {
295298
*/
296299
@JvmStatic
297300
fun field(name: String): Field {
298-
if (name == DocumentKey.KEY_FIELD_NAME) {
299-
return Field(ModelFieldPath.KEY_PATH)
301+
return when (name) {
302+
DocumentKey.KEY_FIELD_NAME -> Field(KEY_PATH)
303+
ModelFieldPath.CREATE_TIME_NAME -> Field(CREATE_TIME_PATH)
304+
ModelFieldPath.UPDATE_TIME_NAME -> Field(UPDATE_TIME_PATH)
305+
else -> Field(FieldPath.fromDotSeparatedPath(name).internalPath)
300306
}
301-
return Field(FieldPath.fromDotSeparatedPath(name).internalPath)
302307
}
303308

304309
/**
@@ -4189,11 +4194,13 @@ class Field internal constructor(private val fieldPath: ModelFieldPath) : Select
41894194
internal fun toProto(): Value =
41904195
Value.newBuilder().setFieldReferenceValue(fieldPath.canonicalString()).build()
41914196

4192-
override fun evaluateContext(context: EvaluationContext) = ::evaluateInternal
4193-
4194-
private fun evaluateInternal(input: MutableDocument): EvaluateResult {
4195-
val value: Value? = input.getField(fieldPath)
4196-
return if (value === null) EvaluateResultUnset else EvaluateResultValue(value)
4197+
override fun evaluateContext(context: EvaluationContext) = block@{ input: MutableDocument ->
4198+
EvaluateResultValue(when (fieldPath) {
4199+
KEY_PATH -> encodeValue(DocumentReference.forPath(input.key.path, context.db))
4200+
CREATE_TIME_PATH -> encodeValue(input.createTime.timestamp)
4201+
UPDATE_TIME_PATH -> encodeValue(input.version.timestamp)
4202+
else -> input.getField(fieldPath) ?: return@block EvaluateResultUnset
4203+
})
41974204
}
41984205
}
41994206

firebase-firestore/src/main/java/com/google/firebase/firestore/pipeline/stage.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ import com.google.firestore.v1.Value
3030
import kotlinx.coroutines.flow.Flow
3131
import kotlinx.coroutines.flow.filter
3232
import kotlinx.coroutines.flow.flow
33+
import kotlinx.coroutines.flow.flowOf
3334
import kotlinx.coroutines.flow.map
35+
import kotlinx.coroutines.flow.take
3436
import kotlinx.coroutines.flow.toList
3537

3638
sealed class BaseStage<T : BaseStage<T>>(
@@ -244,6 +246,13 @@ private constructor(private val collectionId: String, options: InternalOptions)
244246
override fun self(options: InternalOptions) = CollectionGroupSource(collectionId, options)
245247
override fun args(userDataReader: UserDataReader): Sequence<Value> =
246248
sequenceOf(Value.newBuilder().setReferenceValue("").build(), encodeValue(collectionId))
249+
override fun evaluate(
250+
context: EvaluationContext,
251+
inputs: Flow<MutableDocument>
252+
): Flow<MutableDocument> {
253+
// TODO: Does this need to do more?
254+
return inputs
255+
}
247256

248257
companion object {
249258

@@ -511,6 +520,11 @@ internal class LimitStage
511520
internal constructor(private val limit: Int, options: InternalOptions = InternalOptions.EMPTY) :
512521
BaseStage<LimitStage>("limit", options) {
513522
override fun self(options: InternalOptions) = LimitStage(limit, options)
523+
override fun evaluate(
524+
context: EvaluationContext,
525+
inputs: Flow<MutableDocument>
526+
): Flow<MutableDocument> = if (limit > 0) inputs.take(limit) else flowOf()
527+
514528
override fun args(userDataReader: UserDataReader): Sequence<Value> =
515529
sequenceOf(encodeValue(limit))
516530
}

firebase-firestore/src/test/java/com/google/firebase/firestore/pipeline/PipelineTests.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import com.google.firebase.firestore.RealtimePipelineSource
1919
import com.google.firebase.firestore.TestUtil
2020
import com.google.firebase.firestore.model.MutableDocument
2121
import com.google.firebase.firestore.pipeline.Expr.Companion.field
22+
import com.google.firebase.firestore.runPipeline
2223
import com.google.firebase.firestore.testutil.TestUtilKtx.doc
2324
import kotlinx.coroutines.flow.flowOf
2425
import kotlinx.coroutines.flow.toList
@@ -39,7 +40,7 @@ internal class PipelineTests {
3940
val doc2: MutableDocument = doc("foo/2", 0, mapOf("bar" to "43"))
4041
val doc3: MutableDocument = doc("xxx/1", 0, mapOf("bar" to 42))
4142

42-
val list = runPipeline(pipeline, flowOf(doc1, doc2, doc3)).toList()
43+
val list = runPipeline(firestore, pipeline, flowOf(doc1, doc2, doc3)).toList()
4344

4445
assertThat(list).hasSize(1)
4546
}

0 commit comments

Comments
 (0)