Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2008-2015, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.query;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.nio.serialization.DataSerializable;
import com.hazelcast.query.impl.QueryContext;
import com.hazelcast.query.impl.QueryableEntry;
import com.hazelcast.util.IterationType;
import com.hazelcast.util.SortingUtil;
import java.io.IOException;
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* This class is a special Predicate which helps to get a page-by-page result of a query.
* It can be constructed with a page-size, an inner predicate for filtering, and a comparator for sorting.
* This class is not thread-safe and stateless. To be able to reuse for another query, one should call
* {@link PagingPredicate#reset()}
*
* Here is an example usage.
*
* Predicate lessEqualThanFour = Predicates.lessEqual("this", 4);
*
* // We are constructing our paging predicate with a predicate and page size. In this case query results fetched two
* by two.
* PagingPredicate predicate = new PagingPredicate(lessEqualThanFour, 2);
*
* // we are initializing our map with integers from 0 to 10 as keys and values.
* IMap map = hazelcastInstance.getMap(...);
* for (int i = 0; i < 10; i++) {
* map.put(i, i);
* }
*
* // invoking the query
* Collection values = map.values(predicate);
* System.out.println("values = " + values) // will print 'values = [0, 1]'
* predicate.nextPage(); // we are setting up paging predicate to fetch next page in the next call.
* values = map.values(predicate);
* System.out.println("values = " + values);// will print 'values = [2, 3]'
* Entry anchor = predicate.getAnchor();
* System.out.println("anchor -> " + anchor); // will print 'anchor -> 1=1', since the anchor is the last entry of
* the previous page.
* predicate.previousPage(); // we are setting up paging predicate to fetch previous page in the next call
* values = map.values(predicate);
* System.out.println("values = " + values) // will print 'values = [0, 1]'
*
*/
public class PagingPredicate implements IndexAwarePredicate, DataSerializable {
private static final Map.Entry NULL_ANCHOR = new SimpleImmutableEntry(-1, null);
private List> anchorList;
private Predicate predicate;
private Comparator comparator;
private int pageSize;
private int page;
private IterationType iterationType;
/**
* Used for serialization internally
*/
public PagingPredicate() {
}
/**
* Construct with a pageSize
* results will not be filtered
* results will be natural ordered
* throws {@link IllegalArgumentException} if pageSize is not greater than 0
*
* @param pageSize page size
*/
public PagingPredicate(int pageSize) {
if (pageSize <= 0) {
throw new IllegalArgumentException("pageSize should be greater than 0 !!!");
}
this.pageSize = pageSize;
anchorList = new ArrayList>();
}
/**
* Construct with an inner predicate and pageSize
* results will be filtered via inner predicate
* results will be natural ordered
* throws {@link IllegalArgumentException} if pageSize is not greater than 0
* throws {@link IllegalArgumentException} if inner predicate is also {@link PagingPredicate}
*
* @param predicate the inner predicate through which results will be filtered
* @param pageSize the page size
*/
public PagingPredicate(Predicate predicate, int pageSize) {
this(pageSize);
setInnerPredicate(predicate);
}
/**
* Construct with a comparator and pageSize
* results will not be filtered
* results will be ordered via comparator
* throws {@link IllegalArgumentException} if pageSize is not greater than 0
*
* @param comparator the comparator through which results will be ordered
* @param pageSize the page size
*/
public PagingPredicate(Comparator comparator, int pageSize) {
this(pageSize);
this.comparator = comparator;
}
/**
* Construct with an inner predicate, comparator and pageSize
* results will be filtered via inner predicate
* results will be ordered via comparator
* throws {@link IllegalArgumentException} if pageSize is not greater than 0
* throws {@link IllegalArgumentException} if inner predicate is also {@link PagingPredicate}
*
* @param predicate the inner predicate through which results will be filtered
* @param comparator the comparator through which results will be ordered
* @param pageSize the page size
*/
public PagingPredicate(Predicate predicate, Comparator comparator, int pageSize) {
this(pageSize);
setInnerPredicate(predicate);
this.comparator = comparator;
}
/**
* Sets an inner predicate.
* throws {@link IllegalArgumentException} if inner predicate is also {@link PagingPredicate}
*
* @param predicate the inner predicate through which results will be filtered
*/
private void setInnerPredicate(Predicate predicate) {
if (predicate instanceof PagingPredicate) {
throw new IllegalArgumentException("Nested PagingPredicate is not supported!!!");
}
this.predicate = predicate;
}
/**
* Used if inner predicate is instanceof {@link IndexAwarePredicate} for filtering.
*
* @param queryContext
* @return
*/
@Override
public Set filter(QueryContext queryContext) {
if (!(predicate instanceof IndexAwarePredicate)) {
return null;
}
Set set = ((IndexAwarePredicate) predicate).filter(queryContext);
if (set == null || set.isEmpty()) {
return null;
}
List resultList = new ArrayList();
Map.Entry nearestAnchorEntry = getNearestAnchorEntry();
for (QueryableEntry queryableEntry : set) {
if (SortingUtil.compareAnchor(this, queryableEntry, nearestAnchorEntry)) {
resultList.add(queryableEntry);
}
}
List sortedSubList = SortingUtil.getSortedSubList(resultList, this, nearestAnchorEntry);
return new LinkedHashSet(sortedSubList);
}
/**
* Used if inner predicate is instanceof {@link IndexAwarePredicate} for checking if indexed.
*
* @param queryContext
* @return
*/
@Override
public boolean isIndexed(QueryContext queryContext) {
if (predicate instanceof IndexAwarePredicate) {
return ((IndexAwarePredicate) predicate).isIndexed(queryContext);
}
return false;
}
/**
* Used for delegating filtering to inner predicate.
*
* @param mapEntry
* @return
*/
@Override
public boolean apply(Map.Entry mapEntry) {
if (predicate != null) {
return predicate.apply(mapEntry);
}
return true;
}
/**
* resets for reuse
*/
public void reset() {
iterationType = null;
anchorList.clear();
page = 0;
}
/**
* sets the page value to next page
*/
public void nextPage() {
page++;
}
/**
* sets the page value to previous page
*/
public void previousPage() {
if (page != 0) {
page--;
}
}
public IterationType getIterationType() {
return iterationType;
}
public void setIterationType(IterationType iterationType) {
this.iterationType = iterationType;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getPageSize() {
return pageSize;
}
public Predicate getPredicate() {
return predicate;
}
public Comparator getComparator() {
return comparator;
}
/**
* Retrieve the anchor object which is the last value object on the previous page.
*
* Note: This method will return `null` on the first page of the query result.
*
* @return Map.Entry the anchor object which is the last value object on the previous page
*/
public Map.Entry getAnchor() {
Map.Entry anchorEntry = anchorList.get(page);
return anchorEntry == null ? null : anchorEntry.getValue();
}
/**
* After each query, an anchor entry is set for that page.
* The anchor entry is the last entry of the query.
*
* @param anchor the last entry of the query
*/
void setAnchor(int page, Map.Entry anchor) {
SimpleImmutableEntry anchorEntry = new SimpleImmutableEntry(page, anchor);
int anchorCount = anchorList.size();
if (page < anchorCount) {
anchorList.set(page, anchorEntry);
} else if (page == anchorCount) {
anchorList.add(anchorEntry);
} else {
throw new IllegalArgumentException("Anchor index is not correct, expected: " + page + " found: " + anchorCount);
}
}
/**
* After each query, an anchor entry is set for that page. see {@link #setAnchor(int, Map.Entry)}}
* For the next query user may set an arbitrary page. see {@link #setPage(int)}
* for example: user queried first 5 pages which means first 5 anchor is available
* if the next query is for the 10th page then the nearest anchor belongs to page 5
* but if the next query is for the 3nd page then the nearest anchor belongs to page 2
*
* @return nearest anchored entry for current page
*/
Map.Entry getNearestAnchorEntry() {
int anchorCount = anchorList.size();
if (page == 0 || anchorCount == 0) {
return NULL_ANCHOR;
}
Map.Entry anchoredEntry;
if (page < anchorCount) {
anchoredEntry = anchorList.get(page - 1);
} else {
anchoredEntry = anchorList.get(anchorCount - 1);
}
return anchoredEntry;
}
@Override
public void writeData(ObjectDataOutput out) throws IOException {
out.writeObject(predicate);
out.writeObject(comparator);
out.writeInt(page);
out.writeInt(pageSize);
out.writeUTF(iterationType.name());
out.writeInt(anchorList.size());
for (Map.Entry anchor : anchorList) {
out.writeInt(anchor.getKey());
Map.Entry anchorEntry = anchor.getValue();
out.writeObject(anchorEntry.getKey());
out.writeObject(anchorEntry.getValue());
}
}
@Override
public void readData(ObjectDataInput in) throws IOException {
predicate = in.readObject();
comparator = in.readObject();
page = in.readInt();
pageSize = in.readInt();
iterationType = IterationType.valueOf(in.readUTF());
int size = in.readInt();
anchorList = new ArrayList>(size);
for (int i = 0; i < size; i++) {
int anchorPage = in.readInt();
Object anchorKey = in.readObject();
Object anchorValue = in.readObject();
Map.Entry anchorEntry = new SimpleImmutableEntry(anchorKey, anchorValue);
anchorList.add(new SimpleImmutableEntry(anchorPage, anchorEntry));
}
}
}