googleapis / googleapis/google-cloud-java

[java-firestore] Allow clients to cancel ApiStreamObserver early

Ouverte
#13,115 5 commentaires 0 réactions 1 personne assignée Assignée à @tom-andersen Voir sur GitHub
api: firestore
Langage dominant
Java
Étoiles
2.1k
Forks
1.2k
Merge moyen
1 j 23 h
PR mergées (30 j)
154

Description

Hello,

This issue is similar to googleapis/java-firestore#574.

I want to use `query.stream` and perform some client-side filtering. In our case, this is necessary to work around two Firestore limitations:
* No inequality filters on multiple fields.
* Only a single `array-contains` per disjunction is allowed.

So, we perform one inequality check and one `array-contains` in the Firestore query and check for the remaining constraints client-side. The thing is, we only need the first X matching results. After the first X results, we want to stop the stream. Here's some code:

```java
query.stream(
new ApiStreamObserver() {

private static final int X = 10;

private int numMatches = 0;

@Override
public void onNext(DocumentSnapshot value) {
if (matches(value)) {
numMatches++;
publishMatch(value);
}

if (numMatches > X) {
// Stop the stream early somehow
}
}

@Override
public void onError(Throwable t) {
// ...
}

@Override
public void onCompleted() {
// ...
}

private boolean matches(DocumentSnapshot value) {
// ...
}

private void publishMatch(DocumentSnapshot value) {
// ...
}
}
);
```

I found that `ResponseQueryObserver.onStart` in `Query.internalStream` gets passed a `StreamController` instance which has a `cancel` method. Could this be exposed to the user?

I think this is a common use case and would be a nice feature to have, especially when working with large collections where only a handful of results are needed per query.

Guide de contribution

Ouvrir le guide de contribution

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.