Class IdSegmentChain
- All Implemented Interfaces:
Comparable<IdSegment>,Grouped,IdSegment
-
Field Summary
FieldsFields inherited from interface me.ahoo.cosid.segment.IdSegment
SEQUENCE_OVERFLOW, TIME_TO_LIVE_FOREVER -
Constructor Summary
ConstructorsConstructorDescriptionIdSegmentChain(long version, IdSegment idSegment, boolean allowReset) IdSegmentChain(IdSegmentChain previousChain, IdSegment idSegment, boolean allowReset) -
Method Summary
Modifier and TypeMethodDescriptionensureSetNext(Function<IdSegmentChain, IdSegmentChain> idSegmentChainSupplier) intgap(IdSegmentChain end, long step) longGet the time when this segment was fetched.longgetMaxId()Get the maximum ID in this segment.getNext()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.longgroup()longAtomically increment the sequence and return the new value.static IdSegmentChainnewRoot(boolean allowReset) voidsetNext(IdSegmentChain nextIdSegmentChain) toString()booleantrySetNext(Function<IdSegmentChain, IdSegmentChain> idSegmentChainSupplier) try set next Chained ID 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
-
ROOT_VERSION
public static final int ROOT_VERSION- See Also:
-
NOT_SET
-
-
Constructor Details
-
IdSegmentChain
-
IdSegmentChain
-
-
Method Details
-
trySetNext
public boolean trySetNext(Function<IdSegmentChain, IdSegmentChain> idSegmentChainSupplier) throws NextIdSegmentExpiredExceptiontry set next Chained ID segment.- Parameters:
idSegmentChainSupplier-IdSegmentChainsupplier- Returns:
- true if set successfully
- Throws:
NextIdSegmentExpiredException- This exception is thrown if the providedIdSegmentChainhas expired.
-
setNext
-
ensureSetNext
public IdSegmentChain ensureSetNext(Function<IdSegmentChain, IdSegmentChain> idSegmentChainSupplier) throws NextIdSegmentExpiredException- Throws:
NextIdSegmentExpiredException
-
getNext
-
getIdSegment
-
group
-
getVersion
public long getVersion() -
gap
-
newRoot
-
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
-