/** * * * <pre> * The key of the entity to delete. The entity may or may not already exist. * Must have a complete key path and must not be reserved/read-only. * </pre> * * <code>.google.datastore.v1.Key delete = 7;</code> */ public com.google.datastore.v1.Key.Builder getDeleteBuilder() { return getDeleteFieldBuilder().getBuilder(); } /**
creq.addMutationsBuilder().setInsert(entity);
/** * * * <pre> * The mutations to perform. * When mode is `TRANSACTIONAL`, mutations affecting a single entity are * applied in order. The following sequences of mutations affecting a single * entity are not permitted in a single `Commit` request: * - `insert` followed by `insert` * - `update` followed by `insert` * - `upsert` followed by `insert` * - `delete` followed by `update` * When mode is `NON_TRANSACTIONAL`, no two mutations may affect a single * entity. * </pre> * * <code>repeated .google.datastore.v1.Mutation mutations = 6;</code> */ public Builder setMutations( int index, com.google.datastore.v1.Mutation.Builder builderForValue) { if (mutationsBuilder_ == null) { ensureMutationsIsMutable(); mutations_.set(index, builderForValue.build()); onChanged(); } else { mutationsBuilder_.setMessage(index, builderForValue.build()); } return this; } /**
/** * <pre> * The mutations to perform. * When mode is `TRANSACTIONAL`, mutations affecting a single entity are * applied in order. The following sequences of mutations affecting a single * entity are not permitted in a single `Commit` request: * - `insert` followed by `insert` * - `update` followed by `insert` * - `upsert` followed by `insert` * - `delete` followed by `update` * When mode is `NON_TRANSACTIONAL`, no two mutations may affect a single * entity. * </pre> * * <code>repeated .google.datastore.v1.Mutation mutations = 6;</code> */ public Builder addMutations( int index, com.google.datastore.v1.Mutation.Builder builderForValue) { if (mutationsBuilder_ == null) { ensureMutationsIsMutable(); mutations_.add(index, builderForValue.build()); onChanged(); } else { mutationsBuilder_.addMessage(index, builderForValue.build()); } return this; } /**
/** * * * <pre> * The mutations to perform. * When mode is `TRANSACTIONAL`, mutations affecting a single entity are * applied in order. The following sequences of mutations affecting a single * entity are not permitted in a single `Commit` request: * - `insert` followed by `insert` * - `update` followed by `insert` * - `upsert` followed by `insert` * - `delete` followed by `update` * When mode is `NON_TRANSACTIONAL`, no two mutations may affect a single * entity. * </pre> * * <code>repeated .google.datastore.v1.Mutation mutations = 6;</code> */ public Builder addMutations(com.google.datastore.v1.Mutation.Builder builderForValue) { if (mutationsBuilder_ == null) { ensureMutationsIsMutable(); mutations_.add(builderForValue.build()); onChanged(); } else { mutationsBuilder_.addMessage(builderForValue.build()); } return this; } /**
/** * * * <pre> * The mutations to perform. * When mode is `TRANSACTIONAL`, mutations affecting a single entity are * applied in order. The following sequences of mutations affecting a single * entity are not permitted in a single `Commit` request: * - `insert` followed by `insert` * - `update` followed by `insert` * - `upsert` followed by `insert` * - `delete` followed by `update` * When mode is `NON_TRANSACTIONAL`, no two mutations may affect a single * entity. * </pre> * * <code>repeated .google.datastore.v1.Mutation mutations = 6;</code> */ public Builder addMutations( int index, com.google.datastore.v1.Mutation.Builder builderForValue) { if (mutationsBuilder_ == null) { ensureMutationsIsMutable(); mutations_.add(index, builderForValue.build()); onChanged(); } else { mutationsBuilder_.addMessage(index, builderForValue.build()); } return this; } /**
/** * <pre> * The mutations to perform. * When mode is `TRANSACTIONAL`, mutations affecting a single entity are * applied in order. The following sequences of mutations affecting a single * entity are not permitted in a single `Commit` request: * - `insert` followed by `insert` * - `update` followed by `insert` * - `upsert` followed by `insert` * - `delete` followed by `update` * When mode is `NON_TRANSACTIONAL`, no two mutations may affect a single * entity. * </pre> * * <code>repeated .google.datastore.v1.Mutation mutations = 6;</code> */ public Builder setMutations( int index, com.google.datastore.v1.Mutation.Builder builderForValue) { if (mutationsBuilder_ == null) { ensureMutationsIsMutable(); mutations_.set(index, builderForValue.build()); onChanged(); } else { mutationsBuilder_.setMessage(index, builderForValue.build()); } return this; } /**
/** * <pre> * The mutations to perform. * When mode is `TRANSACTIONAL`, mutations affecting a single entity are * applied in order. The following sequences of mutations affecting a single * entity are not permitted in a single `Commit` request: * - `insert` followed by `insert` * - `update` followed by `insert` * - `upsert` followed by `insert` * - `delete` followed by `update` * When mode is `NON_TRANSACTIONAL`, no two mutations may affect a single * entity. * </pre> * * <code>repeated .google.datastore.v1.Mutation mutations = 6;</code> */ public Builder addMutations( com.google.datastore.v1.Mutation.Builder builderForValue) { if (mutationsBuilder_ == null) { ensureMutationsIsMutable(); mutations_.add(builderForValue.build()); onChanged(); } else { mutationsBuilder_.addMessage(builderForValue.build()); } return this; } /**
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 + DatastoreV1.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; } }
/** 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 < DatastoreV1.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); }
protected List<com.google.datastore.v1.Mutation> toMutationPbList() { List<com.google.datastore.v1.Mutation> mutationsPb = new ArrayList<>(); for (FullEntity<IncompleteKey> entity : toAddAutoId()) { mutationsPb.add( com.google.datastore.v1.Mutation.newBuilder().setInsert(entity.toPb()).build()); } for (FullEntity<Key> entity : toAdd().values()) { mutationsPb.add( com.google.datastore.v1.Mutation.newBuilder().setInsert(entity.toPb()).build()); } for (FullEntity<Key> entity : toUpdate().values()) { mutationsPb.add( com.google.datastore.v1.Mutation.newBuilder().setUpdate(entity.toPb()).build()); } for (FullEntity<Key> entity : toPut().values()) { mutationsPb.add( com.google.datastore.v1.Mutation.newBuilder().setUpsert(entity.toPb()).build()); } for (Key key : toDelete()) { mutationsPb.add(com.google.datastore.v1.Mutation.newBuilder().setDelete(key.toPb()).build()); } return mutationsPb; }
/** * 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(); }
@Override public void update(Entity... entities) { if (entities.length > 0) { List<com.google.datastore.v1.Mutation> mutationsPb = new ArrayList<>(); Map<Key, Entity> dedupEntities = new LinkedHashMap<>(); for (Entity entity : entities) { dedupEntities.put(entity.getKey(), entity); } for (Entity entity : dedupEntities.values()) { mutationsPb.add( com.google.datastore.v1.Mutation.newBuilder().setUpdate(entity.toPb()).build()); } commitMutation(mutationsPb); } }
/** * * * <pre> * The key of the entity to delete. The entity may or may not already exist. * Must have a complete key path and must not be reserved/read-only. * </pre> * * <code>.google.datastore.v1.Key delete = 7;</code> */ public com.google.datastore.v1.Key.Builder getDeleteBuilder() { return getDeleteFieldBuilder().getBuilder(); } /**