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
19
# File 'lib/textus/port/store.rb', line 13

def initialize(root:)
  @root = root
  @path = Textus::Store::Geometry.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



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

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

  yield store
ensure
  store&.close
end

Instance Method Details

#closeObject



77
78
79
# File 'lib/textus/port/store.rb', line 77

def close
  connection.close unless connection.closed?
end

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



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

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

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



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

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

#setup!Object



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
# File 'lib/textus/port/store.rb', line 29

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);
  SQL
  self
end

#transactionObject



68
69
70
71
72
73
74
75
# File 'lib/textus/port/store.rb', line 68

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