Class InMemoryEventStore

java.lang.Object
cloud.opencode.base.event.store.InMemoryEventStore
All Implemented Interfaces:
EventStore

public class InMemoryEventStore extends Object implements EventStore
In-Memory Event Store 内存事件存储

Thread-safe in-memory implementation of EventStore.

EventStore的线程安全内存实现。

Features | 主要功能:

  • Thread-safe storage - 线程安全存储
  • Automatic capacity management - 自动容量管理
  • Event replay support - 事件重放支持
  • Time-based queries - 基于时间的查询

Usage Examples | 使用示例:

InMemoryEventStore store = new InMemoryEventStore(10000);
store.save(event);

// Query events
List<EventRecord> records = store.findByType(OrderCreatedEvent.class);

// Replay events
store.replay(OrderEvent.class, e -> processEvent(e));
Since:
JDK 25, opencode-base-event V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • InMemoryEventStore

      public InMemoryEventStore()
      Create in-memory event store with default capacity (10000) 使用默认容量(10000)创建内存事件存储
    • InMemoryEventStore

      public InMemoryEventStore(int maxCapacity)
      Create in-memory event store with specified capacity 使用指定容量创建内存事件存储
      Parameters:
      maxCapacity - the maximum number of events to store | 存储的最大事件数
  • Method Details

    • save

      public EventRecord save(Event event)
      Description copied from interface: EventStore
      Save an event to the store 将事件保存到存储
      Specified by:
      save in interface EventStore
      Parameters:
      event - the event to save | 要保存的事件
      Returns:
      the saved event record | 保存的事件记录
    • findById

      public Optional<EventRecord> findById(String eventId)
      Description copied from interface: EventStore
      Find event record by event ID 根据事件ID查找事件记录
      Specified by:
      findById in interface EventStore
      Parameters:
      eventId - the event ID | 事件ID
      Returns:
      optional containing the event record if found | 如果找到则包含事件记录的Optional
    • findByType

      public List<EventRecord> findByType(Class<? extends Event> eventType)
      Description copied from interface: EventStore
      Find all event records by event type 根据事件类型查找所有事件记录
      Specified by:
      findByType in interface EventStore
      Parameters:
      eventType - the event type class | 事件类型类
      Returns:
      list of event records | 事件记录列表
    • findByTimeRange

      public List<EventRecord> findByTimeRange(Instant from, Instant to)
      Description copied from interface: EventStore
      Find event records within a time range 在时间范围内查找事件记录
      Specified by:
      findByTimeRange in interface EventStore
      Parameters:
      from - start time (inclusive) | 开始时间(包含)
      to - end time (exclusive) | 结束时间(不包含)
      Returns:
      list of event records | 事件记录列表
    • findBySource

      public List<EventRecord> findBySource(String source)
      Description copied from interface: EventStore
      Find event records by source 根据来源查找事件记录
      Specified by:
      findBySource in interface EventStore
      Parameters:
      source - the event source | 事件来源
      Returns:
      list of event records | 事件记录列表
    • replay

      public void replay(Class<? extends Event> eventType, Consumer<Event> handler)
      Description copied from interface: EventStore
      Replay events of a specific type 重放特定类型的事件
      Specified by:
      replay in interface EventStore
      Parameters:
      eventType - the event type to replay | 要重放的事件类型
      handler - the handler to process each event | 处理每个事件的处理器
    • replayByTimeRange

      public void replayByTimeRange(Instant from, Instant to, Consumer<Event> handler)
      Description copied from interface: EventStore
      Replay events within a time range 在时间范围内重放事件
      Specified by:
      replayByTimeRange in interface EventStore
      Parameters:
      from - start time (inclusive) | 开始时间(包含)
      to - end time (exclusive) | 结束时间(不包含)
      handler - the handler to process each event | 处理每个事件的处理器
    • count

      public long count()
      Description copied from interface: EventStore
      Get the total count of stored events 获取存储事件的总数
      Specified by:
      count in interface EventStore
      Returns:
      total event count | 事件总数
    • clear

      public void clear()
      Description copied from interface: EventStore
      Clear all stored events 清除所有存储的事件
      Specified by:
      clear in interface EventStore
    • getMaxCapacity

      public int getMaxCapacity()
      Get the maximum capacity 获取最大容量
      Returns:
      the maximum capacity | 最大容量
    • findAll

      public List<EventRecord> findAll()
      Get all event records 获取所有事件记录
      Returns:
      list of all event records | 所有事件记录列表
    • getCurrentSequence

      public long getCurrentSequence()
      Get the current sequence number 获取当前序列号
      Returns:
      current sequence number | 当前序列号