CloudSpannerProperties.BOUNDED_STALENESS, "0")); timestampBound = (boundedStalenessSeconds <= 0) ? TimestampBound.strong() : TimestampBound.ofMaxStaleness(boundedStalenessSeconds, TimeUnit.SECONDS);
/** Example of single use with timestamp bound. */ // [TARGET singleUse(TimestampBound)] // [VARIABLE my_singer_id] public String singleUseStale(long singerId) { // [START singleUseStale] String column = "FirstName"; Struct row = dbClient .singleUse(TimestampBound.ofMaxStaleness(10, TimeUnit.SECONDS)) .readRow("Singers", Key.of(singerId), Collections.singleton(column)); String firstName = row.getString(column); // [END singleUseStale] return firstName; }
/** Example of single use read only transaction with timestamp bound. */ // [TARGET singleUseReadOnlyTransaction(TimestampBound)] // [VARIABLE my_singer_id] public Timestamp singleUseReadOnlyTransactionTimestamp(long singerId) { // [START singleUseReadOnlyTransactionTimestamp] String column = "FirstName"; ReadOnlyTransaction txn = dbClient.singleUseReadOnlyTransaction(TimestampBound.ofMaxStaleness(10, TimeUnit.SECONDS)); Struct row = txn.readRow("Singers", Key.of(singerId), Collections.singleton(column)); row.getString(column); Timestamp timestamp = txn.getReadTimestamp(); // [END singleUseReadOnlyTransactionTimestamp] return timestamp; }
@Test public void maxStalenessNegative() { expectedException.expect(IllegalArgumentException.class); TimestampBound.ofMaxStaleness(-1, TimeUnit.SECONDS); }
private void keepAlive() { markUsed(); delegate .singleUse(TimestampBound.ofMaxStaleness(60, TimeUnit.SECONDS)) .executeQuery(Statement.newBuilder("SELECT 1").build()) .next(); }
@Test public void multiMaxStaleness() { // Cannot use bounded modes with multi-read transactions. expectedException.expect(IllegalArgumentException.class); client.readOnlyTransaction(TimestampBound.ofMaxStaleness(1, TimeUnit.SECONDS)); } }
@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()); }
@Test public void maxStaleness() { TimestampBound bound = TimestampBound.ofMaxStaleness(3140, TimeUnit.MILLISECONDS); assertThat(bound.getMode()).isEqualTo(Mode.MAX_STALENESS); assertThat(bound.getMaxStaleness(TimeUnit.SECONDS)).isEqualTo(3); assertThat(bound.getMaxStaleness(TimeUnit.MILLISECONDS)).isEqualTo(3140); assertThat(bound.getMaxStaleness(TimeUnit.MICROSECONDS)).isEqualTo(3140000); assertThat(bound.getMaxStaleness(TimeUnit.NANOSECONDS)).isEqualTo(3140000000L); assertThat(bound.toString()).isEqualTo("max_staleness: 3.140s"); assertProto(bound, "max_staleness { seconds: 3 nanos: 140000000 }"); }
@Test public void equalsAndHashCode() { Timestamp ts = Timestamp.ofTimeSecondsAndNanos(1444662894L, 0); Timestamp ts2 = Timestamp.ofTimeSecondsAndNanos(1444662895L, 0); int staleness = 5; EqualsTester tester = new EqualsTester(); tester.addEqualityGroup(TimestampBound.strong(), TimestampBound.strong()); tester.addEqualityGroup(TimestampBound.ofReadTimestamp(ts), TimestampBound.ofReadTimestamp(ts)); tester.addEqualityGroup(TimestampBound.ofReadTimestamp(ts2)); tester.addEqualityGroup( TimestampBound.ofMinReadTimestamp(ts), TimestampBound.ofMinReadTimestamp(ts)); tester.addEqualityGroup(TimestampBound.ofMinReadTimestamp(ts2)); tester.addEqualityGroup( TimestampBound.ofExactStaleness(staleness, TimeUnit.SECONDS), TimestampBound.ofExactStaleness(staleness, TimeUnit.SECONDS)); tester.addEqualityGroup(TimestampBound.ofExactStaleness(staleness, TimeUnit.MILLISECONDS)); tester.addEqualityGroup( TimestampBound.ofMaxStaleness(staleness, TimeUnit.SECONDS), TimestampBound.ofMaxStaleness(staleness, TimeUnit.SECONDS)); tester.addEqualityGroup(TimestampBound.ofMaxStaleness(staleness, TimeUnit.MILLISECONDS)); tester.testEquals(); }
@Test public void serialization() throws Exception { reserializeAndAssert(TimestampBound.strong()); reserializeAndAssert(TimestampBound.ofExactStaleness(10, TimeUnit.NANOSECONDS)); reserializeAndAssert(TimestampBound.ofMaxStaleness(100, TimeUnit.DAYS)); reserializeAndAssert(TimestampBound.ofMinReadTimestamp(Timestamp.now())); reserializeAndAssert(TimestampBound.ofReadTimestamp(Timestamp.now())); }
private void keepAlive() { markUsed(); delegate .singleUse(TimestampBound.ofMaxStaleness(60, TimeUnit.SECONDS)) .executeQuery(Statement.newBuilder("SELECT 1").build()) .next(); }