Class DefaultSegmentId

java.lang.Object
me.ahoo.cosid.segment.DefaultSegmentId
All Implemented Interfaces:
IdGenerator, SegmentId, Statistical, StringIdGenerator

public class DefaultSegmentId extends Object implements SegmentId
Default segment algorithm ID generator.

Provides thread-safe ID generation using a segment-based approach. Allocates ID segments from a central distributor and generates IDs locally within each segment for high throughput.

  • Constructor Details

    • DefaultSegmentId

      public DefaultSegmentId(IdSegmentDistributor maxIdDistributor)
      Creates a generator with infinite segment TTL.
      Parameters:
      maxIdDistributor - the segment distributor
    • DefaultSegmentId

      public DefaultSegmentId(long idSegmentTtl, IdSegmentDistributor maxIdDistributor)
      Creates a generator with specified segment TTL.
      Parameters:
      idSegmentTtl - segment time-to-live in seconds
      maxIdDistributor - the segment distributor
  • Method Details

    • current

      public IdSegment current()
      Description copied from interface: SegmentId
      Get the current ID segment being used for generation.

      This method returns the segment from which IDs are currently being allocated. The segment contains the range of available IDs and tracks the current position within that range.

      Specified by:
      current in interface SegmentId
      Returns:
      The current ID segment
    • generate

      public long generate()
      Description copied from interface: IdGenerator
      Generate a distributed ID as a long value.

      This method generates a unique numeric identifier that is guaranteed to be unique within the distributed system. The exact algorithm used depends on the implementation (e.g., Snowflake, Segment, CosId).

      Specified by:
      generate in interface IdGenerator
      Returns:
      A unique distributed ID as a long value