googleapis / googleapis/google-cloud-java

[java-firestore] Allow clients to cancel ApiStreamObserver early

Aperta
#13,115 5 commenti 0 reazioni 1 assegnatario Assegnata a @tom-andersen Vedi su GitHub
api: firestore
Lingua principale
Java
Stelle
2.1k
Fork
1.2k
Merge medio
1g 23h
PR unite (30g)
154

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.