Class MemorySegmentAllocator

java.lang.Object
cloud.opencode.base.id.segment.MemorySegmentAllocator
All Implemented Interfaces:
SegmentAllocator

public final class MemorySegmentAllocator extends Object implements SegmentAllocator
Memory-based Segment Allocator 基于内存的号段分配器

A simple in-memory implementation of SegmentAllocator. Useful for testing, development, and single-node scenarios.

SegmentAllocator的简单内存实现。 适用于测试、开发和单节点场景。

Features | 主要功能:

  • No external dependencies - 无外部依赖
  • Thread-safe - 线程安全
  • Multiple business tags support - 多业务标识支持
  • Configurable starting value and step - 可配置起始值和步长

Limitations | 限制:

  • Not persistent - data lost on restart | 非持久化 - 重启后数据丢失
  • Not suitable for distributed systems | 不适用于分布式系统

Usage Examples | 使用示例:

// Basic usage
SegmentAllocator allocator = MemorySegmentAllocator.create();
IdGenerator<Long> gen = SegmentIdGenerator.create(allocator, "order");

// Custom step
SegmentAllocator allocator = MemorySegmentAllocator.create(5000);

// Custom start and step
SegmentAllocator allocator = MemorySegmentAllocator.create(10000, 1000);

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
Since:
JDK 25, opencode-base-id V1.1.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • create

      public static MemorySegmentAllocator create()
      Creates a memory allocator with default settings 使用默认设置创建内存分配器
      Returns:
      allocator | 分配器
    • create

      public static MemorySegmentAllocator create(int step)
      Creates a memory allocator with custom step 使用自定义步长创建内存分配器
      Parameters:
      step - the step size | 步长
      Returns:
      allocator | 分配器
    • create

      public static MemorySegmentAllocator create(long startValue, int step)
      Creates a memory allocator with custom start value and step 使用自定义起始值和步长创建内存分配器
      Parameters:
      startValue - the starting value | 起始值
      step - the step size | 步长
      Returns:
      allocator | 分配器
    • allocate

      public SegmentAllocator.Segment allocate(String bizTag)
      Description copied from interface: SegmentAllocator
      Allocates a segment for the given business tag 为给定业务标识分配号段
      Specified by:
      allocate in interface SegmentAllocator
      Parameters:
      bizTag - the business tag | 业务标识
      Returns:
      allocated segment | 分配的号段
    • getStep

      public int getStep()
      Description copied from interface: SegmentAllocator
      Gets the step size 获取步长
      Specified by:
      getStep in interface SegmentAllocator
      Returns:
      step size | 步长
    • getCurrentValue

      public long getCurrentValue(String bizTag)
      Gets the current value for a business tag 获取业务标识的当前值
      Parameters:
      bizTag - the business tag | 业务标识
      Returns:
      current value, or startValue if not allocated | 当前值,如果未分配则返回起始值
    • reset

      public void reset(String bizTag)
      Resets the counter for a business tag 重置业务标识的计数器
      Parameters:
      bizTag - the business tag | 业务标识
    • resetAll

      public void resetAll()
      Resets all counters 重置所有计数器
    • getTagCount

      public int getTagCount()
      Gets the number of registered business tags 获取已注册的业务标识数量
      Returns:
      count | 数量