Search in sources :

Example 6 with Struct

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

the class ITWriteTest method writeStringArrayEmpty.

@Test
public void writeStringArrayEmpty() {
    write(baseInsert().set("StringArrayValue").toStringArray(Arrays.<String>asList()).build());
    Struct row = readLastRow("StringArrayValue");
    assertThat(row.isNull(0)).isFalse();
    assertThat(row.getStringList(0)).containsExactly();
}
Also used : Struct(com.google.cloud.spanner.Struct) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 7 with Struct

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

the class ITWriteTest method writeDateArrayNull.

@Test
public void writeDateArrayNull() {
    write(baseInsert().set("DateArrayValue").toDateArray(null).build());
    Struct row = readLastRow("DateArrayValue");
    assertThat(row.isNull(0)).isTrue();
}
Also used : Struct(com.google.cloud.spanner.Struct) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 8 with Struct

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

the class ITWriteTest method writeTimestampArray.

@Test
public void writeTimestampArray() {
    Timestamp t1 = Timestamp.parseTimestamp("2016-09-18T00:00:00Z");
    Timestamp t2 = Timestamp.parseTimestamp("2016-09-19T00:00:00Z");
    write(baseInsert().set("TimestampArrayValue").toTimestampArray(Arrays.asList(t1, null, t2)).build());
    Struct row = readLastRow("TimestampArrayValue");
    assertThat(row.isNull(0)).isFalse();
    assertThat(row.getTimestampList(0)).containsExactly(t1, null, t2).inOrder();
}
Also used : Timestamp(com.google.cloud.Timestamp) Struct(com.google.cloud.spanner.Struct) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 9 with Struct

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

the class ITWriteTest method writeTimestampArrayNull.

@Test
public void writeTimestampArrayNull() {
    write(baseInsert().set("TimestampArrayValue").toTimestampArray(null).build());
    Struct row = readLastRow("TimestampArrayValue");
    assertThat(row.isNull(0)).isTrue();
}
Also used : Struct(com.google.cloud.spanner.Struct) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 10 with Struct

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

the class ITTransactionTest method doBasicsTest.

private void doBasicsTest(final ReadStrategy strategy) throws InterruptedException {
    final String key = uniqueKey();
    // Initial value.
    client.write(Arrays.asList(Mutation.newInsertBuilder("T").set("K").to(key).set("V").to(0).build()));
    final int numThreads = 3;
    final CountDownLatch commitBarrier = new CountDownLatch(numThreads);
    final CountDownLatch complete = new CountDownLatch(numThreads);
    final TransactionCallable<Long> callable = new TransactionCallable<Long>() {

        @Override
        public Long run(TransactionContext transaction) throws SpannerException {
            Struct row = strategy.read(transaction, key);
            long newValue = row.getLong(0) + 1;
            transaction.buffer(Mutation.newUpdateBuilder("T").set("K").to(key).set("V").to(newValue).build());
            commitBarrier.countDown();
            // Synchronize so that all threads attempt to commit at the same time.
            Uninterruptibles.awaitUninterruptibly(commitBarrier);
            return newValue;
        }
    };
    // We start multiple threads all attempting to update the same value concurrently.  We expect
    // to see at least some of the corresponding transactions abort.
    final Vector<Long> results = new Vector<>();
    final Vector<Timestamp> commitTimestamps = new Vector<>();
    class TxnThread extends Thread {

        @Override
        public void run() {
            TransactionRunner runner = client.readWriteTransaction();
            Long result = runner.run(callable);
            results.add(result);
            commitTimestamps.add(runner.getCommitTimestamp());
            complete.countDown();
        }
    }
    for (int i = 0; i < numThreads; ++i) {
        new TxnThread().start();
    }
    complete.await();
    assertThat(results).hasSize(numThreads);
    List<Long> expectedResults = new ArrayList<>();
    for (int i = 0; i < numThreads; ++i) {
        expectedResults.add(i + 1L);
    }
    assertThat(results).containsAllIn(expectedResults);
    assertThat(Sets.newHashSet(commitTimestamps)).hasSize(numThreads);
    assertThat(client.singleUse(TimestampBound.strong()).readRow("T", Key.of(key), Arrays.asList("V")).getLong(0)).isEqualTo(Long.valueOf(numThreads));
}
Also used : TransactionCallable(com.google.cloud.spanner.TransactionRunner.TransactionCallable) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Timestamp(com.google.cloud.Timestamp) Struct(com.google.cloud.spanner.Struct) TransactionContext(com.google.cloud.spanner.TransactionContext) TransactionRunner(com.google.cloud.spanner.TransactionRunner) Vector(java.util.Vector)

Aggregations

Struct (com.google.cloud.spanner.Struct)106 IntegrationTest (com.google.cloud.spanner.IntegrationTest)101 Test (org.junit.Test)101 Timestamp (com.google.cloud.Timestamp)9 ReadOnlyTransaction (com.google.cloud.spanner.ReadOnlyTransaction)9 Date (com.google.cloud.Date)6 ByteArray (com.google.cloud.ByteArray)5 TimestampBound (com.google.cloud.spanner.TimestampBound)5 TransactionContext (com.google.cloud.spanner.TransactionContext)4 Type (com.google.cloud.spanner.Type)4 SpannerException (com.google.cloud.spanner.SpannerException)3 SpannerExceptionFactory.newSpannerException (com.google.cloud.spanner.SpannerExceptionFactory.newSpannerException)3 TransactionCallable (com.google.cloud.spanner.TransactionRunner.TransactionCallable)3 AbortedException (com.google.cloud.spanner.AbortedException)2 ResultSet (com.google.cloud.spanner.ResultSet)2 ArrayList (java.util.ArrayList)2 TreeMap (java.util.TreeMap)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Ignore (org.junit.Ignore)2 TransactionRunner (com.google.cloud.spanner.TransactionRunner)1