import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
+import java.util.concurrent.locks.ReentrantLock;
import org.eclipse.linuxtools.tmf.event.TmfEvent;
import org.eclipse.linuxtools.tmf.event.TmfTimeRange;
* <p>
* Dummy test trace. Use in conjunction with TmfEventParserStub.
*/
+@SuppressWarnings("nls")
public class TmfTraceStub extends TmfTrace<TmfEvent> {
// ------------------------------------------------------------------------
// The associated event parser
private ITmfEventParser fParser;
+ // The synchronization lock
+ private ReentrantLock fLock = new ReentrantLock();
+
// ------------------------------------------------------------------------
// Constructors
// ------------------------------------------------------------------------
* @throws FileNotFoundException
*/
public TmfTraceStub(String filename) throws FileNotFoundException {
- super("TmfTraceStub", TmfEvent.class, filename);
- fTrace = new RandomAccessFile(filename, "r");
- fParser = new TmfEventParserStub();
+ this(filename, DEFAULT_INDEX_PAGE_SIZE, false);
}
/**
* @throws FileNotFoundException
*/
public TmfTraceStub(String filename, boolean waitForCompletion) throws FileNotFoundException {
- this(filename, DEFAULT_CACHE_SIZE, waitForCompletion);
+ this(filename, DEFAULT_INDEX_PAGE_SIZE, waitForCompletion);
}
/**
* @throws FileNotFoundException
*/
public TmfTraceStub(String filename, int cacheSize, boolean waitForCompletion) throws FileNotFoundException {
- super(filename, TmfEvent.class, filename, cacheSize);
+ super(null, TmfEvent.class, filename, cacheSize, false);
fTrace = new RandomAccessFile(filename, "r");
fParser = new TmfEventParserStub();
}
-// /**
-// * @param other
-// */
-// public TmfTraceStub(TmfTraceStub other) {
-// this(filename, DEFAULT_CACHE_SIZE, waitForCompletion);
-// }
+
+ /**
+ * @param filename
+ * @param cacheSize
+ * @param waitForCompletion
+ * @param parser
+ * @throws FileNotFoundException
+ */
+ public TmfTraceStub(String filename, int cacheSize, boolean waitForCompletion, ITmfEventParser parser) throws FileNotFoundException {
+ super(filename, TmfEvent.class, filename, cacheSize, false);
+ fTrace = new RandomAccessFile(filename, "r");
+ fParser = parser;
+ }
/**
*/
TmfTraceStub clone = null;
try {
clone = (TmfTraceStub) super.clone();
- clone.fTrace = new RandomAccessFile(getName(), "r");
+ clone.fTrace = new RandomAccessFile(getPath(), "r");
clone.fParser = new TmfEventParserStub();
} catch (CloneNotSupportedException e) {
} catch (FileNotFoundException e) {
return clone;
}
- public ITmfTrace createTraceCopy() {
+ @Override
+ public ITmfTrace copy() {
ITmfTrace returnedValue = null;
- try {
- returnedValue = new TmfTraceStub(this.getName());
- }
- catch (FileNotFoundException e) {
- e.printStackTrace();
- }
+ returnedValue = clone();
return returnedValue;
}
@Override
@SuppressWarnings("unchecked")
- public TmfContext seekLocation(ITmfLocation<?> location) {
+ public TmfContext seekLocation(ITmfLocation location) {
+ fLock.lock();
try {
- synchronized(fTrace) {
- // Position the trace at the requested location and
- // returns the corresponding context
- long loc = 0;
- long rank = 0;
- if (location != null) {
- loc = ((TmfLocation<Long>) location).getLocation();
- rank = ITmfContext.UNKNOWN_RANK;
- }
- if (loc != fTrace.getFilePointer()) {
- fTrace.seek(loc);
- }
- TmfContext context = new TmfContext(getCurrentLocation(), rank);
- return context;
- }
+ if (fTrace != null) {
+ // Position the trace at the requested location and
+ // returns the corresponding context
+ long loc = 0;
+ long rank = 0;
+ if (location != null) {
+ loc = ((TmfLocation<Long>) location).getLocation();
+ rank = ITmfContext.UNKNOWN_RANK;
+ }
+ if (loc != fTrace.getFilePointer()) {
+ fTrace.seek(loc);
+ }
+ TmfContext context = new TmfContext(getCurrentLocation(), rank);
+ return context;
+ }
} catch (IOException e) {
e.printStackTrace();
}
+ finally{
+ fLock.unlock();
+ }
return null;
}
+
+ @Override
+ public TmfContext seekLocation(double ratio) {
+ fLock.lock();
+ try {
+ if (fTrace != null) {
+ ITmfLocation<?> location = new TmfLocation<Long>(new Long((long) (ratio * fTrace.length())));
+ TmfContext context = seekLocation(location);
+ context.setRank(ITmfContext.UNKNOWN_RANK);
+ return context;
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ fLock.unlock();
+ }
+
+ return null;
+ }
+
+ @Override
+ @SuppressWarnings("rawtypes")
+ public double getLocationRatio(ITmfLocation location) {
+ fLock.lock();
+ try {
+ if (fTrace != null) {
+ if (location.getLocation() instanceof Long) {
+ return (double) ((Long) location.getLocation()) / fTrace.length();
+ }
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ fLock.unlock();
+ }
+ return 0;
+ }
+
@Override
public TmfLocation<Long> getCurrentLocation() {
+ fLock.lock();
try {
- return new TmfLocation<Long>(fTrace.getFilePointer());
+ if (fTrace != null) {
+ return new TmfLocation<Long>(fTrace.getFilePointer());
+ }
} catch (IOException e) {
e.printStackTrace();
+ } finally {
+ fLock.unlock();
}
return null;
}
@Override
public TmfEvent parseEvent(TmfContext context) {
+ fLock.lock();
try {
- // paserNextEvent updates the context
- TmfEvent event = fParser.parseNextEvent(this, context);
- return event;
+ // parseNextEvent will update the context
+ if (fTrace != null) {
+ TmfEvent event = fParser.parseNextEvent(this, context.clone());
+ return event;
+ }
}
catch (IOException e) {
e.printStackTrace();
+ } finally {
+ fLock.unlock();
}
return null;
}
public void setEndTime(TmfTimestamp endTime) {
super.setEndTime(endTime);
}
+
+ @Override
+ public void dispose() {
+ fLock.lock();
+ try {
+ if (fTrace != null) {
+ fTrace.close();
+ fTrace = null;
+ }
+ } catch (IOException e) {
+ // Ignore
+ } finally {
+ fLock.unlock();
+ }
+ super.dispose();
+ }
}
\ No newline at end of file