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
-
Method Summary
Modifier and TypeMethodDescriptionvoid
boolean
boolean
addAll
(int index, Collection<? extends E> c) boolean
addAll
(Collection<? extends E> c) int
addAllAbsent
(Collection<? extends E> c) void
clear()
computeIfAbsent
(int index, IntFunction<? extends E> mappingFunction) void
get
(int index) boolean
isEmpty()
N.B.: If the list is only populated with null entries, this function will return true.int
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); }
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
boolean
removeAll
(Collection<?> c) boolean
void
replaceAll
(UnaryOperator<E> operator) boolean
retainAll
(Collection<?> c) A function which behaves likeMap.put(Object, Object)
, rather thanList.set(int, Object)
.void
sort
(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, toString
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods 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:
get
in interfaceList<E>
- Overrides:
get
in 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:
isEmpty
in interfaceCollection<E>
- Specified by:
isEmpty
in interfaceList<E>
- Overrides:
isEmpty
in classCopyOnWriteArrayList<E>
- Returns:
- a boolean indicating whether the list is empty
-
add
- Specified by:
add
in interfaceCollection<E>
- Specified by:
add
in interfaceList<E>
- Overrides:
add
in classCopyOnWriteArrayList<E>
-
add
-
remove
- Specified by:
remove
in interfaceCollection<E>
- Specified by:
remove
in interfaceList<E>
- Overrides:
remove
in classCopyOnWriteArrayList<E>
-
removeAll
- Specified by:
removeAll
in interfaceCollection<E>
- Specified by:
removeAll
in interfaceList<E>
- Overrides:
removeAll
in classCopyOnWriteArrayList<E>
-
retainAll
- Specified by:
retainAll
in interfaceCollection<E>
- Specified by:
retainAll
in interfaceList<E>
- Overrides:
retainAll
in classCopyOnWriteArrayList<E>
-
addAllAbsent
- Overrides:
addAllAbsent
in classCopyOnWriteArrayList<E>
-
clear
public void clear()- Specified by:
clear
in interfaceCollection<E>
- Specified by:
clear
in interfaceList<E>
- Overrides:
clear
in classCopyOnWriteArrayList<E>
-
addAll
- Specified by:
addAll
in interfaceCollection<E>
- Specified by:
addAll
in interfaceList<E>
- Overrides:
addAll
in classCopyOnWriteArrayList<E>
-
addAll
-
removeIf
- Specified by:
removeIf
in interfaceCollection<E>
- Overrides:
removeIf
in classCopyOnWriteArrayList<E>
-
replaceAll
- Specified by:
replaceAll
in interfaceList<E>
- Overrides:
replaceAll
in classCopyOnWriteArrayList<E>
-
sort
-
subList
-
computeIfAbsent
-
values
-