本文整理汇总了Java中com.google.datastore.v1.Mutation类的典型用法代码示例。如果您正苦于以下问题:Java Mutation类的具体用法?Java Mutation怎么用?Java Mutation使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Mutation类属于com.google.datastore.v1包,在下文中一共展示了Mutation类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: datastoreWriterFnTest
import com.google.datastore.v1.Mutation; //导入依赖的package包/类
private void datastoreWriterFnTest(int numMutations) throws Exception {
// Create the requested number of mutations.
List<Mutation> mutations = new ArrayList<>(numMutations);
for (int i = 0; i < numMutations; ++i) {
mutations.add(
makeUpsert(Entity.newBuilder().setKey(makeKey("key" + i, i + 1)).build()).build());
}
DatastoreWriterFn datastoreWriter = new DatastoreWriterFn(StaticValueProvider.of(PROJECT_ID),
null, mockDatastoreFactory, new FakeWriteBatcher());
DoFnTester<Mutation, Void> doFnTester = DoFnTester.of(datastoreWriter);
doFnTester.setCloningBehavior(CloningBehavior.DO_NOT_CLONE);
doFnTester.processBundle(mutations);
int start = 0;
while (start < numMutations) {
int end = Math.min(numMutations, start + DATASTORE_BATCH_UPDATE_ENTITIES_START);
CommitRequest.Builder commitRequest = CommitRequest.newBuilder();
commitRequest.setMode(CommitRequest.Mode.NON_TRANSACTIONAL);
commitRequest.addAllMutations(mutations.subList(start, end));
// Verify all the batch requests were made with the expected mutations.
verify(mockDatastore, times(1)).commit(commitRequest.build());
start = end;
}
}
示例2: testDatatoreWriterFnRetriesErrors
import com.google.datastore.v1.Mutation; //导入依赖的package包/类
/** Tests {@link DatastoreWriterFn} with a failed request which is retried. */
@Test
public void testDatatoreWriterFnRetriesErrors() throws Exception {
List<Mutation> mutations = new ArrayList<>();
int numRpcs = 2;
for (int i = 0; i < DATASTORE_BATCH_UPDATE_ENTITIES_START * numRpcs; ++i) {
mutations.add(
makeUpsert(Entity.newBuilder().setKey(makeKey("key" + i, i + 1)).build()).build());
}
CommitResponse successfulCommit = CommitResponse.getDefaultInstance();
when(mockDatastore.commit(any(CommitRequest.class))).thenReturn(successfulCommit)
.thenThrow(
new DatastoreException("commit", Code.DEADLINE_EXCEEDED, "", null))
.thenReturn(successfulCommit);
DatastoreWriterFn datastoreWriter = new DatastoreWriterFn(StaticValueProvider.of(PROJECT_ID),
null, mockDatastoreFactory, new FakeWriteBatcher());
DoFnTester<Mutation, Void> doFnTester = DoFnTester.of(datastoreWriter);
doFnTester.setCloningBehavior(CloningBehavior.DO_NOT_CLONE);
doFnTester.processBundle(mutations);
}
示例3: executeAsyncMutations
import com.google.datastore.v1.Mutation; //导入依赖的package包/类
private ListenableFuture<MutationResult> executeAsyncMutations(final List<Mutation> mutations, final ListenableFuture<TransactionResult> txn) {
final ListenableFuture<Response> httpResponse = Futures.transformAsync(txn, result -> {
final CommitRequest.Builder request = CommitRequest.newBuilder();
if (mutations != null) {
request.addAllMutations(mutations);
}
final ByteString transaction = result.getTransaction();
if (transaction != null) {
request.setTransaction(transaction);
} else {
request.setMode(CommitRequest.Mode.NON_TRANSACTIONAL);
}
final ProtoHttpContent payload = new ProtoHttpContent(request.build());
return ListenableFutureAdapter.asGuavaFuture(prepareRequest("commit", payload).execute());
});
return Futures.transformAsync(httpResponse, response -> {
if (!isSuccessful(response.getStatusCode())) {
throw new DatastoreException(response.getStatusCode(), response.getResponseBody());
}
final CommitResponse commit = CommitResponse.parseFrom(streamResponse(response));
return Futures.immediateFuture(MutationResult.build(commit));
});
}
示例4: Mutate
import com.google.datastore.v1.Mutation; //导入依赖的package包/类
/**
* Note that {@code projectId} is only {@code @Nullable} as a matter of build order, but if
* it is {@code null} at instantiation time, an error will be thrown.
*/
Mutate(@Nullable ValueProvider<String> projectId, @Nullable String localhost,
SimpleFunction<T, Mutation> mutationFn) {
this.projectId = projectId;
this.localhost = localhost;
this.mutationFn = checkNotNull(mutationFn);
}
示例5: processElement
import com.google.datastore.v1.Mutation; //导入依赖的package包/类
@ProcessElement
public void processElement(ProcessContext c) throws Exception {
Mutation write = c.element();
int size = write.getSerializedSize();
if (mutations.size() > 0
&& mutationsSize + size >= DatastoreV1.DATASTORE_BATCH_UPDATE_BYTES_LIMIT) {
flushBatch();
}
mutations.add(c.element());
mutationsSize += size;
if (mutations.size() >= writeBatcher.nextBatchSize(System.currentTimeMillis())) {
flushBatch();
}
}
示例6: apply
import com.google.datastore.v1.Mutation; //导入依赖的package包/类
@Override
public Mutation apply(Entity entity) {
// Verify that the entity to write has a complete key.
checkArgument(isValidKey(entity.getKey()),
"Entities to be written to the Cloud Datastore must have complete keys:\n%s", entity);
return makeUpsert(entity).build();
}
示例7: testAddEntities
import com.google.datastore.v1.Mutation; //导入依赖的package包/类
@Test
/**
* Test that entities with valid keys are transformed to upsert mutations.
*/
public void testAddEntities() throws Exception {
Key key = makeKey("bird", "finch").build();
Entity entity = Entity.newBuilder().setKey(key).build();
UpsertFn upsertFn = new UpsertFn();
Mutation exceptedMutation = makeUpsert(entity).build();
assertEquals(upsertFn.apply(entity), exceptedMutation);
}
示例8: testDeleteEntities
import com.google.datastore.v1.Mutation; //导入依赖的package包/类
/**
* Test that entities with valid keys are transformed to delete mutations.
*/
@Test
public void testDeleteEntities() throws Exception {
Key key = makeKey("bird", "finch").build();
Entity entity = Entity.newBuilder().setKey(key).build();
DeleteEntityFn deleteEntityFn = new DeleteEntityFn();
Mutation exceptedMutation = makeDelete(entity.getKey()).build();
assertEquals(deleteEntityFn.apply(entity), exceptedMutation);
}
示例9: testDeleteKeys
import com.google.datastore.v1.Mutation; //导入依赖的package包/类
/**
* Test that valid keys are transformed to delete mutations.
*/
@Test
public void testDeleteKeys() throws Exception {
Key key = makeKey("bird", "finch").build();
DeleteKeyFn deleteKeyFn = new DeleteKeyFn();
Mutation exceptedMutation = makeDelete(key).build();
assertEquals(deleteKeyFn.apply(key), exceptedMutation);
}
示例10: getPb
import com.google.datastore.v1.Mutation; //导入依赖的package包/类
@Override
public Mutation getPb(final String namespace) {
final com.google.datastore.v1.Mutation.Builder mutation =
com.google.datastore.v1.Mutation.newBuilder();
return mutation.setInsert(entity.build().getPb(namespace)).build();
}
示例11: getPb
import com.google.datastore.v1.Mutation; //导入依赖的package包/类
@Override
public Mutation getPb(final String namespace) {
final com.google.datastore.v1.Mutation.Builder mutation =
com.google.datastore.v1.Mutation.newBuilder();
if (upsert) {
mutation.setUpsert(entity.build().getPb(namespace));
} else {
mutation.setUpdate(entity.build().getPb(namespace));
}
return mutation.build();
}
示例12: executeAsync
import com.google.datastore.v1.Mutation; //导入依赖的package包/类
@Override
public ListenableFuture<MutationResult> executeAsync(final MutationStatement statement, final ListenableFuture<TransactionResult> txn) {
final List<Mutation> mutations = Optional
.ofNullable(statement)
.flatMap(s -> Optional.of(ImmutableList.of(s.getPb(config.getNamespace()))))
.orElse(ImmutableList.of());
return executeAsyncMutations(mutations, txn);
}
示例13: insert
import com.google.datastore.v1.Mutation; //导入依赖的package包/类
/**
* Insert an entity into the datastore.
*
* The entity must have no ids.
*
* @return The key for the inserted entity.
* @throws DatastoreException on error
*/
private Key insert(Entity entity) throws DatastoreException {
CommitRequest req = CommitRequest.newBuilder()
.addMutations(Mutation.newBuilder()
.setInsert(entity))
.setMode(CommitRequest.Mode.NON_TRANSACTIONAL)
.build();
return datastore.commit(req).getMutationResults(0).getKey();
}
示例14: apply
import com.google.datastore.v1.Mutation; //导入依赖的package包/类
@Override
public Mutation.Builder apply(Entity entity) {
return makeUpsert(entity);
}
示例15: getPb
import com.google.datastore.v1.Mutation; //导入依赖的package包/类
@Override
public Mutation getPb(final String namespace) {
final com.google.datastore.v1.Key mutationKey = getKey().getPb(namespace);
return com.google.datastore.v1.Mutation.newBuilder().setDelete(mutationKey).build();
}