@Override public ResultSet read( String table, KeySet keys, Iterable<String> columns, ReadOption... options) { return wrap(delegate.read(table, keys, columns, options)); }
ResultSet read() { // [START read_context_read] ReadContext readContext = dbClient.singleUse(); ResultSet resultSet = readContext.read( "Albums", // KeySet.all() can be used to read all rows in a table. KeySet exposes other // methods to read only a subset of the table. KeySet.all(), Arrays.asList("SingerId", "AlbumId", "AlbumTitle")); // [END read_context_read] return resultSet; }
@Override public Status scan( String table, String startKey, int recordCount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) { if (queriesForReads) { return scanUsingQuery(table, startKey, recordCount, fields, result); } Iterable<String> columns = fields == null ? STANDARD_FIELDS : fields; KeySet keySet = KeySet.newBuilder().addRange(KeyRange.closedClosed(Key.of(startKey), Key.of())).build(); try (ResultSet resultSet = dbClient.singleUse(timestampBound) .read(table, keySet, columns, Options.limit(recordCount))) { while (resultSet.next()) { HashMap<String, ByteIterator> row = new HashMap<>(); decodeStruct(columns, resultSet, row); result.add(row); } return Status.OK; } catch (Exception e) { LOGGER.log(Level.INFO, "scan()", e); return Status.ERROR; } }
@Test public void read() { try (ResultSet resultSet = client .singleUse() .read(TABLE_NAME, KeySet.all(), Arrays.asList("Key", "Data", "Fingerprint", "Size"))) { validate(resultSet); } }
@Test public void prepareClosesOldSingleUseContext() { ReadContext ctx = session.singleUse(TimestampBound.strong()); Mockito.when(rpc.beginTransaction(Mockito.<BeginTransactionRequest>any(), Mockito.eq(options))) .thenReturn(Transaction.newBuilder().setId(ByteString.copyFromUtf8("t1")).build()); session.prepareReadWriteTransaction(); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("invalidated"); ctx.read("Dummy", KeySet.all(), Arrays.asList("C")); }
@Test public void writeClosesOldSingleUseContext() throws ParseException { ReadContext ctx = session.singleUse(TimestampBound.strong()); Mockito.when(rpc.commit(Mockito.<CommitRequest>any(), Mockito.eq(options))) .thenReturn( CommitResponse.newBuilder() .setCommitTimestamp(Timestamps.parse("2015-10-01T10:54:20.021Z")) .build()); session.writeAtLeastOnce(Arrays.<Mutation>asList()); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("invalidated"); ctx.read("Dummy", KeySet.all(), Arrays.asList("C")); }
@Test public void readWithSmallPrefetchChunks() { try (ResultSet resultSet = client .singleUse() .read( TABLE_NAME, KeySet.all(), Arrays.asList("Key", "Data", "Fingerprint", "Size"), Options.prefetchChunks(1))) { validate(resultSet); } }
@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 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")); }
@Test public void rowsAreSnapshots() { List<Struct> rows = new ArrayList<>(); ResultSet resultSet = client .singleUse(TimestampBound.strong()) .read( TABLE_NAME, KeySet.newBuilder() .addKey(Key.of("k2")) .addKey(Key.of("k3")) .addKey(Key.of("k4")) .build(), ALL_COLUMNS); while (resultSet.next()) { rows.add(resultSet.getCurrentRowAsStruct()); } assertThat(rows.size()).isEqualTo(3); assertThat(rows.get(0).getString(0)).isEqualTo("k2"); assertThat(rows.get(0).getString(1)).isEqualTo("v2"); assertThat(rows.get(1).getString(0)).isEqualTo("k3"); assertThat(rows.get(1).getString(1)).isEqualTo("v3"); assertThat(rows.get(2).getString(0)).isEqualTo("k4"); assertThat(rows.get(2).getString(1)).isEqualTo("v4"); }
@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")); }
@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 newSingleUseContextClosesOldMultiUseReadOnlyTransactionContext() { 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")); }
@Test public void newSingleUseReadOnlyTransactionContextClosesOldSingleUseContext() { ReadContext ctx = session.singleUse(TimestampBound.strong()); session.singleUseReadOnlyTransaction(TimestampBound.strong()); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("invalidated"); ctx.read("Dummy", KeySet.all(), Arrays.asList("C")); }
@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); }
client .singleUse(TimestampBound.strong()) .read("T", keys.build(), Arrays.asList("K", "BytesValue")); while (resultSet.next()) { String key = resultSet.getString(0);
client .singleUse(TimestampBound.strong()) .read("T", KeySet.range(KeyRange.prefix(Key.of("boo"))), Arrays.asList("K")); assertThat(resultSet.next()).isFalse();
@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(); }
keys.addKey(Key.of(key1)).addKey(Key.of(key2)); ResultSet resultSet = client.singleUse(TimestampBound.strong()).read("T", keys.build(), Arrays.asList("K")); int rowCount = 0; while (resultSet.next()) {