java.lang.Object
org.axonframework.messaging.eventhandling.processing.streaming.segmenting.Segment
All Implemented Interfaces:
Comparable<Segment>

public class Segment extends Object implements Comparable<Segment>
A representation of a segment and corresponding mask with various capabilities.

Definition

A Segment is a fraction of the total population of events. The 'mask' is a bitmask to be applied to an identifier, resulting in the segmentId of the Segment.

  • Field Details

    • RESOURCE_KEY

      public static final Context.ResourceKey<Segment> RESOURCE_KEY
      The Context.ResourceKey used whenever a Context would contain a segment id.
    • ROOT_SEGMENT

      public static final Segment ROOT_SEGMENT
      Represents the Segment that matches against all input, but can be split to start processing elements in parallel.
  • Constructor Details

    • Segment

      @Internal public Segment(int segmentId, int mask)
      Construct a new Segment instance with given segmentId and mask
      Parameters:
      segmentId - The id of the segment
      mask - The mask of the segment
  • Method Details

    • addToContext

      public static Context addToContext(@Nonnull Context context, @Nonnull Segment segment)
      Adds the given segment to the given context using the RESOURCE_KEY.
      Parameters:
      context - The Context to add the given token to.
      segment - The Segment to add to the given context using the RESOURCE_KEY.
    • fromContext

      public static Optional<Segment> fromContext(@Nonnull Context context)
      Returns an Optional of Segment, returning the resource keyed under the RESOURCE_KEY in the given context.
      Parameters:
      context - The Context to retrieve the Segment from, if present.
      Returns:
      An Optional of Segment, returning the resource keyed under the RESOURCE_KEY in the given context.
    • splitBalanced

      public static List<Segment> splitBalanced(Segment segment, int numberOfTimes)
      Split a given Segment n-times in round robin fashion.
      Parameters:
      segment - The Segment to split.
      numberOfTimes - The number of times to split it.
      Returns:
      a collection of Segment's.
    • mergedWith

      public Segment mergedWith(Segment other)
      Calculates the Segment that represents the merger of this segment with the given other segment.
      Parameters:
      other - the segment to merge this one with
      Returns:
      The Segment representing the merged segments
    • mergeableSegmentId

      public int mergeableSegmentId()
      Returns the segmentId of the segment this one can be merged with
      Returns:
      the segmentId of the segment this one can be merged with
    • isMergeableWith

      public boolean isMergeableWith(Segment other)
      Indicates whether this segment can be merged with the given other segment.

      Two segments can be merged when their mask is identical, and the only difference in SegmentID is in the first 1-bit of their mask.

      Parameters:
      other - the Segment to verify mergeability for
      Returns:
      true if the segments can be merged, otherwise false
    • getSegmentId

      public int getSegmentId()
      Getter for the segment identifier.
      Returns:
      the Segment identifier.
    • getMask

      public int getMask()
      Getter for the segment mask.
      Returns:
      the Segment mask.
    • matches

      public boolean matches(int value)
      Returns true when the mask applied to the given value, matches the segment id.
      Parameters:
      value - The value to be tested.
      Returns:
      true when matching this segment.
    • matches

      public boolean matches(Object value)
      Indicates whether the given value matches this segment. A value matches when the hashCode of a value, after applying this segments mask, equals to this segment ID.
      Parameters:
      value - The value to verify against.
      Returns:
      true if the given value matches this segment, otherwise false
    • split

      public Segment[] split()
      Returns an array with two segments with a corresponding mask.

      The first entry contains the original segmentId, with the newly calculated mask. (Simple left shift, adding a '1' as LSB). The 2nd entry is a new segmentId with the same derived mask.

      Callers must ensure that either the two returned Segments are used, or the instance from which they are derived, but not both.

      Returns:
      an array of two Segment's.
    • splitSegmentId

      public int splitSegmentId()
      Returns the segmentId of the counterpart of this segment, if this segment were to be split.
      Returns:
      the segmentId of the counterpart of this segment
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • compareTo

      public int compareTo(Segment that)
      Specified by:
      compareTo in interface Comparable<Segment>