1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 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 * Patrick Tasse - Initial API and implementation
11 * Marc-Andre Laperle - Add persistent index support
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.core
.trace
.text
;
17 import java
.io
.IOException
;
18 import java
.nio
.ByteBuffer
;
19 import java
.util
.regex
.Matcher
;
20 import java
.util
.regex
.Pattern
;
22 import org
.eclipse
.core
.resources
.IProject
;
23 import org
.eclipse
.core
.resources
.IResource
;
24 import org
.eclipse
.core
.runtime
.IStatus
;
25 import org
.eclipse
.core
.runtime
.Status
;
26 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Activator
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.io
.BufferedRandomAccessFile
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfEventParser
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TraceValidationStatus
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.ITmfPersistentlyIndexable
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.TmfBTreeTraceIndexer
;
40 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.checkpoint
.ITmfCheckpoint
;
41 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpoint
;
42 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.location
.ITmfLocation
;
43 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.location
.TmfLongLocation
;
46 * Extension of TmfTrace for handling of line-based text traces parsed using
47 * regular expressions. Each line that matches the first line pattern indicates
48 * the start of a new event. The subsequent lines can contain additional
49 * information that is added to the current event.
52 * TmfEvent class returned by this trace
56 public abstract class TextTrace
<T
extends TmfEvent
> extends TmfTrace
implements ITmfEventParser
, ITmfPersistentlyIndexable
{
58 private static final TmfLongLocation NULL_LOCATION
= new TmfLongLocation(-1L);
59 private static final int MAX_LINES
= 100;
60 private static final int MAX_CONFIDENCE
= 100;
62 /** The default separator used for multi-line fields */
63 protected static final String SEPARATOR
= " | "; //$NON-NLS-1$
66 protected BufferedRandomAccessFile fFile
;
77 * The default implementation computes the confidence as the percentage of
78 * lines in the first 100 lines of the file which match the first line
82 public IStatus
validate(IProject project
, String path
) {
83 File file
= new File(path
);
85 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, "File not found: " + path
); //$NON-NLS-1$
88 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, "Not a file. It's a directory: " + path
); //$NON-NLS-1$
91 try (BufferedRandomAccessFile rafile
= new BufferedRandomAccessFile(path
, "r")) { //$NON-NLS-1$
94 String line
= rafile
.getNextLine();
95 while ((line
!= null) && (lineCount
++ < MAX_LINES
)) {
96 Matcher matcher
= getFirstLinePattern().matcher(line
);
97 if (matcher
.matches()) {
100 confidence
= MAX_CONFIDENCE
* matches
/ lineCount
;
101 line
= rafile
.getNextLine();
103 } catch (IOException e
) {
104 Activator
.logError("Error validating file: " + path
, e
); //$NON-NLS-1$
105 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, "IOException validating file: " + path
, e
); //$NON-NLS-1$
108 return new TraceValidationStatus(confidence
, Activator
.PLUGIN_ID
);
113 public void initTrace(IResource resource
, String path
, Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
114 super.initTrace(resource
, path
, type
);
116 fFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
117 } catch (IOException e
) {
118 throw new TmfTraceException(e
.getMessage(), e
);
123 public synchronized void dispose() {
128 } catch (IOException e
) {
136 public synchronized TmfContext
seekEvent(ITmfLocation location
) {
137 TextTraceContext context
= new TextTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
138 if (NULL_LOCATION
.equals(location
) || fFile
== null) {
142 if (location
== null) {
144 } else if (location
.getLocationInfo() instanceof Long
) {
145 fFile
.seek((Long
) location
.getLocationInfo());
147 long rawPos
= fFile
.getFilePointer();
148 String line
= fFile
.getNextLine();
149 while (line
!= null) {
150 Matcher matcher
= getFirstLinePattern().matcher(line
);
151 if (matcher
.matches()) {
152 setupContext(context
, rawPos
, line
, matcher
);
155 rawPos
= fFile
.getFilePointer();
156 line
= fFile
.getNextLine();
159 } catch (IOException e
) {
160 Activator
.logError("Error seeking file: " + getPath(), e
); //$NON-NLS-1$
165 private void setupContext(TextTraceContext context
, long rawPos
, String line
, Matcher matcher
) throws IOException
{
166 context
.setLocation(new TmfLongLocation(rawPos
));
167 context
.firstLineMatcher
= matcher
;
168 context
.firstLine
= line
;
169 context
.nextLineLocation
= fFile
.getFilePointer();
173 public synchronized TmfContext
seekEvent(double ratio
) {
175 return new TextTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
178 long pos
= (long) (ratio
* fFile
.length());
181 if (fFile
.read() == '\n') {
186 ITmfLocation location
= new TmfLongLocation(Long
.valueOf(pos
));
187 TmfContext context
= seekEvent(location
);
188 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
190 } catch (IOException e
) {
191 Activator
.logError("Error seeking file: " + getPath(), e
); //$NON-NLS-1$
192 return new TextTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
197 public double getLocationRatio(ITmfLocation location
) {
202 long length
= fFile
.length();
206 if (location
.getLocationInfo() instanceof Long
) {
207 return (double) ((Long
) location
.getLocationInfo()) / length
;
209 } catch (IOException e
) {
210 Activator
.logError("Error reading file: " + getPath(), e
); //$NON-NLS-1$
216 public ITmfLocation
getCurrentLocation() {
221 public ITmfEvent
parseEvent(ITmfContext tmfContext
) {
222 ITmfContext context
= seekEvent(tmfContext
.getLocation());
223 return parse(context
);
227 public synchronized T
getNext(ITmfContext context
) {
228 ITmfContext savedContext
= new TmfContext(context
.getLocation(), context
.getRank());
229 T event
= parse(context
);
231 updateAttributes(savedContext
, event
.getTimestamp());
232 context
.increaseRank();
238 * Parse the next event. The context is advanced.
242 * @return the next event or null
244 protected synchronized T
parse(ITmfContext tmfContext
) {
248 if (!(tmfContext
instanceof TextTraceContext
)) {
251 TextTraceContext context
= (TextTraceContext
) tmfContext
;
252 if (context
.getLocation() == null || !(context
.getLocation().getLocationInfo() instanceof Long
) || NULL_LOCATION
.equals(context
.getLocation())) {
256 T event
= parseFirstLine(context
.firstLineMatcher
, context
.firstLine
);
259 if (fFile
.getFilePointer() != context
.nextLineLocation
) {
260 fFile
.seek(context
.nextLineLocation
);
262 long rawPos
= fFile
.getFilePointer();
263 String line
= fFile
.getNextLine();
264 while (line
!= null) {
265 Matcher matcher
= getFirstLinePattern().matcher(line
);
266 if (matcher
.matches()) {
267 setupContext(context
, rawPos
, line
, matcher
);
270 parseNextLine(event
, line
);
271 rawPos
= fFile
.getFilePointer();
272 line
= fFile
.getNextLine();
274 } catch (IOException e
) {
275 Activator
.logError("Error reading file: " + getPath(), e
); //$NON-NLS-1$
278 context
.setLocation(NULL_LOCATION
);
283 * Gets the first line pattern.
285 * @return The first line pattern
287 protected abstract Pattern
getFirstLinePattern();
290 * Parses the first line data and returns a new event.
296 * @return The parsed event
298 protected abstract T
parseFirstLine(Matcher matcher
, String line
);
301 * Parses the next line data for the current event.
304 * The current event being parsed
308 protected abstract void parseNextLine(T event
, String line
);
310 // ------------------------------------------------------------------------
312 // ------------------------------------------------------------------------
315 * Strip quotes surrounding a string
319 * @return The string without quotes
321 protected static String
replaceQuotes(String input
) {
322 String out
= input
.replaceAll("^\"|(\"\\s*)$", ""); //$NON-NLS-1$//$NON-NLS-2$
327 * Strip brackets surrounding a string
331 * @return The string without brackets
333 protected static String
replaceBrackets(String input
) {
334 String out
= input
.replaceAll("^\\{|(\\}\\s*)$", ""); //$NON-NLS-1$//$NON-NLS-2$
338 private static int fCheckpointSize
= -1;
341 public synchronized int getCheckpointSize() {
342 if (fCheckpointSize
== -1) {
343 TmfCheckpoint c
= new TmfCheckpoint(TmfTimestamp
.ZERO
, new TmfLongLocation(0L), 0);
344 ByteBuffer b
= ByteBuffer
.allocate(ITmfCheckpoint
.MAX_SERIALIZE_SIZE
);
347 fCheckpointSize
= b
.position();
350 return fCheckpointSize
;
354 protected ITmfTraceIndexer
createIndexer(int interval
) {
355 return new TmfBTreeTraceIndexer(this, interval
);
359 public ITmfLocation
restoreLocation(ByteBuffer bufferIn
) {
360 return new TmfLongLocation(bufferIn
);