Class: ClaudeMemory::Store::StoreManager
- Inherits:
-
Object
- Object
- ClaudeMemory::Store::StoreManager
- Defined in:
- lib/claude_memory/store/store_manager.rb
Overview
Dual-database connection manager for global and project stores. Lazily opens SQLiteStore connections to the global database (~/.claude/memory.sqlite3) and the project database (.claude/memory.sqlite3 under the project root). Commands query both databases by default, with project facts taking precedence.
Instance Attribute Summary collapse
-
#global_db_path ⇒ String
readonly
Filesystem path to the global database.
-
#global_store ⇒ SQLiteStore?
readonly
Global store (nil until #ensure_global! is called).
-
#project_db_path ⇒ String
readonly
Filesystem path to the project database.
-
#project_path ⇒ String
readonly
Project directory path.
-
#project_store ⇒ SQLiteStore?
readonly
Project store (nil until #ensure_project! is called).
Class Method Summary collapse
-
.default_global_db_path(env = ENV) ⇒ String
Default global database path from Configuration.
-
.default_project_db_path(project_path = Dir.pwd) ⇒ String
Default project database path for a given project directory.
Instance Method Summary collapse
-
#close ⇒ void
Close both database connections and reset store references.
-
#ensure_both! ⇒ void
Open both global and project stores.
-
#ensure_global! ⇒ SQLiteStore
Open the global store, creating the directory and database if needed.
-
#ensure_project! ⇒ SQLiteStore
Open the project store, creating the directory and database if needed.
-
#global_exists? ⇒ Boolean
Check whether the global database file exists on disk.
-
#initialize(global_db_path: nil, project_db_path: nil, project_path: nil, env: ENV) ⇒ StoreManager
constructor
A new instance of StoreManager.
-
#project_exists? ⇒ Boolean
Check whether the project database file exists on disk.
-
#promote_fact(fact_id) ⇒ Integer?
Copy a project-scoped fact (with its entities and provenance) into the global store, making it available across all projects.
-
#store_for_scope(scope) ⇒ SQLiteStore
Return the appropriate store for a given scope string.
Constructor Details
#initialize(global_db_path: nil, project_db_path: nil, project_path: nil, env: ENV) ⇒ StoreManager
Returns a new instance of StoreManager.
26 27 28 29 30 31 32 33 34 |
# File 'lib/claude_memory/store/store_manager.rb', line 26 def initialize(global_db_path: nil, project_db_path: nil, project_path: nil, env: ENV) config = Configuration.new(env) @project_path = project_path || config.project_dir @global_db_path = global_db_path || config.global_db_path @project_db_path = project_db_path || config.project_db_path(@project_path) @global_store = nil @project_store = nil end |
Instance Attribute Details
#global_db_path ⇒ String (readonly)
Returns filesystem path to the global database.
78 79 80 |
# File 'lib/claude_memory/store/store_manager.rb', line 78 def global_db_path @global_db_path end |
#global_store ⇒ SQLiteStore? (readonly)
Returns global store (nil until #ensure_global! is called).
14 15 16 |
# File 'lib/claude_memory/store/store_manager.rb', line 14 def global_store @global_store end |
#project_db_path ⇒ String (readonly)
Returns filesystem path to the project database.
81 82 83 |
# File 'lib/claude_memory/store/store_manager.rb', line 81 def project_db_path @project_db_path end |
#project_path ⇒ String (readonly)
Returns project directory path.
20 21 22 |
# File 'lib/claude_memory/store/store_manager.rb', line 20 def project_path @project_path end |
#project_store ⇒ SQLiteStore? (readonly)
Returns project store (nil until #ensure_project! is called).
17 18 19 |
# File 'lib/claude_memory/store/store_manager.rb', line 17 def project_store @project_store end |
Class Method Details
.default_global_db_path(env = ENV) ⇒ String
Default global database path from Configuration.
39 40 41 |
# File 'lib/claude_memory/store/store_manager.rb', line 39 def self.default_global_db_path(env = ENV) Configuration.new(env).global_db_path end |
.default_project_db_path(project_path = Dir.pwd) ⇒ String
Default project database path for a given project directory.
46 47 48 |
# File 'lib/claude_memory/store/store_manager.rb', line 46 def self.default_project_db_path(project_path = Dir.pwd) Configuration.new.project_db_path(project_path) end |
Instance Method Details
#close ⇒ void
This method returns an undefined value.
Close both database connections and reset store references.
97 98 99 100 101 102 |
# File 'lib/claude_memory/store/store_manager.rb', line 97 def close @global_store&.close @project_store&.close @global_store = nil @project_store = nil end |
#ensure_both! ⇒ void
This method returns an undefined value.
Open both global and project stores.
72 73 74 75 |
# File 'lib/claude_memory/store/store_manager.rb', line 72 def ensure_both! ensure_global! ensure_project! end |
#ensure_global! ⇒ SQLiteStore
Open the global store, creating the directory and database if needed. No-op if already open.
53 54 55 56 57 58 |
# File 'lib/claude_memory/store/store_manager.rb', line 53 def ensure_global! return @global_store if @global_store FileUtils.mkdir_p(File.dirname(@global_db_path)) @global_store = SQLiteStore.new(@global_db_path) end |
#ensure_project! ⇒ SQLiteStore
Open the project store, creating the directory and database if needed. No-op if already open.
63 64 65 66 67 68 |
# File 'lib/claude_memory/store/store_manager.rb', line 63 def ensure_project! return @project_store if @project_store FileUtils.mkdir_p(File.dirname(@project_db_path)) @project_store = SQLiteStore.new(@project_db_path) end |
#global_exists? ⇒ Boolean
Check whether the global database file exists on disk.
85 86 87 |
# File 'lib/claude_memory/store/store_manager.rb', line 85 def global_exists? File.exist?(@global_db_path) end |
#project_exists? ⇒ Boolean
Check whether the project database file exists on disk.
91 92 93 |
# File 'lib/claude_memory/store/store_manager.rb', line 91 def project_exists? File.exist?(@project_db_path) end |
#promote_fact(fact_id) ⇒ Integer?
Copy a project-scoped fact (with its entities and provenance) into the global store, making it available across all projects. Runs the global writes in a single transaction for atomicity.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/claude_memory/store/store_manager.rb', line 128 def promote_fact(fact_id) ensure_both! fact = @project_store.facts.where(id: fact_id).first return nil unless fact subject = @project_store.entities.where(id: fact[:subject_entity_id]).first return nil unless subject # Read all project data before entering global transaction object = fact[:object_entity_id] ? @project_store.entities.where(id: fact[:object_entity_id]).first : nil provenance_records = @project_store.provenance.where(fact_id: fact_id).all # Wrap all global database operations in a transaction for atomicity @global_store.db.transaction do global_subject_id = @global_store.find_or_create_entity( type: subject[:type], name: subject[:canonical_name] ) global_object_id = nil if object global_object_id = @global_store.find_or_create_entity( type: object[:type], name: object[:canonical_name] ) end global_fact_id = @global_store.insert_fact( subject_entity_id: global_subject_id, predicate: fact[:predicate], object_entity_id: global_object_id, object_literal: fact[:object_literal], datatype: fact[:datatype], polarity: fact[:polarity], valid_from: fact[:valid_from], status: fact[:status], confidence: fact[:confidence], created_from: "promoted:#{@project_path}:#{fact_id}", scope: "global", project_path: nil ) provenance_records.each do |prov| @global_store.insert_provenance( fact_id: global_fact_id, content_item_id: nil, quote: prov[:quote], attribution_entity_id: nil, strength: prov[:strength] ) end global_fact_id end end |
#store_for_scope(scope) ⇒ SQLiteStore
Return the appropriate store for a given scope string.
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/claude_memory/store/store_manager.rb', line 108 def store_for_scope(scope) case scope when "global" ensure_global! @global_store when "project" ensure_project! @project_store else raise ArgumentError, "Invalid scope: #{scope}. Use 'global' or 'project'" end end |