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; }
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; }
@Test public void serializationAll() { KeySet keySet = KeySet.all(); checkProto(keySet, "all: true"); }
@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 all() { KeySet set = KeySet.all(); assertThat(set.isAll()).isTrue(); assertThat(set.getKeys()).isEmpty(); assertThat(set.getRanges()).isEmpty(); }
@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 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 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 read() { BitSet seenRows = new BitSet(numRows); TimestampBound bound = getRandomBound(); PartitionOptions partitionParams = getRandomPartitionOptions(); batchTxn = client.batchReadOnlyTransaction(bound); List<Partition> partitions = batchTxn.partitionRead( partitionParams, TABLE_NAME, KeySet.all(), Arrays.asList("Key", "Data", "Fingerprint", "Size")); BatchTransactionId txnID = batchTxn.getBatchTransactionId(); fetchAndValidateRows(partitions, txnID, seenRows); }
@Override public Void run(TransactionContext transaction) throws SpannerException { BatchClient batchClient = env.getTestHelper().getBatchClient(db); BatchReadOnlyTransaction batchTxn = batchClient.batchReadOnlyTransaction(TimestampBound.strong()); batchTxn.partitionReadUsingIndex( PartitionOptions.getDefaultInstance(), "Test", "Index", KeySet.all(), Arrays.asList("Fingerprint")); return null; } });
@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 testToString() { assertThat(KeySet.all().toString()).isEqualTo("{<all>}"); assertThat(KeySet.singleKey(Key.of("x")).toString()).isEqualTo("{[x]}"); assertThat(KeySet.range(KeyRange.closedOpen(Key.of("a"), Key.of("z"))).toString()) .isEqualTo("{[[a],[z])}"); }
@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 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 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 javaSerialization() throws Exception { reserializeAndAssert( KeySet.all() .toBuilder() .addKey(Key.of("a", 1)) .addRange(KeyRange.closedOpen(Key.of("m"), Key.of("p"))) .build()); }
@Test public void serializationMultiWithAll() { KeySet keySet = KeySet.all() .toBuilder() .addKey(Key.of("a", 1)) .addRange(KeyRange.closedOpen(Key.of("m"), Key.of("p"))) .build(); checkProto( keySet, "keys { values { string_value: 'a' } values { string_value: '1' } }" + " ranges { start_closed { values { string_value: 'm' } }" + " end_open { values { string_value: 'p' } } }" + " all:true"); }
@Test public void equalsAndHashCode() { EqualsTester tester = new EqualsTester(); // Equality, not identity. tester.addEqualityGroup( Mutation.newInsertBuilder("T1").build(), Mutation.newInsertBuilder("T1").build()); // Operation types are distinguished. tester.addEqualityGroup(Mutation.newInsertOrUpdateBuilder("T1").build()); tester.addEqualityGroup(Mutation.newUpdateBuilder("T1").build()); tester.addEqualityGroup(Mutation.newReplaceBuilder("T1").build()); // Table is distinguished. tester.addEqualityGroup(Mutation.newInsertBuilder("T2").build()); // Columns/values are distinguished (but by equality, not identity). tester.addEqualityGroup( Mutation.newInsertBuilder("T1").set("C").to("V").build(), Mutation.newInsertBuilder("T1").set("C").to("V").build()); // Deletes consider the key set. tester.addEqualityGroup(Mutation.delete("T1", KeySet.all())); tester.addEqualityGroup( Mutation.delete("T1", KeySet.singleKey(Key.of("k"))), Mutation.delete("T1", Key.of("k"))); tester.testEquals(); }