1 /*******************************************************************************
2 * Copyright (c) 2010, 2013 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 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.parsers
.custom
;
16 import java
.io
.FileNotFoundException
;
17 import java
.io
.IOException
;
18 import java
.nio
.ByteBuffer
;
19 import java
.util
.HashMap
;
20 import java
.util
.Iterator
;
21 import java
.util
.List
;
22 import java
.util
.Map
.Entry
;
23 import java
.util
.regex
.Matcher
;
25 import org
.eclipse
.core
.resources
.IProject
;
26 import org
.eclipse
.core
.resources
.IResource
;
27 import org
.eclipse
.core
.runtime
.IStatus
;
28 import org
.eclipse
.core
.runtime
.Status
;
29 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Activator
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.io
.BufferedRandomAccessFile
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.parsers
.custom
.CustomTxtTraceDefinition
.InputLine
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfEventParser
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.ITmfPersistentlyIndexable
;
40 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
41 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.TmfBTreeTraceIndexer
;
42 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.checkpoint
.ITmfCheckpoint
;
43 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpoint
;
44 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.location
.ITmfLocation
;
45 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.location
.TmfLongLocation
;
48 * Base class for custom plain text traces.
50 * @author Patrick Tassé
53 public class CustomTxtTrace
extends TmfTrace
implements ITmfEventParser
, ITmfPersistentlyIndexable
{
55 private static final TmfLongLocation NULL_LOCATION
= new TmfLongLocation((Long
) null);
56 private static final int DEFAULT_CACHE_SIZE
= 100;
58 private final CustomTxtTraceDefinition fDefinition
;
59 private final CustomTxtEventType fEventType
;
60 private BufferedRandomAccessFile fFile
;
66 * Text trace definition
68 public CustomTxtTrace(final CustomTxtTraceDefinition definition
) {
69 fDefinition
= definition
;
70 fEventType
= new CustomTxtEventType(fDefinition
);
71 setCacheSize(DEFAULT_CACHE_SIZE
);
80 * Text trace definition
82 * Path to the trace file
85 * @throws TmfTraceException
86 * If we couldn't open the trace at 'path'
88 public CustomTxtTrace(final IResource resource
,
89 final CustomTxtTraceDefinition definition
, final String path
,
90 final int cacheSize
) throws TmfTraceException
{
92 setCacheSize((cacheSize
> 0) ? cacheSize
: DEFAULT_CACHE_SIZE
);
93 initTrace(resource
, path
, CustomTxtEvent
.class);
97 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> eventType
) throws TmfTraceException
{
98 super.initTrace(resource
, path
, eventType
);
100 fFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
101 } catch (IOException e
) {
102 throw new TmfTraceException(e
.getMessage(), e
);
107 public synchronized void dispose() {
112 } catch (IOException e
) {
120 public ITmfTraceIndexer
getIndexer() {
121 return super.getIndexer();
125 public synchronized TmfContext
seekEvent(final ITmfLocation location
) {
126 final CustomTxtTraceContext context
= new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
127 if (NULL_LOCATION
.equals(location
) || fFile
== null) {
131 if (location
== null) {
133 } else if (location
.getLocationInfo() instanceof Long
) {
134 fFile
.seek((Long
) location
.getLocationInfo());
137 long rawPos
= fFile
.getFilePointer();
138 while ((line
= fFile
.getNextLine()) != null) {
139 for (final InputLine input
: getFirstLines()) {
140 final Matcher matcher
= input
.getPattern().matcher(line
);
141 if (matcher
.find()) {
142 context
.setLocation(new TmfLongLocation(rawPos
));
143 context
.firstLineMatcher
= matcher
;
144 context
.firstLine
= line
;
145 context
.nextLineLocation
= fFile
.getFilePointer();
146 context
.inputLine
= input
;
150 rawPos
= fFile
.getFilePointer();
153 } catch (final FileNotFoundException e
) {
154 Activator
.logError("Error seeking event. File not found: " + getPath(), e
); //$NON-NLS-1$
156 } catch (final IOException e
) {
157 Activator
.logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
164 public synchronized TmfContext
seekEvent(final double ratio
) {
166 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
169 long pos
= Math
.round(ratio
* fFile
.length());
172 if (fFile
.read() == '\n') {
177 final ITmfLocation location
= new TmfLongLocation(pos
);
178 final TmfContext context
= seekEvent(location
);
179 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
181 } catch (final IOException e
) {
182 Activator
.logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
183 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
188 public synchronized double getLocationRatio(final ITmfLocation location
) {
193 if (location
.getLocationInfo() instanceof Long
) {
194 return (double) ((Long
) location
.getLocationInfo()) / fFile
.length();
196 } catch (final IOException e
) {
197 Activator
.logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
203 public ITmfLocation
getCurrentLocation() {
204 // TODO Auto-generated method stub
209 public synchronized CustomTxtEvent
parseEvent(final ITmfContext tmfContext
) {
210 ITmfContext context
= seekEvent(tmfContext
.getLocation());
211 return parse(context
);
215 public synchronized CustomTxtEvent
getNext(final ITmfContext context
) {
216 final ITmfContext savedContext
= new TmfContext(context
.getLocation(), context
.getRank());
217 final CustomTxtEvent event
= parse(context
);
219 updateAttributes(savedContext
, event
.getTimestamp());
220 context
.increaseRank();
225 private synchronized CustomTxtEvent
parse(final ITmfContext tmfContext
) {
229 if (!(tmfContext
instanceof CustomTxtTraceContext
)) {
233 final CustomTxtTraceContext context
= (CustomTxtTraceContext
) tmfContext
;
234 if (context
.getLocation() == null || !(context
.getLocation().getLocationInfo() instanceof Long
) || NULL_LOCATION
.equals(context
.getLocation())) {
238 CustomTxtEvent event
= parseFirstLine(context
);
240 final HashMap
<InputLine
, Integer
> countMap
= new HashMap
<>();
241 InputLine currentInput
= null;
242 if (context
.inputLine
.childrenInputs
!= null && context
.inputLine
.childrenInputs
.size() > 0) {
243 currentInput
= context
.inputLine
.childrenInputs
.get(0);
244 countMap
.put(currentInput
, 0);
248 if (fFile
.getFilePointer() != context
.nextLineLocation
) {
249 fFile
.seek(context
.nextLineLocation
);
252 long rawPos
= fFile
.getFilePointer();
253 while ((line
= fFile
.getNextLine()) != null) {
254 boolean processed
= false;
255 if (currentInput
== null) {
256 for (final InputLine input
: getFirstLines()) {
257 final Matcher matcher
= input
.getPattern().matcher(line
);
258 if (matcher
.find()) {
259 context
.setLocation(new TmfLongLocation(rawPos
));
260 context
.firstLineMatcher
= matcher
;
261 context
.firstLine
= line
;
262 context
.nextLineLocation
= fFile
.getFilePointer();
263 context
.inputLine
= input
;
268 if (countMap
.get(currentInput
) >= currentInput
.getMinCount()) {
269 final List
<InputLine
> nextInputs
= currentInput
.getNextInputs(countMap
);
270 if (nextInputs
.size() == 0 || nextInputs
.get(nextInputs
.size() - 1).getMinCount() == 0) {
271 for (final InputLine input
: getFirstLines()) {
272 final Matcher matcher
= input
.getPattern().matcher(line
);
273 if (matcher
.find()) {
274 context
.setLocation(new TmfLongLocation(rawPos
));
275 context
.firstLineMatcher
= matcher
;
276 context
.firstLine
= line
;
277 context
.nextLineLocation
= fFile
.getFilePointer();
278 context
.inputLine
= input
;
283 for (final InputLine input
: nextInputs
) {
284 final Matcher matcher
= input
.getPattern().matcher(line
);
285 if (matcher
.find()) {
286 event
.processGroups(input
, matcher
);
287 currentInput
= input
;
288 if (countMap
.get(currentInput
) == null) {
289 countMap
.put(currentInput
, 1);
291 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
293 Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
294 while (iter
.hasNext()) {
295 final InputLine inputLine
= iter
.next();
296 if (inputLine
.level
> currentInput
.level
) {
300 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
301 currentInput
= currentInput
.childrenInputs
.get(0);
302 countMap
.put(currentInput
, 0);
303 } else if (countMap
.get(currentInput
) >= currentInput
.getMaxCount()) {
304 if (currentInput
.getNextInputs(countMap
).size() > 0) {
305 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
306 if (countMap
.get(currentInput
) == null) {
307 countMap
.put(currentInput
, 0);
309 iter
= countMap
.keySet().iterator();
310 while (iter
.hasNext()) {
311 final InputLine inputLine
= iter
.next();
312 if (inputLine
.level
> currentInput
.level
) {
325 if (!processed
&& currentInput
!= null) {
326 final Matcher matcher
= currentInput
.getPattern().matcher(line
);
327 if (matcher
.find()) {
328 event
.processGroups(currentInput
, matcher
);
329 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
330 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
331 currentInput
= currentInput
.childrenInputs
.get(0);
332 countMap
.put(currentInput
, 0);
333 } else if (countMap
.get(currentInput
) >= currentInput
.getMaxCount()) {
334 if (currentInput
.getNextInputs(countMap
).size() > 0) {
335 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
336 if (countMap
.get(currentInput
) == null) {
337 countMap
.put(currentInput
, 0);
339 final Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
340 while (iter
.hasNext()) {
341 final InputLine inputLine
= iter
.next();
342 if (inputLine
.level
> currentInput
.level
) {
351 ((StringBuffer
) event
.getContent().getValue()).append("\n").append(line
); //$NON-NLS-1$
354 rawPos
= fFile
.getFilePointer();
356 } catch (final IOException e
) {
357 Activator
.logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
359 for (final Entry
<InputLine
, Integer
> entry
: countMap
.entrySet()) {
360 if (entry
.getValue() < entry
.getKey().getMinCount()) {
364 context
.setLocation(NULL_LOCATION
);
369 * @return The first few lines of the text file
371 public List
<InputLine
> getFirstLines() {
372 return fDefinition
.inputs
;
376 * Parse the first line of the trace (to recognize the type).
380 * @return The first event
382 public CustomTxtEvent
parseFirstLine(final CustomTxtTraceContext context
) {
383 final CustomTxtEvent event
= new CustomTxtEvent(fDefinition
, this, TmfTimestamp
.ZERO
, "", fEventType
, ""); //$NON-NLS-1$ //$NON-NLS-2$
384 event
.processGroups(context
.inputLine
, context
.firstLineMatcher
);
385 event
.setContent(new CustomEventContent(event
, new StringBuffer(context
.firstLine
)));
390 * Get the trace definition.
392 * @return The trace definition
394 public CustomTraceDefinition
getDefinition() {
399 public IStatus
validate(IProject project
, String path
) {
400 File file
= new File(path
);
401 if (file
.exists() && file
.isFile() && file
.canRead() && file
.length() > 0) {
402 return Status
.OK_STATUS
;
404 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CustomTrace_FileNotFound
+ ": " + path
); //$NON-NLS-1$
407 private static int fCheckpointSize
= -1;
410 public synchronized int getCheckpointSize() {
411 if (fCheckpointSize
== -1) {
412 TmfCheckpoint c
= new TmfCheckpoint(TmfTimestamp
.ZERO
, new TmfLongLocation(0L), 0);
413 ByteBuffer b
= ByteBuffer
.allocate(ITmfCheckpoint
.MAX_SERIALIZE_SIZE
);
416 fCheckpointSize
= b
.position();
419 return fCheckpointSize
;
423 public ITmfLocation
restoreLocation(ByteBuffer bufferIn
) {
424 return new TmfLongLocation(bufferIn
);
428 protected ITmfTraceIndexer
createIndexer(int interval
) {
429 return new TmfBTreeTraceIndexer(this, interval
);