Commit | Line | Data |
---|---|---|
26a6a7eb AM |
1 | /******************************************************************************* |
2 | * Copyright (c) 2015 EfficiOS Inc., Alexandre Montplaisir | |
3 | * | |
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 | * | |
9 | * Contributors: | |
10 | * Alexandre Montplaisir - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.tracecompass.segmentstore.core.treemap; | |
14 | ||
15 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; | |
16 | ||
17 | import java.util.Comparator; | |
18 | import java.util.HashMap; | |
19 | import java.util.Iterator; | |
20 | import java.util.Map; | |
21 | import java.util.TreeMap; | |
22 | ||
23 | import org.eclipse.jdt.annotation.Nullable; | |
24 | import org.eclipse.tracecompass.segmentstore.core.ISegment; | |
25 | import org.eclipse.tracecompass.segmentstore.core.ISegmentStore; | |
26 | ||
27 | import com.google.common.collect.Iterables; | |
28 | import com.google.common.collect.Sets; | |
29 | import com.google.common.collect.TreeMultimap; | |
30 | ||
31 | /** | |
32 | * Implementation of a {@link ISegmentStore} using in-memory {@link TreeMap}'s. | |
33 | * This relatively simple implementation holds everything in memory, and as such | |
34 | * cannot contain too much data. | |
35 | * | |
36 | * @param <T> | |
37 | * The time of time range held | |
38 | * | |
39 | * @author Alexandre Montplaisir | |
40 | */ | |
41 | public class TreeMapStore<T extends ISegment> implements ISegmentStore<T> { | |
42 | ||
43 | private final TreeMultimap<Long, T> fStartTimesIndex; | |
44 | private final TreeMultimap<Long, T> fEndTimesIndex; | |
45 | ||
46 | private final Map<Long, T> fPositionMap; | |
52d9d1f6 AM |
47 | |
48 | private volatile long fSize; | |
26a6a7eb AM |
49 | |
50 | /** | |
51 | *Constructor | |
52 | */ | |
53 | public TreeMapStore() { | |
54 | fStartTimesIndex = checkNotNull(TreeMultimap.<Long, T> create(LONG_COMPARATOR, INTERVAL_START_COMPARATOR)); | |
55 | fEndTimesIndex = checkNotNull(TreeMultimap.<Long, T> create(LONG_COMPARATOR, INTERVAL_END_COMPARATOR)); | |
56 | fPositionMap = new HashMap<>(); | |
57 | fSize = 0; | |
58 | } | |
59 | ||
60 | @Override | |
61 | public Iterator<T> iterator() { | |
62 | return checkNotNull(fStartTimesIndex.values().iterator()); | |
63 | } | |
64 | ||
65 | @Override | |
66 | public synchronized void addElement(T val) { | |
67 | fStartTimesIndex.put(Long.valueOf(val.getStart()), val); | |
4d16d52b | 68 | fEndTimesIndex.put(Long.valueOf(val.getEnd()), val); |
26a6a7eb AM |
69 | fPositionMap.put(fSize, val); |
70 | fSize++; | |
71 | } | |
72 | ||
73 | @Override | |
74 | public long getNbElements() { | |
75 | return fSize; | |
76 | } | |
77 | ||
78 | @Override | |
79 | public T getElementAtIndex(long index) { | |
80 | return checkNotNull(fPositionMap.get(Long.valueOf(index))); | |
81 | } | |
82 | ||
83 | @Override | |
84 | public Iterable<T> getIntersectingElements(long position) { | |
85 | /* | |
86 | * The intervals intersecting 't' are those whose 1) start time is | |
87 | * *lower* than 't' AND 2) end time is *higher* than 't'. | |
88 | */ | |
89 | Iterable<T> matchStarts = Iterables.concat(fStartTimesIndex.asMap().headMap(position, true).values()); | |
90 | Iterable<T> matchEnds = Iterables.concat(fEndTimesIndex.asMap().tailMap(position, true).values()); | |
91 | ||
92 | return checkNotNull(Sets.intersection(Sets.newHashSet(matchStarts), Sets.newHashSet(matchEnds))); | |
93 | } | |
94 | ||
95 | @Override | |
96 | public Iterable<T> getIntersectingElements(long start, long end) { | |
97 | Iterable<T> matchStarts = Iterables.concat(fStartTimesIndex.asMap().headMap(end, true).values()); | |
98 | Iterable<T> matchEnds = Iterables.concat(fEndTimesIndex.asMap().tailMap(start, true).values()); | |
99 | ||
100 | return checkNotNull(Sets.intersection(Sets.newHashSet(matchStarts), Sets.newHashSet(matchEnds))); | |
101 | } | |
102 | ||
103 | @Override | |
52d9d1f6 | 104 | public synchronized void dispose() { |
26a6a7eb AM |
105 | fStartTimesIndex.clear(); |
106 | fEndTimesIndex.clear(); | |
107 | fPositionMap.clear(); | |
52d9d1f6 | 108 | fSize = 0; |
26a6a7eb AM |
109 | } |
110 | ||
111 | // ------------------------------------------------------------------------ | |
112 | // Comparators, used for the tree maps | |
113 | // ------------------------------------------------------------------------ | |
114 | ||
115 | private static final Comparator<Long> LONG_COMPARATOR = new Comparator<Long>() { | |
116 | @Override | |
117 | public int compare(@Nullable Long o1, @Nullable Long o2) { | |
118 | if (o1 == null || o2 == null) { | |
119 | throw new IllegalArgumentException(); | |
120 | } | |
121 | return o1.compareTo(o2); | |
122 | } | |
123 | }; | |
124 | ||
125 | private static final Comparator<ISegment> INTERVAL_START_COMPARATOR = new Comparator<ISegment>() { | |
126 | @Override | |
127 | public int compare(@Nullable ISegment o1, @Nullable ISegment o2) { | |
128 | if (o1 == null || o2 == null) { | |
129 | throw new IllegalArgumentException(); | |
130 | } | |
131 | return Long.compare(o1.getStart(), o2.getStart()); | |
132 | } | |
133 | }; | |
134 | ||
135 | private static final Comparator<ISegment> INTERVAL_END_COMPARATOR = new Comparator<ISegment>() { | |
136 | @Override | |
137 | public int compare(@Nullable ISegment o1, @Nullable ISegment o2) { | |
138 | if (o1 == null || o2 == null) { | |
139 | throw new IllegalArgumentException(); | |
140 | } | |
141 | return Long.compare(o1.getEnd(), o2.getEnd()); | |
142 | } | |
143 | }; | |
144 | ||
145 | ||
146 | } |