Class: KBS::Blackboard::Persistence::HybridStore

Inherits:
Store
  • Object
show all
Defined in:
lib/kbs/blackboard/persistence/hybrid_store.rb

Overview

Hybrid store combining Redis (facts, messages) with SQLite (audit trail)

Benefits:

  • Fast in-memory fact access via Redis

  • Durable audit trail via SQLite

  • Best of both worlds for production systems

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redis_url: 'redis://localhost:6379/0', redis: nil, db_path: 'audit.db', session_id: nil) ⇒ HybridStore

Returns a new instance of HybridStore.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/kbs/blackboard/persistence/hybrid_store.rb', line 19

def initialize(
  redis_url: 'redis://localhost:6379/0',
  redis: nil,
  db_path: 'audit.db',
  session_id: nil
)
  @session_id = session_id

  # Redis for hot data (facts, messages)
  @redis_store = RedisStore.new(
    url: redis_url,
    redis: redis,
    session_id: @session_id
  )

  # SQLite for cold data (audit trail)
  @sqlite_store = SqliteStore.new(
    db_path: db_path,
    session_id: @session_id
  )
end

Instance Attribute Details

#redis_storeObject (readonly)

Returns the value of attribute redis_store.



17
18
19
# File 'lib/kbs/blackboard/persistence/hybrid_store.rb', line 17

def redis_store
  @redis_store
end

#session_idObject (readonly)

Returns the value of attribute session_id.



17
18
19
# File 'lib/kbs/blackboard/persistence/hybrid_store.rb', line 17

def session_id
  @session_id
end

#sqlite_storeObject (readonly)

Returns the value of attribute sqlite_store.



17
18
19
# File 'lib/kbs/blackboard/persistence/hybrid_store.rb', line 17

def sqlite_store
  @sqlite_store
end

Instance Method Details

#add_fact(uuid, type, attributes) ⇒ Object

Fact operations delegated to Redis (fast)



42
43
44
# File 'lib/kbs/blackboard/persistence/hybrid_store.rb', line 42

def add_fact(uuid, type, attributes)
  @redis_store.add_fact(uuid, type, attributes)
end

#clear_session(session_id) ⇒ Object



70
71
72
# File 'lib/kbs/blackboard/persistence/hybrid_store.rb', line 70

def clear_session(session_id)
  @redis_store.clear_session(session_id)
end

#closeObject



96
97
98
99
# File 'lib/kbs/blackboard/persistence/hybrid_store.rb', line 96

def close
  @redis_store.close
  @sqlite_store.close
end

#connectionObject

Provide access to both connections for MessageQueue/AuditLog Memory class will detect hybrid store and use appropriate components



103
104
105
# File 'lib/kbs/blackboard/persistence/hybrid_store.rb', line 103

def connection
  @redis_store.connection
end

#dbObject



107
108
109
# File 'lib/kbs/blackboard/persistence/hybrid_store.rb', line 107

def db
  @sqlite_store.db
end

#get_fact(uuid) ⇒ Object



54
55
56
# File 'lib/kbs/blackboard/persistence/hybrid_store.rb', line 54

def get_fact(uuid)
  @redis_store.get_fact(uuid)
end

#get_facts(type = nil, pattern = {}) ⇒ Object



58
59
60
# File 'lib/kbs/blackboard/persistence/hybrid_store.rb', line 58

def get_facts(type = nil, pattern = {})
  @redis_store.get_facts(type, pattern)
end

#hybrid?Boolean

Helper to check if this is a hybrid store

Returns:

  • (Boolean)


112
113
114
# File 'lib/kbs/blackboard/persistence/hybrid_store.rb', line 112

def hybrid?
  true
end

#query_facts(conditions = nil, params = []) ⇒ Object



62
63
64
# File 'lib/kbs/blackboard/persistence/hybrid_store.rb', line 62

def query_facts(conditions = nil, params = [])
  @redis_store.query_facts(conditions, params)
end

#register_knowledge_source(name, description: nil, topics: []) ⇒ Object



66
67
68
# File 'lib/kbs/blackboard/persistence/hybrid_store.rb', line 66

def register_knowledge_source(name, description: nil, topics: [])
  @redis_store.register_knowledge_source(name, description: description, topics: topics)
end

#remove_fact(uuid) ⇒ Object



46
47
48
# File 'lib/kbs/blackboard/persistence/hybrid_store.rb', line 46

def remove_fact(uuid)
  @redis_store.remove_fact(uuid)
end

#statsObject

Stats combined from both stores



75
76
77
78
79
80
81
82
83
# File 'lib/kbs/blackboard/persistence/hybrid_store.rb', line 75

def stats
  redis_stats = @redis_store.stats
  sqlite_stats = @sqlite_store.stats

  # Prefer Redis for fact counts (authoritative)
  redis_stats.merge(
    audit_records: sqlite_stats[:total_facts] # SQLite tracks audit records
  )
end

#transaction(&block) ⇒ Object



90
91
92
93
94
# File 'lib/kbs/blackboard/persistence/hybrid_store.rb', line 90

def transaction(&block)
  # Redis and SQLite transactions are separate
  # Execute block in context of both
  yield
end

#update_fact(uuid, attributes) ⇒ Object



50
51
52
# File 'lib/kbs/blackboard/persistence/hybrid_store.rb', line 50

def update_fact(uuid, attributes)
  @redis_store.update_fact(uuid, attributes)
end

#vacuumObject



85
86
87
88
# File 'lib/kbs/blackboard/persistence/hybrid_store.rb', line 85

def vacuum
  @redis_store.vacuum
  @sqlite_store.vacuum
end