Package me.ahoo.cosid.segment
Class DefaultIdSegment
java.lang.Object
me.ahoo.cosid.segment.DefaultIdSegment
- All Implemented Interfaces:
Comparable<IdSegment>,Grouped,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:
- Maintaining an offset (starting ID) and max ID (ending ID)
- Tracking the current sequence (next available ID)
- Atomically incrementing the sequence when IDs are allocated
- Detecting overflow when the sequence exceeds the max ID
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final DefaultIdSegmentA sentinel value representing an overflow segment.Fields inherited from interface me.ahoo.cosid.segment.IdSegment
SEQUENCE_OVERFLOW, TIME_TO_LIVE_FOREVER -
Constructor Summary
ConstructorsConstructorDescriptionDefaultIdSegment(long maxId, long step) Create a new DefaultIdSegment with default configuration.DefaultIdSegment(long maxId, long step, long fetchTime, long ttl, GroupedKey group) Create a new DefaultIdSegment with full custom configuration. -
Method Summary
Modifier and TypeMethodDescriptionlongGet the time when this segment was fetched.longgetMaxId()Get the maximum ID in this segment.longGet the offset (starting ID) of this segment.longGet the current sequence number within this segment.longgetStep()Get the step size for ID allocation.longgetTtl()Get the time-to-live for this segment.group()Get the group key for this segment.longAtomically increment the sequence and return the new value.toString()Get the string representation of this segment.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface me.ahoo.cosid.segment.IdSegment
compareTo, ensureNextIdSegment, isAvailable, isExpired, isOverflow, isOverflow
-
Field Details
-
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 segmentstep- The step size for ID allocation
-
DefaultIdSegment
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 segmentstep- The step size for ID allocationfetchTime- The time when this segment was fetchedttl- The time-to-live for this segmentgroup- The group key for this segment
-
-
Method Details
-
group
Get the group key for this segment. -
getFetchTime
public long getFetchTime()Get the time when this segment was fetched.- Specified by:
getFetchTimein interfaceIdSegment- Returns:
- The fetch time
-
getTtl
public long getTtl()Get the time-to-live for this segment. -
getMaxId
public long getMaxId()Get the maximum ID in this segment. -
getOffset
public long getOffset()Get the offset (starting ID) of this segment. -
getSequence
public long getSequence()Get the current sequence number within this segment.- Specified by:
getSequencein interfaceIdSegment- Returns:
- The current sequence number
-
getStep
public long getStep()Get the step size for ID allocation. -
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:
- Check if the segment has already overflowed
- Atomically increment the sequence number
- Check if the new sequence represents an overflow
- Update grouped accessor if needed
- Return the allocated ID or overflow indicator
- Specified by:
incrementAndGetin interfaceIdSegment- Returns:
- The next allocated ID, or
IdSegment.SEQUENCE_OVERFLOWif exhausted
-
toString
Get the string representation of this segment.
-