|
| 1 | +use std::collections::HashMap; |
| 2 | +use std::sync::Arc; |
| 3 | + |
| 4 | +use napi::bindgen_prelude::Either3; |
| 5 | +use scylla::transport::topology::{Keyspace, MaterializedView, Strategy, Table}; |
| 6 | +use scylla::transport::ClusterData; |
| 7 | + |
| 8 | +// ============= ClusterData ============= // |
| 9 | +#[napi] |
| 10 | +pub struct ScyllaClusterData { |
| 11 | + inner: Arc<ClusterData>, |
| 12 | +} |
| 13 | + |
| 14 | +impl From<Arc<ClusterData>> for ScyllaClusterData { |
| 15 | + fn from(cluster_data: Arc<ClusterData>) -> Self { |
| 16 | + ScyllaClusterData { |
| 17 | + inner: cluster_data, |
| 18 | + } |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +#[napi] |
| 23 | +impl ScyllaClusterData { |
| 24 | + #[napi] |
| 25 | + /// Access keyspaces details collected by the driver Driver collects various schema details like |
| 26 | + /// tables, partitioners, columns, types. They can be read using this method |
| 27 | + pub fn get_keyspace_info(&self) -> Option<HashMap<String, ScyllaKeyspace>> { |
| 28 | + let keyspaces_info = self.inner.get_keyspace_info(); |
| 29 | + |
| 30 | + if keyspaces_info.is_empty() { |
| 31 | + None |
| 32 | + } else { |
| 33 | + Some( |
| 34 | + keyspaces_info |
| 35 | + .iter() |
| 36 | + .map(|(k, v)| (k.clone(), ScyllaKeyspace::from((*v).clone()))) |
| 37 | + .collect(), |
| 38 | + ) |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | +// ======================================= // |
| 43 | + |
| 44 | +// ============= Keyspace ============= // |
| 45 | +#[napi(object)] |
| 46 | +#[derive(Clone)] |
| 47 | +pub struct ScyllaKeyspace { |
| 48 | + pub strategy: ScyllaStrategy, |
| 49 | + pub tables: HashMap<String, ScyllaTable>, |
| 50 | + pub views: HashMap<String, ScyllaMaterializedView>, |
| 51 | + // pub user_defined_types: HashMap<String, ScyllaUserDefinedType>, |
| 52 | +} |
| 53 | + |
| 54 | +impl From<Keyspace> for ScyllaKeyspace { |
| 55 | + fn from(keyspace: Keyspace) -> Self { |
| 56 | + ScyllaKeyspace { |
| 57 | + tables: keyspace |
| 58 | + .tables |
| 59 | + .into_iter() |
| 60 | + .map(|(k, v)| (k, ScyllaTable::from(v))) |
| 61 | + .collect(), |
| 62 | + views: keyspace |
| 63 | + .views |
| 64 | + .into_iter() |
| 65 | + .map(|(k, v)| (k, ScyllaMaterializedView::from(v))) |
| 66 | + .collect(), |
| 67 | + strategy: keyspace.strategy.into(), |
| 68 | + // TODO: Implement ScyllaUserDefinedType |
| 69 | + // user_defined_types: keyspace.user_defined_types.into_iter().map(|(k, v)| (k, ScyllaUserDefinedType::from(v))).collect(), |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | +// ======================================= // |
| 74 | + |
| 75 | +// ============= Strategy ============= // |
| 76 | +#[napi(object)] |
| 77 | +#[derive(Clone)] |
| 78 | +pub struct ScyllaStrategy { |
| 79 | + pub kind: String, |
| 80 | + pub data: Option<Either3<SimpleStrategy, NetworkTopologyStrategy, Other>>, |
| 81 | +} |
| 82 | + |
| 83 | +#[napi(object)] |
| 84 | +#[derive(Clone)] |
| 85 | +pub struct SimpleStrategy { |
| 86 | + pub replication_factor: u32, |
| 87 | +} |
| 88 | + |
| 89 | +#[napi(object)] |
| 90 | +#[derive(Clone)] |
| 91 | +pub struct NetworkTopologyStrategy { |
| 92 | + pub datacenter_repfactors: HashMap<String, i32>, |
| 93 | +} |
| 94 | + |
| 95 | +#[napi(object)] |
| 96 | +#[derive(Clone)] |
| 97 | +pub struct Other { |
| 98 | + pub name: String, |
| 99 | + pub data: HashMap<String, String>, |
| 100 | +} |
| 101 | + |
| 102 | +impl From<Strategy> for ScyllaStrategy { |
| 103 | + fn from(strategy: Strategy) -> Self { |
| 104 | + match strategy { |
| 105 | + Strategy::SimpleStrategy { replication_factor } => ScyllaStrategy { |
| 106 | + kind: "SimpleStrategy".to_string(), |
| 107 | + data: Some(Either3::A(SimpleStrategy { |
| 108 | + replication_factor: replication_factor as u32, |
| 109 | + })), |
| 110 | + }, |
| 111 | + Strategy::NetworkTopologyStrategy { |
| 112 | + datacenter_repfactors, |
| 113 | + } => ScyllaStrategy { |
| 114 | + kind: "NetworkTopologyStrategy".to_string(), |
| 115 | + data: Some(Either3::B(NetworkTopologyStrategy { |
| 116 | + datacenter_repfactors: datacenter_repfactors |
| 117 | + .into_iter() |
| 118 | + .map(|(k, v)| (k, v as i32)) |
| 119 | + .collect(), |
| 120 | + })), |
| 121 | + }, |
| 122 | + Strategy::Other { name, data } => ScyllaStrategy { |
| 123 | + kind: name.clone(), |
| 124 | + data: Some(Either3::C(Other { |
| 125 | + name: name.clone(), |
| 126 | + data, |
| 127 | + })), |
| 128 | + }, |
| 129 | + Strategy::LocalStrategy => ScyllaStrategy { |
| 130 | + kind: "LocalStrategy".to_string(), |
| 131 | + data: None, |
| 132 | + }, |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | +// ======================================= // |
| 137 | + |
| 138 | +// ============= Table ============= // |
| 139 | +#[napi(object)] |
| 140 | +#[derive(Clone)] |
| 141 | +pub struct ScyllaTable { |
| 142 | + pub columns: Vec<String>, |
| 143 | + pub partition_key: Vec<String>, |
| 144 | + pub clustering_key: Vec<String>, |
| 145 | + pub partitioner: Option<String>, |
| 146 | +} |
| 147 | + |
| 148 | +impl From<Table> for ScyllaTable { |
| 149 | + fn from(table: Table) -> Self { |
| 150 | + ScyllaTable { |
| 151 | + columns: table.columns.clone().into_keys().collect::<Vec<String>>(), |
| 152 | + partition_key: table.partition_key.clone(), |
| 153 | + clustering_key: table.clustering_key.clone(), |
| 154 | + partitioner: table.partitioner.clone(), |
| 155 | + } |
| 156 | + } |
| 157 | +} |
| 158 | +// ======================================= // |
| 159 | + |
| 160 | +// ============= MaterializedView ============= // |
| 161 | +#[napi(object)] |
| 162 | +#[derive(Clone)] |
| 163 | +pub struct ScyllaMaterializedView { |
| 164 | + pub view_metadata: ScyllaTable, |
| 165 | + pub base_table_name: String, |
| 166 | +} |
| 167 | + |
| 168 | +impl From<MaterializedView> for ScyllaMaterializedView { |
| 169 | + fn from(view: MaterializedView) -> Self { |
| 170 | + ScyllaMaterializedView { |
| 171 | + view_metadata: ScyllaTable::from(view.view_metadata), |
| 172 | + base_table_name: view.base_table_name, |
| 173 | + } |
| 174 | + } |
| 175 | +} |
| 176 | +// ======================================= // |
0 commit comments