Struct readRowUsingIndex() { // [START read_context_read_index] ReadContext readContext = dbClient.singleUse(); Struct row = readContext.readRowUsingIndex( "Albums", "AlbumsByAlbumId", Key.of(1, "Green"), Arrays.asList("AlbumId", "AlbumTitle")); // [END read_context_read_index] return row; }
@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); }
Struct readRow() { // [START read_context_read_row] ReadContext readContext = dbClient.singleUse(); Struct row = readContext.readRow("Albums", Key.of(2, 1), Arrays.asList("MarketingBudget")); // [END read_context_read_row] return row; }
/** Example of single use. */ // [TARGET singleUse()] // [VARIABLE my_singer_id] public String singleUse(long singerId) { // [START singleUse] String column = "FirstName"; Struct row = dbClient.singleUse().readRow("Singers", Key.of(singerId), Collections.singleton(column)); String firstName = row.getString(column); // [END singleUse] return firstName; }
private Struct readLastRow(String... columns) { return client .singleUse(TimestampBound.strong()) .readRow("T", Key.of(lastKey), Arrays.asList(columns)); }
@Test public void read() { try (ResultSet resultSet = client .singleUse() .read(TABLE_NAME, KeySet.all(), Arrays.asList("Key", "Data", "Fingerprint", "Size"))) { validate(resultSet); } }
@Override public void run() { client .singleUse(TimestampBound.strong()) .readRow(TABLE_NAME, Key.of("k1"), ALL_COLUMNS); } });
@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(); }
@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"); }
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 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 queryWithSmallPrefetchChunks() { try (ResultSet resultSet = client .singleUse() .executeQuery( Statement.of( "SELECT Key, Data, Fingerprint, Size FROM " + TABLE_NAME + " ORDER BY Key"), Options.prefetchChunks(1))) { validate(resultSet); } }
@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 emptyRead() { ResultSet resultSet = client .singleUse(TimestampBound.strong()) .read( TABLE_NAME, KeySet.range(KeyRange.closedOpen(Key.of("k99"), Key.of("z"))), ALL_COLUMNS); assertThat(resultSet.next()).isFalse(); assertThat(resultSet.getType()).isEqualTo(TABLE_TYPE); }
@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()); }
@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 rollback() { TransactionManager manager = client.transactionManager(); TransactionContext txn = manager.begin(); txn.buffer( Mutation.newInsertBuilder("T").set("K").to("Key2").set("BoolValue").to(true).build()); manager.rollback(); assertThat(manager.getState()).isEqualTo(TransactionState.ROLLED_BACK); // Row should not have been inserted. assertThat(client.singleUse().readRow("T", Key.of("Key2"), Arrays.asList("K", "BoolValue"))) .isNull(); }
@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 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); }