Class IdSegmentChain
- All Implemented Interfaces:
Comparable<IdSegment>,Grouped,IdSegment
This class chains multiple ID segments together, allowing for seamless transition between segments without blocking. When one segment is exhausted, the next segment in the chain is used.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final IdSegmentChainSentinel value indicating next has not been set.static final intVersion number for the root chain.Fields inherited from interface me.ahoo.cosid.segment.IdSegment
SEQUENCE_OVERFLOW, TIME_TO_LIVE_FOREVER -
Constructor Summary
ConstructorsConstructorDescriptionIdSegmentChain(long version, IdSegment idSegment, boolean allowReset) Creates a new chain segment with explicit version.IdSegmentChain(IdSegmentChain previousChain, IdSegment idSegment, boolean allowReset) Creates a new chain segment linked to a previous chain. -
Method Summary
Modifier and TypeMethodDescriptionensureSetNext(Function<IdSegmentChain, IdSegmentChain> idSegmentChainSupplier) Ensures the next segment is set, retrying until successful.intgap(IdSegmentChain end, long step) Calculates the gap between this chain's sequence and another's max ID.longGet the time when this segment was fetched.Gets the ID segment for this chain.longgetMaxId()Get the maximum ID in this segment.getNext()Gets the next segment in the chain.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.longGets the version number of this chain.group()Gets the grouping key for this object.longAtomically increment the sequence and return the new value.static IdSegmentChainnewRoot(boolean allowReset) Creates a new root chain.voidsetNext(IdSegmentChain nextIdSegmentChain) Sets the next segment in the chain.toString()booleantrySetNext(Function<IdSegmentChain, IdSegmentChain> idSegmentChainSupplier) Attempts to set the next segment in the chain.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
-
ROOT_VERSION
public static final int ROOT_VERSIONVersion number for the root chain.- See Also:
-
NOT_SET
Sentinel value indicating next has not been set.
-
-
Constructor Details
-
IdSegmentChain
Creates a new chain segment linked to a previous chain.- Parameters:
previousChain- the previous chain in the linkidSegment- the ID segment for this chainallowReset- whether reset is allowed
-
IdSegmentChain
Creates a new chain segment with explicit version.- Parameters:
version- the version numberidSegment- the ID segment for this chainallowReset- whether reset is allowed
-
-
Method Details
-
trySetNext
public boolean trySetNext(Function<IdSegmentChain, IdSegmentChain> idSegmentChainSupplier) throws NextIdSegmentExpiredExceptionAttempts to set the next segment in the chain.If next is already set, returns false without modifying.
- Parameters:
idSegmentChainSupplier- supplier that creates the next segment based on this- Returns:
- true if set successfully, false if next was already set
- Throws:
NextIdSegmentExpiredException- if the provided segment has expired
-
setNext
Sets the next segment in the chain.- Parameters:
nextIdSegmentChain- the next segment
-
ensureSetNext
public IdSegmentChain ensureSetNext(Function<IdSegmentChain, IdSegmentChain> idSegmentChainSupplier) throws NextIdSegmentExpiredExceptionEnsures the next segment is set, retrying until successful.- Parameters:
idSegmentChainSupplier- supplier that creates the next segment- Returns:
- the chain that has next set
- Throws:
NextIdSegmentExpiredException- if all segments expire
-
getNext
Gets the next segment in the chain.- Returns:
- the next segment or null if not yet set
-
getIdSegment
Gets the ID segment for this chain.- Returns:
- the ID segment
-
group
Description copied from interface:GroupedGets the grouping key for this object.Default implementation returns
GroupedKey.NEVER, indicating this object should not be grouped. -
getVersion
public long getVersion()Gets the version number of this chain.- Returns:
- the version
-
gap
Calculates the gap between this chain's sequence and another's max ID.- Parameters:
end- the end chainstep- the step size- Returns:
- the number of IDs between sequences
-
newRoot
Creates a new root chain.- Parameters:
allowReset- whether reset is allowed- Returns:
- a new root chain
-
getFetchTime
public long getFetchTime()Description copied from interface:IdSegmentGet 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:
getFetchTimein interfaceIdSegment- Returns:
- The fetch time in seconds
-
getTtl
public long getTtl()Description copied from interface:IdSegmentGet 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_FOREVERindicates the segment never expires.Unit:
TimeUnit.SECONDS -
getMaxId
public long getMaxId()Description copied from interface:IdSegmentGet 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.
-
getOffset
public long getOffset()Description copied from interface:IdSegmentGet 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.
-
getSequence
public long getSequence()Description copied from interface:IdSegmentGet the current sequence number within this segment.This represents the next ID that will be allocated from this segment, or
IdSegment.SEQUENCE_OVERFLOWif the segment has been exhausted.- Specified by:
getSequencein interfaceIdSegment- Returns:
- The current sequence number
-
getStep
public long getStep()Description copied from interface:IdSegmentGet 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.
-
incrementAndGet
public long incrementAndGet()Description copied from interface:IdSegmentAtomically 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:
incrementAndGetin interfaceIdSegment- Returns:
- The next allocated ID, or
IdSegment.SEQUENCE_OVERFLOWif exhausted
-
toString
-