Class: Textus::Port::Store
- Inherits:
-
Object
- Object
- Textus::Port::Store
- 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
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
- #execute(sql, params = []) ⇒ Object
-
#initialize(root:) ⇒ Store
constructor
A new instance of Store.
- #query_value(sql, params = []) ⇒ Object
- #setup! ⇒ Object
- #transaction ⇒ Object
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
#path ⇒ Object (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
Instance Method Details
#close ⇒ Object
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 |
#transaction ⇒ Object
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 |