Class SegmentBuffer

java.lang.Object
cloud.opencode.base.id.segment.SegmentBuffer

public final class SegmentBuffer extends Object
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

    Fields
    Modifier and Type
    Field
    Description
    static final double
    Default preload threshold (20%) 默认预加载阈值(20%)
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a segment buffer 创建号段缓冲区
    SegmentBuffer(double preloadThreshold)
    Creates a segment buffer with custom preload threshold 使用自定义预加载阈值创建号段缓冲区
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    Gets the current value 获取当前值
    long
    Gets the max value 获取最大值
    long
    Gets the next value 获取下一个值
    long
    Gets the remaining count 获取剩余数量
    int
    Gets the step size 获取步长
    int
    Gets the usage percentage 获取使用百分比
    void
    Initializes the buffer with a segment 使用号段初始化缓冲区
    boolean
    Checks if buffer is exhausted 检查缓冲区是否耗尽
    boolean
    Checks if initialized 检查是否已初始化
    boolean
    Checks if should preload next segment 检查是否应预加载下一个号段
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • DEFAULT_PRELOAD_THRESHOLD

      public static final double DEFAULT_PRELOAD_THRESHOLD
      Default 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

      public void init(SegmentAllocator.Segment segment)
      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

      public String toString()
      Overrides:
      toString in class Object