use crate::encoding::{Tag, INT_OFFSET, PAYLOAD_BITS};
use crate::sql::{Request, Statement};
pub const SCHEMA_VERSION: &str = "1";
#[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,
}
impl Default for StoreOptions {
fn default() -> Self {
Self {
graph_index: true,
text_index: false,
}
}
}
pub fn schema_sql(options: &StoreOptions) -> String {
let mut out =
String::from("-- oxilite schema (generated by oxilite_core::schema::schema_sql)\n");
for s in create_schema(options).statements {
out.push_str(&s.sql);
out.push_str(";\n");
}
out
}
pub fn create_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",
"CREATE TABLE IF NOT EXISTS tbox_closure (\
kind INTEGER NOT NULL, sub INTEGER NOT NULL, sup INTEGER NOT NULL, PRIMARY KEY (kind, sup, sub)) WITHOUT ROWID, STRICT",
"CREATE INDEX IF NOT EXISTS tbox_closure_sub ON tbox_closure(kind, sub, sup)",
"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 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)
}