Refactor TmfTrace and dependencies - finalize ITmfTraceIndexer
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / parsers / custom / CustomTxtTrace.java
1 /*******************************************************************************
2 * Copyright (c) 2010 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 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.internal.tmf.ui.parsers.custom;
14
15 import java.io.File;
16 import java.io.FileNotFoundException;
17 import java.io.IOException;
18 import java.util.HashMap;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.Map.Entry;
22 import java.util.regex.Matcher;
23
24 import org.eclipse.core.resources.IResource;
25 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTraceDefinition.InputLine;
26 import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
27 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
28 import org.eclipse.linuxtools.tmf.core.io.BufferedRandomAccessFile;
29 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
30 import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
31 import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
32 import org.eclipse.linuxtools.tmf.core.trace.TmfLocation;
33 import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
34
35 public class CustomTxtTrace extends TmfTrace<CustomTxtEvent> {
36
37 private static final TmfLocation<Long> NULL_LOCATION = new TmfLocation<Long>((Long) null);
38 private static final int DEFAULT_CACHE_SIZE = 100;
39
40 private final CustomTxtTraceDefinition fDefinition;
41 private final CustomTxtEventType fEventType;
42
43 public CustomTxtTrace(final CustomTxtTraceDefinition definition) {
44 fDefinition = definition;
45 fEventType = new CustomTxtEventType(fDefinition);
46 }
47
48 public CustomTxtTrace(final IResource resource, final CustomTxtTraceDefinition definition, final String path, final int pageSize) throws FileNotFoundException {
49 super(resource, CustomTxtEvent.class, path, (pageSize > 0) ? pageSize : DEFAULT_CACHE_SIZE);
50 fDefinition = definition;
51 fEventType = new CustomTxtEventType(fDefinition);
52 }
53
54 @Override
55 public void initTrace(final IResource resource, final String path, final Class<CustomTxtEvent> eventType) throws FileNotFoundException {
56 super.initTrace(resource, path, eventType);
57 }
58
59 @Override
60 public TmfContext seekLocation(final ITmfLocation<?> location) {
61 final CustomTxtTraceContext context = new CustomTxtTraceContext(NULL_LOCATION, ITmfContext.INITIAL_RANK);
62 if (NULL_LOCATION.equals(location) || !new File(getPath()).isFile())
63 return context;
64 BufferedRandomAccessFile raFile = null;
65 try {
66 raFile = new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
67 if (location != null && location.getLocation() instanceof Long) {
68 raFile.seek((Long)location.getLocation());
69 }
70 String line;
71 long rawPos = raFile.getFilePointer();
72 while ((line = raFile.getNextLine()) != null) {
73 for (final InputLine input : getFirstLines()) {
74 final Matcher matcher = input.getPattern().matcher(line);
75 if (matcher.find()) {
76 context.setLocation(new TmfLocation<Long>(rawPos));
77 context.raFile = raFile;
78 context.firstLineMatcher = matcher;
79 context.firstLine = line;
80 context.nextLineLocation = raFile.getFilePointer();
81 context.inputLine = input;
82 return context;
83 }
84 }
85 rawPos = raFile.getFilePointer();
86 }
87 return context;
88 } catch (final FileNotFoundException e) {
89 e.printStackTrace();
90 return context;
91 } catch (final IOException e) {
92 e.printStackTrace();
93 return context;
94 } finally {
95 if (raFile != null) {
96 try {
97 raFile.close();
98 } catch (final IOException e) {
99 }
100 }
101 }
102
103 }
104
105 @Override
106 public TmfContext seekLocation(final double ratio) {
107 BufferedRandomAccessFile raFile = null;
108 try {
109 raFile = new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
110 long pos = (long) (ratio * raFile.length());
111 while (pos > 0) {
112 raFile.seek(pos - 1);
113 if (raFile.read() == '\n') {
114 break;
115 }
116 pos--;
117 }
118 final ITmfLocation<?> location = new TmfLocation<Long>(pos);
119 final TmfContext context = seekLocation(location);
120 context.setRank(ITmfContext.UNKNOWN_RANK);
121 return context;
122 } catch (final FileNotFoundException e) {
123 e.printStackTrace();
124 return new CustomTxtTraceContext(NULL_LOCATION, ITmfContext.INITIAL_RANK);
125 } catch (final IOException e) {
126 e.printStackTrace();
127 return new CustomTxtTraceContext(NULL_LOCATION, ITmfContext.INITIAL_RANK);
128 } finally {
129 if (raFile != null) {
130 try {
131 raFile.close();
132 } catch (final IOException e) {
133 }
134 }
135 }
136 }
137
138 @Override
139 public double getLocationRatio(final ITmfLocation<?> location) {
140 BufferedRandomAccessFile raFile = null;
141 try {
142 if (location.getLocation() instanceof Long) {
143 raFile = new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
144 return (double) ((Long) location.getLocation()) / raFile.length();
145 }
146 } catch (final FileNotFoundException e) {
147 e.printStackTrace();
148 } catch (final IOException e) {
149 e.printStackTrace();
150 } finally {
151 if (raFile != null) {
152 try {
153 raFile.close();
154 } catch (final IOException e) {
155 }
156 }
157 }
158 return 0;
159 }
160
161 @Override
162 public ITmfLocation<?> getCurrentLocation() {
163 // TODO Auto-generated method stub
164 return null;
165 }
166
167 @Override
168 public synchronized TmfEvent getNextEvent(final ITmfContext context) {
169 final ITmfContext savedContext = context.clone();
170 final TmfEvent event = parseEvent(context);
171 if (event != null) {
172 updateAttributes(savedContext, event.getTimestamp());
173 context.increaseRank();
174 }
175 return event;
176 }
177
178 @Override
179 public TmfEvent parseEvent(final ITmfContext tmfContext) {
180 if (!(tmfContext instanceof CustomTxtTraceContext))
181 return null;
182
183 final CustomTxtTraceContext context = (CustomTxtTraceContext) tmfContext;
184 if (!(context.getLocation().getLocation() instanceof Long) || NULL_LOCATION.equals(context.getLocation()))
185 return null;
186
187 CustomTxtEvent event = parseFirstLine(context);
188
189 final HashMap<InputLine, Integer> countMap = new HashMap<InputLine, Integer>();
190 InputLine currentInput = null;
191 if (context.inputLine.childrenInputs != null && context.inputLine.childrenInputs.size() > 0) {
192 currentInput = context.inputLine.childrenInputs.get(0);
193 countMap.put(currentInput, 0);
194 }
195
196 synchronized (context.raFile) {
197 try {
198 if (context.raFile.getFilePointer() != context.nextLineLocation) {
199 context.raFile.seek(context.nextLineLocation);
200 }
201 String line;
202 long rawPos = context.raFile.getFilePointer();
203 while ((line = context.raFile.getNextLine()) != null) {
204 boolean processed = false;
205 if (currentInput == null) {
206 for (final InputLine input : getFirstLines()) {
207 final Matcher matcher = input.getPattern().matcher(line);
208 if (matcher.find()) {
209 context.setLocation(new TmfLocation<Long>(rawPos));
210 context.firstLineMatcher = matcher;
211 context.firstLine = line;
212 context.nextLineLocation = context.raFile.getFilePointer();
213 context.inputLine = input;
214 return event;
215 }
216 }
217 } else {
218 if (countMap.get(currentInput) >= currentInput.getMinCount()) {
219 final List<InputLine> nextInputs = currentInput.getNextInputs(countMap);
220 if (nextInputs.size() == 0 || nextInputs.get(nextInputs.size() - 1).getMinCount() == 0) {
221 for (final InputLine input : getFirstLines()) {
222 final Matcher matcher = input.getPattern().matcher(line);
223 if (matcher.find()) {
224 context.setLocation(new TmfLocation<Long>(rawPos));
225 context.firstLineMatcher = matcher;
226 context.firstLine = line;
227 context.nextLineLocation = context.raFile.getFilePointer();
228 context.inputLine = input;
229 return event;
230 }
231 }
232 }
233 for (final InputLine input : nextInputs) {
234 final Matcher matcher = input.getPattern().matcher(line);
235 if (matcher.find()) {
236 event.processGroups(input, matcher);
237 currentInput = input;
238 if (countMap.get(currentInput) == null) {
239 countMap.put(currentInput, 1);
240 } else {
241 countMap.put(currentInput, countMap.get(currentInput) + 1);
242 }
243 Iterator<InputLine> iter = countMap.keySet().iterator();
244 while (iter.hasNext()) {
245 final InputLine inputLine = iter.next();
246 if (inputLine.level > currentInput.level) {
247 iter.remove();
248 }
249 }
250 if (currentInput.childrenInputs != null && currentInput.childrenInputs.size() > 0) {
251 currentInput = currentInput.childrenInputs.get(0);
252 countMap.put(currentInput, 0);
253 } else if (countMap.get(currentInput) >= currentInput.getMaxCount())
254 if (currentInput.getNextInputs(countMap).size() > 0) {
255 currentInput = currentInput.getNextInputs(countMap).get(0);
256 if (countMap.get(currentInput) == null) {
257 countMap.put(currentInput, 0);
258 }
259 iter = countMap.keySet().iterator();
260 while (iter.hasNext()) {
261 final InputLine inputLine = iter.next();
262 if (inputLine.level > currentInput.level) {
263 iter.remove();
264 }
265 }
266 } else {
267 currentInput = null;
268 }
269 processed = true;
270 break;
271 }
272 }
273 }
274 if (! processed) {
275 final Matcher matcher = currentInput.getPattern().matcher(line);
276 if (matcher.find()) {
277 event.processGroups(currentInput, matcher);
278 countMap.put(currentInput, countMap.get(currentInput) + 1);
279 if (currentInput.childrenInputs != null && currentInput.childrenInputs.size() > 0) {
280 currentInput = currentInput.childrenInputs.get(0);
281 countMap.put(currentInput, 0);
282 } else if (countMap.get(currentInput) >= currentInput.getMaxCount())
283 if (currentInput.getNextInputs(countMap).size() > 0) {
284 currentInput = currentInput.getNextInputs(countMap).get(0);
285 if (countMap.get(currentInput) == null) {
286 countMap.put(currentInput, 0);
287 }
288 final Iterator<InputLine> iter = countMap.keySet().iterator();
289 while (iter.hasNext()) {
290 final InputLine inputLine = iter.next();
291 if (inputLine.level > currentInput.level) {
292 iter.remove();
293 }
294 }
295 } else {
296 currentInput = null;
297 }
298 }
299 ((StringBuffer) event.getContent().getValue()).append("\n").append(line); //$NON-NLS-1$
300 }
301 }
302 rawPos = context.raFile.getFilePointer();
303 }
304 } catch (final IOException e) {
305 e.printStackTrace();
306 }
307 }
308 for(final Entry<InputLine, Integer> entry : countMap.entrySet())
309 if (entry.getValue() < entry.getKey().getMinCount()) {
310 event = null;
311 }
312 context.setLocation(NULL_LOCATION);
313 return event;
314 }
315
316 public List<InputLine> getFirstLines() {
317 return fDefinition.inputs;
318 }
319
320 public CustomTxtEvent parseFirstLine(final CustomTxtTraceContext context) {
321 final CustomTxtEvent event = new CustomTxtEvent(fDefinition, this, TmfTimestamp.ZERO, "", fEventType, ""); //$NON-NLS-1$ //$NON-NLS-2$
322 event.processGroups(context.inputLine, context.firstLineMatcher);
323 event.setContent(new CustomEventContent(event, context.firstLine));
324 return event;
325 }
326
327 public CustomTraceDefinition getDefinition() {
328 return fDefinition;
329 }
330 }
This page took 0.039939 seconds and 6 git commands to generate.