use crate::encoding::{Tag, INT_OFFSET, PAYLOAD_BITS};
use crate::sql::{Request, Statement};
pub const SCHEMA_VERSION: &str = "2";
pub const TBOX_TABLE: &str = "CREATE TABLE IF NOT EXISTS tbox_closure (\
kind INTEGER NOT NULL, scope INTEGER NOT NULL, sub INTEGER NOT NULL, sup INTEGER NOT NULL, \
PRIMARY KEY (kind, scope, sup, sub)) WITHOUT ROWID, STRICT";
pub const TBOX_INDEX: &str =
"CREATE INDEX IF NOT EXISTS tbox_closure_sub ON tbox_closure(kind, scope, sub, sup)";
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(default, rename_all = "camelCase"))]
pub struct StoreOptions {
pub graph_index: bool,
pub text_index: bool,
pub versioning: crate::version::Versioning,
pub stamp_index: bool,
pub as_of_index: bool,
pub system_graphs: bool,
}
impl Default for StoreOptions {
fn default() -> Self {
Self {
graph_index: true,
text_index: false,
versioning: crate::version::Versioning::Off,
stamp_index: false,
as_of_index: false,
system_graphs: false,
}
}
}
impl StoreOptions {
pub fn level_change(&self) -> crate::version::LevelChange {
crate::version::LevelChange {
as_of_index: self.as_of_index.then_some(true),
stamp_index: self.stamp_index.then_some(true),
..Default::default()
}
}
}
pub fn schema_sql(options: &StoreOptions) -> String {
schema_sql_with(options, &[])
}
pub fn schema_sql_with(options: &StoreOptions, extra: &[Statement]) -> String {
let mut out =
String::from("-- oxilite schema (generated by oxilite_core::schema::schema_sql)\n");
for s in create_schema(options).statements.iter().chain(extra) {
out.push_str(&s.sql);
out.push_str(";\n");
}
out
}
pub fn create_schema(options: &StoreOptions) -> Request {
let mut r = base_schema(options);
if options.versioning > crate::version::Versioning::Off {
let change = crate::version::change_statements(
&crate::version::VersionState::default(),
options.versioning,
&options.level_change(),
)
.expect("versioning from off is always possible");
r.statements.extend(change);
}
if options.system_graphs {
r.statements
.extend(system_graph_statements(&crate::sql::Capabilities::d1()));
}
r
}
pub fn system_graph_statements(caps: &crate::sql::Capabilities) -> Vec<Statement> {
let quads = crate::registry::system_quads();
let mut s = crate::writer::EncodedQuads::new(quads.iter().map(oxrdf::Quad::as_ref))
.insert_statements(caps);
s.extend(crate::reason::closure_statements());
s.extend(crate::shapes::refresh_statements());
s
}
pub fn base_schema(options: &StoreOptions) -> Request {
let mut s = vec![
"CREATE TABLE IF NOT EXISTS oxilite_meta (key TEXT PRIMARY KEY, value TEXT NOT NULL) STRICT",
"CREATE TABLE IF NOT EXISTS terms (\
id INTEGER PRIMARY KEY, \
lex TEXT NOT NULL, \
dt TEXT, \
lang TEXT, \
dir INTEGER, \
num REAL, \
nt INTEGER, \
ts REAL) STRICT",
"CREATE INDEX IF NOT EXISTS terms_num ON terms(num) WHERE num IS NOT NULL",
"CREATE INDEX IF NOT EXISTS terms_ts ON terms(ts) WHERE ts IS NOT NULL",
"CREATE TRIGGER IF NOT EXISTS terms_collision BEFORE INSERT ON terms \
WHEN EXISTS (SELECT 1 FROM terms t WHERE t.id = NEW.id AND \
(t.lex IS NOT NEW.lex OR t.dt IS NOT NEW.dt OR t.lang IS NOT NEW.lang OR t.dir IS NOT NEW.dir)) \
BEGIN SELECT RAISE(ABORT, 'oxilite: term hash collision'); END",
"CREATE TABLE IF NOT EXISTS triple_terms (\
id INTEGER PRIMARY KEY, s INTEGER NOT NULL, p INTEGER NOT NULL, o INTEGER NOT NULL, vk TEXT NOT NULL, sk TEXT NOT NULL) STRICT",
"CREATE TABLE IF NOT EXISTS quads (\
s INTEGER NOT NULL, p INTEGER NOT NULL, o INTEGER NOT NULL, g INTEGER NOT NULL DEFAULT 0, \
PRIMARY KEY (s, p, o, g)) WITHOUT ROWID, STRICT",
"CREATE INDEX IF NOT EXISTS quads_posg ON quads(p, o, s, g)",
"CREATE INDEX IF NOT EXISTS quads_ospg ON quads(o, s, p, g)",
"CREATE TABLE IF NOT EXISTS graphs (id INTEGER PRIMARY KEY) STRICT",
"CREATE TABLE IF NOT EXISTS stats_pred (\
p INTEGER PRIMARY KEY, triples INTEGER NOT NULL, distinct_s INTEGER NOT NULL, distinct_o INTEGER NOT NULL) STRICT",
"CREATE TABLE IF NOT EXISTS stats_class (o INTEGER PRIMARY KEY, instances INTEGER NOT NULL) STRICT",
"CREATE TABLE IF NOT EXISTS stats_po (p INTEGER NOT NULL, o INTEGER NOT NULL, n INTEGER NOT NULL, PRIMARY KEY (p, o)) WITHOUT ROWID, STRICT",
TBOX_TABLE,
TBOX_INDEX,
"CREATE TABLE IF NOT EXISTS quads_inf (\
s INTEGER NOT NULL, p INTEGER NOT NULL, o INTEGER NOT NULL, g INTEGER NOT NULL DEFAULT 0, \
PRIMARY KEY (s, p, o, g)) WITHOUT ROWID, STRICT",
"CREATE INDEX IF NOT EXISTS quads_inf_posg ON quads_inf(p, o, s, g)",
"CREATE INDEX IF NOT EXISTS quads_inf_ospg ON quads_inf(o, s, p, g)",
"CREATE TABLE IF NOT EXISTS quads_inf_src (\
src INTEGER NOT NULL, s INTEGER NOT NULL, p INTEGER NOT NULL, o INTEGER NOT NULL, g INTEGER NOT NULL DEFAULT 0, \
PRIMARY KEY (s, p, o, g, src)) WITHOUT ROWID, STRICT",
"CREATE TABLE IF NOT EXISTS inf_producers (id INTEGER PRIMARY KEY, name TEXT NOT NULL) STRICT",
"CREATE TABLE IF NOT EXISTS shapes_index (\
target TEXT NOT NULL, path TEXT NOT NULL, datatype TEXT, min_count INTEGER, \
max_count INTEGER, pattern TEXT, relationship INTEGER NOT NULL DEFAULT 0, \
PRIMARY KEY (target, path)) WITHOUT ROWID, STRICT",
"CREATE TABLE IF NOT EXISTS shapes_in (\
target TEXT NOT NULL, path TEXT NOT NULL, id INTEGER NOT NULL, \
lex TEXT, dt TEXT, lang TEXT, dir INTEGER, \
PRIMARY KEY (target, path, id)) WITHOUT ROWID, STRICT",
"CREATE TABLE IF NOT EXISTS datalog_work (\
run INTEGER NOT NULL, rel INTEGER NOT NULL, \
c0 INTEGER NOT NULL DEFAULT 0, c1 INTEGER NOT NULL DEFAULT 0, \
c2 INTEGER NOT NULL DEFAULT 0, c3 INTEGER NOT NULL DEFAULT 0, \
c4 INTEGER NOT NULL DEFAULT 0, c5 INTEGER NOT NULL DEFAULT 0, \
PRIMARY KEY (run, rel, c0, c1, c2, c3, c4, c5)) WITHOUT ROWID, STRICT",
"CREATE TABLE IF NOT EXISTS update_buffer (\
op INTEGER NOT NULL, s INTEGER NOT NULL, p INTEGER NOT NULL, o INTEGER NOT NULL, g INTEGER NOT NULL) STRICT",
"CREATE TABLE IF NOT EXISTS oxilite_guard (\
graph_does_not_exist INTEGER CHECK (graph_does_not_exist IS NULL), \
graph_already_exists INTEGER CHECK (graph_already_exists IS NULL), \
computed_value_not_storable INTEGER CHECK (computed_value_not_storable IS NULL)) STRICT",
]
.into_iter()
.map(Statement::from)
.collect::<Vec<_>>();
if options.graph_index {
s.push("CREATE INDEX IF NOT EXISTS quads_gspo ON quads(g, s, p, o)".into());
}
if options.text_index {
s.extend(crate::text::schema_statements());
}
s.push(
format!(
"INSERT OR IGNORE INTO oxilite_meta(key, value) VALUES ('schema_version', '{SCHEMA_VERSION}'), ('graph_index', '{}'), ('int_offset', '{INT_OFFSET}'), ('payload_bits', '{PAYLOAD_BITS}'), ('integer_tag', '{}')",
u8::from(options.graph_index),
Tag::Integer as u8
)
.into(),
);
Request::atomic(s)
}