public IdentifiedDataSerializable createNew(Integer arg) { return new PagingPredicate(); } };
private void goToRandomPage() { int pageNumber = randomInt(maxPage); predicate.setPage(pageNumber); } }
private static void setAnchor(List<Map.Entry> list, PagingPredicate pagingPredicate, int nearestPage) { if (list.isEmpty()) { return; } int size = list.size(); int pageSize = pagingPredicate.getPageSize(); int page = pagingPredicate.getPage(); for (int i = pageSize; i <= size && nearestPage < page; i += pageSize) { Map.Entry anchor = list.get(i - 1); nearestPage++; PagingPredicateAccessor.setAnchor(pagingPredicate, nearestPage, anchor); } }
public int compare(QueryableEntry entry1, QueryableEntry entry2) { return SortingUtil.compare(pagingPredicate.getComparator(), pagingPredicate.getIterationType(), entry1, entry2); } };
@Override public void handleRequest(HttpServerExchange exchange) throws Exception { IMap<String, RefreshToken> tokens = CacheStartupHookProvider.hz.getMap("tokens"); Deque<String> userIdDeque = exchange.getQueryParameters().get("userId"); String userId = userIdDeque == null? "%" : userIdDeque.getFirst() + "%"; int page = Integer.valueOf(exchange.getQueryParameters().get("page").getFirst()) - 1; Deque<String> pageSizeDeque = exchange.getQueryParameters().get("pageSize"); int pageSize = pageSizeDeque == null? 10 : Integer.valueOf(pageSizeDeque.getFirst()); if(logger.isDebugEnabled()) logger.debug("userId = " + userId + " page = " + page + " pageSize = " + pageSize); LikePredicate likePredicate = new LikePredicate("userId", userId); PagingPredicate pagingPredicate = new PagingPredicate(likePredicate, new RefreshTokenComparator(), pageSize); pagingPredicate.setPage(page); Collection<RefreshToken> values = tokens.values(pagingPredicate); exchange.getResponseHeaders().add(new HttpString("Content-Type"), "application/json"); exchange.getResponseSender().send(Config.getInstance().getMapper().writeValueAsString(values)); processAudit(exchange); } }
PagingPredicate pp = new PagingPredicate(predicateToUse, sortToUse, rows); long x = offset / rows; while (x > 0) { pp.nextPage(); x--; predicateToUse = new PagingPredicate(predicateToUse, sortToUse, Integer.MAX_VALUE);
PagingPredicate pagingPredicate = new PagingPredicate(equalPredicate, descendingComparator, 4); pagingPredicate.nextPage(); values = map.values(pagingPredicate); System.out.print("\nPage 2 -> "); pagingPredicate.nextPage(); values = map.values(pagingPredicate); System.out.print("\nPage 3 -> "); pagingPredicate = new PagingPredicate(3); pagingPredicate.setPage(5); values = map.values(pagingPredicate); System.out.print("\n\nPage 6 -> ");
@SuppressWarnings("unchecked") public static ResultSet getSortedQueryResultSet(List<Map.Entry> list, PagingPredicate pagingPredicate, IterationType iterationType) { if (list.isEmpty()) { return new ResultSet(); } Comparator<Map.Entry> comparator = SortingUtil.newComparator(pagingPredicate.getComparator(), iterationType); Collections.sort(list, comparator); Map.Entry<Integer, Map.Entry> nearestAnchorEntry = getNearestAnchorEntry(pagingPredicate); int nearestPage = nearestAnchorEntry.getKey(); int page = pagingPredicate.getPage(); int pageSize = pagingPredicate.getPageSize(); long begin = pageSize * ((long) page - nearestPage - 1); int size = list.size(); if (begin > size) { return new ResultSet(); } long end = begin + pageSize; if (end > size) { end = size; } setAnchor(list, pagingPredicate, nearestPage); // it's safe to cast begin and end back to int here since they are limited by the list size List<Map.Entry> subList = list.subList((int) begin, (int) end); return new ResultSet(subList, iterationType); }
/** * @param A query in Spring form * @return The same in Hazelcast form */ public Predicate<?, ?> resolve(KeyValueQuery<?> query) { if (query == null) { return null; } final Object criteria = query.getCriteria(); if (criteria == null) { return null; } if (criteria instanceof PagingPredicate) { PagingPredicate pagingPredicate = (PagingPredicate) criteria; query.limit(pagingPredicate.getPageSize()); return pagingPredicate.getPredicate(); } if (criteria instanceof Predicate) { return (Predicate<?, ?>) criteria; } throw new UnsupportedOperationException(query.toString()); }
@Override protected void timeStep() throws Exception { createNewPredicateIfNeeded(); evaluatePredicate(); predicate.nextPage(); } }
@Override public void handleRequest(HttpServerExchange exchange) throws Exception { IMap<String, Service> services = CacheStartupHookProvider.hz.getMap("services"); Deque<String> serviceIdDeque = exchange.getQueryParameters().get("serviceId"); String serviceId = serviceIdDeque == null? "%" : serviceIdDeque.getFirst() + "%"; int page = Integer.valueOf(exchange.getQueryParameters().get("page").getFirst()) - 1; Deque<String> pageSizeDeque = exchange.getQueryParameters().get("pageSize"); int pageSize = pageSizeDeque == null? 10 : Integer.valueOf(pageSizeDeque.getFirst()); LikePredicate likePredicate = new LikePredicate("serviceId", serviceId); PagingPredicate pagingPredicate = new PagingPredicate(likePredicate, new ServiceComparator(), pageSize); pagingPredicate.setPage(page); Collection<Service> values = services.values(pagingPredicate); exchange.getResponseHeaders().add(Headers.CONTENT_TYPE, "application/json"); exchange.getResponseSender().send(Config.getInstance().getMapper().writeValueAsString(values)); processAudit(exchange); } }
@TimeStep(prob = 0.2) public void pagePredicate(ThreadState state) { double maxSalary = state.randomDouble() * Employee.MAX_SALARY; Predicate predicate = Predicates.lessThan("salary", maxSalary); SalaryComparator salaryComparator = new SalaryComparator(); PagingPredicate pagingPredicate = new PagingPredicate(predicate, salaryComparator, pageSize); Collection<Employee> employees; List<Employee> employeeList; do { employees = map.values(pagingPredicate); employeeList = fillListWithQueryResultSet(employees); Employee nextEmployee; Employee currentEmployee; for (int i = 0; i < employeeList.size() - 1; i++) { currentEmployee = employeeList.get(i); nextEmployee = employeeList.get(i + 1); // check the order & max salary assertTrue(format(baseAssertMessage, currentEmployee.getSalary(), predicate), currentEmployee.getSalary() <= nextEmployee.getSalary() && nextEmployee.getSalary() < maxSalary); } pagingPredicate.nextPage(); } while (!employees.isEmpty()); state.operationCounter.pagePredicateCount++; }
@SuppressWarnings("unchecked") public static ResultSet getSortedQueryResultSet(List<Map.Entry> list, PagingPredicate pagingPredicate, IterationType iterationType) { if (list.isEmpty()) { return new ResultSet(); } Comparator<Map.Entry> comparator = SortingUtil.newComparator(pagingPredicate.getComparator(), iterationType); Collections.sort(list, comparator); Map.Entry<Integer, Map.Entry> nearestAnchorEntry = getNearestAnchorEntry(pagingPredicate); int nearestPage = nearestAnchorEntry.getKey(); int page = pagingPredicate.getPage(); int pageSize = pagingPredicate.getPageSize(); long begin = pageSize * ((long) page - nearestPage - 1); int size = list.size(); if (begin > size) { return new ResultSet(); } long end = begin + pageSize; if (end > size) { end = size; } setAnchor(list, pagingPredicate, nearestPage); // it's safe to cast begin and end back to int here since they are limited by the list size List<Map.Entry> subList = list.subList((int) begin, (int) end); return new ResultSet(subList, iterationType); }
public int compare(QueryableEntry entry1, QueryableEntry entry2) { return SortingUtil.compare(pagingPredicate.getComparator(), pagingPredicate.getIterationType(), entry1, entry2); } };
@Override public void handleRequest(HttpServerExchange exchange) throws Exception { IMap<String, Client> clients = CacheStartupHookProvider.hz.getMap("clients"); Deque<String> clientNameDeque = exchange.getQueryParameters().get("clientName"); String clientName = clientNameDeque == null? "%" : clientNameDeque.getFirst() + "%"; int page = Integer.valueOf(exchange.getQueryParameters().get("page").getFirst()) - 1; Deque<String> pageSizeDeque = exchange.getQueryParameters().get("pageSize"); int pageSize = pageSizeDeque == null? 10 : Integer.valueOf(pageSizeDeque.getFirst()); LikePredicate likePredicate = new LikePredicate("clientName", clientName); PagingPredicate pagingPredicate = new PagingPredicate(likePredicate, new ClientComparator(), pageSize); pagingPredicate.setPage(page); Collection<Client> values = clients.values(pagingPredicate); List results = new ArrayList(); for (Client value : values) { Client c = Client.copyClient(value); c.setClientSecret(null); results.add(c); } exchange.getResponseHeaders().add(new HttpString("Content-Type"), "application/json"); exchange.getResponseSender().send(Config.getInstance().getMapper().writeValueAsString(results)); processAudit(exchange); } }
private PagingPredicate createNewPredicate() { return new PagingPredicate(innerPredicate, pageSize); } }
PagingPredicate pagingPredicate = new PagingPredicate( pred , 5); Collection<Employee> employees; pagingPredicate.nextPage();
private static void setAnchor(List<Map.Entry> list, PagingPredicate pagingPredicate, int nearestPage) { if (list.isEmpty()) { return; } int size = list.size(); int pageSize = pagingPredicate.getPageSize(); int page = pagingPredicate.getPage(); for (int i = pageSize; i <= size && nearestPage < page; i += pageSize) { Map.Entry anchor = list.get(i - 1); nearestPage++; PagingPredicateAccessor.setAnchor(pagingPredicate, nearestPage, anchor); } }
public static boolean compareAnchor(PagingPredicate pagingPredicate, QueryableEntry queryEntry, Map.Entry<Integer, Map.Entry> nearestAnchorEntry) { if (pagingPredicate == null) { return true; } Map.Entry anchor = nearestAnchorEntry.getValue(); if (anchor == null) { return true; } Comparator<Map.Entry> comparator = pagingPredicate.getComparator(); IterationType iterationType = pagingPredicate.getIterationType(); return SortingUtil.compare(comparator, iterationType, anchor, queryEntry) < 0; }
@SuppressWarnings("unchecked") @Override public void handleRequest(HttpServerExchange exchange) throws Exception { IMap<String, User> users = CacheStartupHookProvider.hz.getMap("users"); Deque<String> userIdDeque = exchange.getQueryParameters().get("userId"); String userId = userIdDeque == null? "%" : userIdDeque.getFirst() + "%"; int page = Integer.valueOf(exchange.getQueryParameters().get("page").getFirst()) - 1; Deque<String> pageSizeDeque = exchange.getQueryParameters().get("pageSize"); int pageSize = pageSizeDeque == null? 10 : Integer.valueOf(pageSizeDeque.getFirst()); LikePredicate likePredicate = new LikePredicate("userId", userId); PagingPredicate pagingPredicate = new PagingPredicate(likePredicate, new UserComparator(), pageSize); pagingPredicate.setPage(page); Collection<User> values = users.values(pagingPredicate); for (User value : values) { value.setPassword(null); } exchange.getResponseHeaders().add(Headers.CONTENT_TYPE, "application/json"); exchange.getResponseSender().send(Config.getInstance().getMapper().writeValueAsString(values)); processAudit(exchange); } }