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; | |
da132663 LPD |
22 | import org.eclipse.tracecompass.internal.provisional.datastore.core.interval.IHTIntervalReader; |
23 | import org.eclipse.tracecompass.internal.provisional.segmentstore.core.ISegment2; | |
664a3a81 LPD |
24 | import org.eclipse.tracecompass.internal.segmentstore.core.arraylist.ArrayListStore; |
25 | import org.eclipse.tracecompass.internal.segmentstore.core.arraylist.LazyArrayListStore; | |
da132663 | 26 | import org.eclipse.tracecompass.internal.segmentstore.core.segmentHistoryTree.HistoryTreeSegmentStore; |
664a3a81 LPD |
27 | import org.eclipse.tracecompass.internal.segmentstore.core.treemap.TreeMapStore; |
28 | ||
29 | /** | |
30 | * Factory to create Segment Stores. | |
31 | * | |
32 | * Since segment stores are meant to be accessed using the {@link ISegmentStore} | |
33 | * interface, you can use this factory to instantiate new ones. | |
34 | * | |
35 | * @author Loïc Prieur-Drevon | |
36 | * @param <E> | |
37 | * The type of segment held in this store | |
38 | * @since 1.1 | |
39 | */ | |
40 | public final class SegmentStoreFactory<E> { | |
41 | /** | |
42 | * Flags to determine the type of SegmentStore to use. | |
43 | */ | |
44 | public enum SegmentStoreType { | |
45 | /** | |
46 | * Segment Store should be as fast as possible to build and read. | |
47 | * Performance of individual operations may be slower, but overall the | |
48 | * speed should be faster. | |
49 | */ | |
50 | Fast, | |
51 | /** | |
52 | * Segment Store based should have predictable performance. This does | |
53 | * not mean it's faster, it should not give a surprise random slow down | |
54 | * though. If it slows down, it will be on the {@link ISegment} that | |
55 | * slows it down. | |
56 | */ | |
57 | Stable, | |
58 | /** | |
59 | * Segment Store should contain no duplicate segments | |
60 | */ | |
61 | Distinct | |
62 | } | |
63 | ||
64 | private SegmentStoreFactory() { | |
65 | // Do nothing | |
66 | } | |
67 | ||
68 | /** | |
69 | * New SegmentStore factory method | |
70 | * | |
71 | * @param segmentTypes | |
72 | * Flags used to determine the type of Segment Store that will be | |
73 | * created | |
74 | * | |
75 | * @return a new {@link ISegmentStore} | |
76 | */ | |
77 | public static <E extends ISegment> ISegmentStore<E> createSegmentStore(@Nullable SegmentStoreType... segmentTypes) { | |
78 | Set<@NonNull SegmentStoreType> segments = getListOfFlags(segmentTypes); | |
79 | if (segments.contains(SegmentStoreType.Distinct)) { | |
80 | return createTreeMapStore(); | |
81 | } | |
82 | if (segments.contains(SegmentStoreType.Stable)) { | |
83 | return createArrayListStore(); | |
84 | } | |
85 | // default option is the fastest | |
86 | return createLazyArrayListStore(); | |
87 | ||
88 | } | |
89 | ||
90 | /** | |
91 | * New SegmentStore factory method to create store from an array of Objects | |
92 | * | |
93 | * @param segmentTypes | |
94 | * Flags used to determine the type of Segment Store that will be | |
95 | * created | |
96 | * @param array | |
97 | * the {@link Object} array we want the returned segment store to | |
98 | * contain, {@link Object} are only inserted if they extend | |
99 | * {@link ISegment} | |
100 | * @return an {@link ISegmentStore} containing the {@link ISegment}s from | |
101 | * array. | |
102 | */ | |
103 | public static <E extends ISegment> ISegmentStore<E> createSegmentStore(Object[] array, SegmentStoreType... segmentTypes) { | |
104 | Set<@NonNull SegmentStoreType> segments = getListOfFlags(segmentTypes); | |
105 | if (segments.contains(SegmentStoreType.Distinct)) { | |
106 | ISegmentStore<E> store = createTreeMapStore(); | |
107 | for (Object elem : array) { | |
108 | if (elem instanceof ISegment) { | |
109 | store.add((E) elem); // warning from type, it should be fine | |
110 | } | |
111 | } | |
112 | return store; | |
113 | } | |
114 | if (segments.contains(SegmentStoreType.Stable)) { | |
115 | return new ArrayListStore<>(array); | |
116 | } | |
117 | // default option is the fastest | |
118 | return new LazyArrayListStore<>(array); | |
119 | } | |
120 | ||
da132663 LPD |
121 | /** |
122 | * SegmentStore factory method that creates a segment store on disk | |
123 | * | |
124 | * @param segmentFile | |
125 | * The file where to store the segments | |
126 | * @param segmentReader | |
127 | * The factory to read the segments from a safe byte buffer | |
128 | * | |
129 | * @return an {@link ISegmentStore} | |
130 | * @throws IOException | |
131 | * Exceptions when creating the segment store | |
f48e5dda | 132 | * @since 2.0 |
da132663 LPD |
133 | */ |
134 | public static <E extends ISegment2> ISegmentStore<E> createOnDiskSegmentStore(Path segmentFile, IHTIntervalReader<E> segmentReader) throws IOException { | |
135 | return new HistoryTreeSegmentStore<>(segmentFile, segmentReader); | |
136 | } | |
137 | ||
664a3a81 LPD |
138 | private static Set<@NonNull SegmentStoreType> getListOfFlags(SegmentStoreType... segmentTypes) { |
139 | Set<@NonNull SegmentStoreType> segments = new HashSet<>(); | |
140 | for(@Nullable SegmentStoreType segmentType : segmentTypes ) { | |
141 | if(segmentType != null) { | |
142 | segments.add(segmentType); | |
143 | } | |
144 | } | |
145 | return segments; | |
146 | } | |
147 | ||
148 | /** | |
149 | * New {@link TreeMapStore} factory method | |
150 | * | |
151 | * @return the new Segment Store | |
152 | */ | |
153 | private static <E extends ISegment> ISegmentStore<E> createTreeMapStore() { | |
154 | return new TreeMapStore<>(); | |
155 | } | |
156 | ||
157 | /** | |
158 | * New {@link ArrayListStore} factory method | |
159 | * | |
160 | * @return the new Segment Store | |
161 | */ | |
162 | private static <E extends ISegment> ISegmentStore<E> createArrayListStore() { | |
163 | return new ArrayListStore<>(); | |
164 | } | |
165 | ||
166 | /** | |
167 | * New {@link LazyArrayListStore} factory method | |
168 | * | |
169 | * @return the new Segment Store | |
170 | */ | |
171 | private static <E extends ISegment> ISegmentStore<E> createLazyArrayListStore() { | |
172 | return new LazyArrayListStore<>(); | |
173 | } | |
174 | ||
175 | } |