Class MemorySegmentAllocator
java.lang.Object
cloud.opencode.base.id.segment.MemorySegmentAllocator
- All Implemented Interfaces:
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:
-
Nested Class Summary
Nested classes/interfaces inherited from interface SegmentAllocator
SegmentAllocator.Segment -
Field Summary
Fields inherited from interface SegmentAllocator
DEFAULT_STEP -
Method Summary
Modifier and TypeMethodDescriptionAllocates a segment for the given business tag 为给定业务标识分配号段static MemorySegmentAllocatorcreate()Creates a memory allocator with default settings 使用默认设置创建内存分配器static MemorySegmentAllocatorcreate(int step) Creates a memory allocator with custom step 使用自定义步长创建内存分配器static MemorySegmentAllocatorcreate(long startValue, int step) Creates a memory allocator with custom start value and step 使用自定义起始值和步长创建内存分配器longgetCurrentValue(String bizTag) Gets the current value for a business tag 获取业务标识的当前值intgetStep()Gets the step size 获取步长intGets the number of registered business tags 获取已注册的业务标识数量voidResets the counter for a business tag 重置业务标识的计数器voidresetAll()Resets all counters 重置所有计数器
-
Method Details
-
create
Creates a memory allocator with default settings 使用默认设置创建内存分配器- Returns:
- allocator | 分配器
-
create
Creates a memory allocator with custom step 使用自定义步长创建内存分配器- Parameters:
step- the step size | 步长- Returns:
- allocator | 分配器
-
create
Creates a memory allocator with custom start value and step 使用自定义起始值和步长创建内存分配器- Parameters:
startValue- the starting value | 起始值step- the step size | 步长- Returns:
- allocator | 分配器
-
allocate
Description copied from interface:SegmentAllocatorAllocates a segment for the given business tag 为给定业务标识分配号段- Specified by:
allocatein interfaceSegmentAllocator- Parameters:
bizTag- the business tag | 业务标识- Returns:
- allocated segment | 分配的号段
-
getStep
public int getStep()Description copied from interface:SegmentAllocatorGets the step size 获取步长- Specified by:
getStepin interfaceSegmentAllocator- Returns:
- step size | 步长
-
getCurrentValue
Gets the current value for a business tag 获取业务标识的当前值- Parameters:
bizTag- the business tag | 业务标识- Returns:
- current value, or startValue if not allocated | 当前值,如果未分配则返回起始值
-
reset
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 | 数量
-