Skip to content

Commit

Permalink
order normalization and field unify
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-hui committed May 27, 2024
1 parent 8bc968f commit 8c864f7
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ class Pipeline private constructor(private val stages: List<Stage>, private val
val projMap = mutableMapOf<String, Expr>()
for (proj in selectables) {
when (proj) {
is Field -> projMap[proj.field] = proj
is Field -> projMap[proj.path.encodedPath] = proj
is AggregatorTarget -> projMap[proj.fieldName] = proj.accumulator
is Fields -> proj.fs?.forEach { projMap[it.field] = it }
is Fields -> proj.fs?.forEach { projMap[it.path.encodedPath] = it }
}
}
return projMap
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.google.cloud.firestore

import com.google.cloud.Timestamp
import com.google.cloud.firestore.pipeline.Field
import com.google.firestore.v1.Document
import com.google.firestore.v1.Value
import java.util.Date
Expand Down Expand Up @@ -76,6 +77,10 @@ internal constructor(
return this.extractField(fieldPath) != null
}

fun contains(field: Field): Boolean {
return this.extractField(field.path) != null
}

fun get(field: String): Any? {
return get(FieldPath.fromDotSeparatedString(field))
}
Expand All @@ -90,12 +95,20 @@ internal constructor(
return UserDataConverter.decodeValue(rpcContext, value)
}

fun get(field: Field): Any? {
return get(field.path)
}

fun <T> get(fieldPath: FieldPath, valueType: Class<T>): T? {
val data = get(fieldPath)
return if (data == null) null
else CustomClassMapper.convertToCustomClass(data, valueType, reference)
}

fun <T> get(field: Field, valueType: Class<T>): T? {
return get(field.path, valueType)
}

fun extractField(fieldPath: FieldPath): Value? {
var value: Value? = null

Expand All @@ -114,6 +127,10 @@ internal constructor(
return value
}

fun extractField(field: Field): Value? {
return extractField(field.path)
}

fun getBoolean(field: String): Boolean? {
return get(field) as Boolean?
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1997,9 +1997,10 @@ public Pipeline toPipeline() {
}

// Orders
if (this.options.getFieldOrders() != null && !this.options.getFieldOrders().isEmpty()) {
List<FieldOrder> normalizedOrderbys = this.createImplicitOrderBy();
if (normalizedOrderbys != null && !normalizedOrderbys.isEmpty()) {
List<Ordering> orders =
this.options.getFieldOrders().stream()
normalizedOrderbys.stream()
.map(
fieldOrder ->
Ordering.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.google.cloud.firestore.pipeline
import com.google.cloud.Timestamp
import com.google.cloud.firestore.Blob
import com.google.cloud.firestore.DocumentReference
import com.google.cloud.firestore.FieldPath
import com.google.cloud.firestore.GeoPoint
import com.google.cloud.firestore.Pipeline
import com.google.cloud.firestore.encodeValue
Expand Down Expand Up @@ -249,26 +250,26 @@ data class Constant internal constructor(private val value: Any?) : Expr {
}

data class Field internal constructor(
internal val field: String,
internal val path: FieldPath,
private var pipeline: Pipeline? = null
) :
Expr, Selectable {
companion object {
const val DOCUMENT_ID: String = "__path__"
const val DOCUMENT_ID: String = "__name__"

@JvmStatic
fun of(path: String): Field {
return Field(path)
return Field(FieldPath.of(path))
}

@JvmStatic
fun ofAll(): Field {
return Field("")
return Field(FieldPath.of(""))
}
}

internal fun toProto(): Value {
return Value.newBuilder().setFieldReferenceValue(field).build()
return Value.newBuilder().setFieldReferenceValue(path.toString()).build()
}
}

Expand Down
Loading

0 comments on commit 8c864f7

Please sign in to comment.