timing.core: Make ArrayList use the array of segments if possible
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.timing.core / src / org / eclipse / tracecompass / analysis / timing / core / segmentstore / AbstractSegmentStoreAnalysisModule.java
index 61fdd52ce57738787ad78b5c1d51fea511772149..597b547e451c491beda94367b7a93653e99912c6 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2015 Ericsson
+ * Copyright (c) 2015, 2016 Ericsson
  *
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
@@ -17,26 +17,22 @@ import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.ArrayList;
-import java.util.Collection;
+import java.util.Collections;
 import java.util.List;
 
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.ListenerList;
 import org.eclipse.jdt.annotation.Nullable;
+import org.eclipse.tracecompass.common.core.NonNullUtils;
+import org.eclipse.tracecompass.internal.analysis.timing.core.store.ArrayListStore;
 import org.eclipse.tracecompass.segmentstore.core.ISegment;
 import org.eclipse.tracecompass.segmentstore.core.ISegmentStore;
-import org.eclipse.tracecompass.segmentstore.core.treemap.TreeMapStore;
 import org.eclipse.tracecompass.tmf.core.analysis.TmfAbstractAnalysisModule;
-import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
 import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException;
-import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest;
-import org.eclipse.tracecompass.tmf.core.request.TmfEventRequest;
 import org.eclipse.tracecompass.tmf.core.segment.ISegmentAspect;
 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
 
-import com.google.common.collect.ImmutableList;
-
 /**
  * Abstract analysis module to generate a segment store. It is a base class that
  * can be used as a shortcut by analysis who just need to build a single segment
@@ -46,30 +42,18 @@ import com.google.common.collect.ImmutableList;
  * @since 2.0
  *
  */
