Class IdSegmentChain

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

public class IdSegmentChain extends Object implements IdSegment
Chained ID segment.
  • Field Details

  • Constructor Details

    • IdSegmentChain

      public IdSegmentChain(IdSegmentChain previousChain, IdSegment idSegment, boolean allowReset)
    • IdSegmentChain

      public IdSegmentChain(long version, IdSegment idSegment, boolean allowReset)
  • Method Details

    • trySetNext

      public boolean trySetNext(Function<IdSegmentChain,IdSegmentChain> idSegmentChainSupplier) throws NextIdSegmentExpiredException
      try set next Chained ID segment.
      Parameters:
      idSegmentChainSupplier - IdSegmentChain supplier
      Returns:
      true if set successfully
      Throws:
      NextIdSegmentExpiredException - This exception is thrown if the provided IdSegmentChain has expired.
    • setNext

      public void setNext(IdSegmentChain nextIdSegmentChain)
    • ensureSetNext

      public IdSegmentChain ensureSetNext(Function<IdSegmentChain,IdSegmentChain> idSegmentChainSupplier) throws NextIdSegmentExpiredException
      Throws:
      NextIdSegmentExpiredException
    • getNext

      public IdSegmentChain getNext()
    • getIdSegment

      public IdSegment getIdSegment()
    • group

      public GroupedKey group()
      Specified by:
      group in interface Grouped
    • getVersion

      public long getVersion()
    • gap

      public int gap(IdSegmentChain end, long step)
    • newRoot

      public static IdSegmentChain newRoot(boolean allowReset)
    • getFetchTime

      public long getFetchTime()
      Description copied from interface: IdSegment
      Get the time when this segment was fetched.

      This timestamp is used for expiration calculations and represents when the segment was allocated from the central distributor.

      Unit: TimeUnit.SECONDS

      Specified by:
      getFetchTime in interface IdSegment
      Returns:
      The fetch time in seconds
    • getTtl

      public long getTtl()
      Description copied from interface: IdSegment
      Get the time-to-live for this segment.

      This determines how long the segment remains valid before it should be refreshed or replaced. A value of IdSegment.TIME_TO_LIVE_FOREVER indicates the segment never expires.

      Unit: TimeUnit.SECONDS

      Specified by:
      getTtl in interface IdSegment
      Returns:
      The time-to-live in seconds
    • getMaxId

      public long getMaxId()
      Description copied from interface: IdSegment
      Get the maximum ID in this segment.

      This is the upper bound of the ID range allocated to this segment. IDs generated from this segment will not exceed this value.

      Specified by:
      getMaxId in interface IdSegment
      Returns:
      The maximum ID in this segment
    • getOffset

      public long getOffset()
      Description copied from interface: IdSegment
      Get the offset (starting ID) of this segment.

      This is the lower bound of the ID range allocated to this segment. The first ID generated from this segment will typically be this value or slightly higher depending on the step size.

      Specified by:
      getOffset in interface IdSegment
      Returns:
      The offset of this segment
    • getSequence

      public long getSequence()
      Description copied from interface: IdSegment
      Get the current sequence number within this segment.

      This represents the next ID that will be allocated from this segment, or IdSegment.SEQUENCE_OVERFLOW if the segment has been exhausted.

      Specified by:
      getSequence in interface IdSegment
      Returns:
      The current sequence number
    • getStep

      public long getStep()
      Description copied from interface: IdSegment
      Get the step size for ID allocation.

      This determines how much the sequence number is incremented each time an ID is allocated. A step size of 1 allocates consecutive IDs, while larger step sizes can be used for sharding or other purposes.

      Specified by:
      getStep in interface IdSegment
      Returns:
      The step size for ID allocation
    • incrementAndGet

      public long incrementAndGet()
      Description copied from interface: IdSegment
      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.

      Specified by:
      incrementAndGet in interface IdSegment
      Returns:
      The next allocated ID, or IdSegment.SEQUENCE_OVERFLOW if exhausted
    • toString

      public String toString()
      Overrides:
      toString in class Object