1 /*******************************************************************************
2 * Copyright (c) 2015 EfficiOS Inc., Alexandre Montplaisir and others.
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
10 * Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.segmentstore
.core
.treemap
;
15 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
17 import java
.util
.Collection
;
18 import java
.util
.Iterator
;
19 import java
.util
.TreeMap
;
20 import java
.util
.concurrent
.locks
.ReadWriteLock
;
21 import java
.util
.concurrent
.locks
.ReentrantReadWriteLock
;
23 import org
.eclipse
.jdt
.annotation
.Nullable
;
24 import org
.eclipse
.tracecompass
.segmentstore
.core
.ISegment
;
25 import org
.eclipse
.tracecompass
.segmentstore
.core
.ISegmentStore
;
26 import org
.eclipse
.tracecompass
.segmentstore
.core
.SegmentComparators
;
28 import com
.google
.common
.collect
.ImmutableList
;
29 import com
.google
.common
.collect
.Iterables
;
30 import com
.google
.common
.collect
.Ordering
;
31 import com
.google
.common
.collect
.Sets
;
32 import com
.google
.common
.collect
.TreeMultimap
;
35 * Implementation of a {@link ISegmentStore} using in-memory {@link TreeMap}'s.
36 * This relatively simple implementation holds everything in memory, and as such
37 * cannot contain too much data.
39 * The TreeMapStore itself is Iterable, and its iteration order will be by
40 * ascending order of start times. For segments with identical start times, the
41 * secondary comparator will be the end time. If even those are equal, it will
42 * defer to the segments' natural ordering ({@link ISegment#compareTo}).
44 * The store's tree maps will not accept duplicate key-value pairs, which means
45 * that if you want several segments with the same start and end times, make
46 * sure their compareTo() differentiates them.
48 * Removal operations are not supported.
51 * The type of segment held in this store
53 * @author Alexandre Montplaisir
55 public class TreeMapStore
<E
extends ISegment
> implements ISegmentStore
<E
> {
57 private final ReadWriteLock fLock
= new ReentrantReadWriteLock(false);
59 private final TreeMultimap
<Long
, E
> fStartTimesIndex
;
60 private final TreeMultimap
<Long
, E
> fEndTimesIndex
;
62 private volatile long fSize
;
64 private @Nullable transient Iterable
<E
> fLastSnapshot
= null;
69 public TreeMapStore() {
71 * For the start times index, the "key comparator" will compare the
72 * start times as longs directly. This is the primary comparator for its
75 * The secondary "value" comparator will check the end times first, and
76 * in the event of a tie, defer to the ISegment's Comparable
77 * implementation, a.k.a. its natural ordering.
79 * The same is done for the end times index, but swapping the first two
80 * comparators instead.
82 fStartTimesIndex
= checkNotNull(TreeMultimap
.create(
83 SegmentComparators
.LONG_COMPARATOR
,
84 Ordering
.from(SegmentComparators
.INTERVAL_END_COMPARATOR
).compound(Ordering
.natural())));
86 fEndTimesIndex
= checkNotNull(TreeMultimap
.create(
87 SegmentComparators
.LONG_COMPARATOR
,
88 Ordering
.from(SegmentComparators
.INTERVAL_START_COMPARATOR
).compound(Ordering
.natural())));
93 // ------------------------------------------------------------------------
94 // Methods from Collection
95 // ------------------------------------------------------------------------
98 public Iterator
<E
> iterator() {
99 fLock
.readLock().lock();
101 Iterable
<E
> lastSnapshot
= fLastSnapshot
;
102 if (lastSnapshot
== null) {
103 lastSnapshot
= checkNotNull(ImmutableList
.copyOf(fStartTimesIndex
.values()));
104 fLastSnapshot
= lastSnapshot
;
106 return checkNotNull(lastSnapshot
.iterator());
108 fLock
.readLock().unlock();
113 public boolean add(@Nullable E val
) {
115 throw new IllegalArgumentException();
118 fLock
.writeLock().lock();
120 if (fStartTimesIndex
.put(Long
.valueOf(val
.getStart()), val
)) {
121 fEndTimesIndex
.put(Long
.valueOf(val
.getEnd()), val
);
123 fLastSnapshot
= null;
128 fLock
.writeLock().unlock();
134 return Long
.valueOf(fSize
).intValue();
138 public boolean isEmpty() {
143 public boolean contains(@Nullable Object o
) {
144 fLock
.readLock().lock();
146 return fStartTimesIndex
.containsValue(o
);
148 fLock
.readLock().unlock();
154 public boolean containsAll(@Nullable Collection
<?
> c
) {
155 fLock
.readLock().lock();
157 return fStartTimesIndex
.values().containsAll(c
);
159 fLock
.readLock().unlock();
164 public Object
[] toArray() {
165 fLock
.readLock().lock();
167 return fStartTimesIndex
.values().toArray();
169 fLock
.readLock().unlock();
174 public <T
> T
[] toArray(T
[] a
) {
175 fLock
.readLock().lock();
177 return fStartTimesIndex
.values().toArray(a
);
179 fLock
.readLock().unlock();
184 public boolean remove(@Nullable Object o
) {
185 throw new UnsupportedOperationException();
189 public boolean addAll(@Nullable Collection
<?
extends E
> c
) {
191 throw new IllegalArgumentException();
194 fLock
.writeLock().lock();
196 boolean changed
= false;
198 if (this.add(elem
)) {
204 fLock
.writeLock().unlock();
209 public boolean removeAll(@Nullable Collection
<?
> c
) {
210 throw new UnsupportedOperationException();
214 public boolean retainAll(@Nullable Collection
<?
> c
) {
215 throw new UnsupportedOperationException();
219 public void clear() {
220 throw new UnsupportedOperationException();
223 // ------------------------------------------------------------------------
224 // Methods added by ISegmentStore
225 // ------------------------------------------------------------------------
228 public Iterable
<E
> getIntersectingElements(long position
) {
230 * The intervals intersecting 't' are those whose 1) start time is
231 * *lower* than 't' AND 2) end time is *higher* than 't'.
233 fLock
.readLock().lock();
235 Iterable
<E
> matchStarts
= Iterables
.concat(fStartTimesIndex
.asMap().headMap(position
, true).values());
236 Iterable
<E
> matchEnds
= Iterables
.concat(fEndTimesIndex
.asMap().tailMap(position
, true).values());
237 return checkNotNull(Sets
.intersection(Sets
.newHashSet(matchStarts
), Sets
.newHashSet(matchEnds
)));
239 fLock
.readLock().unlock();
244 public Iterable
<E
> getIntersectingElements(long start
, long end
) {
245 fLock
.readLock().lock();
247 Iterable
<E
> matchStarts
= Iterables
.concat(fStartTimesIndex
.asMap().headMap(end
, true).values());
248 Iterable
<E
> matchEnds
= Iterables
.concat(fEndTimesIndex
.asMap().tailMap(start
, true).values());
249 return checkNotNull(Sets
.intersection(Sets
.newHashSet(matchStarts
), Sets
.newHashSet(matchEnds
)));
251 fLock
.readLock().unlock();
256 public void dispose() {
257 fLock
.writeLock().lock();
259 fStartTimesIndex
.clear();
260 fEndTimesIndex
.clear();
263 fLock
.writeLock().unlock();