/** * Creates a new group. * * @param primary a primary mutation. * @param other other mutations, usually interleaved in parent. * @return new mutation group. */ public static MutationGroup create(Mutation primary, Mutation... other) { return create(primary, Arrays.asList(other)); }
public static MutationGroup create(Mutation primary, Iterable<Mutation> other) { return new MutationGroup(ImmutableList.<Mutation>builder().add(primary).addAll(other).build()); }
@DoFn.ProcessElement public void processElement(ProcessContext c) { MutationGroup mg = c.element(); if (mg.primary().getOperation() == Op.DELETE && !isPointDelete(mg.primary())) { // Ranged deletes are not batchable. c.output(unbatchableMutationsTag, Arrays.asList(mg)); unBatchableMutationGroupsCounter.inc(); return; } SpannerSchema spannerSchema = c.sideInput(schemaView); long groupSize = MutationSizeEstimator.sizeOf(mg); long groupCells = MutationCellCounter.countOf(spannerSchema, mg); if (groupSize >= batchSizeBytes || groupCells >= maxNumMutations) { c.output(unbatchableMutationsTag, Arrays.asList(mg)); unBatchableMutationGroupsCounter.inc(); } else { c.output(mg); batchableMutationGroupsCounter.inc(); } } }
@ProcessElement public void processElement(ProcessContext c) throws Exception { SpannerSchema spannerSchema = c.sideInput(schemaView); MutationKeyEncoder encoder = new MutationKeyEncoder(spannerSchema); MutationGroup mg = c.element(); long groupSize = MutationSizeEstimator.sizeOf(mg); long groupCells = MutationCellCounter.countOf(spannerSchema, mg); synchronized (this) { if (((batchCells + groupCells) > maxNumMutations) || (batchSizeBytes + groupSize) > maxBatchSizeBytes) { c.output(sortAndGetList()); initSorter(); } mutationsToSort.add(KV.of(encoder.encodeTableNameAndKey(mg.primary()), encode(mg))); batchSizeBytes += groupSize; batchCells += groupCells; } } }
@Override public MutationGroup apply(String userId) { // Immediately block the user. Mutation userMutation = Mutation.newUpdateBuilder("Users") .set("id").to(userId) .set("state").to("BLOCKED") .build(); long generatedId = Hashing.sha1().newHasher() .putString(userId, Charsets.UTF_8) .putLong(timestamp.getSeconds()) .putLong(timestamp.getNanos()) .hash() .asLong(); // Add an entry to pending review requests. Mutation pendingReview = Mutation.newInsertOrUpdateBuilder("PendingReviews") .set("id").to(generatedId) // Must be deterministically generated. .set("userId").to(userId) .set("action").to("REVIEW ACCOUNT") .set("note").to("Suspicious activity detected.") .build(); return MutationGroup.create(userMutation, pendingReview); } }));
private static MutationGroup g(Mutation m, Mutation... other) { return MutationGroup.create(m, other); }
@ProcessElement public void processElement(ProcessContext c) { Mutation value = c.element(); c.output(MutationGroup.create(value)); } }
@Test public void group() throws Exception { Mutation int64 = Mutation.newInsertOrUpdateBuilder("test").set("one").to(1).build(); Mutation float64 = Mutation.newInsertOrUpdateBuilder("test").set("one").to(2.9).build(); Mutation bool = Mutation.newInsertOrUpdateBuilder("test").set("one").to(false).build(); MutationGroup group = MutationGroup.create(int64, float64, bool); assertThat(MutationSizeEstimator.sizeOf(group), is(17L)); } }