@Override public Status delete(String table, String key) { try { dbClient.writeAtLeastOnce(Arrays.asList(Mutation.delete(table, Key.of(key)))); } catch (Exception e) { LOGGER.log(Level.INFO, "delete()", e); return Status.ERROR; } return Status.OK; }
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 basics() { checkKeyRange(KeyRange.closedOpen(Key.of(1), Key.of(2)), Key.of(1), CLOSED, Key.of(2), OPEN); checkKeyRange( KeyRange.closedClosed(Key.of(1), Key.of(2)), Key.of(1), CLOSED, Key.of(2), CLOSED); checkKeyRange(KeyRange.openOpen(Key.of(1), Key.of(2)), Key.of(1), OPEN, Key.of(2), OPEN); checkKeyRange(KeyRange.openClosed(Key.of(1), Key.of(2)), Key.of(1), OPEN, Key.of(2), CLOSED); }
/** 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; }
@Test public void serialization() throws Exception { reserializeAndAssert(KeyRange.closedOpen(Key.of(1), Key.of(2))); reserializeAndAssert(KeyRange.closedClosed(Key.of(1), Key.of(2))); reserializeAndAssert(KeyRange.openOpen(Key.of(1), Key.of(2))); reserializeAndAssert(KeyRange.openClosed(Key.of(1), Key.of(2))); } }
private Struct readLastRow(String... columns) { return client .singleUse(TimestampBound.strong()) .readRow("T", Key.of(lastKey), Arrays.asList(columns)); }
@Test public void serializationSingleKeyString() { KeySet keySet = KeySet.singleKey(Key.of("abc")); checkProto(keySet, "keys { values { string_value: 'abc' } }"); }
@Test public void serializationSingleKeyMultiPart() { KeySet keySet = KeySet.singleKey(Key.of("a", false)); checkProto(keySet, "keys { values { string_value: 'a' } values { bool_value: false } }"); }
@Test public void serializationEmpty() { KeySet keySet = KeySet.singleKey(Key.of()); checkProto(keySet, "keys {}"); }
@Test public void serializationSingleKeyInt64() { KeySet keySet = KeySet.singleKey(Key.of(1234L)); checkProto(keySet, "keys { values { string_value: '1234' } }"); }
@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"); }
@Test public void toBuilder() { Key k = Key.of(1, 2).toBuilder().append(3).build(); assertThat(k.size()).isEqualTo(3); assertThat(k.getParts()).containsExactly(1L, 2L, 3L).inOrder(); }
@Test public void builderRequiresEnd() { expectedException.expect(IllegalStateException.class); expectedException.expectMessage("end(Key)"); KeyRange.newBuilder().setStart(Key.of("a")).build(); }
@Test public void builderRequiresStart() { expectedException.expect(IllegalStateException.class); expectedException.expectMessage("start(Key)"); KeyRange.newBuilder().setEnd(Key.of("z")).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); }