Class: VectorMCP::Transport::HttpStream::EventStore Private

Inherits:
Object
  • Object
show all
Defined in:
lib/vector_mcp/transport/http_stream/event_store.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Manages Server-Sent Events storage for resumable connections.

Handles:

  • Event storage with unique IDs

  • Event replay from a specific Last-Event-ID

  • Circular buffer for memory efficiency

  • Thread-safe operations

Defined Under Namespace

Classes: Event

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_events) ⇒ EventStore

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes a new event store.

Parameters:

  • max_events (Integer)

    Maximum number of events to retain



36
37
38
39
40
41
42
# File 'lib/vector_mcp/transport/http_stream/event_store.rb', line 36

def initialize(max_events)
  @max_events = max_events
  @events = Concurrent::Array.new
  @event_index = Concurrent::Hash.new # event_id -> logical position
  @offset = 0 # number of events shifted off the front
  @current_sequence = Concurrent::AtomicFixnum.new(0)
end

Instance Attribute Details

#loggerObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



31
32
33
# File 'lib/vector_mcp/transport/http_stream/event_store.rb', line 31

def logger
  @logger
end

#max_eventsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



31
32
33
# File 'lib/vector_mcp/transport/http_stream/event_store.rb', line 31

def max_events
  @max_events
end

Instance Method Details

#clearvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Clears all stored events.



138
139
140
141
142
# File 'lib/vector_mcp/transport/http_stream/event_store.rb', line 138

def clear
  @events.clear
  @event_index.clear
  @offset = 0
end

#event_countInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Gets the total number of stored events.

Returns:

  • (Integer)

    Number of events currently stored



109
110
111
# File 'lib/vector_mcp/transport/http_stream/event_store.rb', line 109

def event_count
  @events.length
end

#event_exists?(event_id) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Checks if an event ID exists in the store.

Parameters:

  • event_id (String)

    The event ID to check

Returns:

  • (Boolean)

    True if event exists



131
132
133
# File 'lib/vector_mcp/transport/http_stream/event_store.rb', line 131

def event_exists?(event_id)
  @event_index.key?(event_id)
end

#get_event(event_id) ⇒ Event?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Retrieves a specific event by ID.

Parameters:

  • event_id (String)

    The event ID to look up

Returns:

  • (Event, nil)

    The stored event, or nil if it is no longer retained



99
100
101
102
103
104
# File 'lib/vector_mcp/transport/http_stream/event_store.rb', line 99

def get_event(event_id)
  logical = @event_index[event_id]
  return nil if logical.nil?

  @events[logical - @offset]
end

#get_events_after(last_event_id, session_id: nil, stream_id: nil) ⇒ Array<Event>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Retrieves events starting from a specific event ID, optionally filtered by session.

Parameters:

  • last_event_id (String)

    The last event ID received by client

  • session_id (String, nil) (defaults to: nil)

    Filter events to this session only

  • stream_id (String, nil) (defaults to: nil)

    Filter events to this stream only

Returns:

  • (Array<Event>)

    Array of events after the specified ID



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/vector_mcp/transport/http_stream/event_store.rb', line 77

def get_events_after(last_event_id, session_id: nil, stream_id: nil)
  events = if last_event_id.nil?
             @events.to_a
           else
             logical = @event_index[last_event_id]
             return [] if logical.nil?

             physical = logical - @offset + 1
             return [] if physical >= @events.length

             @events[physical..]
           end

  events = events.select { |e| e.session_id == session_id } if session_id
  events = events.select { |e| e.stream_id == stream_id } if stream_id
  events
end

#newest_event_idString?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Gets the newest event ID (for debugging/monitoring).

Returns:

  • (String, nil)

    The newest event ID or nil if no events



123
124
125
# File 'lib/vector_mcp/transport/http_stream/event_store.rb', line 123

def newest_event_id
  @events.last&.id
end

#oldest_event_idString?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Gets the oldest event ID (for debugging/monitoring).

Returns:

  • (String, nil)

    The oldest event ID or nil if no events



116
117
118
# File 'lib/vector_mcp/transport/http_stream/event_store.rb', line 116

def oldest_event_id
  @events.first&.id
end

#statsHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Gets statistics about the event store.

Returns:

  • (Hash)

    Statistics hash



147
148
149
150
151
152
153
154
155
# File 'lib/vector_mcp/transport/http_stream/event_store.rb', line 147

def stats
  {
    total_events: event_count,
    max_events: @max_events,
    oldest_event_id: oldest_event_id,
    newest_event_id: newest_event_id,
    memory_usage_ratio: event_count.to_f / @max_events
  }
end

#store_event(data, type = nil, session_id: nil, stream_id: nil) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Stores a new event and returns its ID.

Parameters:

  • data (String)

    The event data

  • type (String) (defaults to: nil)

    The event type (optional)

  • session_id (String, nil) (defaults to: nil)

    The session ID to scope this event to

  • stream_id (String, nil) (defaults to: nil)

    The stream ID to scope this event to

Returns:

  • (String)

    The generated event ID



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/vector_mcp/transport/http_stream/event_store.rb', line 51

def store_event(data, type = nil, session_id: nil, stream_id: nil)
  event_id = generate_event_id
  timestamp = Time.now

  event = Event.new(event_id, data, type, timestamp, session_id, stream_id)

  # Add to events array and record logical position
  @events.push(event)
  @event_index[event_id] = @offset + @events.length - 1

  # Maintain circular buffer
  if @events.length > @max_events
    removed_event = @events.shift
    @event_index.delete(removed_event.id)
    @offset += 1
  end

  event_id
end