Hazelcast 偏移谓词

Hazelcast offset Predicate

在 Hazelcast 中,已实现 PagingPredicate 这有助于逐页获取数据。

有没有可能得到有offset和limit的数据? (不是一页一页)

BetweenPredicate 可以满足您的需求。

那时我没有找到更好的解决方案 使用 PagingPredicate 和 从此结果中获取子列表以获取具有偏移量和限制的数据,就像在 SQL 数据库中一样。

方法示例:

public Set<M> findOffsetValues(int offset,int limit) {
    PagingPredicate pagingPredicate = new PagingPredicate<>(getDefaultComporator(), limit);
    pagingPredicate.setPage(offset / limit);
    List<M> page = new ArrayList<>(itemsCacheMap.values(pagePredicate));
    pagePredicate.nextPage();
    page.addAll(itemsCacheMap.values(pagePredicate));
    int startIndex = offset % limit;
    return new LinkedHashSet<>(CollectionUtil.saveSublist(page, startIndex, startIndex + limit));
}

实用方法CollectionUtil.saveSublist

public static <E> List<E> saveSublist(List<E> list, int fromIndex, int toIndex) {
    int checkedFromIndex = list.size() == 0
                           ? 0
                           : fromIndex >= list.size() ? list.size() - 1 : fromIndex;
    int checkedToIndex = toIndex > list.size() ? list.size() : toIndex;
    return list.subList(checkedFromIndex, checkedToIndex);
}