@Test public void delete() { KeySet keySet = KeySet.singleKey(Key.of("k1")); Mutation m = Mutation.delete("T1", keySet); assertThat(m.getOperation()).isEqualTo(Mutation.Op.DELETE); assertThat(m.getKeySet()).isEqualTo(keySet); assertThat(m.toString()).isEqualTo("delete(T1{[k1]})"); }
/** * Check if the mutation is a delete by a single primary key operation. * * @param m mutation * @return true if mutation is a point delete */ public static boolean isPointDelete(Mutation m) { return m.getOperation() == Mutation.Op.DELETE && Iterables.isEmpty(m.getKeySet().getRanges()) && Iterables.size(m.getKeySet().getKeys()) == 1; } }
/** * Builds a lexicographically sortable binary key based on a primary key descriptor. * * @param m a spanner mutation. * @return a binary string that preserves the ordering of the primary key. */ public byte[] encodeTableNameAndKey(Mutation m) { OrderedCode orderedCode = new OrderedCode(); orderedCode.writeBytes(m.getTable().getBytes(StandardCharsets.UTF_8)); if (m.getOperation() == Op.DELETE) { if (isPointDelete(m)) { Key next = m.getKeySet().getKeys().iterator().next(); encodeKey(orderedCode, m.getTable(), next); } else { // The key is left empty for non-point deletes, since there is no general way to batch them. } } else { encodeKey(orderedCode, m); } return orderedCode.getEncodedBytes(); }
/** Estimates a size of mutation in bytes. */ static long sizeOf(Mutation m) { if (m.getOperation() == Mutation.Op.DELETE) { return sizeOf(m.getKeySet()); } long result = 0; for (Value v : m.getValues()) { switch (v.getType().getCode()) { case ARRAY: result += estimateArrayValue(v); break; case STRUCT: throw new IllegalArgumentException("Structs are not supported in mutation."); default: result += estimatePrimitiveValue(v); } } return result; }
/** Count the number of cells modified by {@link MutationGroup}. */ public static long countOf(SpannerSchema spannerSchema, MutationGroup mutationGroup) { long mutatedCells = 0L; for (Mutation mutation : mutationGroup) { if (mutation.getOperation() == Op.DELETE) { // For single key deletes sum up all the columns in the schema. // There is no clear way to estimate range deletes, so they are ignored. if (isPointDelete(mutation)) { final KeySet keySet = mutation.getKeySet(); final long rows = Iterables.size(keySet.getKeys()); mutatedCells += rows * spannerSchema.getCellsMutatedPerRow(mutation.getTable()); } } else { // sum the cells of the columns included in the mutation for (String column : mutation.getColumns()) { mutatedCells += spannerSchema.getCellsMutatedPerColumn(mutation.getTable(), column); } } } return mutatedCells; } }