Skip to content

Commit c02f8d4

Browse files
committed
---
yaml --- r: 59 b: refs/heads/master c: 805d2ea h: refs/heads/master i: 57: 579f183 55: 7dd7d1f v: v3
1 parent e6163c4 commit c02f8d4

5 files changed

Lines changed: 249 additions & 86 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: a18313fbf6c8816c8e0cfadcfd94a56942449f35
2+
refs/heads/master: 805d2ea724d4847bfe3f6726d4746573973a3b8e

trunk/src/main/java/com/google/gcloud/datastore/QueryResult.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,20 @@
44

55
/**
66
* The result of a Google Cloud Datastore query submission.
7-
* When the result is not typed it is possible to cast it to its appropriate type according to
8-
* the {@link #getType} result.
7+
* When result is not typed it is possible to cast it to its appropriate type according to
8+
* the {@link #resultClass} value.
99
*
10-
* @param V the type of values the result holds.
10+
* @param V the type of the results value.
1111
*/
1212
public interface QueryResult<T> extends Iterator<T> {
1313

1414
/**
15-
* Returns the actual type of the result's values.
16-
* When needed the result could be casted accordingly:
17-
* <pre> {@code
18-
* Type.FULL -> (QueryResult<Entity>)
19-
* Type.PROJECTION -> (QueryResult<PartialEntity>)
20-
* Type.KEY_ONLY -> (QueryResult<Key>)
21-
* } </pre>
15+
* Returns the actual class of the result's values.
2216
*/
23-
Query.Type getType();
17+
Class<?> resultClass();
2418

2519
/**
26-
* Return the Cursor for the next result. Not currently implemented (depends on v1beta3).
20+
* Returns the Cursor for the next result. Not currently implemented (depends on v1beta3).
2721
*/
28-
Cursor getCursor();
22+
Cursor cursor();
2923
}

trunk/src/main/java/com/google/gcloud/datastore/QueryResultImpl.java

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import com.google.api.client.repackaged.com.google.common.base.Preconditions;
44
import com.google.api.services.datastore.DatastoreV1;
5+
import com.google.common.collect.AbstractIterator;
56
import com.google.common.collect.ImmutableMap;
6-
import com.google.protobuf.ByteString;
77

88
import java.util.Iterator;
99

10-
class QueryResultImpl<T> implements QueryResult<T> {
10+
class QueryResultImpl<T> extends AbstractIterator<T> implements QueryResult<T> {
1111

1212
private static final ImmutableMap<DatastoreV1.EntityResult.ResultType, Query.Type>
1313
RESULT_TYPE_CONVERTER;
@@ -19,8 +19,6 @@ class QueryResultImpl<T> implements QueryResult<T> {
1919
private Query.Type type;
2020
private DatastoreV1.QueryResultBatch resultPb;
2121
private Iterator<DatastoreV1.EntityResult> entityResultPbIter;
22-
private ByteString endCursor;
23-
private int count;
2422

2523
static {
2624
ImmutableMap.Builder<DatastoreV1.EntityResult.ResultType, Query.Type> builder =
@@ -57,45 +55,31 @@ private DatastoreV1.QueryResultBatch sendRequest() {
5755
query.populatePb(requestPb);
5856
resultPb = datastore.runQuery(requestPb.build()).getBatch();
5957
entityResultPbIter = resultPb.getEntityResultList().iterator();
60-
if (DatastoreV1.QueryResultBatch.MoreResultsType.NOT_FINISHED == resultPb.getMoreResults()) {
61-
endCursor = resultPb.getEndCursor();
62-
} else {
63-
endCursor = null;
64-
}
6558
type = RESULT_TYPE_CONVERTER.get(resultPb.getEntityResultType());
6659
Preconditions.checkState(resultClass.isAssignableFrom(type.resultClass()),
6760
"Unexpected result type");
6861
return resultPb;
6962
}
7063

7164
@Override
72-
public boolean hasNext() {
73-
return entityResultPbIter.hasNext() || endCursor != null;
74-
}
75-
76-
@Override
77-
public T next() {
78-
if (!hasNext() && endCursor != null) {
65+
protected T computeNext() {
66+
while (!entityResultPbIter.hasNext()
67+
&& resultPb.getMoreResults() == DatastoreV1.QueryResultBatch.MoreResultsType.NOT_FINISHED) {
7968
query = query.nextQuery(resultPb);
8069
sendRequest();
8170
}
82-
DatastoreV1.Entity entity = entityResultPbIter.next().getEntity();
83-
count++;
84-
return type.convert(entity);
85-
}
86-
87-
@Override
88-
public void remove() {
89-
throw new UnsupportedOperationException("QueryResult is read-only");
71+
return entityResultPbIter.hasNext()
72+
? type.<T>convert(entityResultPbIter.next().getEntity())
73+
: endOfData();
9074
}
9175

9276
@Override
93-
public Query.Type getType() {
94-
return type;
77+
public Class<?> resultClass() {
78+
return type.resultClass().value();
9579
}
9680

9781
@Override
98-
public Cursor getCursor() {
82+
public Cursor cursor() {
9983
// TODO(ozarov): implement when v1beta3 is available
10084
return null;
10185
}

0 commit comments

Comments
 (0)