/** Example to do a batch strong read. */ BatchReadOnlyTransaction readStrong() { // [START batch_client_strong_read] BatchReadOnlyTransaction txn = batchClient.batchReadOnlyTransaction(TimestampBound.strong()); // [END batch_client_strong_read] return txn; }
private Struct readLastRow(String... columns) { return client .singleUse(TimestampBound.strong()) .readRow("T", Key.of(lastKey), Arrays.asList(columns)); }
@Test public void newSingleUseContextClosesOldSingleUseContext() { ReadContext ctx = session.singleUse(TimestampBound.strong()); session.singleUse(TimestampBound.strong()); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("invalidated"); ctx.read("Dummy", KeySet.all(), Arrays.asList("C")); }
@Test public void newMultiUseReadOnlyTransactionContextClosesOldSingleUseContext() { ReadContext ctx = session.singleUse(TimestampBound.strong()); session.readOnlyTransaction(TimestampBound.strong()); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("invalidated"); ctx.read("Dummy", KeySet.all(), Arrays.asList("C")); }
@Override public void run() { client .singleUse(TimestampBound.strong()) .readRow(TABLE_NAME, Key.of("k1"), ALL_COLUMNS); } });
@Test public void newSingleUseContextClosesOldSingleUseReadOnlyTransactionContext() { ReadContext ctx = session.singleUseReadOnlyTransaction(TimestampBound.strong()); session.singleUse(TimestampBound.strong()); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("invalidated"); ctx.read("Dummy", KeySet.all(), Arrays.asList("C")); }
private List<Struct> resultRows(Statement statement, Type expectedRowType) { ArrayList<Struct> results = new ArrayList<>(); ResultSet resultSet = statement.executeQuery(client.singleUse(TimestampBound.strong())); while (resultSet.next()) { Struct row = resultSet.getCurrentRowAsStruct(); results.add(row); } assertThat(resultSet.getType()).isEqualTo(expectedRowType); assertThat(resultSet.next()).isFalse(); return results; }
@Test public void strong() { TimestampBound bound = TimestampBound.strong(); assertThat(bound.getMode()).isEqualTo(Mode.STRONG); assertThat(bound.toString()).isEqualTo("strong"); assertProto(bound, "strong: true"); }
@Test public void pointReadNotFound() { Struct row = client.singleUse(TimestampBound.strong()).readRow(TABLE_NAME, Key.of("k999"), ALL_COLUMNS); assertThat(row).isNull(); }
@Test public void indexPointReadNotFound() { Struct row = client .singleUse(TimestampBound.strong()) .readRowUsingIndex(TABLE_NAME, INDEX_NAME, Key.of("v999"), ALL_COLUMNS); assertThat(row).isNull(); }
private Struct executeWithRowResultType(Statement statement, Type expectedRowType) { ResultSet resultSet = statement.executeQuery(client.singleUse(TimestampBound.strong())); assertThat(resultSet.next()).isTrue(); assertThat(resultSet.getType()).isEqualTo(expectedRowType); Struct row = resultSet.getCurrentRowAsStruct(); assertThat(resultSet.next()).isFalse(); return row; }
@Test public void indexPointRead() { Struct row = client .singleUse(TimestampBound.strong()) .readRowUsingIndex(TABLE_NAME, INDEX_NAME, Key.of("v1"), ALL_COLUMNS); assertThat(row).isNotNull(); assertThat(row.getString(0)).isEqualTo("k1"); assertThat(row.getString(1)).isEqualTo("v1"); }
@Test public void transactionClosesOldSingleUseContext() { ReadContext ctx = session.singleUse(TimestampBound.strong()); // Note that we don't even run the transaction - just preparing the runner is sufficient. session.readWriteTransaction(); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("invalidated"); ctx.read("Dummy", KeySet.all(), Arrays.asList("C")); }
@Test public void columnNotFound() { expectedException.expect(isSpannerException(ErrorCode.NOT_FOUND)); expectedException.expectMessage("BadColumnName"); client .singleUse(TimestampBound.strong()) .readRow(TABLE_NAME, Key.of("k1"), Arrays.asList("Key", "BadColumnName")); }
@Test public void analyzePlan() { Statement statement = Statement.of("SELECT 1 AS column UNION ALL SELECT 2"); ResultSet resultSet = statement.analyzeQuery(client.singleUse(TimestampBound.strong()), QueryAnalyzeMode.PLAN); assertThat(resultSet.next()).isFalse(); assertThat(resultSet.getType()).isEqualTo(Type.struct(StructField.of("column", Type.int64()))); ResultSetStats receivedStats = resultSet.getStats(); assertThat(receivedStats).isNotNull(); assertThat(receivedStats.hasQueryPlan()).isTrue(); assertThat(receivedStats.hasQueryStats()).isFalse(); }
@Test public void tableNotFound() { expectedException.expect(isSpannerException(ErrorCode.NOT_FOUND)); expectedException.expectMessage("BadTableName"); client.singleUse(TimestampBound.strong()).readRow("BadTableName", Key.of("k1"), ALL_COLUMNS); }
@Test public void unboundParameter() { ResultSet resultSet = Statement.of("SELECT @v").executeQuery(client.singleUse(TimestampBound.strong())); expectedException.expect(isSpannerException(ErrorCode.INVALID_ARGUMENT)); expectedException.expectMessage("No parameter found for binding: v"); resultSet.next(); }
@Test public void cursorErrorDeferred() { // Error should be deferred until next(). This gives consistent behavior with respect to // non-blocking implementations (e.g., gRPC). ResultSet resultSet = client .singleUse(TimestampBound.strong()) .read("BadTableName", KeySet.singleKey(Key.of("k1")), ALL_COLUMNS); expectedException.expect(isSpannerException(ErrorCode.NOT_FOUND)); expectedException.expectMessage("BadTableName"); resultSet.next(); }
@Test public void invalidDatabase() { RemoteSpannerHelper helper = env.getTestHelper(); DatabaseClient invalidClient = helper.getClient().getDatabaseClient(DatabaseId.of(helper.getInstanceId(), "invalid")); expectedException.expect(isSpannerException(ErrorCode.NOT_FOUND)); invalidClient .singleUse(TimestampBound.strong()) .readRow(TABLE_NAME, Key.of("k99"), ALL_COLUMNS); }
@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())); }