1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.internal
.segmentstore
.core
.arraylist
;
12 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
14 import java
.util
.ArrayList
;
15 import java
.util
.Collection
;
16 import java
.util
.Collections
;
17 import java
.util
.Comparator
;
18 import java
.util
.Iterator
;
19 import java
.util
.List
;
20 import java
.util
.concurrent
.locks
.ReentrantLock
;
21 import java
.util
.stream
.Collectors
;
23 import org
.eclipse
.jdt
.annotation
.NonNull
;
24 import org
.eclipse
.jdt
.annotation
.Nullable
;
25 import org
.eclipse
.tracecompass
.segmentstore
.core
.BasicSegment
;
26 import org
.eclipse
.tracecompass
.segmentstore
.core
.ISegment
;
27 import org
.eclipse
.tracecompass
.segmentstore
.core
.ISegmentStore
;
28 import org
.eclipse
.tracecompass
.segmentstore
.core
.SegmentComparators
;
30 import com
.google
.common
.collect
.ImmutableList
;
31 import com
.google
.common
.collect
.Ordering
;
34 * Implementation of an {@link ISegmentStore} using one in-memory
35 * {@link ArrayList}. This relatively simple implementation holds everything in
36 * memory, and as such cannot contain too much data.
38 * The LazyArrayListStore itself is {@link Iterable}, and its iteration order
39 * will be by ascending order of start times. For segments with identical start
40 * times, the secondary comparator will be the end time. If even those are
41 * equal, it will defer to the segments' natural ordering (
42 * {@link ISegment#compareTo}).
44 * This structure sorts in a lazy way to allow faster insertions. However, if
45 * the structure is out of order, the next read (getting intersecting elements,
46 * iterating...) will perform a sort. It may have inconsistent performance, but
47 * should be faster at building when receiving shuffled datasets than the
48 * {@link ArrayListStore}.
50 * Removal operations are not supported.
53 * The type of segment held in this store
55 * @author Matthew Khouzam
57 public class LazyArrayListStore
<@NonNull E
extends ISegment
> implements ISegmentStore
<E
> {
59 private final Comparator
<E
> COMPARATOR
= Ordering
.from(SegmentComparators
.INTERVAL_START_COMPARATOR
)
60 .compound(SegmentComparators
.INTERVAL_END_COMPARATOR
);
62 private final ReentrantLock fLock
= new ReentrantLock(false);
64 private final List
<E
> fStore
= new ArrayList
<>();
66 private @Nullable transient Iterable
<E
> fLastSnapshot
= null;
68 private volatile boolean fDirty
= false;
73 public LazyArrayListStore() {
81 * an array of elements to wrap in the segment store
84 public LazyArrayListStore(Object
[] array
) {
85 for (int i
= 0; i
< array
.length
; i
++) {
86 if (array
[i
] instanceof ISegment
) {
87 E element
= (E
) array
[i
];
88 setDirtyIfNeeded(element
);
97 private void setDirtyIfNeeded(@NonNull E value
) {
98 if (!fStore
.isEmpty() && COMPARATOR
.compare(fStore
.get(size() - 1), value
) > 0) {
102 // ------------------------------------------------------------------------
103 // Methods from Collection
104 // ------------------------------------------------------------------------
107 public Iterator
<E
> iterator() {
113 Iterable
<E
> lastSnapshot
= fLastSnapshot
;
114 if (lastSnapshot
== null) {
115 lastSnapshot
= ImmutableList
.copyOf(fStore
);
116 fLastSnapshot
= lastSnapshot
;
118 return checkNotNull(lastSnapshot
.iterator());
125 * DO NOT CALL FROM OUTSIDE OF A LOCK!
127 private void sortStore() {
128 fStore
.sort(COMPARATOR
);
133 public boolean add(@Nullable E val
) {
135 throw new IllegalArgumentException("Cannot add null value"); //$NON-NLS-1$
140 setDirtyIfNeeded(val
);
142 fLastSnapshot
= null;
153 return fStore
.size();
160 public boolean isEmpty() {
163 return fStore
.isEmpty();
170 public boolean contains(@Nullable Object o
) {
173 return fStore
.contains(o
);
180 public boolean containsAll(@Nullable Collection
<?
> c
) {
183 return fStore
.containsAll(c
);
190 public Object
[] toArray() {
196 return fStore
.toArray();
203 public <T
> T
[] toArray(T
[] a
) {
209 return fStore
.toArray(a
);
216 public boolean addAll(@Nullable Collection
<?
extends E
> c
) {
218 throw new IllegalArgumentException();
223 boolean changed
= false;
236 public void clear() {
240 fLastSnapshot
= null;
247 // ------------------------------------------------------------------------
248 // Methods added by ISegmentStore
249 // ------------------------------------------------------------------------
252 public Iterable
<E
> getIntersectingElements(long start
, long end
) {
258 int index
= Collections
.binarySearch(fStore
, new BasicSegment(end
, Long
.MAX_VALUE
));
259 index
= (index
>= 0) ? index
: -index
- 1;
260 return fStore
.subList(0, index
).stream().filter(element
-> !(start
> element
.getEnd() || end
< element
.getStart())).collect(Collectors
.toList());
267 public void dispose() {