rpuch commented on code in PR #4562: URL: https://github.com/apache/ignite-3/pull/4562#discussion_r1802477130
########## 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: `LogicalNode` just returns the maps and list it's given to its constructor as is, so if they are modifiable, we can get in trouble. 1. We can make copies in `LogicalNode` constructor 2. We can make copies in its serializer 3. Or we can add an unmodifiable wrapper when storing them Of these, I like option 1 more than others (for 2, we can forget to make a copy outside of the constructor, and for 3, it will create an unnecessary object for the cases when an immutable collection is passed to the constructor). What do you think? -- 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