Commit | Line | Data |
---|---|---|
4ab33d2b AO |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009 Ericsson | |
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 | * Francois Chouinard (fchouinard@gmail.com) - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.eventlog; | |
14 | ||
15 | import java.io.FileNotFoundException; | |
16 | import java.io.IOException; | |
17 | import java.io.RandomAccessFile; | |
18 | import java.util.Collections; | |
19 | import java.util.Vector; | |
20 | ||
21 | import org.eclipse.linuxtools.tmf.event.TmfEvent; | |
1f506a43 | 22 | import org.eclipse.linuxtools.tmf.event.TmfTimeRange; |
4ab33d2b AO |
23 | import org.eclipse.linuxtools.tmf.event.TmfTimestamp; |
24 | ||
25 | /** | |
26 | * <b><u>TmfEventStream</u></b> | |
27 | * <p> | |
28 | * FIXME: Preliminary version that works only for a single file | |
29 | * FIXME: Need to handle more generic streams (sockets, pipes, ...), directories, ... | |
30 | * FIXME: Will also need to update properly the set of bookmarks for streaming data | |
31 | */ | |
32 | public abstract class TmfEventStream extends RandomAccessFile implements ITmfStreamLocator { | |
33 | ||
34 | // ======================================================================== | |
35 | // Constants | |
36 | // ======================================================================== | |
37 | ||
38 | // The default number of events to cache | |
39 | public static final int DEFAULT_CACHE_SIZE = 1000; | |
40 | ||
41 | // ======================================================================== | |
42 | // Attributes | |
43 | // ======================================================================== | |
44 | ||
45 | // The file parser | |
46 | private final ITmfEventParser fParser; | |
47 | ||
48 | // The cache size | |
49 | private final int fCacheSize; | |
50 | ||
51 | // The set of event stream bookmarks (for random access) | |
52 | private Vector<TmfStreamBookmark> fBookmarks = new Vector<TmfStreamBookmark>(); | |
53 | ||
54 | // The number of events collected | |
55 | private int fNbEvents = 0; | |
56 | ||
57 | // The time span of the event stream | |
1f506a43 | 58 | private TmfTimeRange fTimeRange = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigBang); |
4ab33d2b AO |
59 | |
60 | // ======================================================================== | |
61 | // Constructors | |
62 | // ======================================================================== | |
63 | ||
64 | /** | |
65 | * @param filename | |
66 | * @param parser | |
67 | * @throws FileNotFoundException | |
68 | */ | |
69 | public TmfEventStream(String filename, ITmfEventParser parser) throws FileNotFoundException { | |
70 | this(filename, parser, DEFAULT_CACHE_SIZE); | |
71 | } | |
72 | ||
73 | /** | |
74 | * @param filename | |
75 | * @param parser | |
76 | * @param cacheSize | |
77 | * @throws FileNotFoundException | |
78 | */ | |
79 | public TmfEventStream(String filename, ITmfEventParser parser, int cacheSize) throws FileNotFoundException { | |
80 | super(filename, "r"); | |
81 | assert(parser != null); | |
82 | fParser = parser; | |
83 | fCacheSize = cacheSize; | |
84 | bookmark(); | |
85 | } | |
86 | ||
87 | // ======================================================================== | |
88 | // Accessors | |
89 | // ======================================================================== | |
90 | ||
91 | /** | |
92 | * @return | |
93 | */ | |
94 | public int getCacheSize() { | |
95 | return fCacheSize; | |
96 | } | |
97 | ||
98 | /** | |
99 | * @return | |
100 | */ | |
101 | public int getNbEvents() { | |
102 | return fNbEvents; | |
103 | } | |
104 | ||
105 | /** | |
106 | * @return | |
107 | */ | |
1f506a43 | 108 | public TmfTimeRange getTimeRange() { |
4ab33d2b AO |
109 | return fTimeRange; |
110 | } | |
111 | ||
112 | // ======================================================================== | |
113 | // Operators | |
114 | // ======================================================================== | |
115 | ||
116 | /** | |
117 | * Positions the stream at the first event with timestamp. If there is no | |
118 | * such event, positions the stream at the next event, if any. | |
119 | * | |
120 | * @param timestamp | |
121 | * @return | |
122 | * @throws IOException | |
123 | */ | |
124 | public synchronized TmfEvent seek(TmfTimestamp timestamp) throws IOException { | |
125 | ||
126 | // First, find the right bookmark | |
127 | // TODO: Check the performance of bsearch on ordered Vector<>. Should be OK but... | |
128 | int index = Collections.binarySearch(fBookmarks, new TmfStreamBookmark(timestamp, 0)); | |
129 | ||
130 | // In the very likely event that the bookmark was not found, bsearch | |
131 | // returns its negated would-be location (not an offset...). From that | |
132 | // index, we can then position the stream and locate the event. | |
133 | if (index < 0) { | |
134 | index = Math.max(0, -(index + 2)); | |
135 | } | |
136 | ||
137 | seekLocation(fBookmarks.elementAt(index).getLocation()); | |
138 | TmfEvent event; | |
139 | do { | |
140 | event = getNextEvent(); | |
141 | } while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0); | |
142 | ||
143 | return event; | |
144 | } | |
145 | ||
146 | /** | |
147 | * @return | |
148 | */ | |
149 | public synchronized TmfEvent getNextEvent() { | |
150 | try { | |
151 | return fParser.getNextEvent(this); | |
152 | } catch (IOException e) { | |
153 | e.printStackTrace(); | |
154 | } | |
155 | return null; | |
156 | } | |
157 | ||
158 | // ======================================================================== | |
159 | // Helper functions | |
160 | // ======================================================================== | |
161 | ||
162 | /** | |
163 | * Parse the file and store bookmarks at every fCacheSize event. | |
164 | * Done once at the creation of the event stream. | |
165 | * | |
166 | * TODO: Consider a Job with progress bar, etc... | |
167 | */ | |
168 | protected synchronized void bookmark() { | |
169 | try { | |
170 | seek(0); | |
171 | TmfTimestamp startTime = new TmfTimestamp(); | |
172 | TmfTimestamp lastTime = new TmfTimestamp(); | |
173 | Object location = getCurrentLocation(); | |
174 | ||
175 | TmfEvent event = getNextEvent(); | |
176 | if (event != null) { | |
177 | startTime = event.getTimestamp(); | |
178 | while (event != null) { | |
179 | lastTime = event.getTimestamp(); | |
180 | if ((fNbEvents++ % fCacheSize) == 0) { | |
181 | TmfStreamBookmark bookmark = new TmfStreamBookmark(lastTime, location); | |
182 | fBookmarks.add(bookmark); | |
183 | } | |
184 | location = getCurrentLocation(); | |
185 | event = getNextEvent(); | |
186 | } | |
1f506a43 | 187 | fTimeRange = new TmfTimeRange(startTime, lastTime); |
4ab33d2b AO |
188 | } |
189 | seek(0); | |
190 | } catch (IOException e) { | |
191 | // TODO Auto-generated catch block | |
192 | e.printStackTrace(); | |
193 | } | |
194 | } | |
195 | ||
196 | } |