Package com.linkedin.venice.utils
Class SparseConcurrentList<E>
- java.lang.Object
-
- java.util.concurrent.CopyOnWriteArrayList<E>
-
- com.linkedin.venice.utils.SparseConcurrentList<E>
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Cloneable
,java.lang.Iterable<E>
,java.util.Collection<E>
,java.util.List<E>
,java.util.RandomAccess
- Direct Known Subclasses:
SparseConcurrentListWithOffset
public class SparseConcurrentList<E> extends java.util.concurrent.CopyOnWriteArrayList<E>
AList
implementation with some usability improvements around resizing. In particular, the list providesMap
semantics in some cases where the regularList
behavior would be to throwArrayIndexOutOfBoundsException
. Note on concurrency and performance characteristics: This class extendsCopyOnWriteArrayList
and thus mimics its general characteristics: it is threadsafe, very efficient for read operations, but it incurs a locking overhead on mutation operations. Unfortunately, the locking overhead may be up to double that of the parent class, because we cannot get access toCopyOnWriteArrayList.lock
since it is package private and Java doesn't allow us to define new classes in the java.* package. So instead we are making every mutation operation synchronized. The end result should be that the inner lock inside the parent class never has any contention, since the locking is effectively external. In any case, even if this double-locking has any overhead, it should not be a big concern, since the read operations are still lock-free, and (at the time of this writing), only the read operations are used on the hot path.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description SparseConcurrentList()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(int index, E element)
boolean
add(E e)
boolean
addAll(int index, java.util.Collection<? extends E> c)
boolean
addAll(java.util.Collection<? extends E> c)
int
addAllAbsent(java.util.Collection<? extends E> c)
void
clear()
E
computeIfAbsent(int index, java.util.function.IntFunction<? extends E> mappingFunction)
void
forEach(java.util.function.Consumer<? super E> itemConsumer)
E
get(int index)
boolean
isEmpty()
N.B.: If the list is only populated with null entries, this function will return true.int
nonNullSize()
N.B.: The intent of having a separate function for this, and to not override the behavior ofCopyOnWriteArrayList.size()
is that if the code does a for loop using the size, we want them to be able to get every element, e.g.:for (int i = 0; i < list.size(); i++) { doSomething(list.get(i)); }
An alternative is to callvalues()
which filters out nulls, e.g.:for (E element: list.values()) { doSomething(element); }
E
remove(int index)
A function which behaves likeMap.remove(Object)
, rather thanList.remove(int)
, in the sense that it removes the item from the collection, returns the previous value (if any), but *does not* shift subsequent items to the left (as the regularList.remove(int)
would.boolean
remove(java.lang.Object o)
boolean
removeAll(java.util.Collection<?> c)
boolean
removeIf(java.util.function.Predicate<? super E> filter)
void
replaceAll(java.util.function.UnaryOperator<E> operator)
boolean
retainAll(java.util.Collection<?> c)
E
set(int index, E item)
A function which behaves likeMap.put(Object, Object)
, rather thanList.set(int, Object)
.void
sort(java.util.Comparator<? super E> c)
java.util.List<E>
subList(int fromIndex, int toIndex)
java.util.Collection<E>
values()
-
Methods inherited from class java.util.concurrent.CopyOnWriteArrayList
addIfAbsent, clone, contains, containsAll, equals, hashCode, indexOf, indexOf, iterator, lastIndexOf, lastIndexOf, listIterator, listIterator, size, spliterator, toArray, toArray, toString
-
-
-
-
Method Detail
-
set
public E set(int index, E item)
A function which behaves likeMap.put(Object, Object)
, rather thanList.set(int, Object)
.
-
get
public E get(int index)
- Specified by:
get
in interfacejava.util.List<E>
- Overrides:
get
in classjava.util.concurrent.CopyOnWriteArrayList<E>
- Parameters:
index
- of the item to retrieve- Returns:
- the item at this index, or null
- Throws:
java.lang.IllegalArgumentException
- if the index is < 0, but NOT if the index is > the capacity of the list
-
remove
public E remove(int index)
A function which behaves likeMap.remove(Object)
, rather thanList.remove(int)
, in the sense that it removes the item from the collection, returns the previous value (if any), but *does not* shift subsequent items to the left (as the regularList.remove(int)
would.
-
forEach
public void forEach(java.util.function.Consumer<? super E> itemConsumer)
-
nonNullSize
public int nonNullSize()
N.B.: The intent of having a separate function for this, and to not override the behavior ofCopyOnWriteArrayList.size()
is that if the code does a for loop using the size, we want them to be able to get every element, e.g.:for (int i = 0; i < list.size(); i++) { doSomething(list.get(i)); }
An alternative is to callvalues()
which filters out nulls, e.g.:for (E element: list.values()) { doSomething(element); }
- Returns:
- the number of non-null elements in this list, from a cached counter (updated at mutation time).
-
isEmpty
public boolean isEmpty()
N.B.: If the list is only populated with null entries, this function will return true.
-
add
public boolean add(E e)
-
add
public void add(int index, E element)
-
remove
public boolean remove(java.lang.Object o)
-
removeAll
public boolean removeAll(java.util.Collection<?> c)
-
retainAll
public boolean retainAll(java.util.Collection<?> c)
-
addAllAbsent
public int addAllAbsent(java.util.Collection<? extends E> c)
- Overrides:
addAllAbsent
in classjava.util.concurrent.CopyOnWriteArrayList<E>
-
clear
public void clear()
-
addAll
public boolean addAll(java.util.Collection<? extends E> c)
-
addAll
public boolean addAll(int index, java.util.Collection<? extends E> c)
-
removeIf
public boolean removeIf(java.util.function.Predicate<? super E> filter)
-
replaceAll
public void replaceAll(java.util.function.UnaryOperator<E> operator)
-
sort
public void sort(java.util.Comparator<? super E> c)
-
subList
public java.util.List<E> subList(int fromIndex, int toIndex)
-
computeIfAbsent
public E computeIfAbsent(int index, java.util.function.IntFunction<? extends E> mappingFunction)
-
values
public java.util.Collection<E> values()
-
-