Packages

A Gleam library for building and orchestrating agents on the BEAM.

Current section

Files

Jump to
pig src pig@workspace@schema.erl
Raw

src/pig@workspace@schema.erl

-module(pig@workspace@schema).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/pig/workspace/schema.gleam").
-export([init/1]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(" Schema initialization for pig workspace.\n").
-file("src/pig/workspace/schema.gleam", 23).
?DOC(
" Initialize the database schema for the pig workspace.\n"
"\n"
" This function creates all necessary tables and indexes for the\n"
" virtual filesystem and key-value store. It is idempotent, meaning\n"
" it can be called multiple times without error.\n"
"\n"
" # Pragmas\n"
" - Sets WAL mode for better concurrency\n"
" - Sets busy timeout to 5 seconds\n"
"\n"
" # Tables Created\n"
" - `vfs_inode`: File inode metadata\n"
" - `vfs_dentry`: Directory entries (name -> inode mapping)\n"
" - `vfs_data`: File data chunks\n"
" - `kv_store`: Key-value storage\n"
"\n"
" # Root Directory\n"
" Creates the root directory inode (ino=1, mode=16877) if it doesn't exist.\n"
).
-spec init(sqlight:connection()) -> {ok, nil} | {error, sqlight:error()}.
init(Conn) ->
Schema_sql = <<"
PRAGMA journal_mode = WAL;
PRAGMA busy_timeout = 5000;
CREATE TABLE IF NOT EXISTS vfs_inode (
ino INTEGER PRIMARY KEY AUTOINCREMENT,
mode INTEGER NOT NULL,
size INTEGER NOT NULL DEFAULT 0,
mtime INTEGER NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_vfs_inode_mtime ON vfs_inode(mtime);
CREATE TABLE IF NOT EXISTS vfs_dentry (
name TEXT NOT NULL,
parent_ino INTEGER NOT NULL,
ino INTEGER NOT NULL,
UNIQUE(parent_ino, name)
);
CREATE INDEX IF NOT EXISTS idx_vfs_dentry_parent
ON vfs_dentry(parent_ino, name);
CREATE TABLE IF NOT EXISTS vfs_data (
ino INTEGER NOT NULL,
chunk_index INTEGER NOT NULL,
data BLOB NOT NULL,
PRIMARY KEY (ino, chunk_index)
);
CREATE TABLE IF NOT EXISTS kv_store (
key TEXT PRIMARY KEY,
value TEXT NOT NULL,
created_at INTEGER NOT NULL DEFAULT (unixepoch()),
updated_at INTEGER NOT NULL DEFAULT (unixepoch())
);
CREATE INDEX IF NOT EXISTS idx_kv_store_updated
ON kv_store(updated_at);
INSERT OR IGNORE INTO vfs_inode (ino, mode, size, mtime)
VALUES (1, 16877, 0, unixepoch());
"/utf8>>,
sqlight:exec(Schema_sql, Conn).