/** * Construct a key with parts specified by {@code values}. Each object in {@code values} must be * either {@code null} or one of the following supported types: * * <ul> * <li>{@code Boolean} for the {@code BOOL} Cloud Spanner type * <li>{@code Integer}, {@code Long} for {@code INT64} * <li>{@code Float}, {@code Double} for {@code FLOAT64} * <li>{@code String} for {@code STRING} * <li>{@link ByteArray} for {@code BYTES} * </ul> * * @throws IllegalArgumentException if any member of {@code values} is not a supported type */ public static Key of(Object... values) { // A literal Key.of(null) results in a null array being passed. Provide a clearer error. checkNotNull( values, "'values' cannot be null. For a literal key containing a single null value, " + "call Key.of((Object) null)."); Builder b = new Builder(false /* builder never leaves this scope */); for (Object value : values) { b.appendObject(value); } return b.build(); }
@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(); }
static void deleteExampleData(DatabaseClient dbClient) { List<Mutation> mutations = new ArrayList<>(); // KeySet.all() can be used to delete all the rows in a table. mutations.add(Mutation.delete("Albums", KeySet.all())); // KeySet.singleKey() can be used to delete one row at a time. for (Singer singer : SINGERS) { mutations.add( Mutation.delete("Singers", KeySet.singleKey(Key.newBuilder().append(singer.singerId).build()))); } dbClient.write(mutations); System.out.printf("Records deleted.\n"); } // [END spanner_delete_data]
Mutation range = Mutation.delete( "test", KeySet.range(KeyRange.openOpen(Key.of(1L), Key.newBuilder().build()))); MutationGroup[] mutationGroups = new MutationGroup[] {
Mutation range = Mutation.delete( "test", KeySet.range(KeyRange.openOpen(Key.of(1L), Key.newBuilder().build()))); MutationGroup[] mutationGroups = new MutationGroup[] {
/** * Construct a key with parts specified by {@code values}. Each object in {@code values} must be * either {@code null} or one of the following supported types: * * <ul> * <li>{@code Boolean} for the {@code BOOL} Cloud Spanner type * <li>{@code Integer}, {@code Long} for {@code INT64} * <li>{@code Float}, {@code Double} for {@code FLOAT64} * <li>{@code String} for {@code STRING} * <li>{@link ByteArray} for {@code BYTES} * </ul> * * @throws IllegalArgumentException if any member of {@code values} is not a supported type */ public static Key of(Object... values) { // A literal Key.of(null) results in a null array being passed. Provide a clearer error. checkNotNull( values, "'values' cannot be null. For a literal key containing a single null value, " + "call Key.of((Object) null)."); Builder b = new Builder(false /* builder never leaves this scope */); for (Object value : values) { b.appendObject(value); } return b.build(); }
@Override public Key convertToKey(Object key) { Assert.notNull(key, "Key of an entity to be written cannot be null!"); Key k; boolean isIterable = Iterable.class.isAssignableFrom(key.getClass()); boolean isArray = Object[].class.isAssignableFrom(key.getClass()); if ((isIterable || isArray) && !ByteArray.class.isAssignableFrom(key.getClass())) { Key.Builder kb = Key.newBuilder(); for (Object keyPart : (isArray ? (Arrays.asList((Object[]) key)) : ((Iterable) key))) { kb.appendObject(convertKeyPart(keyPart)); } k = kb.build(); if (k.size() == 0) { throw new SpannerDataException( "A key must have at least one component, but 0 were given."); } } else { k = Key.class.isAssignableFrom(key.getClass()) ? (Key) key : Key.of(convertKeyPart(key)); } return k; }
@Override public Key convertToKey(Object key) { Assert.notNull(key, "Key of an entity to be written cannot be null!"); Key k; boolean isIterable = Iterable.class.isAssignableFrom(key.getClass()); boolean isArray = Object[].class.isAssignableFrom(key.getClass()); if ((isIterable || isArray) && !ByteArray.class.isAssignableFrom(key.getClass())) { Key.Builder kb = Key.newBuilder(); for (Object keyPart : (isArray ? (Arrays.asList((Object[]) key)) : ((Iterable) key))) { kb.appendObject(convertKeyPart(keyPart)); } k = kb.build(); if (k.size() == 0) { throw new SpannerDataException( "A key must have at least one component, but 0 were given."); } } else { k = Key.class.isAssignableFrom(key.getClass()) ? (Key) key : Key.of(convertKeyPart(key)); } return k; }
.append(Timestamp.parseTimestamp(timestamp)) .append(Date.parseDate(date)) .build(); assertThat(k.size()).isEqualTo(10); assertThat(k.getParts())
tester.addEqualityGroup(Key.of(), Key.newBuilder().build()); Key.newBuilder().append((Boolean) null).build(), Key.newBuilder().append((Long) null).build(), Key.newBuilder().append((Double) null).build(), Key.newBuilder().append((String) null).build(), Key.newBuilder().append((ByteArray) null).build(), Key.newBuilder().append((Timestamp) null).build(), Key.newBuilder().append((Date) null).build(), Key.newBuilder().appendObject(null).build()); tester.addEqualityGroup(Key.of(true), Key.newBuilder().append(true).build()); tester.addEqualityGroup(Key.of(false), Key.newBuilder().append(false).build()); tester.addEqualityGroup(Key.of(1), Key.of(1L), Key.newBuilder().append(1).build()); tester.addEqualityGroup(Key.of(2), Key.of(2L), Key.newBuilder().append(2).build()); tester.addEqualityGroup(Key.of(1, 2)); tester.addEqualityGroup(Key.of(1.0f), Key.of(1.0d), Key.newBuilder().append(1.0).build()); tester.addEqualityGroup(Key.of(2.0f), Key.of(2.0d), Key.newBuilder().append(2.0).build()); tester.addEqualityGroup(Key.of("a"), Key.newBuilder().append("a").build()); tester.addEqualityGroup(Key.of("a", "b", "c")); tester.addEqualityGroup( Key.of(ByteArray.copyFrom("a")), Key.newBuilder().append(ByteArray.copyFrom("a")).build()); Timestamp t = Timestamp.parseTimestamp("2015-09-15T00:00:00Z"); tester.addEqualityGroup(Key.of(t), Key.newBuilder().append(t).build()); Date d = Date.parseDate("2016-09-15"); tester.addEqualityGroup(Key.of(d), Key.newBuilder().append(d).build()); tester.addEqualityGroup(Key.of("a", 2, null));