Search code examples
javaoffsethazelcastin-memory-databasehazelcast-imap

Hazelcast offset Predicate


In Hazelcast, has been implemented PagingPredicate which helps to get data page by page.

Is there any possibility to get data with offset and limit? (Not page by page)


Solution

  • I didn't find better solution then calculate and get two pages using PagingPredicate and get sub list from this result to get data with offset and limit like in SQL databases.

    Method example:

    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));
    }
    

    Utility method 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);
    }