Improve NormalizedNodeInputStreamReader 96/78096/8
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 25 Nov 2018 17:29:17 +0000 (18:29 +0100)
committerRobert Varga <nite@hq.sk>
Sat, 1 Dec 2018 08:11:13 +0000 (08:11 +0000)
We really want to retain source iteration order of predicates
being streamed into the system, as they usually match the order
locally.

Refactor NormalizedNodeInputStreamReader to read complete
NodeIdentifierWithPredicate objects, thus allowing us to use
ImmutableOffsetMapTemplate to skip some data checks and copying
around.

MDSAL: YANGTOOLS-917
Change-Id: I162919c7e56109fc98933a4eeae8669b030dc992
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeInputStreamReader.java

index 2f94037..428a578 100755 (executable)
@@ -26,6 +26,7 @@ import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.transform.dom.DOMSource;
 import org.opendaylight.controller.cluster.datastore.node.utils.QNameFactory;
+import org.opendaylight.yangtools.util.ImmutableOffsetMapTemplate;
 import org.opendaylight.yangtools.yang.common.Empty;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
@@ -136,8 +137,7 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput
                 return leafSetEntryBuilder().withNodeIdentifier(leafIdentifier).withValue(value).build();
 
             case NodeTypes.MAP_ENTRY_NODE :
-                NodeIdentifierWithPredicates entryIdentifier = new NodeIdentifierWithPredicates(
-                        readQName(), readKeyValueMap());
+                NodeIdentifierWithPredicates entryIdentifier = readNormalizedNodeWithPredicates();
 
                 LOG.trace("Reading map entry node {} ", entryIdentifier);
 
@@ -276,15 +276,26 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput
         return children;
     }
 
-    private Map<QName, Object> readKeyValueMap() throws IOException {
-        int count = input.readInt();
-        Map<QName, Object> keyValueMap = new HashMap<>(count);
+    private NodeIdentifierWithPredicates readNormalizedNodeWithPredicates() throws IOException {
+        final QName qname = readQName();
+        final int count = input.readInt();
+        switch (count) {
+            case 0:
+                return new NodeIdentifierWithPredicates(qname);
+            case 1:
+                return new NodeIdentifierWithPredicates(qname, readQName(), readObject());
+            default:
+                // ImmutableList is used by ImmutableOffsetMapTemplate for lookups, hence we use that.
+                final Builder<QName> keys = ImmutableList.builderWithExpectedSize(count);
+                final Object[] values = new Object[count];
+                for (int i = 0; i < count; i++) {
+                    keys.add(readQName());
+                    values[i] = readObject();
+                }
 
-        for (int i = 0; i < count; i++) {
-            keyValueMap.put(readQName(), readObject());
+                return new NodeIdentifierWithPredicates(qname, ImmutableOffsetMapTemplate.ordered(keys.build())
+                    .instantiateWithValues(values));
         }
-
-        return keyValueMap;
     }
 
     private Object readObject() throws IOException {
@@ -403,7 +414,7 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput
                 return new NodeIdentifier(readQName());
 
             case PathArgumentTypes.NODE_IDENTIFIER_WITH_PREDICATES :
-                return new NodeIdentifierWithPredicates(readQName(), readKeyValueMap());
+                return readNormalizedNodeWithPredicates();
 
             case PathArgumentTypes.NODE_IDENTIFIER_WITH_VALUE :
                 return new NodeWithValue<>(readQName(), readObject());

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.