-public abstract class AbstractSegmentStoreAnalysisModule extends TmfAbstractAnalysisModule {
+public abstract class AbstractSegmentStoreAnalysisModule extends TmfAbstractAnalysisModule implements ISegmentStoreProvider {
 
     private final ListenerList fListeners = new ListenerList(ListenerList.IDENTITY);
 
     private @Nullable ISegmentStore<ISegment> fSegmentStore;
 
-    private @Nullable ITmfEventRequest fOngoingRequest = null;
-
-    /**
-     * Listener for the viewers
-     *
-     * @param listener
-     *            listener for each type of viewer
-     */
+    @Override
     public void addListener(IAnalysisProgressListener listener) {
         fListeners.add(listener);
     }
 
-    /**
-     * Removes a listener for the viewers
-     *
-     * @param listener
-     *            listener for each type of viewer to remove
-     */
+    @Override
     public void removeListener(IAnalysisProgressListener listener) {
         fListeners.remove(listener);
     }
@@ -89,16 +73,9 @@ public abstract class AbstractSegmentStoreAnalysisModule extends TmfAbstractAnal
         return listeners;
     }
 
-    /**
-     * Return the pre-defined set of segment aspects exposed by this analysis.
-     *
-     * It should not be null, but could be empty.
-     *
-     * @return The segment aspects for this analysis
-     */
+    @Override
     public Iterable<ISegmentAspect> getSegmentAspects() {
-        Collection<ISegmentAspect> coll = ImmutableList.of();
-        return checkNotNull(coll);
+        return Collections.emptyList();
     }
 
     /**
@@ -110,15 +87,6 @@ public abstract class AbstractSegmentStoreAnalysisModule extends TmfAbstractAnal
         return null;
     }
 
-    /**
-     * Returns the analysis request for creating the segment store
-     *
-     * @param segmentStore
-     *            a segment store to fill
-     * @return the segment store analysis request implementation
-     */
-    protected abstract AbstractSegmentStoreAnalysisRequest createAnalysisRequest(ISegmentStore<ISegment> segmentStore);
-
     /**
      * Read an object from the ObjectInputStream.
      *
@@ -133,20 +101,27 @@ public abstract class AbstractSegmentStoreAnalysisModule extends TmfAbstractAnal
     protected abstract Object[] readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException;
 
     /**
-     * Returns the result in a from the analysis in a ISegmentStore
+     * Fills the segment store. This is the main method that children classes
+     * need to implement to build the segment store. For example, if the
+     * segments are found by parsing the events of a trace, the event request
+     * would be done in this method.
+     *
+     * Note: After this method, the segment store should be completed, so it
+     * should also close the segment store at the end of the analysis
      *
-     * @return Results from the analysis in a ISegmentStore
+     * @param segmentStore
+     *            The segment store to fill
+     * @param monitor
+     *            Progress monitor
+     * @return Whether the segments was resolved successfully or not
+     * @throws TmfAnalysisException
+     *             Method may throw an analysis exception
      */
-    public @Nullable ISegmentStore<ISegment> getResults() {
-        return fSegmentStore;
-    }
+    protected abstract boolean buildAnalysisSegments(ISegmentStore<ISegment> segmentStore, IProgressMonitor monitor) throws TmfAnalysisException;
 
     @Override
-    protected void canceling() {
-        ITmfEventRequest req = fOngoingRequest;
-        if ((req != null) && (!req.isCompleted())) {
-            req.cancel();
-        }
+    public @Nullable ISegmentStore<ISegment> getSegmentStore() {
+        return fSegmentStore;
     }
 
     @Override
@@ -172,17 +147,9 @@ public abstract class AbstractSegmentStoreAnalysisModule extends TmfAbstractAnal
                 /* Attempt to read the existing file */
                 try (ObjectInputStream ois = new ObjectInputStream(Files.newInputStream(file))) {
                     Object[] segmentArray = readObject(ois);
-                    final ISegmentStore<ISegment> store = new TreeMapStore<>();
-                    for (Object element : segmentArray) {
-                        if (element instanceof ISegment) {
-                            ISegment segment = (ISegment) element;
-                            store.add(segment);
-                        }
-                    }
+                    ISegmentStore<ISegment> store = new ArrayListStore<>(NonNullUtils.checkNotNullContents(segmentArray));
                     fSegmentStore = store;
-                    for (IAnalysisProgressListener listener : getListeners()) {
-                        listener.onComplete(this, store);
-                    }
+                    sendUpdate(store);
                     return true;
                 } catch (IOException | ClassNotFoundException | ClassCastException e) {
                     /*
@@ -196,30 +163,12 @@ public abstract class AbstractSegmentStoreAnalysisModule extends TmfAbstractAnal
                 }
             }
         }
-        ISegmentStore<ISegment> segmentStore = new TreeMapStore<>();
-
-        /* Cancel an ongoing request */
-        ITmfEventRequest req = fOngoingRequest;
-        if ((req != null) && (!req.isCompleted())) {
-            req.cancel();
-        }
 
-        /* Create a new request */
-        req = createAnalysisRequest(segmentStore);
-        fOngoingRequest = req;
-        trace.sendRequest(req);
-
-        try {
-            req.waitForCompletion();
-        } catch (InterruptedException e) {
-        }
-
-        /* Do not process the results if the request was cancelled */
-        if (req.isCancelled() || req.isFailed()) {
+        ISegmentStore<ISegment> segmentStore = new ArrayListStore<>();
+        boolean completed = buildAnalysisSegments(segmentStore, monitor);
+        if (!completed) {
             return false;
         }
-
-        /* The request will fill 'syscalls' */
         fSegmentStore = segmentStore;
 
         if (dataFileName != null) {
@@ -237,42 +186,20 @@ public abstract class AbstractSegmentStoreAnalysisModule extends TmfAbstractAnal
             }
         }
 
-        for (IAnalysisProgressListener listener : getListeners()) {
-            listener.onComplete(this, segmentStore);
-        }
+        sendUpdate(segmentStore);
 
         return true;
     }
 
     /**
-     * Abstract event request to fill a a segment store
+     * Send the segment store to all its listener
+     *
+     * @param store
+     *            The segment store to broadcast
      */
-    protected static abstract class AbstractSegmentStoreAnalysisRequest extends TmfEventRequest {
-
-        private final ISegmentStore<ISegment> fFullLatencyStore;
-
-        /**
-         * Constructor
-         *
-         * @param latencyStore
-         *            a latency segment store to fill
-         */
-        public AbstractSegmentStoreAnalysisRequest(ISegmentStore<ISegment> latencyStore) {
-            super(ITmfEvent.class, 0, ITmfEventRequest.ALL_DATA, ExecutionType.BACKGROUND);
-            /*
-             * We do NOT make a copy here! We want to modify the list that was
-             * passed in parameter.
-             */
-            fFullLatencyStore = latencyStore;
-        }
-
-        /**
-         * Returns the segment store
-         *
-         * @return the segment store
-         */
-        public ISegmentStore<ISegment> getSegmentStore() {
-            return fFullLatencyStore;
+    protected void sendUpdate(final ISegmentStore<ISegment> store) {
+        for (IAnalysisProgressListener listener : getListeners()) {
+            listener.onComplete(this, store);
         }
     }
 }
\ No newline at end of file
This page took 0.038746 seconds and 5 git commands to generate.