Class SegmentBuffer
java.lang.Object
cloud.opencode.base.id.segment.SegmentBuffer
Segment Buffer for Double Buffering
双缓冲号段缓冲区
Internal buffer used by SegmentIdGenerator for double-buffering. Allows preloading the next segment while the current one is in use.
SegmentIdGenerator用于双缓冲的内部缓冲区。 允许在使用当前号段时预加载下一个号段。
Features | 主要功能:
- Atomic counter for thread safety - 原子计数器确保线程安全
- Buffer state tracking - 缓冲区状态跟踪
- Preload threshold detection - 预加载阈值检测
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
Usage Examples | 使用示例:
// SegmentBuffer is used internally by SegmentIdGenerator
// SegmentBuffer由SegmentIdGenerator内部使用
SegmentBuffer buffer = new SegmentBuffer(allocator);
long id = buffer.nextId();
- Since:
- JDK 25, opencode-base-id V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final doubleDefault preload threshold (20%) 默认预加载阈值(20%) -
Constructor Summary
ConstructorsConstructorDescriptionCreates a segment buffer 创建号段缓冲区SegmentBuffer(double preloadThreshold) Creates a segment buffer with custom preload threshold 使用自定义预加载阈值创建号段缓冲区 -
Method Summary
Modifier and TypeMethodDescriptionlongGets the current value 获取当前值longGets the max value 获取最大值longgetNext()Gets the next value 获取下一个值longGets the remaining count 获取剩余数量intgetStep()Gets the step size 获取步长intGets the usage percentage 获取使用百分比voidinit(SegmentAllocator.Segment segment) Initializes the buffer with a segment 使用号段初始化缓冲区booleanChecks if buffer is exhausted 检查缓冲区是否耗尽booleanChecks if initialized 检查是否已初始化booleanChecks if should preload next segment 检查是否应预加载下一个号段toString()
-
Field Details
-
DEFAULT_PRELOAD_THRESHOLD
public static final double DEFAULT_PRELOAD_THRESHOLDDefault preload threshold (20%) 默认预加载阈值(20%)- See Also:
-
-
Constructor Details
-
SegmentBuffer
public SegmentBuffer()Creates a segment buffer 创建号段缓冲区 -
SegmentBuffer
public SegmentBuffer(double preloadThreshold) Creates a segment buffer with custom preload threshold 使用自定义预加载阈值创建号段缓冲区- Parameters:
preloadThreshold- the threshold (0.0 - 1.0) | 阈值(0.0 - 1.0)
-
-
Method Details
-
init
Initializes the buffer with a segment 使用号段初始化缓冲区- Parameters:
segment- the segment | 号段
-
getNext
public long getNext()Gets the next value 获取下一个值Uses compare-and-set loop to prevent overshoot past maxValue and overflow near Long.MAX_VALUE.
使用CAS循环防止超过maxValue以及在Long.MAX_VALUE附近溢出。
- Returns:
- next value, or -1 if exhausted | 下一个值,如果耗尽返回-1
-
isExhausted
public boolean isExhausted()Checks if buffer is exhausted 检查缓冲区是否耗尽- Returns:
- true if exhausted | 如果耗尽返回true
-
shouldPreload
public boolean shouldPreload()Checks if should preload next segment 检查是否应预加载下一个号段- Returns:
- true if should preload | 如果应预加载返回true
-
getCurrentValue
public long getCurrentValue()Gets the current value 获取当前值- Returns:
- current value | 当前值
-
getMaxValue
public long getMaxValue()Gets the max value 获取最大值- Returns:
- max value | 最大值
-
getStep
public int getStep()Gets the step size 获取步长- Returns:
- step size | 步长
-
isInitialized
public boolean isInitialized()Checks if initialized 检查是否已初始化- Returns:
- true if initialized | 如果已初始化返回true
-
getUsagePercent
public int getUsagePercent()Gets the usage percentage 获取使用百分比- Returns:
- usage percentage (0-100) | 使用百分比(0-100)
-
getRemaining
public long getRemaining()Gets the remaining count 获取剩余数量- Returns:
- remaining count | 剩余数量
-
toString
-