1 /*******************************************************************************
2 * Copyright (c) 2009 Ericsson
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
10 * Francois Chouinard (fchouinard@gmail.com) - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.eventlog
;
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
;
21 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEvent
;
22 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimeRange
;
23 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimestamp
;
26 * <b><u>TmfEventStream</u></b>
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
32 public abstract class TmfEventStream
extends RandomAccessFile
implements ITmfStreamLocator
{
34 // ========================================================================
36 // ========================================================================
38 // The default number of events to cache
39 public static final int DEFAULT_CACHE_SIZE
= 1000;
41 // ========================================================================
43 // ========================================================================
46 private final ITmfEventParser fParser
;
49 private final int fCacheSize
;
51 // The set of event stream bookmarks (for random access)
52 private Vector
<TmfStreamBookmark
> fBookmarks
= new Vector
<TmfStreamBookmark
>();
54 // The number of events collected
55 private int fNbEvents
= 0;
57 // The time span of the event stream
58 private TmfTimeRange fTimeRange
= new TmfTimeRange(TmfTimestamp
.BigBang
, TmfTimestamp
.BigBang
);
60 // ========================================================================
62 // ========================================================================
67 * @throws FileNotFoundException
69 public TmfEventStream(String filename
, ITmfEventParser parser
) throws FileNotFoundException
{
70 this(filename
, parser
, DEFAULT_CACHE_SIZE
);
77 * @throws FileNotFoundException
79 public TmfEventStream(String filename
, ITmfEventParser parser
, int cacheSize
) throws FileNotFoundException
{
81 assert(parser
!= null);
83 fCacheSize
= cacheSize
;
87 // ========================================================================
89 // ========================================================================
94 public int getCacheSize() {
101 public int getNbEvents() {
108 public TmfTimeRange
getTimeRange() {
112 // ========================================================================
114 // ========================================================================
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.
122 * @throws IOException
124 public synchronized TmfEvent
seek(TmfTimestamp timestamp
) throws IOException
{
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));
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.
134 index
= Math
.max(0, -(index
+ 2));
137 seekLocation(fBookmarks
.elementAt(index
).getLocation());
140 event
= getNextEvent();
141 } while (event
!= null && event
.getTimestamp().compareTo(timestamp
, false) < 0);
149 public synchronized TmfEvent
getNextEvent() {
151 return fParser
.getNextEvent(this);
152 } catch (IOException e
) {
158 // ========================================================================
160 // ========================================================================
163 * Parse the file and store bookmarks at every fCacheSize event.
164 * Done once at the creation of the event stream.
166 * TODO: Consider a Job with progress bar, etc...
168 protected synchronized void bookmark() {
171 TmfTimestamp startTime
= new TmfTimestamp();
172 TmfTimestamp lastTime
= new TmfTimestamp();
173 Object location
= getCurrentLocation();
175 TmfEvent event
= getNextEvent();
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
);
184 location
= getCurrentLocation();
185 event
= getNextEvent();
187 fTimeRange
= new TmfTimeRange(startTime
, lastTime
);
190 } catch (IOException e
) {
191 // TODO Auto-generated catch block