Commit | Line | Data |
---|---|---|
664a3a81 LPD |
1 | /******************************************************************************* |
2 | * Copyright (c) 2016 Polytechnique | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Loïc Prieur-Drevon - Initial API and implementation | |
11 | ******************************************************************************/ | |
12 | ||
13 | package org.eclipse.tracecompass.segmentstore.core; | |
14 | ||
da132663 LPD |
15 | import java.io.IOException; |
16 | import java.nio.file.Path; | |
664a3a81 LPD |
17 | import java.util.HashSet; |
18 | import java.util.Set; | |
19 | ||
20 | import org.eclipse.jdt.annotation.NonNull; | |
21 | import org.eclipse.jdt.annotation.Nullable; | |
dad84716 | 22 | import org.eclipse.tracecompass.datastore.core.interval.IHTIntervalReader; |
664a3a81 LPD |
23 | import org.eclipse.tracecompass.internal.segmentstore.core.arraylist.ArrayListStore; |
24 | import org.eclipse.tracecompass.internal.segmentstore.core.arraylist.LazyArrayListStore; | |
da132663 | 25 | import org.eclipse.tracecompass.internal.segmentstore.core.segmentHistoryTree.HistoryTreeSegmentStore; |
664a3a81 LPD |
26 | import org.eclipse.tracecompass.internal.segmentstore.core.treemap.TreeMapStore; |
27 | ||
28 | /** | |
29 | * Factory to create Segment Stores. | |
30 | * | |
31 | * Since segment stores are meant to be accessed using the {@link ISegmentStore} | |
32 | * interface, you can use this factory to instantiate new ones. | |
33 | * | |
34 | * @author Loïc Prieur-Drevon | |
35 | * @param <E> | |
36 | * The type of segment held in this store | |
37 | * @since 1.1 | |
38 | */ | |
39 | public final class SegmentStoreFactory<E> { | |
40 | /** | |
41 | * Flags to determine the type of SegmentStore to use. | |
42 | */ | |
43 | public enum SegmentStoreType { | |
44 | /** | |
45 | * Segment Store should be as fast as possible to build and read. | |
46 | * Performance of individual operations may be slower, but overall the | |
47 | * speed should be faster. | |
48 | */ | |
49 | Fast, | |
50 | /** | |
51 | * Segment Store based should have predictable performance. This does | |
52 | * not mean it's faster, it should not give a surprise random slow down | |
53 | * though. If it slows down, it will be on the {@link ISegment} that | |
54 | * slows it down. | |
55 | */ | |
56 | Stable, | |
57 | /** | |
58 | * Segment Store should contain no duplicate segments | |
59 | */ | |
c42311ba GB |
60 | Distinct, |
61 | /** | |
62 | * Segment store that doesn't have to reside entirely in memory, ideal | |
63 | * for very large stores, the performance are not as high as the other | |
64 | * segment stores. These kind of stores should be created using the | |
65 | * {@link SegmentStoreFactory#createOnDiskSegmentStore(Path, IHTIntervalReader)} | |
66 | * factory method | |
67 | * | |
68 | * @since 2.0 | |
69 | */ | |
70 | OnDisk | |
664a3a81 LPD |
71 | } |
72 | ||
73 | private SegmentStoreFactory() { | |
74 | // Do nothing | |
75 | } | |
76 | ||
77 | /** | |
78 | * New SegmentStore factory method | |
79 | * | |
80 | * @param segmentTypes | |
81 | * Flags used to determine the type of Segment Store that will be | |
82 | * created | |
83 | * | |
84 | * @return a new {@link ISegmentStore} | |
85 | */ | |
86 | public static <E extends ISegment> ISegmentStore<E> createSegmentStore(@Nullable SegmentStoreType... segmentTypes) { | |
87 | Set<@NonNull SegmentStoreType> segments = getListOfFlags(segmentTypes); | |
88 | if (segments.contains(SegmentStoreType.Distinct)) { | |
89 | return createTreeMapStore(); | |
90 | } | |
91 | if (segments.contains(SegmentStoreType.Stable)) { | |
92 | return createArrayListStore(); | |
93 | } | |
94 | // default option is the fastest | |
95 | return createLazyArrayListStore(); | |
96 | ||
97 | } | |
98 | ||
99 | /** | |
100 | * New SegmentStore factory method to create store from an array of Objects | |
101 | * | |
102 | * @param segmentTypes | |
103 | * Flags used to determine the type of Segment Store that will be | |
104 | * created | |
105 | * @param array | |
106 | * the {@link Object} array we want the returned segment store to | |
107 | * contain, {@link Object} are only inserted if they extend | |
108 | * {@link ISegment} | |
109 | * @return an {@link ISegmentStore} containing the {@link ISegment}s from | |
110 | * array. | |
111 | */ | |
112 | public static <E extends ISegment> ISegmentStore<E> createSegmentStore(Object[] array, SegmentStoreType... segmentTypes) { | |
113 | Set<@NonNull SegmentStoreType> segments = getListOfFlags(segmentTypes); | |
114 | if (segments.contains(SegmentStoreType.Distinct)) { | |
115 | ISegmentStore<E> store = createTreeMapStore(); | |
116 | for (Object elem : array) { | |
117 | if (elem instanceof ISegment) { | |
118 | store.add((E) elem); // warning from type, it should be fine | |
119 | } | |
120 | } | |
121 | return store; | |
122 | } | |
123 | if (segments.contains(SegmentStoreType.Stable)) { | |
124 | return new ArrayListStore<>(array); | |
125 | } | |
126 | // default option is the fastest | |
127 | return new LazyArrayListStore<>(array); | |
128 | } | |
129 | ||
da132663 LPD |
130 | /** |
131 | * SegmentStore factory method that creates a segment store on disk | |
132 | * | |
133 | * @param segmentFile | |
134 | * The file where to store the segments | |
135 | * @param segmentReader | |
136 | * The factory to read the segments from a safe byte buffer | |
137 | * | |
138 | * @return an {@link ISegmentStore} | |
139 | * @throws IOException | |
140 | * Exceptions when creating the segment store | |
f48e5dda | 141 | * @since 2.0 |
da132663 | 142 | */ |
3866be8e | 143 | public static <E extends ISegment> ISegmentStore<E> createOnDiskSegmentStore(Path segmentFile, IHTIntervalReader<E> segmentReader) throws IOException { |
da132663 LPD |
144 | return new HistoryTreeSegmentStore<>(segmentFile, segmentReader); |
145 | } | |
146 | ||
664a3a81 LPD |
147 | private static Set<@NonNull SegmentStoreType> getListOfFlags(SegmentStoreType... segmentTypes) { |
148 | Set<@NonNull SegmentStoreType> segments = new HashSet<>(); | |
149 | for(@Nullable SegmentStoreType segmentType : segmentTypes ) { | |
150 | if(segmentType != null) { | |
151 | segments.add(segmentType); | |
152 | } | |
153 | } | |
154 | return segments; | |
155 | } | |
156 | ||
157 | /** | |
158 | * New {@link TreeMapStore} factory method | |
159 | * | |
160 | * @return the new Segment Store | |
161 | */ | |
162 | private static <E extends ISegment> ISegmentStore<E> createTreeMapStore() { | |
163 | return new TreeMapStore<>(); | |
164 | } | |
165 | ||
166 | /** | |
167 | * New {@link ArrayListStore} factory method | |
168 | * | |
169 | * @return the new Segment Store | |
170 | */ | |
171 | private static <E extends ISegment> ISegmentStore<E> createArrayListStore() { | |
172 | return new ArrayListStore<>(); | |
173 | } | |
174 | ||
175 | /** | |
176 | * New {@link LazyArrayListStore} factory method | |
177 | * | |
178 | * @return the new Segment Store | |
179 | */ | |
180 | private static <E extends ISegment> ISegmentStore<E> createLazyArrayListStore() { | |
181 | return new LazyArrayListStore<>(); | |
182 | } | |
183 | ||
184 | } |