2828import com .google .protobuf .Any ;
2929import com .google .protobuf .FieldMask ;
3030import com .google .protobuf .Message ;
31+ import io .spine .client .OrderBy ;
32+ import io .spine .client .ResponseFormat ;
3133import io .spine .server .entity .EntityRecord ;
3234import io .spine .server .entity .storage .ColumnTypeRegistry ;
3335import io .spine .server .entity .storage .EntityQuery ;
@@ -128,34 +130,23 @@ protected Optional<EntityRecord> readRecord(I id) {
128130 }
129131
130132 @ Override
131- protected Iterator <@ Nullable EntityRecord > readMultipleRecords (Iterable <I > ids ) {
132- return idLookup .findActive (ids );
133- }
134-
135- @ Override
136- protected Iterator <@ Nullable EntityRecord > readMultipleRecords (Iterable <I > ids ,
137- FieldMask fieldMask ) {
138- return idLookup .findActive (ids , fieldMask );
139- }
140-
141- @ Override
142- protected Iterator <EntityRecord > readAllRecords () {
143- Iterator <EntityRecord > result = readAllRecords (FieldMask .getDefaultInstance ());
133+ protected Iterator <EntityRecord > readAllRecords (ResponseFormat format ) {
134+ Iterator <EntityRecord > result = queryLookup .find (activeEntityQueryParams (this ), format );
144135 return result ;
145136 }
146137
147138 @ Override
148- protected Iterator <EntityRecord > readAllRecords (FieldMask fieldMask ) {
149- Iterator <EntityRecord > result = queryLookup .find (activeEntityQueryParams (this ), fieldMask );
150- return result ;
139+ protected Iterator <EntityRecord > readAllRecords (EntityQuery <I > query , ResponseFormat format ) {
140+ if (isQueryForAll (query )) {
141+ return readAll (format );
142+ }
143+ return queryBy (query , format );
151144 }
152145
153146 @ Override
154- protected Iterator <EntityRecord > readAllRecords (EntityQuery <I > query , FieldMask fieldMask ) {
155- if (isQueryForAll (query )) {
156- return readAll (fieldMask );
157- }
158- return queryBy (query , fieldMask );
147+ protected Iterator <@ Nullable EntityRecord > readMultipleRecords (Iterable <I > ids ,
148+ FieldMask fieldMask ) {
149+ return idLookup .findActive (ids , fieldMask );
159150 }
160151
161152 @ SuppressWarnings ("PMD.SimplifyBooleanReturns" )
@@ -167,19 +158,7 @@ private static <I> boolean isQueryForAll(EntityQuery<I> query) {
167158 }
168159
169160 QueryParameters params = query .getParameters ();
170- if (notEmpty (params )) {
171- return false ;
172- }
173-
174- if (params .ordered ()) {
175- return false ;
176- }
177-
178- if (params .limited ()) {
179- return false ;
180- }
181-
182- return true ;
161+ return !notEmpty (params );
183162 }
184163
185164 /**
@@ -190,17 +169,18 @@ private static <I> boolean isQueryForAll(EntityQuery<I> query) {
190169 *
191170 * @param entityQuery
192171 * the {@link EntityQuery} to query the Datastore by
193- * @param fieldMask
194- * the {@code FieldMask} to apply to all the retrieved entity states
172+ * @param responseFormat
173+ * the {@code ResponseFormat} according to which the result is retrieved
195174 * @return an iterator over the resulting entity records
196175 */
197- private Iterator <EntityRecord > queryBy (EntityQuery <I > entityQuery , FieldMask fieldMask ) {
176+ private Iterator <EntityRecord > queryBy (EntityQuery <I > entityQuery ,
177+ ResponseFormat responseFormat ) {
198178 EntityQuery <I > completeQuery = includeLifecycle (entityQuery );
199179 Collection <I > idFilter = completeQuery .getIds ();
200180 QueryParameters params = completeQuery .getParameters ();
201181 Iterator <EntityRecord > result = idFilter .isEmpty ()
202- ? queryLookup .find (params , fieldMask )
203- : queryByIdsAndColumns (idFilter , params , fieldMask );
182+ ? queryLookup .find (params , responseFormat )
183+ : queryByIdsAndColumns (idFilter , params , responseFormat );
204184 return result ;
205185 }
206186
@@ -220,20 +200,23 @@ private EntityQuery<I> includeLifecycle(EntityQuery<I> entityQuery) {
220200 * the IDs to search by
221201 * @param params
222202 * the additional query parameters
223- * @param fieldMask
224- * the {@code FieldMask} to apply to all the retrieved entity states
203+ * @param format
204+ * the format of the response including a field mask to apply, response size limit
205+ * and ordering
225206 * @return an iterator over the resulting entity records
226207 */
227208 private Iterator <EntityRecord > queryByIdsAndColumns (Collection <I > acceptableIds ,
228209 QueryParameters params ,
229- FieldMask fieldMask ) {
210+ ResponseFormat format ) {
230211 Predicate <Entity > inMemPredicate = columnPredicate (params );
231- if (params .ordered ()) {
232- if (params .limited ()) {
233- return idLookup .find (acceptableIds , fieldMask , inMemPredicate ,
234- params .orderBy (), params .limit ());
212+ FieldMask fieldMask = format .getFieldMask ();
213+ if (format .hasOrderBy ()) {
214+ OrderBy order = format .getOrderBy ();
215+ int limit = format .getLimit ();
216+ if (limit > 0 ) {
217+ return idLookup .find (acceptableIds , fieldMask , inMemPredicate , order , limit );
235218 }
236- return idLookup .find (acceptableIds , fieldMask , inMemPredicate , params . orderBy () );
219+ return idLookup .find (acceptableIds , fieldMask , inMemPredicate , order );
237220 }
238221 return idLookup .find (acceptableIds , fieldMask , inMemPredicate );
239222 }
0 commit comments