/** * Creates a new instance which a limited amount of results. * <p><a href="http://dev.commercetools.com/http-api.html#limit">Limit restrictions on the API.</a></p> * @param limit the maximum amount of items of {@code T} which should be included in the {@link PagedQueryResult}. * @return copy of this item with modified limit settings * @see PagedQueryResult#getResults() */ default C withLimit(final long limit) { return withLimit(Long.valueOf(limit)); }
public static <I> QueryDsl<I> nextPage(final QueryDsl<I> current) { final long oldOffset = current.offset().orElse(0L); return current.withOffset(oldOffset + 1); } }
default QueryDsl<T> withSort(final Sort<T> sort) { return withSort(asList(sort)); }
public QueryDslBuilder(final QueryDsl<I> template) { this(template.endpoint(), template.resultMapper()); predicate = template.predicate(); sort = template.sort(); limit = template.limit(); offset = template.offset(); expansionPaths = template.expansionPaths(); additionalQueryParameters = template.additionalQueryParameters(); }
ForAllSubscription(final QueryDsl<T, C> seedQuery, final SphereClient sphereClient, final Subscriber<? super T> subscriber) { this.seedQuery = seedQuery.withSort(QuerySort.of("id asc")).withFetchTotal(false).withLimit(ELEMENTS_PER_QUERY); this.sphereClient = QueueSphereClientDecorator.of(sphereClient, 1, false); this.subscriber = subscriber; }
/** * Gets the results of {@link this} instance's query for a specific page with {@code pageNumber}. * * @param client the CTP client that the query is run on. * @param pageNumber the page number to get the results for. * @return a future containing the results of the requested page of applying the query. */ @Nonnull private CompletionStage<PagedQueryResult<T>> queryPage(final SphereClient client, final long pageNumber) { final QueryDsl<T, C> query = baseQuery .withOffset(pageNumber * pageSize) .withLimit(pageSize); return client.execute(query); }
private void fetchNewElements(final long initialRemainingElements) { final long bulkSize = bulkSize(initialRemainingElements); final Query<T> query = (lastId == null ? seedQuery : seedQuery.withPredicates(ListUtils.listOf(seedQuery.predicates(), QueryPredicate.of(format("id > \"%s\"", lastId))))).withLimit(bulkSize); try { final List<T> results = sphereClient.execute(query).toCompletableFuture().join().getResults(); if (results.size() == 0) { executor.execute(() -> { subscriber.onComplete(); cancel(); }); } else { final T t = results.get(results.size() - 1); lastId = idExtractor.apply(t); final Queue<T> queue = new LinkedList<>(results); final long stillmissingCount = feedFromQueue(initialRemainingElements, queue); elementsQueue = queue; if (stillmissingCount > 0) { fetchNewElements(stillmissingCount); } } } catch (final CompletionException e) { cancel(); subscriber.onError(e); } }
RunningSubscription(final QueryDsl<T, C> seedQuery, final Function<T, String> idExtractor, final SphereClient sphereClient, final Subscriber<? super T> subscriber, final SubscriptionImpl<T, C> tcSubscription) { this.idExtractor = idExtractor; this.tcSubscription = tcSubscription; this.seedQuery = seedQuery.withSort(QuerySort.of("id asc")).withFetchTotal(false); this.sphereClient = QueueSphereClientDecorator.of(sphereClient, 1, false); this.subscriber = subscriber; }
/** * Returns a new query with the new offset as offset. * * @param offset the number of items which should be omitted in the query result. * @return a new query * @throws java.lang.IllegalArgumentException if offset is * not between {@value io.sphere.sdk.queries.Query#MIN_OFFSET} and {@value io.sphere.sdk.queries.Query#MAX_OFFSET}. */ default C withOffset(final long offset) { return withOffset(Long.valueOf(offset)); } }
default QueryDsl<T> plusExpansionPath(final ExpansionPath<T> expansionPath) { return withExpansionPaths(listOf(expansionPaths(), expansionPath)); }