Skip to main content

interstice_sdk_core/
registry.rs

1use interstice_abi::{
2    IntersticeTypeDef, QuerySchema, ReducerSchema, ReplicatedTableSchema, SubscriptionSchema,
3    TableSchema,
4};
5use std::{
6    collections::HashMap,
7    sync::{Arc, Mutex},
8};
9
10#[repr(C)]
11pub struct ReducerRegistration {
12    pub reducer: unsafe extern "C" fn(i32, i32) -> i64,
13    pub schema: fn() -> ReducerSchema,
14}
15
16#[repr(C)]
17pub struct TableRegistration {}
18
19#[repr(C)]
20pub struct SubscriptionRegistration {}
21
22pub type TableSchemaFn = fn() -> TableSchema;
23pub type ReducerSchemaFn = fn() -> ReducerSchema;
24pub type QuerySchemaFn = fn() -> QuerySchema;
25pub type SubscriptionSchemaFn = fn() -> SubscriptionSchema;
26pub type IntersticeTypeDefFn = fn() -> IntersticeTypeDef;
27pub type ReplicatedTableSchemaFn = fn() -> Vec<ReplicatedTableSchema>;
28
29lazy_static::lazy_static! {
30    pub static ref TABLE_REGISTRY: Arc<Mutex<Vec<TableSchemaFn>>> = Arc::new(Mutex::new(Vec::new()));
31    pub static ref REDUCER_REGISTRY: Arc<Mutex<Vec<ReducerSchemaFn>>> = Arc::new(Mutex::new(Vec::new()));
32    pub static ref QUERY_REGISTRY: Arc<Mutex<Vec<QuerySchemaFn>>> = Arc::new(Mutex::new(Vec::new()));
33    pub static ref SUBSCRIPTION_REGISTRY: Arc<Mutex<Vec<SubscriptionSchemaFn>>> = Arc::new(Mutex::new(Vec::new()));
34    pub static ref INTERSTICE_TYPE_DEFINITION_REGISTRY: Arc<Mutex<Vec<IntersticeTypeDefFn>>> = Arc::new(Mutex::new(Vec::new()));
35    pub static ref REPLICATED_TABLE_REGISTRY: Arc<Mutex<Vec<ReplicatedTableSchemaFn>>> = Arc::new(Mutex::new(Vec::new()));
36}
37
38/// Called by each `#[table]` macro to register its schema function
39pub fn register_table(f: TableSchemaFn) {
40    TABLE_REGISTRY.lock().unwrap().push(f);
41}
42
43/// Called by each `#[reducer]` macro to register its schema function
44pub fn register_reducer(f: ReducerSchemaFn) {
45    REDUCER_REGISTRY.lock().unwrap().push(f);
46}
47
48/// Called by each `#[query]` macro to register its schema function
49pub fn register_query(f: QuerySchemaFn) {
50    QUERY_REGISTRY.lock().unwrap().push(f);
51}
52
53/// Called by each `#[reducer]` macro to register its potential subscription schema function
54pub fn register_subscription(s: SubscriptionSchemaFn) {
55    SUBSCRIPTION_REGISTRY.lock().unwrap().push(s);
56}
57
58/// Called by each `#[derive(IntersticeType)]` macro to register its TypeDef function
59pub fn register_type_def(s: IntersticeTypeDefFn) {
60    INTERSTICE_TYPE_DEFINITION_REGISTRY.lock().unwrap().push(s);
61}
62
63pub fn register_replicated_tables(s: ReplicatedTableSchemaFn) {
64    REPLICATED_TABLE_REGISTRY.lock().unwrap().push(s);
65}
66
67pub fn collect_tables() -> Vec<TableSchema> {
68    TABLE_REGISTRY.lock().unwrap().iter().map(|f| f()).collect()
69}
70
71pub fn collect_reducers() -> Vec<ReducerSchema> {
72    REDUCER_REGISTRY
73        .lock()
74        .unwrap()
75        .iter()
76        .map(|f| f())
77        .collect()
78}
79
80pub fn collect_queries() -> Vec<QuerySchema> {
81    QUERY_REGISTRY.lock().unwrap().iter().map(|f| f()).collect()
82}
83
84pub fn collect_subscriptions() -> Vec<SubscriptionSchema> {
85    SUBSCRIPTION_REGISTRY
86        .lock()
87        .unwrap()
88        .iter()
89        .map(|f| f())
90        .collect()
91}
92
93pub fn collect_type_definitions() -> HashMap<String, IntersticeTypeDef> {
94    let mut result = HashMap::new();
95    for interstice_type_def_fn in INTERSTICE_TYPE_DEFINITION_REGISTRY.lock().unwrap().iter() {
96        let interstice_type_def = interstice_type_def_fn();
97        result.insert(interstice_type_def.get_name().clone(), interstice_type_def);
98    }
99    return result;
100}
101
102pub fn collect_replicated_tables() -> Vec<ReplicatedTableSchema> {
103    REPLICATED_TABLE_REGISTRY
104        .lock()
105        .unwrap()
106        .iter()
107        .flat_map(|f| f())
108        .collect()
109}