Search in sources :

Example 31 with Mutation

use of com.google.cloud.spanner.Mutation in project spanner-jdbc by olavloite.

the class XATransaction method cleanupPrepared.

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);
    }
}
Also used : KeySet(com.google.cloud.spanner.KeySet) ResultSet(com.google.cloud.spanner.ResultSet) Mutation(com.google.cloud.spanner.Mutation)

Example 32 with Mutation

use of com.google.cloud.spanner.Mutation in project spanner-jdbc by olavloite.

the class TransactionContextMock method createTransactionContextMock.

@SuppressWarnings("unchecked")
TransactionContext createTransactionContextMock() {
    TransactionContext res = mock(TransactionContext.class);
    when(res.executeQuery(XATransaction.getPreparedMutationsStatement(RecoveredXid.xidToString(XID_WITHOUT_MUTATIONS)))).then(new Answer<ResultSet>() {

        @Override
        public ResultSet answer(InvocationOnMock invocation) throws Throwable {
            return mockPreparedWithoutMutationsResultSet();
        }
    });
    when(res.executeQuery(XATransaction.getPreparedMutationsStatement(RecoveredXid.xidToString(XID_WITH_MUTATIONS)))).then(new Answer<ResultSet>() {

        @Override
        public ResultSet answer(InvocationOnMock invocation) throws Throwable {
            return mockPreparedWithMutationsResultSet();
        }
    });
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            buffer.add((Mutation) invocation.getArgument(0));
            return null;
        }
    }).when(res).buffer(ArgumentMatchers.any(Mutation.class));
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            Iterable<Mutation> it = (Iterable<Mutation>) invocation.getArgument(0);
            it.forEach(e -> buffer.add(e));
            return null;
        }
    }).when(res).buffer(ArgumentMatchers.any(Iterable.class));
    return res;
}
Also used : TransactionContext(com.google.cloud.spanner.TransactionContext) ArgumentMatchers(org.mockito.ArgumentMatchers) Mockito.when(org.mockito.Mockito.when) Mutation(com.google.cloud.spanner.Mutation) ArrayList(java.util.ArrayList) Answer(org.mockito.stubbing.Answer) ResultSet(com.google.cloud.spanner.ResultSet) SQLException(java.sql.SQLException) List(java.util.List) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Mockito.doAnswer(org.mockito.Mockito.doAnswer) RecoveredXid(nl.topicus.jdbc.xa.RecoveredXid) Xid(javax.transaction.xa.Xid) Mockito.mock(org.mockito.Mockito.mock) TransactionContext(com.google.cloud.spanner.TransactionContext) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ResultSet(com.google.cloud.spanner.ResultSet) Mutation(com.google.cloud.spanner.Mutation)

Example 33 with Mutation

use of com.google.cloud.spanner.Mutation in project beam by apache.

the class SpannerIOTest method batching.

@Test
public void batching() throws Exception {
    Mutation one = Mutation.newInsertOrUpdateBuilder("test").set("one").to(1).build();
    Mutation two = Mutation.newInsertOrUpdateBuilder("test").set("two").to(2).build();
    SpannerIO.Write write = SpannerIO.write().withProjectId("test-project").withInstanceId("test-instance").withDatabaseId("test-database").withBatchSizeBytes(1000000000).withServiceFactory(serviceFactory);
    SpannerIO.SpannerWriteFn writerFn = new SpannerIO.SpannerWriteFn(write);
    DoFnTester<Mutation, Void> fnTester = DoFnTester.of(writerFn);
    fnTester.processBundle(Arrays.asList(one, two));
    verify(serviceFactory.mockSpanner()).getDatabaseClient(DatabaseId.of("test-project", "test-instance", "test-database"));
    verify(serviceFactory.mockDatabaseClient(), times(1)).writeAtLeastOnce(argThat(new IterableOfSize(2)));
}
Also used : Mutation(com.google.cloud.spanner.Mutation) Test(org.junit.Test)

Example 34 with Mutation

use of com.google.cloud.spanner.Mutation in project google-cloud-java by GoogleCloudPlatform.

the class ITReadOnlyTxnTest method writeNewValue.

private static void writeNewValue(DatabaseClient client, int i, @Nullable ImmutableList.Builder<History> historyBuilder) {
    String value = "v" + i;
    Mutation m = Mutation.newInsertOrUpdateBuilder(TABLE_NAME).set("StringValue").to(value).build();
    long minCommitNanoTime = System.nanoTime();
    Timestamp timestamp = client.writeAtLeastOnce(Arrays.asList(m));
    if (historyBuilder != null) {
        historyBuilder.add(new History(timestamp, value, minCommitNanoTime));
    }
}
Also used : Mutation(com.google.cloud.spanner.Mutation) Timestamp(com.google.cloud.Timestamp)

Example 35 with Mutation

use of com.google.cloud.spanner.Mutation in project spring-cloud-gcp by spring-cloud.

the class SpannerMutationFactoryImplTests method insertTest.

@Test
public void insertTest() {
    Mutation mutation = this.spannerMutationFactory.insert(new TestEntity());
    assertEquals("custom_test_table", mutation.getTable());
    assertEquals(Op.INSERT, mutation.getOperation());
}
Also used : Mutation(com.google.cloud.spanner.Mutation) Test(org.junit.Test)

Aggregations

Mutation (com.google.cloud.spanner.Mutation)70 Test (org.junit.Test)53 ArrayList (java.util.ArrayList)11 CommitResponse (com.google.cloud.spanner.CommitResponse)4 KeySet (com.google.cloud.spanner.KeySet)4 ReadQueryUpdateTransactionOption (com.google.cloud.spanner.Options.ReadQueryUpdateTransactionOption)4 Timestamp (com.google.cloud.Timestamp)3 DatabaseClient (com.google.cloud.spanner.DatabaseClient)3 ResultSet (com.google.cloud.spanner.ResultSet)3 TransactionContext (com.google.cloud.spanner.TransactionContext)3 BatchableMutationFilterFn (org.apache.beam.sdk.io.gcp.spanner.SpannerIO.BatchableMutationFilterFn)3 Sleeper (org.apache.beam.sdk.util.Sleeper)3 Database (com.google.cloud.spanner.Database)2 DatabaseAdminClient (com.google.cloud.spanner.DatabaseAdminClient)2 Key (com.google.cloud.spanner.Key)2 WriteBuilder (com.google.cloud.spanner.Mutation.WriteBuilder)2 SpannerException (com.google.cloud.spanner.SpannerException)2 TransactionRunner (com.google.cloud.spanner.TransactionRunner)2 CreateDatabaseMetadata (com.google.spanner.admin.database.v1.CreateDatabaseMetadata)2 BigDecimal (java.math.BigDecimal)2