@java.lang.Override public int hashCode() { if (memoizedHashCode != 0) { return memoizedHashCode; } int hash = 41; hash = (19 * hash) + getDescriptor().hashCode(); hash = (37 * hash) + SKIPPED_RESULTS_FIELD_NUMBER; hash = (53 * hash) + getSkippedResults(); hash = (37 * hash) + SKIPPED_CURSOR_FIELD_NUMBER; hash = (53 * hash) + getSkippedCursor().hashCode(); hash = (37 * hash) + ENTITY_RESULT_TYPE_FIELD_NUMBER; hash = (53 * hash) + entityResultType_; if (getEntityResultsCount() > 0) { hash = (37 * hash) + ENTITY_RESULTS_FIELD_NUMBER; hash = (53 * hash) + getEntityResultsList().hashCode(); } hash = (37 * hash) + END_CURSOR_FIELD_NUMBER; hash = (53 * hash) + getEndCursor().hashCode(); hash = (37 * hash) + MORE_RESULTS_FIELD_NUMBER; hash = (53 * hash) + moreResults_; hash = (37 * hash) + SNAPSHOT_VERSION_FIELD_NUMBER; hash = (53 * hash) + com.google.protobuf.Internal.hashLong(getSnapshotVersion()); hash = (29 * hash) + unknownFields.hashCode(); memoizedHashCode = hash; return hash; }
private void sendRequest() { com.google.datastore.v1.RunQueryRequest.Builder requestPb = com.google.datastore.v1.RunQueryRequest.newBuilder(); if (readOptionsPb != null) { requestPb.setReadOptions(readOptionsPb); } requestPb.setPartitionId(partitionIdPb); query.populatePb(requestPb); runQueryResponsePb = datastore.runQuery(requestPb.build()); mostRecentQueryPb = runQueryResponsePb.getQuery(); if (mostRecentQueryPb == null) { mostRecentQueryPb = requestPb.getQuery(); } lastBatch = runQueryResponsePb.getBatch().getMoreResults() != MoreResultsType.NOT_FINISHED; entityResultPbIter = runQueryResponsePb.getBatch().getEntityResultsList().iterator(); actualResultType = ResultType.fromPb(runQueryResponsePb.getBatch().getEntityResultType()); if (Objects.equals(queryResultType, ResultType.PROJECTION_ENTITY)) { // projection entity can represent all type of results actualResultType = ResultType.PROJECTION_ENTITY; } Preconditions.checkState( queryResultType.isAssignableFrom(actualResultType), "Unexpected result type " + actualResultType + " vs " + queryResultType); }
@java.lang.Override public Builder newBuilderForType() { return newBuilder(); }
@java.lang.Override public boolean equals(final java.lang.Object obj) { if (obj == this) { return true; } if (!(obj instanceof com.google.datastore.v1.QueryResultBatch)) { return super.equals(obj); } com.google.datastore.v1.QueryResultBatch other = (com.google.datastore.v1.QueryResultBatch) obj; boolean result = true; result = result && (getSkippedResults() == other.getSkippedResults()); result = result && getSkippedCursor().equals(other.getSkippedCursor()); result = result && entityResultType_ == other.entityResultType_; result = result && getEntityResultsList().equals(other.getEntityResultsList()); result = result && getEndCursor().equals(other.getEndCursor()); result = result && moreResults_ == other.moreResults_; result = result && (getSnapshotVersion() == other.getSnapshotVersion()); result = result && unknownFields.equals(other.unknownFields); return result; }
/** * * * <pre> * A cursor that points to the position after the last skipped result. * Will be set when `skipped_results` != 0. * </pre> * * <code>bytes skipped_cursor = 3;</code> */ public Builder clearSkippedCursor() { skippedCursor_ = getDefaultInstance().getSkippedCursor(); onChanged(); return this; }
/** * * * <pre> * A cursor that points to the position after the last result in the batch. * </pre> * * <code>bytes end_cursor = 4;</code> */ public Builder clearEndCursor() { endCursor_ = getDefaultInstance().getEndCursor(); onChanged(); return this; }
.build(); batch = datastore.runQuery(scatterRequest).getBatch(); for (EntityResult result : batch.getEntityResultsList()) { keySplits.add(result.getEntity().getKey()); scatterPointQuery.setStartCursor(batch.getEndCursor()); scatterPointQuery.getLimitBuilder().setValue( scatterPointQuery.getLimit().getValue() - batch.getEntityResultsCount()); } while (batch.getMoreResults() == MoreResultsType.NOT_FINISHED); Collections.sort(keySplits, DatastoreHelper.getKeyComparator()); return keySplits;
public Builder mergeFrom(com.google.datastore.v1.QueryResultBatch other) { if (other == com.google.datastore.v1.QueryResultBatch.getDefaultInstance()) return this; if (other.getSkippedResults() != 0) { setSkippedResults(other.getSkippedResults()); if (other.getSkippedCursor() != com.google.protobuf.ByteString.EMPTY) { setSkippedCursor(other.getSkippedCursor()); setEntityResultTypeValue(other.getEntityResultTypeValue()); if (other.getEndCursor() != com.google.protobuf.ByteString.EMPTY) { setEndCursor(other.getEndCursor()); setMoreResultsValue(other.getMoreResultsValue()); if (other.getSnapshotVersion() != 0L) { setSnapshotVersion(other.getSnapshotVersion());
@java.lang.Override public int hashCode() { if (memoizedHashCode != 0) { return memoizedHashCode; } int hash = 41; hash = (19 * hash) + getDescriptorForType().hashCode(); hash = (37 * hash) + SKIPPED_RESULTS_FIELD_NUMBER; hash = (53 * hash) + getSkippedResults(); hash = (37 * hash) + SKIPPED_CURSOR_FIELD_NUMBER; hash = (53 * hash) + getSkippedCursor().hashCode(); hash = (37 * hash) + ENTITY_RESULT_TYPE_FIELD_NUMBER; hash = (53 * hash) + entityResultType_; if (getEntityResultsCount() > 0) { hash = (37 * hash) + ENTITY_RESULTS_FIELD_NUMBER; hash = (53 * hash) + getEntityResultsList().hashCode(); } hash = (37 * hash) + END_CURSOR_FIELD_NUMBER; hash = (53 * hash) + getEndCursor().hashCode(); hash = (37 * hash) + MORE_RESULTS_FIELD_NUMBER; hash = (53 * hash) + moreResults_; hash = (37 * hash) + SNAPSHOT_VERSION_FIELD_NUMBER; hash = (53 * hash) + com.google.protobuf.Internal.hashLong( getSnapshotVersion()); hash = (29 * hash) + unknownFields.hashCode(); memoizedHashCode = hash; return hash; }
@Override StructuredQuery<V> nextQuery(com.google.datastore.v1.RunQueryResponse responsePb) { Builder<V> builder = toBuilder(); builder.setStartCursor(new Cursor(responsePb.getBatch().getEndCursor())); if (offset > 0 && responsePb.getBatch().getSkippedResults() < offset) { builder.setOffset(offset - responsePb.getBatch().getSkippedResults()); } else { builder.setOffset(0); if (limit != null) { builder.setLimit(limit - responsePb.getBatch().getEntityResultsCount()); } } return builder.build(); }
EasyMock.expect(rpcMock.runQuery(EasyMock.anyObject(RunQueryRequest.class))) .andReturn(response); if (response.getBatch().getMoreResults() != QueryResultBatch.MoreResultsType.NOT_FINISHED) { endCursors.add(response.getBatch().getEndCursor());
.getBatch(); QueryResultBatch queryResultBatchPb1 = QueryResultBatch.newBuilder() .mergeFrom(queryResultBatchPb) .setMoreResults(QueryResultBatch.MoreResultsType.NOT_FINISHED) .clearEntityResults() .addAllEntityResults(queryResultBatchPb.getEntityResultsList().subList(0, 1)) .setEndCursor(queryResultBatchPb.getEntityResultsList().get(0).getCursor()) .build(); responses.add(RunQueryResponse.newBuilder().setBatch(queryResultBatchPb1).build()); QueryResultBatch queryResultBatchPb2 = QueryResultBatch.newBuilder() .mergeFrom(queryResultBatchPb) .setMoreResults(QueryResultBatch.MoreResultsType.NOT_FINISHED) .clearEntityResults() .addAllEntityResults(queryResultBatchPb.getEntityResultsList().subList(1, 3)) .setEndCursor(queryResultBatchPb.getEntityResultsList().get(2).getCursor()) .build(); responses.add(RunQueryResponse.newBuilder().setBatch(queryResultBatchPb2).build()); QueryResultBatch queryResultBatchPb3 = QueryResultBatch.newBuilder() .mergeFrom(queryResultBatchPb) .setMoreResults(QueryResultBatch.MoreResultsType.NO_MORE_RESULTS) .clearEntityResults() .addAllEntityResults(queryResultBatchPb.getEntityResultsList().subList(3, 5)) .setEndCursor(queryResultBatchPb.getEntityResultsList().get(4).getCursor()) .build(); responses.add(RunQueryResponse.newBuilder().setBatch(queryResultBatchPb3).build());
static QueryResult build(final com.google.datastore.v1.RunQueryResponse response) { final com.google.datastore.v1.QueryResultBatch batch = response.getBatch(); return new QueryResult(ImmutableList.copyOf( response.getBatch().getEntityResultsList().stream() .map(entity -> Entity.builder(entity.getEntity()).build()) .collect(Collectors.toList())), batch.getEndCursor()); }
/** * Run a query on the datastore. * * @return The entities returned by the query. * @throws DatastoreException on error */ private List<Entity> runQuery(Query query) throws DatastoreException { RunQueryRequest.Builder request = RunQueryRequest.newBuilder(); request.setQuery(query); RunQueryResponse response = datastore.runQuery(request.build()); if (response.getBatch().getMoreResults() == QueryResultBatch.MoreResultsType.NOT_FINISHED) { System.err.println("WARNING: partial results\n"); } List<EntityResult> results = response.getBatch().getEntityResultsList(); List<Entity> entities = new ArrayList<Entity>(results.size()); for (EntityResult result : results) { entities.add(result.getEntity()); } return entities; } }
@Override protected T computeNext() { while (!entityResultPbIter.hasNext() && !lastBatch) { query = query.nextQuery(runQueryResponsePb); sendRequest(); } if (!entityResultPbIter.hasNext()) { cursor = runQueryResponsePb.getBatch().getEndCursor(); return endOfData(); } com.google.datastore.v1.EntityResult entityResultPb = entityResultPbIter.next(); cursor = entityResultPb.getCursor(); @SuppressWarnings("unchecked") T result = (T) actualResultType.convert(entityResultPb.getEntity()); return result; }
/** * Cloud Datastore system tables with statistics are periodically updated. This method fetches * the latest timestamp (in microseconds) of statistics update using the {@code __Stat_Total__} * table. */ private static long queryLatestStatisticsTimestamp( Datastore datastore, @Nullable String namespace) throws DatastoreException { Query.Builder query = Query.newBuilder(); // Note: namespace either being null or empty represents the default namespace, in which // case we treat it as not provided by the user. if (Strings.isNullOrEmpty(namespace)) { query.addKindBuilder().setName("__Stat_Total__"); } else { query.addKindBuilder().setName("__Stat_Ns_Total__"); } query.addOrder(makeOrder("timestamp", DESCENDING)); query.setLimit(Int32Value.newBuilder().setValue(1)); RunQueryRequest request = makeRequest(query.build(), namespace); RunQueryResponse response = datastore.runQuery(request); QueryResultBatch batch = response.getBatch(); if (batch.getEntityResultsCount() == 0) { throw new NoSuchElementException("Datastore total statistics unavailable"); } Entity entity = batch.getEntityResults(0).getEntity(); return entity.getProperties().get("timestamp").getTimestampValue().getSeconds() * 1000000; }
QueryResultsImpl( DatastoreImpl datastore, com.google.datastore.v1.ReadOptions readOptionsPb, Query<T> query) { this.datastore = datastore; this.readOptionsPb = readOptionsPb; this.query = query; queryResultType = query.getType(); com.google.datastore.v1.PartitionId.Builder pbBuilder = com.google.datastore.v1.PartitionId.newBuilder(); pbBuilder.setProjectId(datastore.getOptions().getProjectId()); if (query.getNamespace() != null) { pbBuilder.setNamespaceId(query.getNamespace()); } else if (datastore.getOptions().getNamespace() != null) { pbBuilder.setNamespaceId(datastore.getOptions().getNamespace()); } partitionIdPb = pbBuilder.build(); sendRequest(); if (runQueryResponsePb.getBatch().getSkippedResults() > 0) { cursor = runQueryResponsePb.getBatch().getSkippedCursor(); } else { cursor = mostRecentQueryPb.getStartCursor(); } }
@Override public int getSkippedResults() { return runQueryResponsePb.getBatch().getSkippedResults(); } }
.build(); batch = datastore.runQuery(scatterRequest).getBatch(); for (EntityResult result : batch.getEntityResultsList()) { keySplits.add(result.getEntity().getKey()); scatterPointQuery.setStartCursor(batch.getEndCursor()); scatterPointQuery.getLimitBuilder().setValue( scatterPointQuery.getLimit().getValue() - batch.getEntityResultsCount()); } while (batch.getMoreResults() == MoreResultsType.NOT_FINISHED); Collections.sort(keySplits, DatastoreHelper.getKeyComparator()); return keySplits;
@java.lang.Override public boolean equals(final java.lang.Object obj) { if (obj == this) { return true; } if (!(obj instanceof com.google.datastore.v1.QueryResultBatch)) { return super.equals(obj); } com.google.datastore.v1.QueryResultBatch other = (com.google.datastore.v1.QueryResultBatch) obj; boolean result = true; result = result && (getSkippedResults() == other.getSkippedResults()); result = result && getSkippedCursor().equals(other.getSkippedCursor()); result = result && entityResultType_ == other.entityResultType_; result = result && getEntityResultsList().equals(other.getEntityResultsList()); result = result && getEndCursor().equals(other.getEndCursor()); result = result && moreResults_ == other.moreResults_; result = result && (getSnapshotVersion() == other.getSnapshotVersion()); result = result && unknownFields.equals(other.unknownFields); return result; }