googleapis / googleapis/google-cloud-java

[java-firestore] Allow clients to cancel ApiStreamObserver early

オープン
#13,115 コメント 5 件 リアクション 0 件 担当者 1 名 @tom-andersen に割り当て済み GitHub で見る
api: firestore
主要言語
Java
スター
2.1k
フォーク
1.2k
平均マージ
1日 23時間
マージ済み PR(30日)
154

説明

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.

コントリビューションガイド

コントリビューションガイドを開く

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。