Listing fixed rules

main
Ziyang Hu 2 years ago
parent 5326a16595
commit cbf53bfbef

@ -11,12 +11,14 @@ query_script = {SOI ~ (option | rule | const_rule | fixed_rule)+ ~ EOI}
query_script_inner = {"{" ~ (option | rule | const_rule | fixed_rule)+ ~ "}"}
query_script_inner_no_bracket = { (option | rule | const_rule | fixed_rule)+ }
imperative_script = {SOI ~ imperative_stmt+ ~ EOI}
sys_script = {SOI ~ "::" ~ (compact_op | list_relations_op | list_relation_op | remove_relations_op | trigger_relation_op |
trigger_relation_show_op | rename_relations_op | running_op | kill_op | explain_op | access_level_op | index_op) ~ EOI}
sys_script = {SOI ~ "::" ~ (list_relations_op | list_relation_op | remove_relations_op | trigger_relation_op |
trigger_relation_show_op | rename_relations_op | running_op | kill_op | explain_op |
access_level_op | index_op | compact_op | list_fixed_rules) ~ EOI}
index_op = {"index" ~ (index_create | index_drop)}
index_create = {"create" ~ compound_ident ~ ":" ~ ident ~ "{" ~ (ident ~ ",")* ~ ident? ~ "}"}
index_drop = {"drop" ~ compound_ident ~ ":" ~ ident }
compact_op = {"compact"}
list_fixed_rules = {"fixed_rules"}
running_op = {"running"}
kill_op = {"kill" ~ expr}
explain_op = {"explain" ~ "{" ~ query_script_inner_no_bracket ~ "}"}

@ -27,6 +27,7 @@ pub(crate) enum SysOp {
ListRelation(Symbol),
ListRelations,
ListRunning,
ListFixedRules,
KillRunning(u64),
Explain(Box<InputProgram>),
RemoveRelation(Vec<Symbol>),
@ -184,6 +185,7 @@ pub(crate) fn parse_sys(
_ => unreachable!()
}
}
Rule::list_fixed_rules => SysOp::ListFixedRules,
rule => unreachable!("{:?}", rule),
})
}

@ -79,7 +79,7 @@ impl<'a> SessionTx<'a> {
}
for trigger in &old_handle.replace_triggers {
let program =
parse_script(trigger, &Default::default(), &db.algorithms.read().unwrap(), cur_vld)?
parse_script(trigger, &Default::default(), &db.fixed_rules.read().unwrap(), cur_vld)?
.get_single_program()?;
let (_, cleanups) = db
@ -203,7 +203,7 @@ impl<'a> SessionTx<'a> {
let mut program = parse_script(
trigger,
&Default::default(),
&db.algorithms.read().unwrap(),
&db.fixed_rules.read().unwrap(),
cur_vld,
)?
.get_single_program()?;
@ -482,7 +482,7 @@ impl<'a> SessionTx<'a> {
let mut program = parse_script(
trigger,
&Default::default(),
&db.algorithms.read().unwrap(),
&db.fixed_rules.read().unwrap(),
cur_vld,
)?
.get_single_program()?;

@ -88,7 +88,7 @@ pub struct Db<S> {
relation_store_id: Arc<AtomicU64>,
queries_count: AtomicU64,
running_queries: Arc<Mutex<BTreeMap<u64, RunningQueryHandle>>>,
pub(crate) algorithms: ShardedLock<BTreeMap<String, Arc<Box<dyn FixedRule>>>>,
pub(crate) fixed_rules: ShardedLock<BTreeMap<String, Arc<Box<dyn FixedRule>>>>,
#[cfg(not(target_arch = "wasm32"))]
callback_count: AtomicU32,
#[cfg(not(target_arch = "wasm32"))]
@ -191,7 +191,7 @@ impl<'s, S: Storage<'s>> Db<S> {
relation_store_id: Default::default(),
queries_count: Default::default(),
running_queries: Default::default(),
algorithms: ShardedLock::new(DEFAULT_FIXED_RULES.clone()),
fixed_rules: ShardedLock::new(DEFAULT_FIXED_RULES.clone()),
#[cfg(not(target_arch = "wasm32"))]
callback_count: Default::default(),
#[cfg(not(target_arch = "wasm32"))]
@ -548,12 +548,8 @@ impl<'s, S: Storage<'s>> Db<S> {
}
}
/// Register a custom fixed rule implementation.
pub fn register_fixed_rule(
&self,
name: String,
rule_impl: Box<dyn FixedRule>,
) -> Result<()> {
match self.algorithms.write().unwrap().entry(name) {
pub fn register_fixed_rule(&self, name: String, rule_impl: Box<dyn FixedRule>) -> Result<()> {
match self.fixed_rules.write().unwrap().entry(name) {
Entry::Vacant(ent) => {
ent.insert(Arc::new(rule_impl));
Ok(())
@ -708,7 +704,12 @@ impl<'s, S: Storage<'s>> Db<S> {
param_pool: &BTreeMap<String, DataValue>,
cur_vld: ValidityTs,
) -> Result<NamedRows> {
match parse_script(payload, param_pool, &self.algorithms.read().unwrap(), cur_vld)? {
match parse_script(
payload,
param_pool,
&self.fixed_rules.read().unwrap(),
cur_vld,
)? {
CozoScript::Single(p) => self.execute_single(cur_vld, p),
CozoScript::Imperative(ps) => self.execute_imperative(cur_vld, &ps),
CozoScript::Sys(op) => self.run_sys_op(op),
@ -976,6 +977,13 @@ impl<'s, S: Storage<'s>> Db<S> {
))
}
SysOp::ListRelations => self.list_relations(),
SysOp::ListFixedRules => {
let rules = self.fixed_rules.read().unwrap();
Ok(NamedRows::new(
vec!["rule".to_string()],
rules.keys().map(|k| vec![DataValue::from(k as &str)]).collect_vec(),
))
}
SysOp::RemoveRelation(rel_names) => {
let rel_name_strs = rel_names.iter().map(|n| &n.name);
let locks = self.obtain_relation_locks(rel_name_strs);

Loading…
Cancel
Save