Deduplicate MapNode key leaf values 04/85404/5
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 28 Oct 2019 12:20:16 +0000 (13:20 +0100)
committerRobert Varga <nite@hq.sk>
Wed, 20 Nov 2019 15:29:32 +0000 (15:29 +0000)
When we are encountering a leaf node inside a MapEntryNode, it can
be a leaf corresponding to a key -- in which case its value is
already present in NodeIdentifierWithPredicates and we do want to
de-duplicate those objects.

JIRA: CONTROLLER-1908
Change-Id: I2ed65c311f9921aa77c9f23bd1f7681d7f11355a
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit b0d6c04dfc87422c02975b0fa90bf97e9a8fafee)

opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/AbstractLithiumDataInput.java

index 88b06a1ea03b7c06b0b6c61cefa3568a90fa4208..3aa5934b092afdbb3bc7f17fa48cff0e46b79659 100644 (file)
@@ -148,9 +148,22 @@ abstract class AbstractLithiumDataInput extends ForwardingDataInput implements N
     }
 
     private void streamLeaf(final NormalizedNodeStreamWriter writer) throws IOException {
+        writer.leafNode(startLeaf(writer), readObject());
+    }
+
+    // Leaf inside a MapEntryNode, it can potentially be a key leaf, in which case we want to de-duplicate values.
+    private void streamLeaf(final NormalizedNodeStreamWriter writer, final NodeIdentifierWithPredicates entryId)
+            throws IOException {
+        final NodeIdentifier identifier = startLeaf(writer);
+        final Object value = readObject();
+        final Object entryValue = entryId.getKeyValues().get(identifier.getNodeType());
+        writer.leafNode(identifier, entryValue == null ? value : entryValue);
+    }
+
+    private NodeIdentifier startLeaf(final NormalizedNodeStreamWriter writer) throws IOException {
         final NodeIdentifier identifier = readNodeIdentifier();
         LOG.trace("Streaming leaf node {}", identifier);
-        writer.leafNode(identifier, readObject());
+        return identifier;
     }
 
     private void streamLeafSet(final NormalizedNodeStreamWriter writer) throws IOException {
@@ -203,7 +216,17 @@ abstract class AbstractLithiumDataInput extends ForwardingDataInput implements N
         final NodeIdentifierWithPredicates entryIdentifier = readNormalizedNodeWithPredicates();
         LOG.trace("Streaming map entry node {}", entryIdentifier);
         writer.startMapEntryNode(entryIdentifier, NormalizedNodeStreamWriter.UNKNOWN_SIZE);
-        commonStreamContainer(writer);
+
+        // Same loop as commonStreamContainer(), but ...
+        for (byte nodeType = input.readByte(); nodeType != NodeTypes.END_NODE; nodeType = input.readByte()) {
+            if (nodeType == NodeTypes.LEAF_NODE) {
+                // ... leaf nodes may need de-duplication
+                streamLeaf(writer, entryIdentifier);
+            } else {
+                streamNormalizedNode(writer, nodeType);
+            }
+        }
+        writer.endNode();
     }
 
     private void streamUnkeyedList(final NormalizedNodeStreamWriter writer) throws IOException {