Class: Textus::Port::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/textus/port/store.rb

Overview

SQLite-backed runtime store for textus state. Owns the connection, schema setup, WAL mode, and transaction boundary for the index and queue.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root:) ⇒ Store

Returns a new instance of Store.



13
14
15
16
17
18
# File 'lib/textus/port/store.rb', line 13

def initialize(root:)
  @path = Textus::Store::Layout.new(root).store_db_path
  FileUtils.mkdir_p(File.dirname(@path))
  @connection = SQLite3::Database.new(@path)
  @connection.results_as_hash = true
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



11
12
13
# File 'lib/textus/port/store.rb', line 11

def path
  @path
end

Class Method Details

.open(root) ⇒ Object



118
119
120
121
122
123
124
125
126
# File 'lib/textus/port/store.rb', line 118

def self.open(root)
  store = new(root: root)
  store.setup!
  return store unless block_given?

  yield store
ensure
  store&.close
end

Instance Method Details

#audit_events_since(seq:) ⇒ Object



98
99
100
101
102
103
# File 'lib/textus/port/store.rb', line 98

def audit_events_since(seq:)
  execute(
    "SELECT seq, ts, role, verb, key, etag_before, etag_after FROM audit_events WHERE seq > ? ORDER BY seq",
    [seq],
  )
end

#closeObject



114
115
116
# File 'lib/textus/port/store.rb', line 114

def close
  connection.close unless connection.closed?
end

#execute(sql, params = []) ⇒ Object



20
21
22
# File 'lib/textus/port/store.rb', line 20

def execute(sql, params = [])
  @connection.execute(sql, params)
end

#insert_audit_event(seq:, ts:, role:, verb:, key:, etag_before:, etag_after:) ⇒ Object

rubocop:disable Naming/MethodParameterName



91
92
93
94
95
96
# File 'lib/textus/port/store.rb', line 91

def insert_audit_event(seq:, ts:, role:, verb:, key:, etag_before:, etag_after:) # rubocop:disable Naming/MethodParameterName
  execute(
    "INSERT OR IGNORE INTO audit_events (seq, ts, role, verb, key, etag_before, etag_after) VALUES (?, ?, ?, ?, ?, ?, ?)",
    [seq, ts, role, verb, key, etag_before, etag_after],
  )
end

#query_value(sql, params = []) ⇒ Object



24
25
26
# File 'lib/textus/port/store.rb', line 24

def query_value(sql, params = [])
  @connection.get_first_value(sql, params)
end

#search_entries(q: nil, schema: nil, lane: nil, prefix: nil) ⇒ Object

rubocop:disable Naming/MethodParameterName



81
82
83
84
85
86
87
88
89
# File 'lib/textus/port/store.rb', line 81

def search_entries(q: nil, schema: nil, lane: nil, prefix: nil) # rubocop:disable Naming/MethodParameterName
  return nil if q.nil? && schema.nil?

  if q
    fts_search(q: q, schema: schema, lane: lane, prefix: prefix)
  else
    schema_search(schema: schema, lane: lane, prefix: prefix)
  end
end

#setup!Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
73
74
75
76
77
78
79
# File 'lib/textus/port/store.rb', line 28

def setup!
  execute("PRAGMA journal_mode=WAL")
  execute("PRAGMA foreign_keys=ON")
  connection.execute_batch(<<~SQL)
    CREATE TABLE IF NOT EXISTS entries (
      key        TEXT PRIMARY KEY,
      lane       TEXT NOT NULL,
      format     TEXT NOT NULL,
      etag       TEXT,
      content    TEXT,
      extra      TEXT,
      indexed_at TEXT NOT NULL
    ) STRICT;

    CREATE VIRTUAL TABLE IF NOT EXISTS entries_fts USING fts5(
      key, lane, content,
      content=entries, content_rowid=rowid
    );

    CREATE TABLE IF NOT EXISTS jobs (
      id               TEXT PRIMARY KEY,
      type             TEXT NOT NULL,
      args             TEXT NOT NULL,
      state            TEXT NOT NULL DEFAULT 'ready',
      role             TEXT NOT NULL,
      attempts         INTEGER NOT NULL DEFAULT 0,
      max_attempts     INTEGER NOT NULL DEFAULT 3,
      errors           TEXT,
      lease            TEXT,
      created_at       TEXT NOT NULL,
      updated_at       TEXT NOT NULL
    ) STRICT;

    CREATE INDEX IF NOT EXISTS idx_jobs_state ON jobs(state);
    CREATE INDEX IF NOT EXISTS idx_entries_lane ON entries(lane);

    CREATE TABLE IF NOT EXISTS audit_events (
      seq          INTEGER PRIMARY KEY,
      ts           TEXT NOT NULL,
      role         TEXT NOT NULL,
      verb         TEXT NOT NULL,
      key          TEXT NOT NULL,
      etag_before  TEXT,
      etag_after   TEXT
    ) STRICT;

    CREATE INDEX IF NOT EXISTS idx_audit_events_seq ON audit_events(seq);
  SQL
  # Idempotent migration: add schema_ref column if missing (existing stores).
  execute("ALTER TABLE entries ADD COLUMN schema_ref TEXT") rescue nil # rubocop:disable Style/RescueModifier
  self
end

#transactionObject



105
106
107
108
109
110
111
112
# File 'lib/textus/port/store.rb', line 105

def transaction
  connection.transaction
  yield
  connection.commit
rescue StandardError
  connection.rollback if connection.transaction_active?
  raise
end