cassandra-pr mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aweisberg <...@git.apache.org>
Subject [GitHub] cassandra pull request #224: 14405 replicas
Date Wed, 16 May 2018 17:06:10 GMT
Github user aweisberg commented on a diff in the pull request:

    https://github.com/apache/cassandra/pull/224#discussion_r188115552
  
    --- 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();
    --- End diff --
    
    You can do this without converting or wrapping because l2 implements containsEndpoint.
    
    It might make sense to implement containsEndpoint here in ReplicaList so that it doesn't
have to allocate an iterator. You can't do a similar thing with ReplicaSet since you can't
access the Set implementations underlying storage for iteration.
    
    The disadvantage is it makes containsEndpoint bi-morphic which is probably fine.
    
    Granted the iterator is probably allocated on the stack since escape analysis should work
in this instance and its methods get inlined so??? 
    
    Also Iterables.any requires allocating the predicate since it's binding a value. Probably
worth avoiding that.


---

---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org


Mime
View raw message