Class SlidingWindow<E,​R,​Q>

  • Type Parameters:
    E - The type of an event to be reported
    R - The type of the record stored per time slot. Must be thread safe
    Q - The type of query results
    Direct Known Subclasses:
    AverageSlidingWindow, EventRateSlidingWindow

    public abstract class SlidingWindow<E,​R,​Q>
    extends Object
    An abstract sliding window. A sliding window object is notified with events as they occur, and supports querying the recently collected data. The data aggregation and the query semantics are implementation dependent.
    Author:
    Eyal Schneider
    • Constructor Detail

      • SlidingWindow

        public SlidingWindow​(int slotsCount,
                             int slotTime,
                             TimeUnit slotTimeUnit,
                             ScheduledExecutorService ex)
        Constructor
        Parameters:
        slotsCount - The number of time slots in the sliding window
        slotTime - The time duration represented by one slot
        slotTimeUnit - The time unit of slotTime
        ex - The executor to run the time-pulse task on.
    • Method Detail

      • newEvent

        public void newEvent​(E event)
        Reports a new event
        Parameters:
        event - The event
      • newEvent

        protected abstract void newEvent​(E event,
                                         R rec)
        Reports a new event to be appended into a given time slot record
        Parameters:
        event - The event
        rec - The record to be updated
      • newRecord

        protected abstract R newRecord()
        Returns:
        A new, empty record. Called only during the construction of the sliding window.
      • newRecordArray

        protected abstract R[] newRecordArray​(int size)
        Parameters:
        size - The required size
        Returns:
        A new, empty record array of the given size.
      • clearRecord

        protected abstract void clearRecord​(R record)
        clears a given record by resetting its data. It is up to the implementation to decide how to deal with data consistency in case of a concurrent query that inspects that record. It is highly unlikely that an event is registered in the record during this operation.
      • query

        public Q query()
        Returns:
        The query result, relevant for the complete time period covered by the sliding window.
      • query

        public Q query​(int lookback)
        Parameters:
        lookback - The number of time slots to look back on (must be between 1 and the number of slots defined in the constructor)
        Returns:
        The query result, relevant for the time period covered by the given lookback.
      • query

        protected abstract Q query​(R[] records)
        Parameters:
        records - the array of records to inspect. Ordered from oldest to newest.
        Returns:
        The query result, relevant for the time period covered by the given lookback.
      • shutdown

        public void shutdown()
        Terminates the sliding window. Note: subsequent queries may return undefined results!
      • getSlotTime

        public int getSlotTime()
        Returns:
        The time duration represented by a single slot
      • getSlotTimeUnit

        public TimeUnit getSlotTimeUnit()
        Returns:
        The time unit by which getSlotTime() is measured
      • getWindowTimeSpanMs

        public long getWindowTimeSpanMs()
        Returns:
        The total time covered by this sliding window, in milliseconds
      • getSlotsCount

        public int getSlotsCount()
        Returns:
        The number of slots in this sliding window
      • getRecord

        protected R getRecord​(int pos)
        Parameters:
        pos - A position in the cyclic array
        Returns:
        The record in the given position