googleapis / googleapis/google-cloud-java

[java-firestore] Allow clients to cancel ApiStreamObserver early

Abierto
#13,115 5 comentarios 0 reacciones 1 asignado Asignado a @tom-andersen Ver en GitHub
api: firestore
Lenguaje dominante
Java
Estrellas
2.1k
Forks
1.2k
Merge medio
1 d 23 h
PR fusionados (30 d)
154

Descripción

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.

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.