From: Robert Varga Date: Sun, 25 Nov 2018 17:29:17 +0000 (+0100) Subject: Improve NormalizedNodeInputStreamReader X-Git-Tag: release/neon~51 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=315a23ea8d215ac5b29fea613f31fd35f6a6899b Improve NormalizedNodeInputStreamReader 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 --- diff --git a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeInputStreamReader.java b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeInputStreamReader.java index 2f9403707c..428a578e80 100755 --- a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeInputStreamReader.java +++ b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeInputStreamReader.java @@ -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 readKeyValueMap() throws IOException { - int count = input.readInt(); - Map 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 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());