Java Code Examples for com.google.cloud.spanner.Mutation#delete()

The following examples show how to use com.google.cloud.spanner.Mutation#delete() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XATransaction.java    From spanner-jdbc with MIT License 6 votes vote down vote up
private static void cleanupPrepared(TransactionContext transaction, String xid) {
  boolean foundRecords = false;
  KeySet.Builder builder = KeySet.newBuilder();
  try (ResultSet rs = transaction.executeQuery(getPreparedMutationsStatement(xid))) {
    while (rs.next()) {
      foundRecords = true;
      long number = rs.getLong(0);
      builder.addKey(Key.of(xid, number));
    }
  }
  if (foundRecords) {
    Mutation delete =
        Mutation.delete(CloudSpannerXAConnection.XA_PREPARED_MUTATIONS_TABLE, builder.build());
    transaction.buffer(delete);
  }
}
 
Example 2
Source File: CloudSpannerPreparedStatement.java    From spanner-jdbc with MIT License 6 votes vote down vote up
private Mutation createDeleteMutation(Delete delete, boolean generateParameterMetaData)
    throws SQLException {
  String table = unquoteIdentifier(delete.getTable().getFullyQualifiedName());
  getParameterStore().setTable(table);
  Expression where = delete.getWhere();
  if (where == null) {
    // Delete all
    return Mutation.delete(table, KeySet.all());
  } else {
    // Delete one
    DeleteKeyBuilder keyBuilder =
        new DeleteKeyBuilder(getConnection().getTable(table), generateParameterMetaData);
    visitDeleteWhereClause(where, keyBuilder, generateParameterMetaData);
    return Mutation.delete(table, keyBuilder.getKeyBuilder().build());
  }
}
 
Example 3
Source File: XATransactionTest.java    From spanner-jdbc with MIT License 6 votes vote down vote up
@Test
public void testMutationSerialization()
    throws IOException, ClassNotFoundException, NoSuchMethodException, SecurityException,
    IllegalAccessException, IllegalArgumentException, InvocationTargetException {
  Mutation original = Mutation.newInsertBuilder("FOO").set("BAR").to("test").build();
  Mutation deserialized = serializeDeserialize(original);
  assertEquals(original, deserialized);

  original = Mutation.newUpdateBuilder("FOO").set("BAR").to("bla").build();
  deserialized = serializeDeserialize(original);
  assertEquals(original, deserialized);

  original = Mutation.delete("FOO", Key.of("bla"));
  deserialized = serializeDeserialize(original);
  assertEquals(original, deserialized);

  original = Mutation.delete("FOO", KeySet.all());
  deserialized = serializeDeserialize(original);
  assertEquals(original, deserialized);

  original =
      Mutation.delete("FOO", KeySet.range(KeyRange.closedClosed(Key.of("foo"), Key.of("bar"))));
  deserialized = serializeDeserialize(original);
  assertEquals(original, deserialized);
}
 
Example 4
Source File: SpannerTemplateTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void deleteByKeyTest() {
	Key key = Key.of("key");
	Mutation mutation = Mutation.delete("custom_test_table", key);
	KeySet keys = KeySet.newBuilder().addKey(key).build();
	List<Mutation> mutations = Collections.singletonList(mutation);
	when(this.mutationFactory.delete(eq(TestEntity.class), same(key)))
			.thenReturn(mutation);

	verifyBeforeAndAfterEvents(new BeforeDeleteEvent(mutations, null, keys, TestEntity.class),
			new AfterDeleteEvent(mutations, null, keys, TestEntity.class),
			() -> this.spannerTemplate.delete(TestEntity.class, key), x -> x.verify(this.databaseClient, times(1))
					.write(eq(Collections.singletonList(mutation))));
}
 
Example 5
Source File: SpannerTemplateTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void deleteObjectTest() {
	Mutation mutation = Mutation.delete("custom_test_table", Key.of("key"));
	List<Mutation> mutations = Collections.singletonList(mutation);
	TestEntity entity = new TestEntity();
	when(this.mutationFactory.delete(entity)).thenReturn(mutation);

	verifyBeforeAndAfterEvents(new BeforeDeleteEvent(mutations, Collections.singletonList(entity), null, null),
			new AfterDeleteEvent(mutations, Collections.singletonList(entity), null, null),
			() -> this.spannerTemplate.delete(entity), x -> x.verify(this.databaseClient, times(1))
					.write(eq(Collections.singletonList(mutation))));
}
 
Example 6
Source File: SpannerTemplateTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void deleteAllObjectTest() {
	Mutation mutation = Mutation.delete("custom_test_table", Key.of("key"));
	TestEntity entity = new TestEntity();
	List entities = Arrays.asList(entity, entity, entity);
	List<Mutation> mutations = Arrays.asList(mutation, mutation, mutation);
	when(this.mutationFactory.delete(entity)).thenReturn(mutation);

	verifyBeforeAndAfterEvents(new BeforeDeleteEvent(mutations, entities, null, null),
			new AfterDeleteEvent(mutations, entities, null, null),
			() -> this.spannerTemplate.deleteAll(Arrays.asList(entity, entity, entity)),
			x -> x.verify(this.databaseClient, times(1))
					.write(eq(mutations)));
}
 
Example 7
Source File: SpannerTemplateTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void deleteKeysTest() {
	KeySet keys = KeySet.newBuilder().addKey(Key.of("key1")).addKey(Key.of("key2"))
			.build();
	Mutation mutation = Mutation.delete("custom_test_table", keys);
	List<Mutation> mutations = Collections.singletonList(mutation);
	when(this.mutationFactory.delete(eq(TestEntity.class), same(keys)))
			.thenReturn(mutation);

	verifyBeforeAndAfterEvents(new BeforeDeleteEvent(mutations, null, keys, TestEntity.class),
			new AfterDeleteEvent(mutations, null, keys, TestEntity.class),
			() -> this.spannerTemplate.delete(TestEntity.class, keys), x -> x.verify(this.databaseClient, times(1))
					.write(eq(Collections.singletonList(mutation))));
}
 
Example 8
Source File: SpannerIOWriteTest.java    From beam with Apache License 2.0 5 votes vote down vote up
private static Mutation del(Long... keys) {

    KeySet.Builder builder = KeySet.newBuilder();
    for (Long key : keys) {
      builder.addKey(Key.of(key));
    }
    return Mutation.delete("test", builder.build());
  }
 
Example 9
Source File: SpannerMutationFactoryImpl.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Override
public Mutation delete(Class entityClass, KeySet keys) {
	SpannerPersistentEntity<?> persistentEntity = this.spannerMappingContext
			.getPersistentEntity(entityClass);
	return Mutation.delete(persistentEntity.tableName(), keys);
}
 
Example 10
Source File: SpannerIOWriteTest.java    From beam with Apache License 2.0 4 votes vote down vote up
private static Mutation delRange(Long start, Long end) {
  return Mutation.delete("test", KeySet.range(KeyRange.closedClosed(Key.of(start), Key.of(end))));
}