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:
Serializable,Cloneable,Iterable<E>,Collection<E>,List<E>,RandomAccess
- Direct Known Subclasses:
SparseConcurrentListWithOffset
A
List implementation with some usability improvements around resizing. In particular,
the list provides Map semantics in some cases where the regular List behavior would be to
throw ArrayIndexOutOfBoundsException.
Note on concurrency and performance characteristics:
This class extends CopyOnWriteArrayList 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 to CopyOnWriteArrayList.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:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidbooleanbooleanaddAll(int index, Collection<? extends E> c) booleanaddAll(Collection<? extends E> c) intaddAllAbsent(Collection<? extends E> c) voidclear()computeIfAbsent(int index, IntFunction<? extends E> mappingFunction) voidget(int index) booleanisEmpty()N.B.: If the list is only populated with null entries, this function will return true.intN.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); }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.booleanbooleanremoveAll(Collection<?> c) booleanvoidreplaceAll(UnaryOperator<E> operator) booleanretainAll(Collection<?> c) A function which behaves likeMap.put(Object, Object), rather thanList.set(int, Object).voidsort(Comparator<? super E> c) subList(int fromIndex, int toIndex) 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, toStringMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, stream, toArray
-
Constructor Details
-
SparseConcurrentList
public SparseConcurrentList()
-
-
Method Details
-
set
A function which behaves likeMap.put(Object, Object), rather thanList.set(int, Object). -
get
- Specified by:
getin interfaceList<E>- Overrides:
getin classCopyOnWriteArrayList<E>- Parameters:
index- of the item to retrieve- Returns:
- the item at this index, or null
- Throws:
IllegalArgumentException- if the index is < 0, but NOT if the index is > the capacity of the list
-
remove
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
-
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.- Specified by:
isEmptyin interfaceCollection<E>- Specified by:
isEmptyin interfaceList<E>- Overrides:
isEmptyin classCopyOnWriteArrayList<E>- Returns:
- a boolean indicating whether the list is empty
-
add
- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceList<E>- Overrides:
addin classCopyOnWriteArrayList<E>
-
add
-
remove
- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceList<E>- Overrides:
removein classCopyOnWriteArrayList<E>
-
removeAll
- Specified by:
removeAllin interfaceCollection<E>- Specified by:
removeAllin interfaceList<E>- Overrides:
removeAllin classCopyOnWriteArrayList<E>
-
retainAll
- Specified by:
retainAllin interfaceCollection<E>- Specified by:
retainAllin interfaceList<E>- Overrides:
retainAllin classCopyOnWriteArrayList<E>
-
addAllAbsent
- Overrides:
addAllAbsentin classCopyOnWriteArrayList<E>
-
clear
public void clear()- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceList<E>- Overrides:
clearin classCopyOnWriteArrayList<E>
-
addAll
- Specified by:
addAllin interfaceCollection<E>- Specified by:
addAllin interfaceList<E>- Overrides:
addAllin classCopyOnWriteArrayList<E>
-
addAll
-
removeIf
- Specified by:
removeIfin interfaceCollection<E>- Overrides:
removeIfin classCopyOnWriteArrayList<E>
-
replaceAll
- Specified by:
replaceAllin interfaceList<E>- Overrides:
replaceAllin classCopyOnWriteArrayList<E>
-
sort
-
subList
-
computeIfAbsent
-
values
-