Github user aweisberg commented on a diff in the pull request:
https://github.com/apache/cassandra/pull/224#discussion_r192505398
--- Diff: src/java/org/apache/cassandra/locator/ReplicaList.java ---
@@ -0,0 +1,244 @@
+/*
+ * 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.cassandra.locator;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Objects;
+import java.util.function.Predicate;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterables;
+
+public class ReplicaList extends Replicas
+{
+ static final ReplicaList EMPTY = new ReplicaList(ImmutableList.of());
+
+ private final List<Replica> replicaList;
+
+ public ReplicaList()
+ {
+ replicaList = new ArrayList<>();
+ }
+
+ public ReplicaList(int capacity)
+ {
+ replicaList = new ArrayList<>(capacity);
+ }
+
+ public ReplicaList(ReplicaList from)
+ {
+ replicaList = new ArrayList<>(from.replicaList);
+ }
+
+ public ReplicaList(Replicas from)
+ {
+ replicaList = new ArrayList<>(from.size());
+ addAll(from);
+ }
+
+ public ReplicaList(Collection<Replica> from)
+ {
+ replicaList = new ArrayList<>(from);
+ }
+
+ private ReplicaList(List<Replica> replicaList)
+ {
+ this.replicaList = replicaList;
+ }
+
+ public boolean equals(Object o)
+ {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ ReplicaList that = (ReplicaList) o;
+ return Objects.equals(replicaList, that.replicaList);
+ }
+
+ public int hashCode()
+ {
+
+ return Objects.hash(replicaList);
+ }
+
+ @Override
+ public String toString()
+ {
+ return replicaList.toString();
+ }
+
+ @Override
+ public boolean add(Replica replica)
+ {
+ return replicaList.add(replica);
+ }
+
+ @Override
+ public void addAll(Iterable<Replica> replicas)
+ {
+ Iterables.addAll(replicaList, replicas);
+ }
+
+ @Override
+ public int size()
+ {
+ return replicaList.size();
+ }
+
+ @Override
+ public Iterator<Replica> iterator()
+ {
+ return replicaList.iterator();
+ }
+
+ public Replica get(int idx)
+ {
+ return replicaList.get(idx);
+ }
+
+ public List<InetAddressAndPort> asEndpointList()
+ {
+ List<InetAddressAndPort> endpoints = new
ArrayList<>(replicaList.size());
+ for (Replica replica: replicaList)
+ {
+ endpoints.add(replica.getEndpoint());
+ }
+ return endpoints;
+ }
+
+ @Override
+ public void removeEndpoint(InetAddressAndPort endpoint)
+ {
+ replicaList.removeIf(r -> r.getEndpoint().equals(endpoint));
+ }
+
+ @Override
+ public void removeReplica(Replica replica)
+ {
+ replicaList.remove(replica);
+ }
+
+ public ReplicaList filter(Predicate<Replica> predicate)
+ {
+ ArrayList<Replica> newReplicaList = new ArrayList<>(size());
+ for (Replica replica: replicaList)
+ {
+ if (predicate.test(replica))
+ {
+ newReplicaList.add(replica);
+ }
+ }
+ return new ReplicaList(newReplicaList);
+ }
+
+ public void sort(Comparator<Replica> comparator)
+ {
+ replicaList.sort(comparator);
+ }
+
+ public static ReplicaList intersectEndpoints(ReplicaList l1,
ReplicaList l2)
+ {
+ Replicas.checkFull(l1);
+ Replicas.checkFull(l2);
+ // Note: we don't use Guava Sets.intersection() for 3 reasons:
+ // 1) retainAll would be inefficient if l1 and l2 are large but
in practice both are the replicas for a range and
+ // so will be very small (< RF). In that case, retainAll is in
fact more efficient.
+ // 2) we do ultimately need a list so converting everything to
sets don't make sense
+ // 3) l1 and l2 are sorted by proximity. The use of retainAll
maintain that sorting in the result, while using sets wouldn't.
+ Collection<InetAddressAndPort> endpoints = l2.asEndpointList();
+ return l1.filter(r -> endpoints.contains(r.getEndpoint()));
+ }
+
+ public static ReplicaList of(Replica... replicas)
--- End diff --
Hmmm so what I thought was a specialization would be possible, but since
it's mutable you have to allocate a real list. So really I think it's fine to
have of() allocate the wrapper array. So of() could just return a global
immutable one. The singleton one could do a singleton list wrapper.
TL;DR this is not important enough to use up more of your time. You can
delete the specializations.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]