Class: Dbwatcher::Storage::SessionStorage
- Inherits:
-
BaseStorage
- Object
- BaseStorage
- Dbwatcher::Storage::SessionStorage
- Includes:
- Concerns::DataNormalizer, Concerns::Validatable
- Defined in:
- lib/dbwatcher/storage/session_storage.rb
Overview
Handles persistence and retrieval of database monitoring sessions
This class manages the storage of session data including metadata, timestamps, and associated database changes. Sessions are stored as individual JSON files with an index for efficient querying. Follows Ruby style guide patterns for storage class organization.
Constant Summary collapse
- DEFAULT_INDEX_FILENAME =
Configuration constants
"index.json"
- SESSIONS_DIRECTORY =
"sessions"
Constants inherited from BaseStorage
BaseStorage::DEFAULT_PERMISSIONS, BaseStorage::JSON_FILE_EXTENSION
Instance Attribute Summary collapse
-
#index_file ⇒ String
readonly
Path to index file.
-
#mutex ⇒ Mutex
readonly
Thread safety mutex.
-
#operations ⇒ SessionOperations
readonly
Operations helper.
-
#sessions_path ⇒ String
readonly
Path to sessions directory.
Attributes inherited from BaseStorage
Instance Method Summary collapse
-
#all ⇒ Array<Hash>
Returns all session summaries from the index.
-
#cleanup_old_sessions ⇒ void
Removes old session files based on configuration.
-
#clear_all ⇒ Integer
Clears all session storage.
-
#count ⇒ Integer
Counts total number of sessions.
-
#exists?(id) ⇒ Boolean
Checks if a session exists.
-
#find(id) ⇒ Session?
Finds a session by ID.
-
#find!(id) ⇒ Session
Finds a session by ID or raises an exception.
-
#initialize(storage_path = nil) ⇒ SessionStorage
constructor
Initializes a new SessionStorage instance.
-
#load(id) ⇒ Session, NullSession
deprecated
Deprecated.
Use #find instead
-
#save(session) ⇒ Boolean
Persists a session to storage.
-
#save!(session) ⇒ Boolean
Alternative save method that raises on failure.
Methods included from Concerns::DataNormalizer
#extract_value, #normalize_change, #normalize_hash_keys, #normalize_operation, #normalize_record_id, #normalize_session_data, #normalize_table_name, #normalize_timestamp
Methods included from Concerns::Validatable
included, #valid_id?, #valid_name?, #validate_id!, #validate_name!, #validate_presence!
Methods included from Concerns::Timestampable
#age, included, #initialize_timestamps, #recently_created?, #recently_updated?, #touch_updated_at
Methods included from Concerns::ErrorHandler
#safe_operation, #with_error_handling
Constructor Details
#initialize(storage_path = nil) ⇒ SessionStorage
Initializes a new SessionStorage instance
Sets up the necessary directories and index files for session storage. Creates the sessions directory and index file if they don’t exist. Includes thread safety for concurrent operations.
56 57 58 59 60 61 62 63 64 |
# File 'lib/dbwatcher/storage/session_storage.rb', line 56 def initialize(storage_path = nil) super @sessions_path = File.join(self.storage_path, SESSIONS_DIRECTORY) @index_file = File.join(self.storage_path, DEFAULT_INDEX_FILENAME) @operations = SessionOperations.new(@sessions_path, @index_file) @mutex = Mutex.new setup_directories end |
Instance Attribute Details
#index_file ⇒ String (readonly)
Returns path to index file.
41 42 43 |
# File 'lib/dbwatcher/storage/session_storage.rb', line 41 def index_file @index_file end |
#mutex ⇒ Mutex (readonly)
Returns thread safety mutex.
47 48 49 |
# File 'lib/dbwatcher/storage/session_storage.rb', line 47 def mutex @mutex end |
#operations ⇒ SessionOperations (readonly)
Returns operations helper.
44 45 46 |
# File 'lib/dbwatcher/storage/session_storage.rb', line 44 def operations @operations end |
#sessions_path ⇒ String (readonly)
Returns path to sessions directory.
38 39 40 |
# File 'lib/dbwatcher/storage/session_storage.rb', line 38 def sessions_path @sessions_path end |
Instance Method Details
#all ⇒ Array<Hash>
Returns all session summaries from the index
147 148 149 |
# File 'lib/dbwatcher/storage/session_storage.rb', line 147 def all safe_read_json(index_file) end |
#cleanup_old_sessions ⇒ void
This method returns an undefined value.
Removes old session files based on configuration
Automatically called after each save operation to maintain storage size within configured limits.
195 196 197 198 199 200 |
# File 'lib/dbwatcher/storage/session_storage.rb', line 195 def cleanup_old_sessions return unless cleanup_enabled? cutoff_date = calculate_cleanup_cutoff remove_old_session_files(cutoff_date) end |
#clear_all ⇒ Integer
Clears all session storage
Removes all session files and reinitializes the storage structure. This operation cannot be undone.
175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/dbwatcher/storage/session_storage.rb', line 175 def clear_all with_error_handling("clear all sessions") do # Count files before deleting file_count = count_session_files safe_delete_directory(sessions_path) safe_write_json(index_file, []) setup_directories touch_updated_at file_count end end |
#count ⇒ Integer
Counts total number of sessions
165 166 167 |
# File 'lib/dbwatcher/storage/session_storage.rb', line 165 def count all.size end |
#exists?(id) ⇒ Boolean
Checks if a session exists
155 156 157 158 159 160 |
# File 'lib/dbwatcher/storage/session_storage.rb', line 155 def exists?(id) return false unless valid_id?(id) session_file = operations.session_file_path(id) file_manager.file_exists?(session_file) end |
#find(id) ⇒ Session?
Finds a session by ID
Retrieves session data from storage and constructs a Session object. Returns nil if the session is not found.
117 118 119 120 121 122 123 124 |
# File 'lib/dbwatcher/storage/session_storage.rb', line 117 def find(id) return nil unless valid_id?(id) session_data = load_session_data(id) return nil if session_data.empty? build_session_from_data(session_data) end |
#find!(id) ⇒ Session
Finds a session by ID or raises an exception
131 132 133 |
# File 'lib/dbwatcher/storage/session_storage.rb', line 131 def find!(id) find(id) or raise SessionNotFoundError, "Session with id '#{id}' not found" end |
#load(id) ⇒ Session, NullSession
Use #find instead
Loads a session by ID (legacy method, use find instead)
140 141 142 |
# File 'lib/dbwatcher/storage/session_storage.rb', line 140 def load(id) find(id) || NullSession.instance end |
#save(session) ⇒ Boolean
Persists a session to storage
Saves the session data to a JSON file and updates the session index. Automatically triggers cleanup of old sessions after successful save. Uses thread safety to prevent concurrent write conflicts.
rubocop:disable Naming/PredicateMethod
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/dbwatcher/storage/session_storage.rb', line 80 def save(session) session_data = normalize_session_data(session) validate_session_data!(session_data) mutex.synchronize do persist_session_file(session_data) update_session_index(session_data) trigger_cleanup end true end |
#save!(session) ⇒ Boolean
Alternative save method that raises on failure
100 101 102 103 104 |
# File 'lib/dbwatcher/storage/session_storage.rb', line 100 def save!(session) with_error_handling("save session") do save(session) or raise StorageError, "Failed to save session" end end |