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
.Collections
;
20 import java
.util
.List
;
21 import java
.util
.regex
.Matcher
;
22 import java
.util
.regex
.Pattern
;
24 import org
.eclipse
.core
.resources
.IProject
;
25 import org
.eclipse
.core
.resources
.IResource
;
26 import org
.eclipse
.core
.runtime
.IStatus
;
27 import org
.eclipse
.core
.runtime
.Status
;
28 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Activator
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.io
.BufferedRandomAccessFile
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfEventParser
;
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 TextTraceEvent
> 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 sum of weighted
78 * values of the first 100 lines of the file which match any of the provided
79 * validation patterns. For each matching line a weighted value between 1.5
80 * and 2.0 is assigned based on the group count of the matching patterns.
81 * The higher the group count, the closer the weighted value will be to 2.0.
84 public IStatus
validate(IProject project
, String path
) {
85 File file
= new File(path
);
87 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, "File not found: " + path
); //$NON-NLS-1$
90 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, "Not a file. It's a directory: " + path
); //$NON-NLS-1$
93 try (BufferedRandomAccessFile rafile
= new BufferedRandomAccessFile(path
, "r")) { //$NON-NLS-1$
96 String line
= rafile
.getNextLine();
97 List
<Pattern
> validationPatterns
= getValidationPatterns();
98 while ((line
!= null) && (lineCount
++ < MAX_LINES
)) {
99 for(Pattern pattern
: validationPatterns
) {
100 Matcher matcher
= pattern
.matcher(line
);
101 if (matcher
.matches()) {
102 int groupCount
= matcher
.groupCount();
103 matches
+= (1.0 + groupCount
/ ((double) groupCount
+ 1));
106 confidence
= (int) (MAX_CONFIDENCE
* matches
/ lineCount
);
107 line
= rafile
.getNextLine();
109 } catch (IOException e
) {
110 Activator
.logError("Error validating file: " + path
, e
); //$NON-NLS-1$
111 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, "IOException validating file: " + path
, e
); //$NON-NLS-1$
114 return new TraceValidationStatus(confidence
, Activator
.PLUGIN_ID
);
118 public void initTrace(IResource resource
, String path
, Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
119 super.initTrace(resource
, path
, type
);
121 fFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
122 } catch (IOException e
) {
123 throw new TmfTraceException(e
.getMessage(), e
);
128 public synchronized void dispose() {
133 } catch (IOException e
) {
141 public synchronized TextTraceContext
seekEvent(ITmfLocation location
) {
142 TextTraceContext context
= new TextTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
143 if (NULL_LOCATION
.equals(location
) || fFile
== null) {
147 if (location
== null) {
149 } else if (location
.getLocationInfo() instanceof Long
) {
150 fFile
.seek((Long
) location
.getLocationInfo());
152 long rawPos
= fFile
.getFilePointer();
153 String line
= fFile
.getNextLine();
154 while (line
!= null) {
155 Matcher matcher
= getFirstLinePattern().matcher(line
);
156 if (matcher
.matches()) {
157 setupContext(context
, rawPos
, line
, matcher
);
160 rawPos
= fFile
.getFilePointer();
161 line
= fFile
.getNextLine();
164 } catch (IOException e
) {
165 Activator
.logError("Error seeking file: " + getPath(), e
); //$NON-NLS-1$
170 private void setupContext(TextTraceContext context
, long rawPos
, String line
, Matcher matcher
) throws IOException
{
171 context
.setLocation(new TmfLongLocation(rawPos
));
172 context
.firstLineMatcher
= matcher
;
173 context
.firstLine
= line
;
174 context
.nextLineLocation
= fFile
.getFilePointer();
178 public synchronized TextTraceContext
seekEvent(double ratio
) {
180 return new TextTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
183 long pos
= (long) (ratio
* fFile
.length());
186 if (fFile
.read() == '\n') {
191 ITmfLocation location
= new TmfLongLocation(Long
.valueOf(pos
));
192 TextTraceContext context
= seekEvent(location
);
193 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
195 } catch (IOException e
) {
196 Activator
.logError("Error seeking file: " + getPath(), e
); //$NON-NLS-1$
197 return new TextTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
202 public double getLocationRatio(ITmfLocation location
) {
207 long length
= fFile
.length();
211 if (location
.getLocationInfo() instanceof Long
) {
212 return (double) ((Long
) location
.getLocationInfo()) / length
;
214 } catch (IOException e
) {
215 Activator
.logError("Error reading file: " + getPath(), e
); //$NON-NLS-1$
221 public ITmfLocation
getCurrentLocation() {
226 public TextTraceEvent
parseEvent(ITmfContext tmfContext
) {
227 TextTraceContext context
= seekEvent(tmfContext
.getLocation());
228 return parse(context
);
232 public synchronized T
getNext(ITmfContext context
) {
233 if (!(context
instanceof TextTraceContext
)) {
234 throw new IllegalArgumentException();
236 TextTraceContext savedContext
= new TextTraceContext(context
.getLocation(), context
.getRank());
237 T event
= parse((TextTraceContext
) context
);
239 updateAttributes(savedContext
, event
.getTimestamp());
240 context
.increaseRank();
246 * Parse the next event. The context is advanced.
250 * @return the next event or null
252 protected synchronized T
parse(TextTraceContext tmfContext
) {
256 TextTraceContext context
= tmfContext
;
257 if (context
.getLocation() == null || !(context
.getLocation().getLocationInfo() instanceof Long
) || NULL_LOCATION
.equals(context
.getLocation())) {
261 T event
= parseFirstLine(context
.firstLineMatcher
, context
.firstLine
);
264 if (fFile
.getFilePointer() != context
.nextLineLocation
) {
265 fFile
.seek(context
.nextLineLocation
);
267 long rawPos
= fFile
.getFilePointer();
268 String line
= fFile
.getNextLine();
269 while (line
!= null) {
270 Matcher matcher
= getFirstLinePattern().matcher(line
);
271 if (matcher
.matches()) {
272 setupContext(context
, rawPos
, line
, matcher
);
275 parseNextLine(event
, line
);
276 rawPos
= fFile
.getFilePointer();
277 line
= fFile
.getNextLine();
279 } catch (IOException e
) {
280 Activator
.logError("Error reading file: " + getPath(), e
); //$NON-NLS-1$
283 context
.setLocation(NULL_LOCATION
);
288 * Gets the first line pattern.
290 * @return The first line pattern
292 protected abstract Pattern
getFirstLinePattern();
295 * Parses the first line data and returns a new event. When constructing the
296 * event, the concrete trace should use the trace's timestamp transform to
297 * create the timestamp, by either transforming the parsed time value
298 * directly or by using the method {@link #createTimestamp(long)}.
304 * @return The parsed event
306 protected abstract T
parseFirstLine(Matcher matcher
, String line
);
309 * Parses the next line data for the current event.
312 * The current event being parsed
316 protected abstract void parseNextLine(T event
, String line
);
319 * Returns a ordered list of validation patterns that will be used in
320 * the default {@link #validate(IProject, String)} method to match
321 * the first 100 to compute the confidence level
323 * @return collection of patterns to validate against
326 protected List
<Pattern
> getValidationPatterns() {
327 return Collections
.singletonList(getFirstLinePattern());
330 // ------------------------------------------------------------------------
332 // ------------------------------------------------------------------------
335 * Strip quotes surrounding a string
339 * @return The string without quotes
341 protected static String
replaceQuotes(String input
) {
342 String out
= input
.replaceAll("^\"|(\"\\s*)$", ""); //$NON-NLS-1$//$NON-NLS-2$
347 * Strip brackets surrounding a string
351 * @return The string without brackets
353 protected static String
replaceBrackets(String input
) {
354 String out
= input
.replaceAll("^\\{|(\\}\\s*)$", ""); //$NON-NLS-1$//$NON-NLS-2$
358 private static int fCheckpointSize
= -1;
361 public synchronized int getCheckpointSize() {
362 if (fCheckpointSize
== -1) {
363 TmfCheckpoint c
= new TmfCheckpoint(TmfTimestamp
.ZERO
, new TmfLongLocation(0L), 0);
364 ByteBuffer b
= ByteBuffer
.allocate(ITmfCheckpoint
.MAX_SERIALIZE_SIZE
);
367 fCheckpointSize
= b
.position();
370 return fCheckpointSize
;
374 protected ITmfTraceIndexer
createIndexer(int interval
) {
375 return new TmfBTreeTraceIndexer(this, interval
);
379 public ITmfLocation
restoreLocation(ByteBuffer bufferIn
) {
380 return new TmfLongLocation(bufferIn
);