@Override public Status update(String table, String key, Map<String, ByteIterator> values) { Mutation.WriteBuilder m = Mutation.newInsertOrUpdateBuilder(table); m.set(PRIMARY_KEY_COLUMN).to(key); for (Map.Entry<String, ByteIterator> e : values.entrySet()) { m.set(e.getKey()).to(e.getValue().toString()); } try { dbClient.writeAtLeastOnce(Arrays.asList(m.build())); } catch (Exception e) { LOGGER.log(Level.INFO, "update()", e); return Status.ERROR; } return Status.OK; }
@Test public void writeDateArrayEmpty() { write(baseInsert().set("DateArrayValue").toDateArray(Arrays.<Date>asList()).build()); Struct row = readLastRow("DateArrayValue"); assertThat(row.isNull(0)).isFalse(); assertThat(row.getDateList(0)).containsExactly(); }
@Test public void writeBytesArrayEmpty() { write(baseInsert().set("BytesArrayValue").toBytesArray(Arrays.<ByteArray>asList()).build()); Struct row = readLastRow("BytesArrayValue"); assertThat(row.isNull(0)).isFalse(); assertThat(row.getBytesList(0)).containsExactly(); }
@Test public void notInBinding() { ValueBinder<Mutation.WriteBuilder> binder = Mutation.newInsertBuilder("T1").set("C1"); binder.to(1234); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("No binding currently active"); binder.to(5678); }
@Test public void insert() { Mutation m = Mutation.newInsertBuilder("T1").set("C1").to(true).set("C2").to(1234).build(); assertThat(m.getTable()).isEqualTo("T1"); assertThat(m.getOperation()).isEqualTo(Mutation.Op.INSERT); assertThat(m.getColumns()).containsExactly("C1", "C2").inOrder(); assertThat(m.getValues()).containsExactly(Value.bool(true), Value.int64(1234)).inOrder(); assertThat(m.toString()).isEqualTo("insert(T1{C1=true,C2=1234})"); }
@Test public void incorrectType() { expectedException.expect(isSpannerException(ErrorCode.FAILED_PRECONDITION)); expectedException.expectMessage("Expected STRING"); // Attempt to set 'V' to INT64, not STRING. // NOTE: an interest effect of not sending type metadata is that BYTES and INT64 are accepted // here... write(baseInsert().set("StringValue").to(1.234).build()); }
@Test public void writeTimestampArrayNull() { write(baseInsert().set("TimestampArrayValue").toTimestampArray(null).build()); Struct row = readLastRow("TimestampArrayValue"); assertThat(row.isNull(0)).isTrue(); }
@Test public void writeStringArray() { write( baseInsert().set("StringArrayValue").toStringArray(Arrays.asList("a", null, "b")).build()); Struct row = readLastRow("StringArrayValue"); assertThat(row.isNull(0)).isFalse(); assertThat(row.getStringList(0)).containsExactly("a", null, "b").inOrder(); }
@Test public void writeStringArrayEmpty() { write(baseInsert().set("StringArrayValue").toStringArray(Arrays.<String>asList()).build()); Struct row = readLastRow("StringArrayValue"); assertThat(row.isNull(0)).isFalse(); assertThat(row.getStringList(0)).containsExactly(); }
@Test public void writeStringArrayNull() { write(baseInsert().set("StringArrayValue").toStringArray(null).build()); Struct row = readLastRow("StringArrayValue"); assertThat(row.isNull(0)).isTrue(); }
@Test public void writeBoolArrayEmpty() { write(baseInsert().set("BoolArrayValue").toBoolArray(new boolean[] {}).build()); Struct row = readLastRow("BoolArrayValue"); assertThat(row.isNull(0)).isFalse(); assertThat(row.getBooleanList(0)).containsExactly(); }
@Test public void writeInt64() { write(baseInsert().set("Int64Value").to(1234).build()); Struct row = readLastRow("Int64Value"); assertThat(row.isNull(0)).isFalse(); assertThat(row.getLong(0)).isEqualTo(1234L); }
@Test public void writeBoolNull() { write(baseInsert().set("BoolValue").to((Boolean) null).build()); Struct row = readLastRow("BoolValue"); assertThat(row.isNull(0)).isTrue(); }
@Test public void rollback() { TransactionManager manager = client.transactionManager(); TransactionContext txn = manager.begin(); txn.buffer( Mutation.newInsertBuilder("T").set("K").to("Key2").set("BoolValue").to(true).build()); manager.rollback(); assertThat(manager.getState()).isEqualTo(TransactionState.ROLLED_BACK); // Row should not have been inserted. assertThat(client.singleUse().readRow("T", Key.of("Key2"), Arrays.asList("K", "BoolValue"))) .isNull(); }
@Test public void writeBytesArray() { List<ByteArray> data = Arrays.asList(ByteArray.copyFrom("a"), ByteArray.copyFrom("b"), null); write(baseInsert().set("BytesArrayValue").toBytesArray(data).build()); Struct row = readLastRow("BytesArrayValue"); assertThat(row.isNull(0)).isFalse(); assertThat(row.getBytesList(0)).isEqualTo(data); }
@Test public void writeBytesArrayNull() { write(baseInsert().set("BytesArrayValue").toBytesArray(null).build()); Struct row = readLastRow("BytesArrayValue"); assertThat(row.isNull(0)).isTrue(); }
@Test public void writeBoolArray() { write(baseInsert().set("BoolArrayValue").toBoolArray(Arrays.asList(true, null, false)).build()); Struct row = readLastRow("BoolArrayValue"); assertThat(row.isNull(0)).isFalse(); assertThat(row.getBooleanList(0)).containsExactly(true, null, false).inOrder(); expectedException.expect(NullPointerException.class); row.getBooleanArray(0); }
@Test public void writeString() { write(baseInsert().set("StringValue").to("V1").build()); Struct row = readLastRow("StringValue"); assertThat(row.isNull(0)).isFalse(); assertThat(row.getString(0)).isEqualTo("V1"); }
@Test public void update() { Mutation m = Mutation.newUpdateBuilder("T1").set("C1").to(true).set("C2").to(1234).build(); assertThat(m.getTable()).isEqualTo("T1"); assertThat(m.getOperation()).isEqualTo(Mutation.Op.UPDATE); assertThat(m.getColumns()).containsExactly("C1", "C2").inOrder(); assertThat(m.getValues()).containsExactly(Value.bool(true), Value.int64(1234)).inOrder(); assertThat(m.toString()).isEqualTo("update(T1{C1=true,C2=1234})"); }