Class: Msnav::WindowsStore

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

Overview

Writable sidecar for msnav's own mutable state: the editor-window registry and open-command queue. coderag dropped this responsibility entirely (its index is a read-only contract now), so msnav owns the storage — and versions it itself via PRAGMA user_version. Lives next to the index in the hub's storage dir, shared through the same bind mount, so every msnav daemon on the hub sees the same windows. Created on demand; safe to delete (ephemeral liveness state).

Constant Summary collapse

USER_VERSION =
1
DDL =
[
  "CREATE TABLE IF NOT EXISTS windows (" \
  "window_id TEXT PRIMARY KEY, authority TEXT NOT NULL DEFAULT ''," \
  "roots TEXT NOT NULL, path_mappings TEXT NOT NULL," \
  "host_roots TEXT NOT NULL, seq INTEGER NOT NULL, last_poll REAL NOT NULL)",
  "CREATE TABLE IF NOT EXISTS window_commands (" \
  "id INTEGER PRIMARY KEY AUTOINCREMENT, window_id TEXT NOT NULL," \
  "payload TEXT NOT NULL, created_at REAL NOT NULL)",
  "CREATE INDEX IF NOT EXISTS idx_window_commands_window " \
  "ON window_commands(window_id)",
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ WindowsStore

Returns a new instance of WindowsStore.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/msnav/store.rb', line 139

def initialize(path)
  @path = Pathname.new(path.to_s)
  @path.parent.mkpath
  @mutex = Mutex.new
  @conn = SQLite3::Database.new(@path.to_s)
  @conn.busy_timeout = 5000
  @conn.execute("PRAGMA journal_mode=WAL")
  version = @conn.get_first_value("PRAGMA user_version").to_i
  if version > USER_VERSION
    raise IndexStaleError,
          "#{@path} was created by a newer msnav (registry v#{version}, " \
          "this msnav understands v#{USER_VERSION}) — upgrade msnav"
  end
  DDL.each { |stmt| @conn.execute(stmt) }
  @conn.execute("PRAGMA user_version = #{USER_VERSION}") if version.zero?
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



137
138
139
# File 'lib/msnav/store.rb', line 137

def path
  @path
end

Instance Method Details

#closeObject



160
161
162
163
164
# File 'lib/msnav/store.rb', line 160

def close
  @mutex.synchronize do
    @conn.close unless @conn.closed?
  end
end

#with_connObject



156
157
158
# File 'lib/msnav/store.rb', line 156

def with_conn
  @mutex.synchronize { yield @conn }
end