Class: SmartPrompt::PersistenceLayer

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_prompt/persistence_layer.rb

Overview

PersistenceLayer handles saving and loading session data to/from disk

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ PersistenceLayer

Returns a new instance of PersistenceLayer.



68
69
70
71
72
73
74
75
76
77
# File 'lib/smart_prompt/persistence_layer.rb', line 68

def initialize(config = {})
  @backend = config[:backend] || :filesystem
  @storage_path = config[:storage_path] || "./history_data"
  @async_writer = AsyncWriter.new
  @enabled = config[:enabled] != false
  @async = config[:async] != false

  # Create storage directory if persistence is enabled
  ensure_storage_directory if @enabled
end

Instance Attribute Details

#enabledObject (readonly)

Returns the value of attribute enabled.



66
67
68
# File 'lib/smart_prompt/persistence_layer.rb', line 66

def enabled
  @enabled
end

#storage_pathObject (readonly)

Returns the value of attribute storage_path.



66
67
68
# File 'lib/smart_prompt/persistence_layer.rb', line 66

def storage_path
  @storage_path
end

Instance Method Details

#delete(session_id) ⇒ Object

Delete a session from disk



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/smart_prompt/persistence_layer.rb', line 125

def delete(session_id)
  return unless @enabled

  file_path = session_file_path(session_id)
  if File.exist?(file_path)
    File.delete(file_path)
    SmartPrompt.logger.info "Session #{session_id} deleted from disk"
  end
rescue => e
  SmartPrompt.logger.error "Failed to delete session #{session_id}: #{e.message}"
end

#exists?(session_id) ⇒ Boolean

Check if a session exists on disk

Returns:

  • (Boolean)


138
139
140
141
142
143
# File 'lib/smart_prompt/persistence_layer.rb', line 138

def exists?(session_id)
  return false unless @enabled

  file_path = session_file_path(session_id)
  File.exist?(file_path)
end

#list_sessionsObject

List all session IDs stored on disk



146
147
148
149
150
151
152
153
154
155
# File 'lib/smart_prompt/persistence_layer.rb', line 146

def list_sessions
  return [] unless @enabled

  Dir.glob(File.join(@storage_path, "*.json")).map do |file|
    File.basename(file, ".json")
  end
rescue => e
  SmartPrompt.logger.error "Failed to list sessions: #{e.message}"
  []
end

#load(session_id) ⇒ Object

Load a session from disk



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/smart_prompt/persistence_layer.rb', line 108

def load(session_id)
  return nil unless @enabled

  file_path = session_file_path(session_id)
  return nil unless File.exist?(file_path)

  data = File.read(file_path)
  session_data = deserialize_session(data)
  
  SmartPrompt.logger.info "Session #{session_id} loaded from #{file_path}"
  session_data
rescue => e
  SmartPrompt.logger.error "Failed to load session #{session_id}: #{e.message}"
  nil
end

#save(session) ⇒ Object

Save a session synchronously



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/smart_prompt/persistence_layer.rb', line 80

def save(session)
  return unless @enabled

  file_path = session_file_path(session.id)
  data = serialize_session(session)

  File.write(file_path, data)
  SmartPrompt.logger.info "Session #{session.id} saved to #{file_path}"
rescue => e
  SmartPrompt.logger.error "Failed to save session #{session.id}: #{e.message}"
  # Continue operating with in-memory storage (fallback behavior)
end

#save_async(session) ⇒ Object

Save a session asynchronously



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/smart_prompt/persistence_layer.rb', line 94

def save_async(session)
  return unless @enabled

  if @async
    @async_writer.enqueue do
      save(session)
    end
  else
    # If async is disabled, fall back to synchronous save
    save(session)
  end
end

#shutdownObject

Stop the async writer gracefully



158
159
160
# File 'lib/smart_prompt/persistence_layer.rb', line 158

def shutdown
  @async_writer.stop if @async_writer
end