segstore: introduce sorted iterators
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.segmentstore.core / src / org / eclipse / tracecompass / segmentstore / core / ISegmentStore.java
index 41af51d1442467ab303c59af30d518a7f48bc3e0..132f08a9a68f3178e1b285fe6d4720cf9af94c2f 100644 (file)
 
 package org.eclipse.tracecompass.segmentstore.core;
 
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.List;
+
+import org.eclipse.jdt.annotation.NonNull;
+
+import com.google.common.collect.Lists;
+
 /**
  * Interface for segment-storing backends.
  *
- * @param <T>
+ * @param <E>
  *            The type of {@link ISegment} element that will be stored in this
  *            database.
  *
  * @author Alexandre Montplaisir
  */
-public interface ISegmentStore<T extends ISegment> extends Iterable<T> {
+public interface ISegmentStore<E extends ISegment> extends Collection<E> {
 
     /**
-     * Add an element to the database.
+     * Sorted Iterator
      *
-     * @param elem The element to add.
+     * @param order
+     *            The desired order for the returned iterator
+     * @return An iterator over all the segments in the store in the desired order
+     * @since 1.1
      */
-    void addElement(T elem);
+    default Iterable<E> iterator(Comparator<ISegment> order){
+        return getIntersectingElements(Long.MIN_VALUE, Long.MAX_VALUE, order);
+    }
 
     /**
-     * Get the number of element currently existing in the database.
+     * Retrieve all elements that inclusively cross the given position.
      *
-     * @return The number of elements.
+     * @param position
+     *            The target position. This would represent a timestamp, if the
+     *            tree's X axis represents time.
+     * @return The intervals that cross this position
      */
-    long getNbElements();
+    default Iterable<E> getIntersectingElements(long position){
+        return getIntersectingElements(position, position);
+    }
 
     /**
-     * Retrieve all elements that inclusively cross the given position.
+     * Retrieve all elements that inclusively cross the given position, sorted
+     * in the specified order.
      *
      * @param position
      *            The target position. This would represent a timestamp, if the
      *            tree's X axis represents time.
+     * @param order
+     *            The desired order for the returned iterator
      * @return The intervals that cross this position
+     * @since 1.1
      */
-    Iterable<T> getIntersectingElements(long position);
+    default Iterable<E> getIntersectingElements(long position, Comparator<ISegment> order) {
+        return getIntersectingElements(position, position, order);
+    }
 
     /**
      * Retrieve all elements that inclusively cross another segment. We define
@@ -64,11 +90,49 @@ public interface ISegmentStore<T extends ISegment> extends Iterable<T> {
      *            The target end position
      * @return The elements overlapping with this segment
      */
-    Iterable<T> getIntersectingElements(long start, long end);
+    Iterable<E> getIntersectingElements(long start, long end);
+
+    /**
+     * Retrieve all elements that inclusively cross another segment, sorted in
+     * the specified order. We define this target segment by its start and end
+     * positions.
+     *
+     * @param start
+     *            The target start position
+     * @param end
+     *            The target end position
+     * @param order
+     *            The desired order for the returned iterator
+     * @return The intervals that cross this position
+     * @since 1.1
+     */
+    default Iterable<E> getIntersectingElements(long start, long end, Comparator<ISegment> order){
+        List<E> list = Lists.newArrayList(getIntersectingElements(start, end));
+        return new Iterable<@NonNull E>() {
+            @Override
+            public Iterator<@NonNull E> iterator() {
+                Collections.sort(list, order);
+                return list.iterator();
+            }
+        };
+    }
 
     /**
      * Dispose the data structure and release any system resources associated
      * with it.
      */
     void dispose();
+
+    /**
+     * Method to close off the segment store. This happens for example when we
+     * are done reading an off-line trace. Implementers can use this method to
+     * save the segment store on disk
+     *
+     * @param deleteFiles
+     *            Whether to delete any file that was created while building the
+     *            segment store
+     */
+    default void close(boolean deleteFiles) {
+
+    }
 }
This page took 0.02844 seconds and 5 git commands to generate.