@java.lang.Override public Builder newBuilderForType() { return newBuilder(); }
/** * * * <pre> * Entities are partitioned into subsets, currently identified by a project * ID and namespace ID. * Queries are scoped to a single partition. * </pre> * * <code>.google.datastore.v1.PartitionId partition_id = 1;</code> */ public Builder mergePartitionId(com.google.datastore.v1.PartitionId value) { if (partitionIdBuilder_ == null) { if (partitionId_ != null) { partitionId_ = com.google.datastore.v1.PartitionId.newBuilder(partitionId_) .mergeFrom(value) .buildPartial(); } else { partitionId_ = value; } onChanged(); } else { partitionIdBuilder_.mergeFrom(value); } return this; } /**
/** * * * <pre> * Entities are partitioned into subsets, identified by a partition ID. * Queries are scoped to a single partition. * This partition ID is normalized with the standard default context * partition ID. * </pre> * * <code>.google.datastore.v1.PartitionId partition_id = 2;</code> */ public Builder mergePartitionId(com.google.datastore.v1.PartitionId value) { if (partitionIdBuilder_ == null) { if (partitionId_ != null) { partitionId_ = com.google.datastore.v1.PartitionId.newBuilder(partitionId_) .mergeFrom(value) .buildPartial(); } else { partitionId_ = value; } onChanged(); } else { partitionIdBuilder_.mergeFrom(value); } return this; } /**
com.google.datastore.v1.Key toPb() { com.google.datastore.v1.Key.Builder keyPb = com.google.datastore.v1.Key.newBuilder(); com.google.datastore.v1.PartitionId.Builder partitionIdPb = com.google.datastore.v1.PartitionId.newBuilder(); partitionIdPb.setProjectId(projectId); partitionIdPb.setNamespaceId(namespace); keyPb.setPartitionId(partitionIdPb.build()); for (PathElement pathEntry : path) { keyPb.addPath(pathEntry.toPb()); } return keyPb.build(); } }
@Test public void testEventualConsistencyQuery() { ReadOptions readOption = ReadOptions.newBuilder().setReadConsistencyValue(ReadConsistency.EVENTUAL_VALUE).build(); com.google.datastore.v1.GqlQuery query = com.google.datastore.v1.GqlQuery.newBuilder().setQueryString("FROM * SELECT *").build(); RunQueryRequest.Builder expectedRequest = RunQueryRequest.newBuilder() .setReadOptions(readOption) .setGqlQuery(query) .setPartitionId(PartitionId.newBuilder().setProjectId(PROJECT_ID).build()); EasyMock.expect(rpcMock.runQuery(expectedRequest.build())) .andReturn(RunQueryResponse.newBuilder().build()); EasyMock.replay(rpcFactoryMock, rpcMock); Datastore datastore = rpcMockOptions.getService(); datastore.run( Query.newGqlQueryBuilder("FROM * SELECT *").build(), ReadOption.eventualConsistency()); EasyMock.verify(rpcFactoryMock, rpcMock); }
@Test public void testLookupEventualConsistency() { ReadOptions readOption = ReadOptions.newBuilder().setReadConsistencyValue(ReadConsistency.EVENTUAL_VALUE).build(); com.google.datastore.v1.Key key = com.google.datastore.v1.Key.newBuilder() .setPartitionId(PartitionId.newBuilder().setProjectId(PROJECT_ID).build()) .addPath( com.google.datastore.v1.Key.PathElement.newBuilder() .setKind("kind1") .setName("name") .build()) .build(); LookupRequest lookupRequest = LookupRequest.newBuilder().setReadOptions(readOption).addKeys(key).build(); EasyMock.expect(rpcMock.lookup(lookupRequest)) .andReturn(LookupResponse.newBuilder().build()) .times(3); EasyMock.replay(rpcFactoryMock, rpcMock); Datastore datastore = rpcMockOptions.getService(); datastore.get(KEY1, ReadOption.eventualConsistency()); datastore.get(ImmutableList.of(KEY1), ReadOption.eventualConsistency()); datastore.fetch(ImmutableList.of(KEY1), ReadOption.eventualConsistency()); EasyMock.verify(rpcFactoryMock, rpcMock); }
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(); } }
@java.lang.Override public Builder newBuilderForType() { return newBuilder(); }
public Builder newBuilderForType() { return newBuilder(); } public static Builder newBuilder() {
private static PartitionId.Builder forNamespace(@Nullable String namespace) { PartitionId.Builder partitionBuilder = PartitionId.newBuilder(); // Namespace either being null or empty represents the default namespace. // Datastore Client libraries expect users to not set the namespace proto field in // either of these cases. if (!Strings.isNullOrEmpty(namespace)) { partitionBuilder.setNamespaceId(namespace); } return partitionBuilder; }
/** * Set the namespace for this {@code Key}. * * @param namespace the namespace to set. * @return this key builder. */ public Builder namespace(final String namespace) { key.setPartitionId( com.google.datastore.v1.PartitionId.newBuilder().setNamespaceId(namespace)); return this; }
/** * <pre> * Entities are partitioned into subsets, currently identified by a project * ID and namespace ID. * Queries are scoped to a single partition. * </pre> * * <code>optional .google.datastore.v1.PartitionId partition_id = 1;</code> */ public Builder mergePartitionId(com.google.datastore.v1.PartitionId value) { if (partitionIdBuilder_ == null) { if (partitionId_ != null) { partitionId_ = com.google.datastore.v1.PartitionId.newBuilder(partitionId_).mergeFrom(value).buildPartial(); } else { partitionId_ = value; } onChanged(); } else { partitionIdBuilder_.mergeFrom(value); } return this; } /**
/** * <pre> * Entities are partitioned into subsets, identified by a partition ID. * Queries are scoped to a single partition. * This partition ID is normalized with the standard default context * partition ID. * </pre> * * <code>optional .google.datastore.v1.PartitionId partition_id = 2;</code> */ public Builder mergePartitionId(com.google.datastore.v1.PartitionId value) { if (partitionIdBuilder_ == null) { if (partitionId_ != null) { partitionId_ = com.google.datastore.v1.PartitionId.newBuilder(partitionId_).mergeFrom(value).buildPartial(); } else { partitionId_ = value; } onChanged(); } else { partitionIdBuilder_.mergeFrom(value); } return this; } /**
/** * * * <pre> * Entities are partitioned into subsets, currently identified by a project * ID and namespace ID. * Queries are scoped to a single partition. * </pre> * * <code>.google.datastore.v1.PartitionId partition_id = 1;</code> */ public Builder mergePartitionId(com.google.datastore.v1.PartitionId value) { if (partitionIdBuilder_ == null) { if (partitionId_ != null) { partitionId_ = com.google.datastore.v1.PartitionId.newBuilder(partitionId_) .mergeFrom(value) .buildPartial(); } else { partitionId_ = value; } onChanged(); } else { partitionIdBuilder_.mergeFrom(value); } return this; } /**
com.google.datastore.v1.Key getPb(final String namespace) { if (namespace == null) { return key; } else { return com.google.datastore.v1.Key .newBuilder(key) .setPartitionId( com.google.datastore.v1.PartitionId .newBuilder().setNamespaceId(namespace)).build(); } }
com.google.datastore.v1.Key toPb() { com.google.datastore.v1.Key.Builder keyPb = com.google.datastore.v1.Key.newBuilder(); com.google.datastore.v1.PartitionId.Builder partitionIdPb = com.google.datastore.v1.PartitionId.newBuilder(); partitionIdPb.setProjectId(projectId); partitionIdPb.setNamespaceId(namespace); keyPb.setPartitionId(partitionIdPb.build()); for (PathElement pathEntry : path) { keyPb.addPath(pathEntry.toPb()); } return keyPb.build(); } }
/** * Get key */ public Key getKey(String name) { Key.Builder keyBuilder = Key.newBuilder(); // Set namespace PartitionId partitionId = PartitionId.newBuilder() .setProjectId(projectId).setNamespaceId(namespace).build(); keyBuilder.setPartitionId(partitionId); // Set parent paths if (parents != null && parents.size() > 0) { for (String parentKey : parents.keySet()) { Key.PathElement.Builder parentPath = Key.PathElement.newBuilder().setKind(parentKey).setName(parents.get(parentKey)); keyBuilder.addPath(parentPath); } } // Set main kind Key.PathElement.Builder path = Key.PathElement.newBuilder().setKind(kind).setName(name); keyBuilder.addPath(path); return keyBuilder.build(); }
@Override public ListenableFuture<QueryResult> executeAsync(final Query statement, final ListenableFuture<TransactionResult> txn) { final ListenableFuture<Response> httpResponse = Futures.transformAsync(txn, result -> { final String namespace = config.getNamespace(); final RunQueryRequest.Builder request = RunQueryRequest.newBuilder() .setQuery(statement.getPb(namespace != null ? namespace : "")); if (namespace != null) { request.setPartitionId(PartitionId.newBuilder().setNamespaceId(namespace)); } final ByteString transaction = result.getTransaction(); if (transaction != null) { request.setReadOptions(ReadOptions.newBuilder().setTransaction(transaction)); } final ProtoHttpContent payload = new ProtoHttpContent(request.build()); return ListenableFutureAdapter.asGuavaFuture(prepareRequest("runQuery", payload).execute()); }, MoreExecutors.directExecutor()); return Futures.transformAsync(httpResponse, response -> { if (!isSuccessful(response.getStatusCode())) { throw new DatastoreException(response.getStatusCode(), response.getResponseBody()); } final RunQueryResponse query = RunQueryResponse.parseFrom(streamResponse(response)); return Futures.immediateFuture(QueryResult.build(query)); }, MoreExecutors.directExecutor()); } }
@ProcessElement public void processElement(ProcessContext c) throws InvalidProtocolBufferException { String entityJson = c.element(); Entity.Builder entityBuilder = Entity.newBuilder(); entityJsonParser.merge(entityJson, entityBuilder); // Build entity who's key has an empty project Id. // This allows DatastoreIO to handle what project Entities are loaded into Key k = entityBuilder.build().getKey(); entityBuilder.setKey(Key.newBuilder() .addAllPath(k.getPathList()) .setPartitionId(PartitionId.newBuilder() .setProjectId("") .setNamespaceId(k.getPartitionId().getNamespaceId()))); c.output(entityBuilder.build()); } }
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(); } }