Skip to content

Commit 023229a

Browse files
authored
fix: query result start with startCursor if specified (#207)
1 parent 2cacc10 commit 023229a

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

google-cloud-datastore/src/main/java/com/google/cloud/datastore/QueryResultsImpl.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,7 @@ private void sendRequest() {
7171
requestPb.setPartitionId(partitionIdPb);
7272
query.populatePb(requestPb);
7373
runQueryResponsePb = datastore.runQuery(requestPb.build());
74-
mostRecentQueryPb = runQueryResponsePb.getQuery();
75-
if (mostRecentQueryPb == null) {
76-
mostRecentQueryPb = requestPb.getQuery();
77-
}
74+
mostRecentQueryPb = requestPb.getQuery();
7875
moreResults = runQueryResponsePb.getBatch().getMoreResults();
7976
lastBatch = moreResults != MoreResultsType.NOT_FINISHED;
8077
entityResultPbIter = runQueryResponsePb.getBatch().getEntityResultsList().iterator();

google-cloud-datastore/src/test/java/com/google/cloud/datastore/ITDatastoreTest.java

+21
Original file line numberDiff line numberDiff line change
@@ -1242,4 +1242,25 @@ public void testGqlQueryWithNullBinding() {
12421242
}
12431243
assertEquals(1, count);
12441244
}
1245+
1246+
@Test
1247+
public void testQueryWithStartCursor() {
1248+
Entity entity1 =
1249+
Entity.newBuilder(Key.newBuilder(PROJECT_ID, KIND1, "name-01").build()).build();
1250+
Entity entity2 =
1251+
Entity.newBuilder(Key.newBuilder(PROJECT_ID, KIND1, "name-02").build()).build();
1252+
Entity entity3 =
1253+
Entity.newBuilder(Key.newBuilder(PROJECT_ID, KIND1, "name-03").build()).build();
1254+
datastore.put(entity1, entity2, entity3);
1255+
QueryResults<Entity> run1 = datastore.run(Query.newEntityQueryBuilder().setKind(KIND1).build());
1256+
run1.next();
1257+
Cursor cursor1 = run1.getCursorAfter();
1258+
assertNotNull(cursor1);
1259+
QueryResults<Entity> run2 =
1260+
datastore.run(Query.newEntityQueryBuilder().setKind(KIND1).setStartCursor(cursor1).build());
1261+
Cursor cursor2 = run2.getCursorAfter();
1262+
assertNotNull(cursor2);
1263+
assertEquals(cursor2, cursor1);
1264+
datastore.delete(entity1.getKey(), entity2.getKey(), entity3.getKey());
1265+
}
12451266
}

google-cloud-datastore/src/test/java/com/google/cloud/datastore/it/ITDatastoreTest.java

+22
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.google.cloud.Timestamp;
2929
import com.google.cloud.datastore.Batch;
3030
import com.google.cloud.datastore.BooleanValue;
31+
import com.google.cloud.datastore.Cursor;
3132
import com.google.cloud.datastore.Datastore;
3233
import com.google.cloud.datastore.DatastoreException;
3334
import com.google.cloud.datastore.DatastoreOptions;
@@ -898,4 +899,25 @@ public void testGqlQueryWithNullBinding() {
898899
assertEquals(ENTITY1, results.next());
899900
assertFalse(results.hasNext());
900901
}
902+
903+
@Test
904+
public void testQueryWithStartCursor() {
905+
Entity entity1 =
906+
Entity.newBuilder(Key.newBuilder(PROJECT_ID, KIND1, "name-01").build()).build();
907+
Entity entity2 =
908+
Entity.newBuilder(Key.newBuilder(PROJECT_ID, KIND1, "name-02").build()).build();
909+
Entity entity3 =
910+
Entity.newBuilder(Key.newBuilder(PROJECT_ID, KIND1, "name-03").build()).build();
911+
DATASTORE.put(entity1, entity2, entity3);
912+
QueryResults<Entity> run1 = DATASTORE.run(Query.newEntityQueryBuilder().setKind(KIND1).build());
913+
run1.next();
914+
Cursor cursor1 = run1.getCursorAfter();
915+
assertNotNull(cursor1);
916+
QueryResults<Entity> run2 =
917+
DATASTORE.run(Query.newEntityQueryBuilder().setKind(KIND1).setStartCursor(cursor1).build());
918+
Cursor cursor2 = run2.getCursorAfter();
919+
assertNotNull(cursor2);
920+
assertEquals(cursor2, cursor1);
921+
DATASTORE.delete(entity1.getKey(), entity2.getKey(), entity3.getKey());
922+
}
901923
}

0 commit comments

Comments
 (0)