From 4c17329ef1b88e75cc24561a3c249e9deb93f05a Mon Sep 17 00:00:00 2001 From: Maros Marsalek Date: Wed, 25 Apr 2018 10:20:59 +0200 Subject: Collect all the updates for subtree writers So far, when a subtree writer was registered on a list node and ModificationDiff detected 2 or more updated list items for that writer, FlatWriterRegistry just picked the first item in list, processed that one and ignored the rest. Change-Id: If66db1eaad5a3b5c35e5586f46fd83a0698e1728 Signed-off-by: Maros Marsalek --- .../impl/write/registry/FlatWriterRegistry.java | 25 ++++++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'infra/translate-impl/src/main') diff --git a/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/write/registry/FlatWriterRegistry.java b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/write/registry/FlatWriterRegistry.java index 35f1c90a1..67a5da32a 100644 --- a/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/write/registry/FlatWriterRegistry.java +++ b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/write/registry/FlatWriterRegistry.java @@ -18,6 +18,7 @@ package io.fd.honeycomb.translate.impl.write.registry; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; +import static java.util.stream.Collectors.toMap; import com.google.common.base.Optional; import com.google.common.collect.ImmutableMap; @@ -33,11 +34,11 @@ import io.fd.honeycomb.translate.write.registry.UpdateFailedException; import io.fd.honeycomb.translate.write.registry.WriterRegistry; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.function.Function; import java.util.stream.Collectors; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -160,25 +161,35 @@ final class FlatWriterRegistry implements WriterRegistry { .orElse(null); } - private Collection getParentDataObjectUpdate(final WriteContext ctx, + static Collection getParentDataObjectUpdate(final WriteContext ctx, final Multimap, ? extends DataObjectUpdate> updates, final Writer writer) { // Now read data for subtree reader root, but first keyed ID is needed and that ID can be cut from updates - InstanceIdentifier firstAffectedChildId = ((SubtreeWriter) writer).getHandledChildTypes().stream() + return ((SubtreeWriter) writer).getHandledChildTypes().stream() .filter(updates::containsKey) .map(unkeyedId -> updates.get(unkeyedId)) .flatMap(doUpdates -> doUpdates.stream()) .map(DataObjectUpdate::getId) - .findFirst() - .get(); + .map(id -> getSingleParentDataObjectUpdate(ctx, (Multimap, DataObjectUpdate>) updates, writer, id)) + // Reduce the list of updates by putting them to a map. If a subtree writer for container gets 2 children updated, it will + // get only a single update, however if its a registered on a listand 2 different list items get their children updated, + // both updates should be preserved. + // Essentially, only group child updates in case the ID from root to writer is identical + .collect(toMap(update -> RWUtils.cutId(update.getId(), writer.getManagedDataObjectType()), Function.identity(), (u1, u2) -> u1)) + .values(); + } + private static DataObjectUpdate getSingleParentDataObjectUpdate(WriteContext ctx, Multimap, DataObjectUpdate> updates, Writer writer, InstanceIdentifier firstAffectedChildId) { final InstanceIdentifier parentKeyedId = RWUtils.cutId(firstAffectedChildId, writer.getManagedDataObjectType()); final Optional parentBefore = ctx.readBefore(parentKeyedId); final Optional parentAfter = ctx.readAfter(parentKeyedId); - return Collections.singleton( - DataObjectUpdate.create(parentKeyedId, parentBefore.orNull(), parentAfter.orNull())); + + // Put the parent update data into updates map so that revert can also access the state + DataObjectUpdate parentUpdate = DataObjectUpdate.create(parentKeyedId, parentBefore.orNull(), parentAfter.orNull()); + updates.put(RWUtils.makeIidWildcarded(parentKeyedId), parentUpdate); + return parentUpdate; } private void bulkUpdate( -- cgit 1.2.3-korg