googleapis / googleapis/google-cloud-java

[java-firestore] Allow clients to cancel ApiStreamObserver early

未关闭
#13,115 5 条评论 0 个 reaction 已指派 1 人 已指派给 @tom-andersen 在 GitHub 查看
api: firestore
主要语言
Java
星标
2.1k
派生
1.2k
平均合并
1 天 23 小时
30 天内合并 PR
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 摘要。