Refine search
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 toBuilder() { KeySet set = KeySet.singleKey(Key.of(1)).toBuilder().addKey(Key.of(2)).build(); assertThat(set.isAll()).isFalse(); assertThat(set.getKeys()).containsExactly(Key.of(1), Key.of(2)).inOrder(); assertThat(set.getRanges()).isEmpty(); set = KeySet.range(KeyRange.closedOpen(Key.of("a"), Key.of("b"))) .toBuilder() .addRange(KeyRange.closedOpen(Key.of("c"), Key.of("d"))) .build(); assertThat(set.isAll()).isFalse(); assertThat(set.getKeys()).isEmpty(); assertThat(set.getRanges()) .containsExactly( KeyRange.closedOpen(Key.of("a"), Key.of("b")), KeyRange.closedOpen(Key.of("c"), Key.of("d"))) .inOrder(); set = KeySet.all().toBuilder().addKey(Key.of(1)).build(); assertThat(set.isAll()).isTrue(); assertThat(set.getKeys()).containsExactly(Key.of(1)); assertThat(set.getRanges()).isEmpty(); }
@Test public void range() { KeySet set = KeySet.range(KeyRange.closedOpen(Key.of("a"), Key.of("b"))); assertThat(set.isAll()).isFalse(); assertThat(set.getKeys()).isEmpty(); assertThat(set.getRanges()).containsExactly(KeyRange.closedOpen(Key.of("a"), Key.of("b"))); }
@Test public void indexRangeReads() { checkRange(Source.INDEX, KeySet.singleKey(Key.of("v1")), 1); checkRange(Source.INDEX, KeyRange.closedOpen(Key.of("v3"), Key.of("v5")), 3, 4); checkRange(Source.INDEX, KeyRange.closedClosed(Key.of("v3"), Key.of("v5")), 3, 4, 5); checkRange(Source.INDEX, KeyRange.openClosed(Key.of("v3"), Key.of("v5")), 4, 5); checkRange(Source.INDEX, KeyRange.openOpen(Key.of("v3"), Key.of("v5")), 4); // Partial key specification. checkRange(Source.INDEX, KeyRange.closedClosed(Key.of("v7"), Key.of()), 7, 8, 9); checkRange(Source.INDEX, KeyRange.openClosed(Key.of("v7"), Key.of()), 8, 9); checkRange(Source.INDEX, KeyRange.closedOpen(Key.of(), Key.of("v11")), 0, 1, 10); checkRange(Source.INDEX, KeyRange.closedClosed(Key.of(), Key.of("v11")), 0, 1, 10, 11); // The following produce empty ranges. checkRange(Source.INDEX, KeyRange.closedOpen(Key.of("v7"), Key.of())); checkRange(Source.INDEX, KeyRange.openOpen(Key.of("v7"), Key.of())); checkRange(Source.INDEX, KeyRange.openOpen(Key.of(), Key.of("v11"))); checkRange(Source.INDEX, KeyRange.openClosed(Key.of(), Key.of("v11"))); // Prefix is component-wise, not string prefix. checkRange(Source.INDEX, KeyRange.prefix(Key.of("v1")), 1); checkRange( Source.INDEX, KeyRange.closedOpen(Key.of("v1"), Key.of("v2")), 1, 10, 11, 12, 13, 14); checkRange(Source.INDEX, KeySet.all(), 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14); // Read from an index with DESC ordering. checkRange(Source.DESC_INDEX, KeySet.all(), 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0); }
@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 equalsAndHashCode() { EqualsTester tester = new EqualsTester(); tester.addEqualityGroup(KeySet.newBuilder().build()); tester.addEqualityGroup(KeySet.all(), KeySet.newBuilder().setAll().build()); tester.addEqualityGroup( KeySet.singleKey(Key.of("a")), KeySet.newBuilder().addKey(Key.of("a")).build()); tester.addEqualityGroup( KeySet.range(KeyRange.closedOpen(Key.of("a"), Key.of("b"))), KeySet.newBuilder().addRange(KeyRange.closedOpen(Key.of("a"), Key.of("b"))).build()); tester.addEqualityGroup(KeySet.newBuilder().addKey(Key.of(1)).addKey(Key.of(2)).build()); // We currently consider order, although this doesn't affect visible results. tester.addEqualityGroup(KeySet.newBuilder().addKey(Key.of(2)).addKey(Key.of(1)).build()); tester.addEqualityGroup(KeySet.newBuilder().setAll().addKey(Key.of("a")).build()); tester.addEqualityGroup( KeySet.newBuilder() .addKey(Key.of("a")) .addRange(KeyRange.closedOpen(Key.of("a"), Key.of("b"))) .build()); tester.testEquals(); }
ResultSet readUsingIndex() { // [START read_context_read_index] ReadContext readContext = dbClient.singleUse(); ResultSet resultSet = readContext.readUsingIndex( "Albums", "AlbumsByAlbumTitle", KeySet.all(), Arrays.asList("AlbumId", "AlbumTitle")); // [END read_context_read_index] return resultSet; }
Timestamp.ofTimeSecondsAndNanos(1, 2), Date.fromYearMonthDay(2017, 04, 17)))); reserializeAndAssert(Mutation.delete("test", KeySet.all())); reserializeAndAssert( Mutation.delete( "test", KeySet.newBuilder() .addRange(KeyRange.closedClosed(Key.of("one", 2, null), Key.of("two", 3, null))) .build())); reserializeAndAssert( Mutation.delete( "test", KeySet.newBuilder() .addRange(KeyRange.closedOpen(Key.of("one", 2, null), Key.of("two", 3, null))) .build())); reserializeAndAssert( Mutation.delete( "test", KeySet.newBuilder() .addRange(KeyRange.openClosed(Key.of("one", 2, null), Key.of("two", 3, null))) .build())); reserializeAndAssert( Mutation.delete( "test", KeySet.newBuilder()
@Test public void indexEmptyRead() { ResultSet resultSet = client .singleUse(TimestampBound.strong()) .readUsingIndex( TABLE_NAME, INDEX_NAME, KeySet.range(KeyRange.closedOpen(Key.of("v99"), Key.of("z"))), ALL_COLUMNS); assertThat(resultSet.next()).isFalse(); assertThat(resultSet.getType()).isEqualTo(TABLE_TYPE); }
@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 testBatchableMutationFilterFn_cells() { Mutation all = Mutation.delete("test", KeySet.all()); Mutation prefix = Mutation.delete("test", KeySet.prefixRange(Key.of(1L))); Mutation range = Mutation.delete( "test", KeySet.range(KeyRange.openOpen(Key.of(1L), Key.newBuilder().build()))); MutationGroup[] mutationGroups = new MutationGroup[] {
@Test public void builder() { KeySet set = KeySet.newBuilder() .addKey(Key.of("k1")) .addKey(Key.of("k2")) .addRange(KeyRange.closedOpen(Key.of("r1"), Key.of("rr1"))) .addRange(KeyRange.closedOpen(Key.of("r2"), Key.of("rr2"))) .build(); assertThat(set.isAll()).isFalse(); // Order isn't strictly important to the API, but it's helpful to preserve it. assertThat(set.getKeys()).containsExactly(Key.of("k1"), Key.of("k2")).inOrder(); assertThat(set.getRanges()) .containsExactly( KeyRange.closedOpen(Key.of("r1"), Key.of("rr1")), KeyRange.closedOpen(Key.of("r2"), Key.of("rr2"))) .inOrder(); assertThat(set.toString()).isEqualTo("{[k1],[k2],[[r1],[rr1]),[[r2],[rr2])}"); }