use of com.google.cloud.spanner.Mutation in project spring-cloud-gcp by spring-cloud.
the class SpannerTemplateTests method deleteEntitiesTest.
@Test
public void deleteEntitiesTest() {
Mutation mutation = Mutation.delete("custom_test_table", Key.of("key"));
Iterable<TestEntity> entities = new ArrayList<TestEntity>();
when(this.mutationFactory.delete(eq(TestEntity.class), same(entities))).thenReturn(mutation);
this.spannerTemplate.delete(TestEntity.class, entities);
verify(this.databaseClient, times(1)).write(eq(Arrays.asList(mutation)));
}
use of com.google.cloud.spanner.Mutation in project spring-cloud-gcp by spring-cloud.
the class SpannerTemplateTests method upsertTest.
@Test
public void upsertTest() {
Mutation mutation = Mutation.newInsertOrUpdateBuilder("custom_test_table").build();
TestEntity entity = new TestEntity();
when(this.mutationFactory.upsert(same(entity), isNull())).thenReturn(mutation);
this.spannerTemplate.upsert(entity);
verify(this.databaseClient, times(1)).write(eq(Arrays.asList(mutation)));
}
use of com.google.cloud.spanner.Mutation in project spring-cloud-gcp by spring-cloud.
the class SpannerTemplateTests method upsertColumnsArrayTest.
@Test
public void upsertColumnsArrayTest() {
Mutation mutation = Mutation.newInsertOrUpdateBuilder("custom_test_table").build();
TestEntity entity = new TestEntity();
when(this.mutationFactory.upsert(same(entity), eq(Optional.of(new HashSet<>(Arrays.asList(new String[] { "a", "b" })))))).thenReturn(mutation);
this.spannerTemplate.upsert(entity, "a", "b");
verify(this.databaseClient, times(1)).write(eq(Arrays.asList(mutation)));
}
use of com.google.cloud.spanner.Mutation in project spring-cloud-gcp by spring-cloud.
the class SpannerTemplateTests method deleteObjectTest.
@Test
public void deleteObjectTest() {
Mutation mutation = Mutation.delete("custom_test_table", Key.of("key"));
TestEntity entity = new TestEntity();
when(this.mutationFactory.delete(entity)).thenReturn(mutation);
this.spannerTemplate.delete(entity);
verify(this.databaseClient, times(1)).write(eq(Arrays.asList(mutation)));
}
use of com.google.cloud.spanner.Mutation in project java-docs-samples by GoogleCloudPlatform.
the class SpannerReadIT method setUp.
@Before
public void setUp() {
instanceId = System.getProperty("spanner.test.instance");
databaseId = "df-spanner-read-it";
spannerOptions = SpannerOptions.getDefaultInstance();
spanner = spannerOptions.getService();
DatabaseAdminClient adminClient = spanner.getDatabaseAdminClient();
try {
adminClient.dropDatabase(instanceId, databaseId);
} catch (SpannerException e) {
// Does not exist, ignore.
}
Operation<Database, CreateDatabaseMetadata> op = adminClient.createDatabase(instanceId, databaseId, Arrays.asList("CREATE TABLE Singers " + "(singerId INT64 NOT NULL, firstName STRING(MAX) NOT NULL, " + "lastName STRING(MAX) NOT NULL,) PRIMARY KEY (singerId)", "CREATE TABLE Albums (singerId INT64 NOT NULL, albumId INT64 NOT NULL, " + "albumTitle STRING(MAX) NOT NULL,) PRIMARY KEY (singerId, albumId)"));
op.waitFor();
List<Mutation> mutations = Arrays.asList(Mutation.newInsertBuilder("singers").set("singerId").to(1L).set("firstName").to("John").set("lastName").to("Lennon").build(), Mutation.newInsertBuilder("singers").set("singerId").to(2L).set("firstName").to("Paul").set("lastName").to("Mccartney").build(), Mutation.newInsertBuilder("singers").set("singerId").to(3L).set("firstName").to("George").set("lastName").to("Harrison").build(), Mutation.newInsertBuilder("singers").set("singerId").to(4L).set("firstName").to("Ringo").set("lastName").to("Starr").build(), Mutation.newInsertBuilder("albums").set("singerId").to(1L).set("albumId").to(1L).set("albumTitle").to("Imagine").build(), Mutation.newInsertBuilder("albums").set("singerId").to(2L).set("albumId").to(1L).set("albumTitle").to("Pipes of Peace").build());
DatabaseClient dbClient = getDbClient();
TransactionRunner runner = dbClient.readWriteTransaction();
runner.run(new TransactionRunner.TransactionCallable<Void>() {
@Nullable
@Override
public Void run(TransactionContext tx) {
tx.buffer(mutations);
return null;
}
});
}
Aggregations