use of com.hazelcast.query.impl.predicates.PagingPredicateImpl in project hazelcast by hazelcast.
the class SortingUtil method getSortedSubList.
public static List<QueryableEntry> getSortedSubList(List<QueryableEntry> list, PagingPredicate pagingPredicate, Map.Entry<Integer, Map.Entry> nearestAnchorEntry) {
if (pagingPredicate == null || list.isEmpty()) {
return list;
}
PagingPredicateImpl pagingPredicateImpl = (PagingPredicateImpl) pagingPredicate;
Comparator<QueryableEntry> comparator = newComparator(pagingPredicateImpl);
Collections.sort(list, comparator);
int nearestPage = nearestAnchorEntry.getKey();
int pageSize = pagingPredicate.getPageSize();
int page = pagingPredicate.getPage();
long totalSize = pageSize * ((long) page - nearestPage);
if (list.size() > totalSize) {
// it's safe to cast totalSize back to int here since it's limited by the list size
list = list.subList(0, (int) totalSize);
}
return list;
}
use of com.hazelcast.query.impl.predicates.PagingPredicateImpl in project hazelcast by hazelcast.
the class SortingUtil method getPageIndexesAndUpdateAnchor.
private static Map.Entry<Integer, Integer> getPageIndexesAndUpdateAnchor(List<? extends Map.Entry> list, PagingPredicate pagingPredicate, IterationType iterationType) {
if (list.isEmpty()) {
return new AbstractMap.SimpleImmutableEntry<Integer, Integer>(-1, -1);
}
PagingPredicateImpl pagingPredicateImpl = (PagingPredicateImpl) pagingPredicate;
Comparator<Map.Entry> comparator = SortingUtil.newComparator(pagingPredicateImpl.getComparator(), iterationType);
Collections.sort(list, comparator);
Map.Entry<Integer, Map.Entry> nearestAnchorEntry = pagingPredicateImpl.getNearestAnchorEntry();
int nearestPage = nearestAnchorEntry.getKey();
int page = pagingPredicateImpl.getPage();
int pageSize = pagingPredicateImpl.getPageSize();
long begin = pageSize * ((long) page - nearestPage - 1);
int size = list.size();
if (begin > size) {
return new AbstractMap.SimpleImmutableEntry<Integer, Integer>(-1, -1);
}
long end = begin + pageSize;
if (end > size) {
end = size;
}
setAnchor(list, pagingPredicateImpl, nearestPage);
// it's safe to cast begin and end back to int here since they are limited by the list size
return new AbstractMap.SimpleImmutableEntry<Integer, Integer>((int) begin, (int) end);
}
use of com.hazelcast.query.impl.predicates.PagingPredicateImpl in project hazelcast by hazelcast.
the class PagingPredicateOptimizationTest method testInnerPredicateOptimization.
@Test
public void testInnerPredicateOptimization() {
RuleBasedQueryOptimizer optimizer = new RuleBasedQueryOptimizer();
Indexes indexes = mock(Indexes.class);
Predicate[] orPredicates = new Predicate[10];
for (int i = 0; i < orPredicates.length; ++i) {
orPredicates[i] = Predicates.equal("a", i);
}
Predicate innerPredicate = Predicates.or(orPredicates);
PagingPredicate<Object, Object> pagingPredicate = Predicates.pagingPredicate(innerPredicate, 10);
Predicate optimized = optimizer.optimize(pagingPredicate, indexes);
assertInstanceOf(PagingPredicateImpl.class, optimized);
Predicate innerOptimized = ((PagingPredicateImpl) optimized).getPredicate();
assertInstanceOf(InPredicate.class, innerOptimized);
}
Aggregations