Class DefaultIdSegment

java.lang.Object
me.ahoo.cosid.segment.DefaultIdSegment
All Implemented Interfaces:
Comparable<IdSegment>, Grouped, IdSegment

public class DefaultIdSegment extends Object implements IdSegment
Default implementation of an ID segment.

This class provides a concrete implementation of the IdSegment interface, representing a contiguous block of IDs allocated for generation. It maintains state information about the segment and provides thread-safe ID allocation within the segment.

Key characteristics:

  • Thread-Safe: Uses atomic operations for ID allocation
  • Immutable Range: Offset and max ID are fixed at construction
  • Expiration Support: Includes TTL for segment lifecycle management
  • Overflow Detection: Automatically detects when segment is exhausted

The segment works by:

  1. Maintaining an offset (starting ID) and max ID (ending ID)
  2. Tracking the current sequence (next available ID)
  3. Atomically incrementing the sequence when IDs are allocated
  4. Detecting overflow when the sequence exceeds the max ID
  • Field Details

    • OVERFLOW

      public static final DefaultIdSegment OVERFLOW
      A sentinel value representing an overflow segment.

      This is used to signal that a segment has been exhausted and no more IDs can be allocated from it.

  • Constructor Details

    • DefaultIdSegment

      public DefaultIdSegment(long maxId, long step)
      Create a new DefaultIdSegment with default configuration.

      This constructor creates a segment with infinite TTL and no grouping.

      Parameters:
      maxId - The maximum ID in this segment
      step - The step size for ID allocation
    • DefaultIdSegment

      public DefaultIdSegment(long maxId, long step, long fetchTime, long ttl, GroupedKey group)
      Create a new DefaultIdSegment with full custom configuration.

      This constructor allows complete control over all parameters of the ID segment.

      Parameters:
      maxId - The maximum ID in this segment
      step - The step size for ID allocation
      fetchTime - The time when this segment was fetched
      ttl - The time-to-live for this segment
      group - The group key for this segment
  • Method Details

    • group

      public GroupedKey group()
      Get the group key for this segment.
      Specified by:
      group in interface Grouped
      Returns:
      The group key
    • getFetchTime

      public long getFetchTime()
      Get the time when this segment was fetched.
      Specified by:
      getFetchTime in interface IdSegment
      Returns:
      The fetch time
    • getTtl

      public long getTtl()
      Get the time-to-live for this segment.
      Specified by:
      getTtl in interface IdSegment
      Returns:
      The time-to-live
    • getMaxId

      public long getMaxId()
      Get the maximum ID in this segment.
      Specified by:
      getMaxId in interface IdSegment
      Returns:
      The maximum ID
    • getOffset

      public long getOffset()
      Get the offset (starting ID) of this segment.
      Specified by:
      getOffset in interface IdSegment
      Returns:
      The offset
    • getSequence

      public long getSequence()
      Get the current sequence number within this segment.
      Specified by:
      getSequence in interface IdSegment
      Returns:
      The current sequence number
    • getStep

      public long getStep()
      Get the step size for ID allocation.
      Specified by:
      getStep in interface IdSegment
      Returns:
      The step size
    • incrementAndGet

      public long incrementAndGet()
      Atomically increment the sequence and return the new value.

      This method provides thread-safe allocation of the next ID from this segment. If the segment has been exhausted, it returns IdSegment.SEQUENCE_OVERFLOW.

      The method performs the following steps:

      1. Check if the segment has already overflowed
      2. Atomically increment the sequence number
      3. Check if the new sequence represents an overflow
      4. Update grouped accessor if needed
      5. Return the allocated ID or overflow indicator
      Specified by:
      incrementAndGet in interface IdSegment
      Returns:
      The next allocated ID, or IdSegment.SEQUENCE_OVERFLOW if exhausted
    • toString

      public String toString()
      Get the string representation of this segment.
      Overrides:
      toString in class Object
      Returns:
      The string representation