-
Notifications
You must be signed in to change notification settings - Fork 642
[realppl 2] Utils #7350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wu-hui
wants to merge
1
commit into
wuandy/RealPpl_1
Choose a base branch
from
wuandy/RealPpl_2
base: wuandy/RealPpl_1
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[realppl 2] Utils #7350
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,10 @@ package com.google.firebase.firestore | |
import com.google.android.gms.tasks.Task | ||
import com.google.android.gms.tasks.TaskCompletionSource | ||
import com.google.firebase.Timestamp | ||
import com.google.firebase.firestore.core.Canonicalizable | ||
import com.google.firebase.firestore.model.Document | ||
import com.google.firebase.firestore.model.DocumentKey | ||
import com.google.firebase.firestore.model.MutableDocument | ||
import com.google.firebase.firestore.model.Values | ||
import com.google.firebase.firestore.pipeline.AddFieldsStage | ||
import com.google.firebase.firestore.pipeline.AggregateFunction | ||
|
@@ -29,6 +32,7 @@ import com.google.firebase.firestore.pipeline.CollectionSource | |
import com.google.firebase.firestore.pipeline.DatabaseSource | ||
import com.google.firebase.firestore.pipeline.DistinctStage | ||
import com.google.firebase.firestore.pipeline.DocumentsSource | ||
import com.google.firebase.firestore.pipeline.EvaluationContext | ||
import com.google.firebase.firestore.pipeline.Expr | ||
import com.google.firebase.firestore.pipeline.Expr.Companion.field | ||
import com.google.firebase.firestore.pipeline.ExprWithAlias | ||
|
@@ -51,6 +55,7 @@ import com.google.firebase.firestore.pipeline.Stage | |
import com.google.firebase.firestore.pipeline.UnionStage | ||
import com.google.firebase.firestore.pipeline.UnnestStage | ||
import com.google.firebase.firestore.pipeline.WhereStage | ||
import com.google.firebase.firestore.util.Assert.fail | ||
import com.google.firestore.v1.ExecutePipelineRequest | ||
import com.google.firestore.v1.StructuredPipeline | ||
import com.google.firestore.v1.Value | ||
|
@@ -759,7 +764,7 @@ internal constructor( | |
firestore: FirebaseFirestore, | ||
userDataReader: UserDataReader, | ||
stages: List<Stage<*>> | ||
) : AbstractPipeline(firestore, userDataReader, stages) { | ||
) : AbstractPipeline(firestore, userDataReader, stages), Canonicalizable { | ||
internal constructor( | ||
firestore: FirebaseFirestore, | ||
userDataReader: UserDataReader, | ||
|
@@ -786,31 +791,107 @@ internal constructor( | |
|
||
fun where(condition: BooleanExpr): RealtimePipeline = append(WhereStage(condition)) | ||
|
||
internal fun rewriteStages(): RealtimePipeline { | ||
internal val rewrittenStages: List<Stage<*>> by lazy { | ||
var hasOrder = false | ||
return with( | ||
buildList { | ||
for (stage in stages) when (stage) { | ||
// Stages whose semantics depend on ordering | ||
is LimitStage, | ||
is OffsetStage -> { | ||
if (!hasOrder) { | ||
hasOrder = true | ||
add(SortStage.BY_DOCUMENT_ID) | ||
} | ||
add(stage) | ||
} | ||
is SortStage -> { | ||
buildList { | ||
for (stage in stages) when (stage) { | ||
// Stages whose semantics depend on ordering | ||
is LimitStage, | ||
is OffsetStage -> { | ||
if (!hasOrder) { | ||
hasOrder = true | ||
add(stage.withStableOrdering()) | ||
add(SortStage.BY_DOCUMENT_ID) | ||
} | ||
else -> add(stage) | ||
add(stage) | ||
} | ||
if (!hasOrder) { | ||
add(SortStage.BY_DOCUMENT_ID) | ||
is SortStage -> { | ||
hasOrder = true | ||
add(stage.withStableOrdering()) | ||
} | ||
else -> add(stage) | ||
} | ||
) | ||
if (!hasOrder) { | ||
add(SortStage.BY_DOCUMENT_ID) | ||
} | ||
} | ||
} | ||
|
||
override fun canonicalId(): String { | ||
return rewrittenStages.joinToString("|") { stage -> (stage as Canonicalizable).canonicalId() } | ||
} | ||
|
||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (other !is RealtimePipeline) return false | ||
return stages == other.stages | ||
} | ||
|
||
override fun hashCode(): Int { | ||
return stages.hashCode() | ||
} | ||
|
||
internal fun evaluate(inputs: List<MutableDocument>): List<MutableDocument> { | ||
val context = EvaluationContext(this) | ||
return rewrittenStages.fold(inputs) { documents, stage -> stage.evaluate(context, documents) } | ||
} | ||
|
||
internal fun matchesAllDocuments(): Boolean { | ||
for (stage in rewrittenStages) { | ||
// Check for LimitStage | ||
if (stage.name == "limit") { | ||
return false | ||
} | ||
|
||
// Check for Where stage | ||
if (stage is WhereStage) { | ||
// Check if it's the special 'exists(__name__)' case | ||
val funcExpr = stage.condition as? FunctionExpr | ||
if (funcExpr?.name == "exists" && funcExpr.params.size == 1) { | ||
val fieldExpr = funcExpr.params[0] as? Field | ||
if (fieldExpr?.fieldPath?.isKeyField == true) { | ||
continue // This specific 'exists(__name__)' filter doesn't count | ||
} | ||
} | ||
return false | ||
} | ||
// TODO(pipeline) : Add checks for other filtering stages like Aggregate, | ||
// Distinct, FindNearest once they are implemented. | ||
} | ||
return true | ||
} | ||
|
||
internal fun hasLimit(): Boolean { | ||
for (stage in rewrittenStages) { | ||
if (stage.name == "limit") { | ||
return true | ||
} | ||
// TODO(pipeline): need to check for other stages that could have a limit, | ||
// like findNearest | ||
} | ||
return false | ||
} | ||
|
||
internal fun matches(doc: Document): Boolean { | ||
val result = evaluate(listOf(doc as MutableDocument)) | ||
return result.isNotEmpty() | ||
} | ||
|
||
private fun evaluateContext(): EvaluationContext { | ||
return EvaluationContext(this) | ||
} | ||
|
||
internal fun comparator(): Comparator<Document> = | ||
getLastEffectiveSortStage().comparator(evaluateContext()) | ||
Comment on lines
+879
to
+884
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
private fun getLastEffectiveSortStage(): SortStage { | ||
for (stage in rewrittenStages.asReversed()) { | ||
if (stage is SortStage) { | ||
return stage | ||
} | ||
// TODO(pipeline): Consider stages that might invalidate ordering later, | ||
// like fineNearest | ||
} | ||
throw fail("RealtimePipeline must contain at least one Sort stage (ensured by RewriteStages).") | ||
} | ||
} | ||
|
||
|
22 changes: 22 additions & 0 deletions
22
firebase-firestore/src/main/java/com/google/firebase/firestore/core/Canonicalizable.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright 2025 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.firebase.firestore.core | ||
|
||
/** An internal interface for classes that can be canonicalized to a string representation. */ | ||
internal interface Canonicalizable { | ||
fun canonicalId(): String | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
stage.name == "limit"
to check for theLimitStage
is a bit brittle. It's better to use a type check withis LimitStage
for improved type safety and maintainability. This avoids potential issues if the stage name is ever changed and makes the code's intent clearer.A similar issue exists in the
hasLimit()
function on line 865.