Search in sources :

Example 61 with Struct

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

the class ITReadOnlyTxnTest method multiReadTimestamp.

@Test
public void multiReadTimestamp() {
    setUpPrivateDatabase();
    History expected = history.get(2);
    try (ReadOnlyTransaction readContext = client.readOnlyTransaction(TimestampBound.ofReadTimestamp(expected.timestamp))) {
        Struct row = readRow(readContext);
        assertThat(row).isNotNull();
        assertThat(row.getString(0)).isEqualTo(expected.value);
        assertThat(readContext.getReadTimestamp()).isEqualTo(expected.timestamp);
        insertAndReadAgain(readContext, readContext.getReadTimestamp(), 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)

Example 62 with Struct

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

the class ITReadOnlyTxnTest method singleMaxStaleness.

@Test
public void singleMaxStaleness() {
    History minimum = history.get(2);
    NavigableMap<Timestamp, String> possibleValues = new TreeMap<>();
    for (History item : history.subList(2, history.size())) {
        possibleValues.put(item.timestamp, item.value);
    }
    // Pick a staleness that cannot precede the second write (which, in practice, is the staleness
    // that exceeds the minimum commit time for the subsequent write).
    long stalenessNanos = System.nanoTime() - history.get(3).minCommitNanoTime;
    TimestampBound bound = TimestampBound.ofMaxStaleness(stalenessNanos, TimeUnit.NANOSECONDS);
    ReadOnlyTransaction readContext = client.singleUseReadOnlyTransaction(bound);
    Struct row = readRow(readContext);
    assertThat(row).isNotNull();
    assertThat(readContext.getReadTimestamp()).isAtLeast(minimum.timestamp);
    assertThat(row.getString(0)).isEqualTo(possibleValues.floorEntry(readContext.getReadTimestamp()).getValue());
    row = readRow(client.singleUse(bound));
    assertThat(row).isNotNull();
    assertThat(row.getString(0)).isIn(possibleValues.values());
}
Also used : TimestampBound(com.google.cloud.spanner.TimestampBound) ReadOnlyTransaction(com.google.cloud.spanner.ReadOnlyTransaction) TreeMap(java.util.TreeMap) Timestamp(com.google.cloud.Timestamp) Struct(com.google.cloud.spanner.Struct) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 63 with Struct

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

the class ITReadOnlyTxnTest method query.

@Test
public void query() {
    // We don't exhaustively test query with all modes - the read tests give us enough confidence
    // that transaction options are generated appropriately.  Just do one test for each type of
    // context to ensure that transaction options are set at all.
    History expected = history.get(2);
    TimestampBound bound = TimestampBound.ofReadTimestamp(expected.timestamp);
    ReadOnlyTransaction readContext = client.singleUseReadOnlyTransaction(bound);
    Struct row = queryRow(readContext);
    assertThat(row).isNotNull();
    assertThat(row.getString(0)).isEqualTo(expected.value);
    assertThat(readContext.getReadTimestamp()).isEqualTo(expected.timestamp);
    readContext = client.readOnlyTransaction(bound);
    row = queryRow(readContext);
    assertThat(row).isNotNull();
    assertThat(row.getString(0)).isEqualTo(expected.value);
    assertThat(readContext.getReadTimestamp()).isEqualTo(expected.timestamp);
    readContext.close();
    row = queryRow(client.singleUse(bound));
    assertThat(row).isNotNull();
    assertThat(row.getString(0)).isEqualTo(expected.value);
}
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 64 with Struct

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

the class ITReadOnlyTxnTest method singleMinReadTimestamp.

@Test
public void singleMinReadTimestamp() {
    int minimumIndex = 2;
    History minimum = history.get(minimumIndex);
    NavigableMap<Timestamp, String> possibleValues = new TreeMap<>();
    for (History item : history.subList(minimumIndex, history.size())) {
        possibleValues.put(item.timestamp, item.value);
    }
    TimestampBound bound = TimestampBound.ofMinReadTimestamp(minimum.timestamp);
    ReadOnlyTransaction readContext = client.singleUseReadOnlyTransaction(bound);
    Struct row = readRow(readContext);
    assertThat(row).isNotNull();
    assertThat(readContext.getReadTimestamp()).isAtLeast(minimum.timestamp);
    assertThat(row.getString(0)).isEqualTo(possibleValues.floorEntry(readContext.getReadTimestamp()).getValue());
    row = readRow(client.singleUse(bound));
    assertThat(row).isNotNull();
    assertThat(row.getString(0)).isIn(possibleValues.values());
}
Also used : TimestampBound(com.google.cloud.spanner.TimestampBound) ReadOnlyTransaction(com.google.cloud.spanner.ReadOnlyTransaction) TreeMap(java.util.TreeMap) Timestamp(com.google.cloud.Timestamp) Struct(com.google.cloud.spanner.Struct) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 65 with Struct

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

the class ITReadTest method pointRead.

@Test
public void pointRead() {
    Struct row = client.singleUse(TimestampBound.strong()).readRow(TABLE_NAME, Key.of("k1"), ALL_COLUMNS);
    assertThat(row).isNotNull();
    assertThat(row.getString(0)).isEqualTo("k1");
    assertThat(row.getString(1)).isEqualTo("v1");
    // Ensure that the Struct implementation supports equality properly.
    assertThat(row).isEqualTo(Struct.newBuilder().set("Key").to("k1").set("StringValue").to("v1").build());
}
Also used : 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