Use ImmutableList.Builder in FrontendClientMetadata 09/98309/1
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 6 Nov 2021 14:20:11 +0000 (15:20 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sat, 6 Nov 2021 14:20:11 +0000 (15:20 +0100)
We are performing a defensive copy in constructor, hence it is
better to allocate a builder, skipping an untrusted copy operation.

Change-Id: I95d7aa90f6da325aaae6a340caf49a6e17fe9f64
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/persisted/FrontendClientMetadata.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/persisted/FrontendShardDataTreeSnapshotMetadataTest.java

index f384c92..7ff4b1e 100644 (file)
@@ -14,7 +14,6 @@ import com.google.common.collect.ImmutableList;
 import java.io.DataInput;
 import java.io.DataOutput;
 import java.io.IOException;
-import java.util.ArrayList;
 import java.util.Collection;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
@@ -63,13 +62,12 @@ public final class FrontendClientMetadata implements Identifiable<ClientIdentifi
         final var purgedHistories = ImmutableUnsignedLongSet.readFrom(in);
 
         final int currentSize = in.readInt();
-        // FIXME: ImmutableList.builder()
-        final var currentHistories = new ArrayList<FrontendHistoryMetadata>(currentSize);
+        final var currentBuilder = ImmutableList.<FrontendHistoryMetadata>builderWithExpectedSize(currentSize);
         for (int i = 0; i < currentSize; ++i) {
-            currentHistories.add(FrontendHistoryMetadata.readFrom(in));
+            currentBuilder.add(FrontendHistoryMetadata.readFrom(in));
         }
 
-        return new FrontendClientMetadata(id, purgedHistories, currentHistories);
+        return new FrontendClientMetadata(id, purgedHistories, currentBuilder.build());
     }
 
     @Override
index dc7e1c8..c02cea9 100644 (file)
@@ -24,7 +24,6 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
 import org.junit.Test;
 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
@@ -108,11 +107,9 @@ public class FrontendShardDataTreeSnapshotMetadataTest {
         tmp.add(0);
         final ImmutableUnsignedLongSet purgedHistories = tmp.immutableCopy();
 
-        final Set<FrontendHistoryMetadata> currentHistories = Set.of(
+        return new FrontendClientMetadata(clientIdentifier, purgedHistories.immutableCopy(), List.of(
             new FrontendHistoryMetadata(num, num, true, ImmutableMap.of(UnsignedLong.ZERO, Boolean.TRUE),
-                purgedHistories));
-
-        return new FrontendClientMetadata(clientIdentifier, purgedHistories.immutableCopy(), currentHistories);
+                purgedHistories)));
     }
 
     private static <T> void testObject(final T object, final T equalObject) {