Use an ImmutableList instance for getFrom() 56/14456/3
authorRobert Varga <rovarga@cisco.com>
Fri, 23 Jan 2015 17:17:14 +0000 (18:17 +0100)
committerRobert Varga <rovarga@cisco.com>
Fri, 23 Jan 2015 22:36:26 +0000 (23:36 +0100)
Since we are creating a view, we do not need to allocate a larger list
-- just use a copy constructor, which will create an
appropriately-sized copy.

Change-Id: I6a4b90f638b3f8511b5ec641ab5f9b1f14632407
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/AbstractReplicatedLogImpl.java
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/AbstractLeader.java

index 3b3ce3587ad3b9593d56b948248f230c6e532079..d1c3fefee8309208a6df6fe9b539a10ee000ddef 100644 (file)
@@ -117,19 +117,18 @@ public abstract class AbstractReplicatedLogImpl implements ReplicatedLog {
     public List<ReplicatedLogEntry> getFrom(long logEntryIndex, int max) {
         int adjustedIndex = adjustedIndex(logEntryIndex);
         int size = journal.size();
-        List<ReplicatedLogEntry> entries = new ArrayList<>(100);
         if (adjustedIndex >= 0 && adjustedIndex < size) {
             // physical index should be less than list size and >= 0
             int maxIndex = adjustedIndex + max;
             if(maxIndex > size){
                 maxIndex = size;
             }
-            entries.addAll(journal.subList(adjustedIndex, maxIndex));
+            return new ArrayList<>(journal.subList(adjustedIndex, maxIndex));
+        } else {
+            return Collections.emptyList();
         }
-        return entries;
     }
 
-
     @Override
     public long size() {
        return journal.size();
index 0a31cde90fb2c0139b72ec805aa5e4f83483bcb4..eba5e7b069769fb337e44d4096619a29430d057d 100644 (file)
@@ -402,7 +402,6 @@ public abstract class AbstractLeader extends AbstractRaftActorBehavior {
                 FollowerLogInformation followerLogInformation = followerToLog.get(followerId);
                 long followerNextIndex = followerLogInformation.getNextIndex();
                 boolean isFollowerActive = followerLogInformation.isFollowerActive();
-                List<ReplicatedLogEntry> entries = null;
 
                 if (mapFollowerToSnapshot.get(followerId) != null) {
                     // if install snapshot is in process , then sent next chunk if possible
@@ -417,6 +416,7 @@ public abstract class AbstractLeader extends AbstractRaftActorBehavior {
                 } else {
                     long leaderLastIndex = context.getReplicatedLog().lastIndex();
                     long leaderSnapShotIndex = context.getReplicatedLog().getSnapshotIndex();
+                    final List<ReplicatedLogEntry> entries;
 
                     if (isFollowerActive &&
                         context.getReplicatedLog().isPresent(followerNextIndex)) {