cozo/runtime/db/
registry.rs1use std::collections::btree_map::Entry;
10use std::sync::Arc;
11#[cfg(not(target_arch = "wasm32"))]
12use std::sync::atomic::Ordering;
13
14use crossbeam::sync::ShardedLock;
15#[cfg(not(target_arch = "wasm32"))]
16use crossbeam::channel::{bounded, unbounded, Receiver};
17use miette::{bail, Result};
18use smartstring::{LazyCompact, SmartString};
19
20use crate::fixed_rule::DEFAULT_FIXED_RULES;
21#[cfg(not(target_arch = "wasm32"))]
22use crate::runtime::callback::{CallbackDeclaration, CallbackOp};
23use crate::storage::Storage;
24use crate::FixedRule;
25
26#[cfg(not(target_arch = "wasm32"))]
27use super::NamedRows;
28use super::Db;
29
30impl<'s, S: Storage<'s>> Db<S> {
31 pub fn register_fixed_rule<R>(&self, name: String, rule_impl: R) -> Result<()>
33 where
34 R: FixedRule + 'static,
35 {
36 match self.fixed_rules.write().unwrap().entry(name) {
37 Entry::Vacant(ent) => {
38 ent.insert(Arc::new(Box::new(rule_impl)));
39 Ok(())
40 }
41 Entry::Occupied(ent) => {
42 bail!(
43 "A fixed rule with the name {} is already registered",
44 ent.key()
45 )
46 }
47 }
48 }
49
50 pub fn unregister_fixed_rule(&self, name: &str) -> Result<bool> {
52 if DEFAULT_FIXED_RULES.contains_key(name) {
53 bail!("Cannot unregister builtin fixed rule {}", name);
54 }
55 Ok(self.fixed_rules.write().unwrap().remove(name).is_some())
56 }
57
58 #[cfg(not(target_arch = "wasm32"))]
61 pub fn register_callback(
62 &self,
63 relation: &str,
64 capacity: Option<usize>,
65 ) -> (u32, Receiver<(CallbackOp, NamedRows, NamedRows)>) {
66 let (sender, receiver) = if let Some(c) = capacity {
67 bounded(c)
68 } else {
69 unbounded()
70 };
71 let cb = CallbackDeclaration {
72 dependent: SmartString::from(relation),
73 sender,
74 };
75
76 let mut guard = self.event_callbacks.write().unwrap();
77 let new_id = self.callback_count.fetch_add(1, Ordering::SeqCst);
78 guard
79 .1
80 .entry(SmartString::from(relation))
81 .or_default()
82 .insert(new_id);
83
84 guard.0.insert(new_id, cb);
85 (new_id, receiver)
86 }
87
88 #[cfg(not(target_arch = "wasm32"))]
90 pub fn unregister_callback(&self, id: u32) -> bool {
91 let mut guard = self.event_callbacks.write().unwrap();
92 let ret = guard.0.remove(&id);
93 if let Some(cb) = &ret {
94 guard.1.get_mut(&cb.dependent).unwrap().remove(&id);
95
96 if guard.1.get(&cb.dependent).unwrap().is_empty() {
97 guard.1.remove(&cb.dependent);
98 }
99 }
100 ret.is_some()
101 }
102
103 pub(crate) fn obtain_relation_locks<'a, T: Iterator<Item = &'a SmartString<LazyCompact>>>(
104 &'s self,
105 rels: T,
106 ) -> Vec<Arc<ShardedLock<()>>> {
107 let mut collected = vec![];
108 let mut pending = vec![];
109 {
110 let locks = self.relation_locks.read().unwrap();
111 for rel in rels {
112 match locks.get(rel) {
113 None => {
114 pending.push(rel);
115 }
116 Some(lock) => collected.push(lock.clone()),
117 }
118 }
119 }
120 if !pending.is_empty() {
121 let mut locks = self.relation_locks.write().unwrap();
122 for rel in pending {
123 let lock = locks.entry(rel.clone()).or_default().clone();
124 collected.push(lock);
125 }
126 }
127 collected
128 }
129}