rpuch commented on code in PR #4562:
URL: https://github.com/apache/ignite-3/pull/4562#discussion_r1802506810


##########
modules/cluster-management/src/main/java/org/apache/ignite/internal/cluster/management/topology/api/LogicalNodeSerializer.java:
##########
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.cluster.management.topology.api;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import org.apache.ignite.internal.network.ClusterNodeSerializer;
+import org.apache.ignite.internal.util.io.IgniteDataInput;
+import org.apache.ignite.internal.util.io.IgniteDataOutput;
+import org.apache.ignite.internal.versioned.VersionedSerializer;
+import org.apache.ignite.network.ClusterNode;
+
+/**
+ * {@link VersionedSerializer} for {@link LogicalNode} instances.
+ */
+public class LogicalNodeSerializer extends VersionedSerializer<LogicalNode> {
+    /** Serializer instance. */
+    public static final LogicalNodeSerializer INSTANCE = new 
LogicalNodeSerializer();
+
+    private final ClusterNodeSerializer clusterNodeSerializer = 
ClusterNodeSerializer.INSTANCE;
+
+    @Override
+    protected void writeExternalData(LogicalNode node, IgniteDataOutput out) 
throws IOException {
+        clusterNodeSerializer.writeExternal(node, out);
+
+        writeStringToStringMap(node.userAttributes(), out);
+        writeStringToStringMap(node.systemAttributes(), out);
+
+        out.writeLength(node.storageProfiles().size());
+        for (String profile : node.storageProfiles()) {
+            out.writeUTF(profile);
+        }
+    }
+
+    private static void writeStringToStringMap(Map<String, String> map, 
IgniteDataOutput output) throws IOException {
+        output.writeLength(map.size());
+
+        for (Entry<String, String> entry : map.entrySet()) {
+            output.writeUTF(entry.getKey());
+            output.writeUTF(entry.getValue());
+        }
+    }
+
+    @Override
+    protected LogicalNode readExternalData(byte protoVer, IgniteDataInput in) 
throws IOException {
+        ClusterNode node = clusterNodeSerializer.readExternal(in);
+
+        Map<String, String> userAttributes = readStringToStringMap(in);
+        Map<String, String> systemAttributes = readStringToStringMap(in);
+        List<String> storageProfiles = readStringList(in);
+
+        return new LogicalNode(node, userAttributes, systemAttributes, 
storageProfiles);
+    }
+
+    private static Map<String, String> readStringToStringMap(IgniteDataInput 
in) throws IOException {
+        int size = in.readLength();
+
+        var map = new HashMap<String, String>(size);
+        for (int i = 0; i < size; i++) {
+            map.put(in.readUTF(), in.readUTF());
+        }
+
+        return Map.copyOf(map);

Review Comment:
   It bites you rarely, but it might be difficult to debug it when it does bite 
you. Also, we don't creeate too many instances of `LogicalNode` (yet?), so I 
moved the defensive copying to the constructor. If we find it costs too much, 
we'll remove it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@ignite.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to