Class: EventSystem::Storage::FileStore
- Defined in:
- lib/event_system/storage/file_store.rb
Overview
File-based event storage implementation Events are stored in JSONL (JSON Lines) format, one event per line
Instance Attribute Summary collapse
-
#current_session ⇒ String
readonly
Get the current session ID.
-
#storage_path ⇒ Object
readonly
Returns the value of attribute storage_path.
Instance Method Summary collapse
-
#close ⇒ void
Close the file handle.
-
#create_session(session_id = nil) ⇒ String
Create a new session.
-
#initialize(directory = "event_logs", session_id = nil) ⇒ FileStore
constructor
Initialize a new file-based event store.
-
#list_sessions ⇒ Array<String>
List available sessions.
-
#load_session(session_id = nil) ⇒ Array<EventSystem::Event>
Load all events from a session.
-
#query(options = {}) ⇒ Array<EventSystem::Event>
Query for events based on options This implementation loads the session and filters in memory For more advanced querying needs, consider using a database.
-
#stats ⇒ Hash
Get storage statistics.
-
#store(event) ⇒ void
Store an event to disk.
-
#switch_session(session_id) ⇒ void
Switch to a different session.
Methods inherited from Base
Constructor Details
#initialize(directory = "event_logs", session_id = nil) ⇒ FileStore
Initialize a new file-based event store
17 18 19 20 21 22 23 24 |
# File 'lib/event_system/storage/file_store.rb', line 17 def initialize(directory = "event_logs", session_id = nil) @directory = directory @storage_path = directory @current_session = session_id || Time.now.strftime("%Y%m%d_%H%M%S") @current_file = nil FileUtils.mkdir_p(@directory) unless Dir.exist?(@directory) end |
Instance Attribute Details
#current_session ⇒ String (readonly)
Get the current session ID
111 112 113 |
# File 'lib/event_system/storage/file_store.rb', line 111 def current_session @current_session end |
#storage_path ⇒ Object (readonly)
Returns the value of attribute storage_path.
12 13 14 |
# File 'lib/event_system/storage/file_store.rb', line 12 def storage_path @storage_path end |
Instance Method Details
#close ⇒ void
This method returns an undefined value.
Close the file handle
134 135 136 |
# File 'lib/event_system/storage/file_store.rb', line 134 def close close_current_file end |
#create_session(session_id = nil) ⇒ String
Create a new session
126 127 128 129 130 |
# File 'lib/event_system/storage/file_store.rb', line 126 def create_session(session_id = nil) close_current_file @current_session = session_id || Time.now.strftime("%Y%m%d_%H%M%S") @current_session end |
#list_sessions ⇒ Array<String>
List available sessions
103 104 105 106 107 |
# File 'lib/event_system/storage/file_store.rb', line 103 def list_sessions Dir.glob(File.join(@directory, "events_*.jsonl")).map do |file| File.basename(file).gsub(/^events_/, "").gsub(/\.jsonl$/, "") end.sort end |
#load_session(session_id = nil) ⇒ Array<EventSystem::Event>
Load all events from a session
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/event_system/storage/file_store.rb', line 77 def load_session(session_id = nil) session_id ||= @current_session events = [] # Try different filename patterns for backward compatibility filename = find_session_file(session_id) return [] unless filename && File.exist?(filename) File.open(filename, "r") do |file| file.each_line do |line| next if line.strip.empty? begin events << EventSystem::Event.from_json(line) rescue JSON::ParserError => e # Skip malformed lines but log the error warn "Skipping malformed event line: #{e.message}" end end end events end |
#query(options = {}) ⇒ Array<EventSystem::Event>
Query for events based on options This implementation loads the session and filters in memory For more advanced querying needs, consider using a database
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/event_system/storage/file_store.rb', line 45 def query( = {}) session_id = [:session_id] || @current_session events = load_session(session_id) # Filter by type if [:type] events = events.select { |e| e.type == [:type] } end # Filter by time range if [:start_time] events = events.select { |e| e. >= [:start_time] } end if [:end_time] events = events.select { |e| e. <= [:end_time] } end # Sort by timestamp (oldest first) events = events.sort_by(&:timestamp) # Limit results if [:limit] events = events.last([:limit]) end events end |
#stats ⇒ Hash
Get storage statistics
140 141 142 143 144 145 146 |
# File 'lib/event_system/storage/file_store.rb', line 140 def stats super.merge( directory: @directory, current_session_file: current_session_file, total_sessions: list_sessions.length ) end |
#store(event) ⇒ void
This method returns an undefined value.
Store an event to disk
29 30 31 32 33 |
# File 'lib/event_system/storage/file_store.rb', line 29 def store(event) ensure_file_open @current_file.puts(event.to_json) @current_file.flush # Ensure data is written immediately end |
#switch_session(session_id) ⇒ void
This method returns an undefined value.
Switch to a different session
118 119 120 121 |
# File 'lib/event_system/storage/file_store.rb', line 118 def switch_session(session_id) close_current_file @current_session = session_id end |