11package com .commercetools .sync .commons .utils ;
22
33import io .sphere .sdk .client .SphereClient ;
4+ import io .sphere .sdk .models .Resource ;
45import io .sphere .sdk .queries .QueryDsl ;
56
67import javax .annotation .Nonnull ;
@@ -17,82 +18,102 @@ private CtpQueryUtils() {
1718 }
1819
1920 /**
20- * Queries all elements matching a query by using an offset based pagination with page size 500.
21- * The method takes a callback {@link Function} that returns a result of type {@code <S>} that is returned on every
22- * page of elements queried. Eventually, the method returns a {@link CompletionStage} that contains a list of all
23- * the results of the callbacks returned from every page.
21+ * Queries all elements matching a query by using a limit based pagination with a combination of id sorting and a
22+ * page size 500. More on the algorithm can be found here: http://dev.commercetools.com/http-api.html#offset.
2423 *
25- * @param client commercetools client
26- * @param query query containing predicates and expansion paths
27- * @param callBack callback function that is called on every page queried.
28- * @param <T> type of one query result element
29- * @param <C> type of the query
30- * @param <S> type of the returned result of the callback function on every page.
31- * @return elements
24+ * <p>The method takes a callback {@link Function} that returns a result of type {@code <S>} that is returned on
25+ * every page of elements queried. Eventually, the method returns a {@link CompletionStage} that contains a list of
26+ * all the results of the callbacks returned from every page.
27+ *
28+ * <p>NOTE: This method fetches all paged results sequentially as opposed to fetching the pages in parallel.
29+ *
30+ * @param client commercetools client
31+ * @param query query containing predicates and expansion paths
32+ * @param pageMapper callback function that is called on every page queried
33+ * @param <T> type of one query result element
34+ * @param <C> type of the query
35+ * @param <S> type of the returned result of the callback function on every page.
36+ * @return a completion stage containing a list of mapped pages as a result.
3237 */
3338 @ Nonnull
34- public static <T , C extends QueryDsl <T , C >, S > CompletionStage <List <S >>
39+ public static <T extends Resource , C extends QueryDsl <T , C >, S > CompletionStage <List <S >>
3540 queryAll (@ Nonnull final SphereClient client , @ Nonnull final QueryDsl <T , C > query ,
36- @ Nonnull final Function <List <T >, S > callBack ) {
37- return queryAll (client , query , callBack , DEFAULT_PAGE_SIZE );
41+ @ Nonnull final Function <List <T >, S > pageMapper ) {
42+ return queryAll (client , query , pageMapper , DEFAULT_PAGE_SIZE );
3843 }
3944
4045 /**
41- * Queries all elements matching a query by using an offset based pagination with page size 500. The method takes a
42- * consumer {@link Consumer} that is applied on on every page of elements queried.
46+ * Queries all elements matching a query by using a limit based pagination with a combination of id sorting and a
47+ * page size 500. More on the algorithm can be found here: http://dev.commercetools.com/http-api.html#offset
48+ *
49+ * <p>The method takes a consumer {@link Consumer} that is applied on on every page of elements queried.
50+ *
51+ * <p>NOTE: This method fetches all paged results sequentially as opposed to fetching the pages in parallel.
4352 *
44- * @param client commercetools client
45- * @param query query containing predicates and expansion paths
46- * @param consumer that is applied on every page queried.
47- * @param <T> type of one query result element
48- * @param <C> type of the query
49- * @return elements
53+ * @param client commercetools client
54+ * @param query query containing predicates and expansion paths
55+ * @param pageConsumer consumer applied on every page queried
56+ * @param <T> type of one query result element
57+ * @param <C> type of the query
58+ * @return a completion stage containing void as a result after the consumer was applied on all pages.
5059 */
5160 @ Nonnull
52- public static <T , C extends QueryDsl <T , C >> CompletionStage <Void >
61+ public static <T extends Resource , C extends QueryDsl <T , C >> CompletionStage <Void >
5362 queryAll (@ Nonnull final SphereClient client , @ Nonnull final QueryDsl <T , C > query ,
54- @ Nonnull final Consumer <List <T >> consumer ) {
55- return queryAll (client , query , consumer , DEFAULT_PAGE_SIZE );
63+ @ Nonnull final Consumer <List <T >> pageConsumer ) {
64+ return queryAll (client , query , pageConsumer , DEFAULT_PAGE_SIZE );
5665 }
5766
5867 /**
59- * Queries all elements matching a query by using an offset based pagination. The method takes a callback
60- * {@link Function} that returns a result of type {@code <S>} that is returned on every page of elements queried.
61- * Eventually, the method returns a {@link CompletionStage} that contains a list of all the results of the
62- * callbacks returned from every page.
68+ * Queries all elements matching a query by using a limit based pagination with a combination of id sorting and the
69+ * supplied {@code pageSize}.
70+ * More on the algorithm can be found here: http://dev.commercetools.com/http-api.html#offset.
6371 *
64- * @param client commercetools client
65- * @param query query containing predicates and expansion paths
66- * @param callback callback function that is called on every page queried.
67- * @param <T> type of one query result element
68- * @param <C> type of the query
69- * @param <S> type of the returned result of the callback function on every page.
70- * @param pageSize the page size.
71- * @return elements
72+ * <p>The method takes a callback {@link Function} that returns a result of type {@code <S>} that is returned on
73+ * every page of elements queried. Eventually, the method returns a {@link CompletionStage} that contains a list of
74+ * all the results of the callbacks returned from every page.
75+ *
76+ * <p>NOTE: This method fetches all paged results sequentially as opposed to fetching the pages in parallel.
77+ *
78+ * @param client commercetools client
79+ * @param query query containing predicates and expansion paths
80+ * @param pageMapper callback function that is called on every page queried
81+ * @param <T> type of one query result element
82+ * @param <C> type of the query
83+ * @param <S> type of the returned result of the callback function on every page.
84+ * @param pageSize the page size.
85+ * @return a completion stage containing a list of mapped pages as a result.
7286 */
7387 @ Nonnull
74- public static <T , C extends QueryDsl <T , C >, S > CompletionStage <List <S >>
88+ public static <T extends Resource , C extends QueryDsl <T , C >, S > CompletionStage <List <S >>
7589 queryAll (@ Nonnull final SphereClient client , @ Nonnull final QueryDsl <T , C > query ,
76- @ Nonnull final Function <List <T >, S > callback , final int pageSize ) {
77- return QueryAll .of (query , pageSize ).run (client , callback );
90+ @ Nonnull final Function <List <T >, S > pageMapper , final int pageSize ) {
91+ final QueryAll <T , C , S > queryAll = QueryAll .of (client , query , pageSize );
92+ return queryAll .run (pageMapper );
7893 }
7994
8095 /**
81- * Queries all elements matching a query by using an offset based pagination. The method takes a consumer
82- * {@link Consumer} that is applied on on every page of elements queried.
96+ * Queries all elements matching a query by using a limit based pagination with a combination of id sorting and the
97+ * supplied {@code pageSize}.
98+ * More on the algorithm can be found here: http://dev.commercetools.com/http-api.html#offset
99+ *
100+ * <p>The method takes a consumer {@link Consumer} that is applied on on every page of elements queried.
101+ *
102+ * <p>NOTE: This method fetches all paged results sequentially as opposed to fetching the pages in parallel.
83103 *
84- * @param client commercetools client
85- * @param query query containing predicates and expansion paths
86- * @param consumer that is applied on every page queried.
87- * @param <T> type of one query result element
88- * @param <C> type of the query
89- * @param pageSize the page size.
90- * @return elements
104+ * @param client commercetools client
105+ * @param query query containing predicates and expansion paths
106+ * @param pageConsumer consumer applied on every page queried
107+ * @param <T> type of one query result element
108+ * @param <C> type of the query
109+ * @param pageSize the page size
110+ * @return a completion stage containing void as a result after the consumer was applied on all pages.
91111 */
92112 @ Nonnull
93- public static <T , C extends QueryDsl <T , C >> CompletionStage <Void >
113+ public static <T extends Resource , C extends QueryDsl <T , C >> CompletionStage <Void >
94114 queryAll (@ Nonnull final SphereClient client , @ Nonnull final QueryDsl <T , C > query ,
95- @ Nonnull final Consumer <List <T >> consumer , final int pageSize ) {
96- return QueryAll .of (query , pageSize ).run (client , consumer );
115+ @ Nonnull final Consumer <List <T >> pageConsumer , final int pageSize ) {
116+ final QueryAll <T , C , Void > queryAll = QueryAll .of (client , query , pageSize );
117+ return queryAll .run (pageConsumer );
97118 }
98119}
0 commit comments