【问题标题】:Why is the cursor returned from my GAE datastore query null?为什么从我的 GAE 数据存储查询返回的游标为空?
【发布时间】:2023-03-08 21:25:02
【问题描述】:

我正在尝试使用此页面https://cloud.google.com/appengine/docs/standard/java/datastore/query-cursors 作为灵感来使用 GAE 光标。 问题是我下面的代码返回的光标为空 - 我不明白为什么,因为我认为我使用与上面的链接相同的结构,即查询的结果是 QueryResultList,我使用 getCursor 方法获得对游标的引用的结果。 NON_SENT_MATCHES_LIMIT 是 20,如果我对查询没有设置限制,则有超过 2000 个实体,所以我希望结果是一个非空游标。

谁能解释一下为什么光标为空?

public ProductRuleMatchWithCursor getNonSentMatchesBatch(Transaction transaction, long shopId, Cursor startCursor) {
    Query.FilterPredicate activeOrderProduct = new Query.FilterPredicate(ProductRuleMatchDBFields.ACTIVE, Query.FilterOperator.EQUAL, true);
    Query.FilterPredicate pendingOrderProduct = new Query.FilterPredicate(ProductRuleMatchDBFields.MATCH_STATUS, Query.FilterOperator.EQUAL, MatchStatus.PENDING.getId());
    Query.FilterPredicate failedOrderProduct = new Query.FilterPredicate(ProductRuleMatchDBFields.MATCH_STATUS, Query.FilterOperator.EQUAL, MatchStatus.FAILED_CAN_RECOVER.getId());
    Query query = new Query(ProductRuleMatchDBFields.PRODUCT_RULE_MATCH_TABLE_NAME).setFilter(Query.CompositeFilterOperator.and(activeOrderProduct, Query.CompositeFilterOperator.or(pendingOrderProduct, failedOrderProduct)));
    query.setAncestor(KeyFactory.createKey(ShopDBFields.SHOP_TABLE_NAME, shopId));
    FetchOptions fetchOptions = FetchOptions.Builder.withLimit(NON_SENT_MATCHES_LIMIT);
    if (startCursor != null){
        fetchOptions.startCursor(startCursor);
    }
    QueryResultList<Entity> entities = datastore.prepare(transaction, query).asQueryResultList(fetchOptions);

    List<ProductRuleMatch> matches = new ArrayList<>();
    for (Entity entity : entities) {
        matches.add(createModelFromEntity(entity));
    }
    Cursor cursor = entities.getCursor();
    logger.info("The cursor is: " + cursor);
    return new ProductRuleMatchWithCursor(matches, cursor);
}

【问题讨论】:

    标签: java google-app-engine cursor google-cloud-datastore


    【解决方案1】:

    啊,好吧 - 如果我再耐心一点,我会在参考链接的文档中找到答案:

    “因为 NOT_EQUAL 和 IN 运算符是通过多个查询实现的,所以使用它们的查询不支持游标,也不支持使用 CompositeFilterOperator.or 方法构造的复合查询。”

    【讨论】:

    • 但是,如果您有一个带有 CompositeFilterOperator.or 的查询并且您需要使用游标,您会怎么做?
    猜你喜欢
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-19
    • 2016-11-01
    • 2021-09-02
    • 1970-01-01
    相关资源
    最近更新 更多