Search in sources :

Example 56 with Struct

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

the class ITReadOnlyTxnTest method singleExactStaleness.

@Test
public void singleExactStaleness() {
    // TODO(user): Use a shorter deadline (when supported) and pass on the call to Cloud Spanner.
    long deadlineNanoTime = System.nanoTime() + TimeUnit.MINUTES.toNanos(1);
    // The only exact staleness values that can be tested reliably are before the first item or
    // later than the last item: we choose the former.
    //
    // Pick a staleness that is "guaranteed" not to observe the first write.  Note that this
    // guarantee doesn't strictly hold in the absence of enforced read deadlines, but we use a
    // deadline large enough to make it practically true.
    long stalenessNanos = 1 + deadlineNanoTime - history.get(0).minCommitNanoTime;
    TimestampBound bound = TimestampBound.ofExactStaleness(stalenessNanos, TimeUnit.NANOSECONDS);
    ReadOnlyTransaction readContext = client.singleUseReadOnlyTransaction(bound);
    Struct row = readRow(readContext);
    assertThat(row).isNull();
    assertThat(readContext.getReadTimestamp().toSqlTimestamp()).isLessThan(history.get(0).timestamp.toSqlTimestamp());
    row = readRow(client.singleUse(bound));
    assertThat(row).isNull();
}
Also used : TimestampBound(com.google.cloud.spanner.TimestampBound) ReadOnlyTransaction(com.google.cloud.spanner.ReadOnlyTransaction) Struct(com.google.cloud.spanner.Struct) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 57 with Struct

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

the class ITReadOnlyTxnTest method queryRow.

private static Struct queryRow(ReadContext ctx) {
    ResultSet resultSet = Statement.of("SELECT StringValue FROM TestTable").executeQuery(ctx);
    // TODO(user): Consider a library routine to consume a single row from a query.
    assertThat(resultSet.next()).isTrue();
    Struct row = resultSet.getCurrentRowAsStruct();
    assertThat(resultSet.next()).isFalse();
    return row;
}
Also used : ResultSet(com.google.cloud.spanner.ResultSet) Struct(com.google.cloud.spanner.Struct)

Example 58 with Struct

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

the class ITReadOnlyTxnTest method insertAndReadAgain.

private void insertAndReadAgain(ReadOnlyTransaction readContext, Timestamp expectedTimestamp, @Nullable String expectedValue) {
    writeNewValue(client, history.size(), null);
    Struct row = readRow(readContext);
    if (expectedValue == null) {
        assertThat(row).isNull();
    } else {
        assertThat(row).isNotNull();
        assertThat(row.getString(0)).isEqualTo(expectedValue);
    }
    assertThat(readContext.getReadTimestamp()).isEqualTo(expectedTimestamp);
}
Also used : Struct(com.google.cloud.spanner.Struct)

Example 59 with Struct

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

the class ITReadOnlyTxnTest method multiExactStaleness.

@Test
public void multiExactStaleness() {
    setUpPrivateDatabase();
    // See singleExactStaleness() for why we pick this timestamp.  We expect to see no value.
    long deadlineNanoTime = System.nanoTime() + TimeUnit.MINUTES.toNanos(1);
    long stalenessNanos = 1 + deadlineNanoTime - history.get(0).minCommitNanoTime;
    try (ReadOnlyTransaction readContext = client.readOnlyTransaction(TimestampBound.ofExactStaleness(stalenessNanos, TimeUnit.NANOSECONDS))) {
        Struct row = readRow(readContext);
        assertThat(row).isNull();
        assertThat(readContext.getReadTimestamp().toSqlTimestamp()).isLessThan(history.get(0).timestamp.toSqlTimestamp());
        insertAndReadAgain(readContext, readContext.getReadTimestamp(), null);
    }
}
Also used : ReadOnlyTransaction(com.google.cloud.spanner.ReadOnlyTransaction) Struct(com.google.cloud.spanner.Struct) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 60 with Struct

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

the class ITReadOnlyTxnTest method singleStrong.

@Test
public void singleStrong() {
    History expected = history.get(history.size() - 1);
    ReadOnlyTransaction readContext = client.singleUseReadOnlyTransaction();
    Struct row = readRow(readContext);
    assertThat(row).isNotNull();
    assertThat(row.getString(0)).isEqualTo(expected.value);
    assertThat(readContext.getReadTimestamp()).isAtLeast(expected.timestamp);
    row = readRow(client.singleUse());
    assertThat(row).isNotNull();
    assertThat(row.getString(0)).isEqualTo(expected.value);
}
Also used : ReadOnlyTransaction(com.google.cloud.spanner.ReadOnlyTransaction) Struct(com.google.cloud.spanner.Struct) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

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