e5de0a72588abbbd55f4e837f7ce7f1c5e9b431a
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
.analysis
.timing
.core
.store
;
12 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
14 import java
.util
.ArrayList
;
15 import java
.util
.Collection
;
16 import java
.util
.Comparator
;
17 import java
.util
.Iterator
;
18 import java
.util
.List
;
19 import java
.util
.concurrent
.locks
.ReentrantLock
;
20 import java
.util
.stream
.Collectors
;
22 import org
.eclipse
.jdt
.annotation
.NonNull
;
23 import org
.eclipse
.jdt
.annotation
.Nullable
;
24 import org
.eclipse
.tracecompass
.segmentstore
.core
.ISegment
;
25 import org
.eclipse
.tracecompass
.segmentstore
.core
.ISegmentStore
;
27 import com
.google
.common
.collect
.ImmutableList
;
30 * Implementation of an {@link ISegmentStore} using one in-memory
31 * {@link ArrayList}. This relatively simple implementation holds everything in
32 * memory, and as such cannot contain too much data.
34 * The LazyArrayListStore itself is {@link Iterable}, and its iteration order
35 * will be by ascending order of start times. For segments with identical start
36 * times, the secondary comparator will be the end time. If even those are
37 * equal, it will defer to the segments' natural ordering (
38 * {@link ISegment#compareTo}).
40 * This structure sorts in a lazy way to allow faster insertions. However, if
41 * the structure is out of order, the next read (getting intersecting elements,
42 * iterating...) will perform a sort. It may have inconsistent performance, but
43 * should be faster at building when receiving shuffled datasets than the
44 * {@link ArrayListStore}.
46 * Removal operations are not supported.
49 * The type of segment held in this store
51 * @author Matthew Khouzam
53 public class LazyArrayListStore
<@NonNull E
extends ISegment
> implements ISegmentStore
<E
> {
55 private static final String ERROR_MESSAGE
= "Cannot remove from a segment store"; //$NON-NLS-1$
57 private final Comparator
<E
> COMPARATOR
= (o1
, o2
) -> {
58 int ret
= Long
.compare(o1
.getStart(), o2
.getStart());
60 return Long
.compare(o1
.getEnd(), o2
.getEnd());
65 private final ReentrantLock fLock
= new ReentrantLock(false);
67 private final @NonNull List
<E
> fStore
= new ArrayList
<>();
69 private @Nullable transient Iterable
<E
> fLastSnapshot
= null;
71 private volatile boolean fDirty
= false;
76 public LazyArrayListStore() {
84 * an array of elements to wrap in the segment store
87 public LazyArrayListStore(Object
[] array
) {
88 for (int i
= 0; i
< array
.length
; i
++) {
89 if (array
[i
] instanceof ISegment
) {
90 E element
= (E
) array
[i
];
91 setDirtyIfNeeded(element
);
100 private void setDirtyIfNeeded(@NonNull E value
) {
101 if (!fStore
.isEmpty() && COMPARATOR
.compare(fStore
.get(size() - 1), value
) > 0) {
105 // ------------------------------------------------------------------------
106 // Methods from Collection
107 // ------------------------------------------------------------------------
110 public Iterator
<E
> iterator() {
116 Iterable
<E
> lastSnapshot
= fLastSnapshot
;
117 if (lastSnapshot
== null) {
118 lastSnapshot
= ImmutableList
.copyOf(fStore
);
119 fLastSnapshot
= lastSnapshot
;
121 return checkNotNull(lastSnapshot
.iterator());
128 * DO NOT CALL FROM OUTSIDE OF A LOCK!
130 private void sortStore() {
131 fStore
.sort(COMPARATOR
);
136 public boolean add(@Nullable E val
) {
138 throw new IllegalArgumentException("Cannot add null value"); //$NON-NLS-1$
143 setDirtyIfNeeded(val
);
145 fLastSnapshot
= null;
154 return fStore
.size();
158 public boolean isEmpty() {
161 return fStore
.isEmpty();
168 public boolean contains(@Nullable Object o
) {
171 return fStore
.contains(o
);
178 public boolean containsAll(@Nullable Collection
<?
> c
) {
181 return fStore
.containsAll(c
);
188 public Object
[] toArray() {
194 return fStore
.toArray();
201 public <T
> T
[] toArray(T
[] a
) {
207 return fStore
.toArray(a
);
214 public boolean addAll(@Nullable Collection
<?
extends E
> c
) {
216 throw new IllegalArgumentException();
221 boolean changed
= false;
234 public boolean removeAll(@Nullable Collection
<?
> c
) {
235 throw new UnsupportedOperationException(ERROR_MESSAGE
);
239 public boolean retainAll(@Nullable Collection
<?
> c
) {
240 throw new UnsupportedOperationException(ERROR_MESSAGE
);
244 public boolean remove(@Nullable Object o
) {
245 throw new UnsupportedOperationException(ERROR_MESSAGE
);
249 public void clear() {
253 fLastSnapshot
= null;
260 // ------------------------------------------------------------------------
261 // Methods added by ISegmentStore
262 // ------------------------------------------------------------------------
265 public Iterable
<E
> getIntersectingElements(long position
) {
271 * The intervals intersecting 't' are those whose 1) start time is
272 * *lower* than 't' AND 2) end time is *higher* than 't'.
275 return fStore
.stream().filter(element
-> position
>= element
.getStart() && position
<= element
.getEnd()).collect(Collectors
.toList());
282 public Iterable
<E
> getIntersectingElements(long start
, long end
) {
288 return fStore
.stream().filter(element
-> !(start
> element
.getEnd() || end
< element
.getStart())).collect(Collectors
.toList());
295 public void dispose() {
This page took 0.046671 seconds and 4 git commands to generate.