Search in sources :

Example 11 with PagingPredicateImpl

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;
}
Also used : QueryableEntry(com.hazelcast.query.impl.QueryableEntry) PagingPredicateImpl(com.hazelcast.query.impl.predicates.PagingPredicateImpl)

Example 12 with PagingPredicateImpl

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);
}
Also used : CachedQueryEntry(com.hazelcast.query.impl.CachedQueryEntry) QueryableEntry(com.hazelcast.query.impl.QueryableEntry) AbstractMap(java.util.AbstractMap) Map(java.util.Map) PagingPredicateImpl(com.hazelcast.query.impl.predicates.PagingPredicateImpl)

Example 13 with PagingPredicateImpl

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);
}
Also used : RuleBasedQueryOptimizer(com.hazelcast.query.impl.predicates.RuleBasedQueryOptimizer) Indexes(com.hazelcast.query.impl.Indexes) InPredicate(com.hazelcast.query.impl.predicates.InPredicate) PagingPredicate(com.hazelcast.query.PagingPredicate) Predicate(com.hazelcast.query.Predicate) PagingPredicateImpl(com.hazelcast.query.impl.predicates.PagingPredicateImpl) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

PagingPredicateImpl (com.hazelcast.query.impl.predicates.PagingPredicateImpl)13 QueryableEntry (com.hazelcast.query.impl.QueryableEntry)5 Map (java.util.Map)5 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)3 PagingPredicateHolder (com.hazelcast.client.impl.protocol.codec.holder.PagingPredicateHolder)3 CachedQueryEntry (com.hazelcast.query.impl.CachedQueryEntry)3 AbstractMap (java.util.AbstractMap)3 Data (com.hazelcast.internal.serialization.Data)2 SerializationService (com.hazelcast.internal.serialization.SerializationService)2 IterationType (com.hazelcast.internal.util.IterationType)2 Predicate (com.hazelcast.query.Predicate)2 MapEntriesWithPagingPredicateCodec (com.hazelcast.client.impl.protocol.codec.MapEntriesWithPagingPredicateCodec)1 MapKeySetWithPagingPredicateCodec (com.hazelcast.client.impl.protocol.codec.MapKeySetWithPagingPredicateCodec)1 MapValuesWithPagingPredicateCodec (com.hazelcast.client.impl.protocol.codec.MapValuesWithPagingPredicateCodec)1 CollectionUtil.objectToDataCollection (com.hazelcast.internal.util.CollectionUtil.objectToDataCollection)1 ToHeapDataConverter.toHeapData (com.hazelcast.internal.util.ToHeapDataConverter.toHeapData)1 LazyMapEntry (com.hazelcast.map.impl.LazyMapEntry)1 MapContainer (com.hazelcast.map.impl.MapContainer)1 PartitionContainer (com.hazelcast.map.impl.PartitionContainer)1 Record (com.hazelcast.map.impl.record.Record)1