-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(schema): fetch schema information (#33)
Signed-off-by: Felipi Lima Matozinho <[email protected]>
- Loading branch information
Showing
8 changed files
with
287 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Cluster } from "../index.js"; | ||
|
||
const nodes = process.env.CLUSTER_NODES?.split(",") ?? ["127.0.0.1:9042"]; | ||
|
||
console.log(`Connecting to ${nodes}`); | ||
|
||
const cluster = new Cluster({ nodes }); | ||
const session = await cluster.connect(); | ||
|
||
const clusterData = await session.getClusterData(); | ||
const keyspaceInfo = clusterData.getKeyspaceInfo(); | ||
|
||
if (!keyspaceInfo) throw new Error("No data found"); | ||
|
||
console.log("ALL KEYSPACES"); | ||
for (const keyspaceName in keyspaceInfo) { | ||
console.log("========================================================"); | ||
const keyspaceData = keyspaceInfo[keyspaceName]; | ||
console.log("Keyspace: ", keyspaceName); | ||
console.log( | ||
"replication strategy: ", | ||
keyspaceData.strategy.kind, | ||
keyspaceData.strategy.data, | ||
); | ||
for (const tableName in keyspaceData.tables) { | ||
console.log("-----------------------"); | ||
const tableData = keyspaceData.tables[tableName]; | ||
console.log("Table: ", tableName); | ||
console.log("partitionKey: ", tableData.partitionKey); | ||
console.log("clusteringKey: ", tableData.clusteringKey); | ||
console.log("columns: ", tableData.columns); | ||
console.log("-----------------------"); | ||
} | ||
console.log("========================================================"); | ||
} | ||
|
||
console.log("================== SPECIFIC KEYSPACES =================="); | ||
console.log( | ||
"keyspace: system_auth | strategy: ", | ||
keyspaceInfo.system_auth.strategy, | ||
); | ||
console.log( | ||
"keyspace: system_traces | strategy: ", | ||
keyspaceInfo.system_traces.strategy, | ||
); | ||
console.log( | ||
"keyspace: system_distributed_everywhere | strategy: ", | ||
keyspaceInfo.system_distributed_everywhere.strategy, | ||
); | ||
console.log( | ||
"keyspace: system_distributed | strategy: ", | ||
keyspaceInfo.system_distributed.strategy, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,4 +51,3 @@ impl From<scylla::statement::Consistency> for Consistency { | |
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod metrics; | ||
pub mod scylla_session; | ||
pub mod topology; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
use std::collections::HashMap; | ||
use std::sync::Arc; | ||
|
||
use napi::bindgen_prelude::Either3; | ||
use scylla::transport::topology::{Keyspace, MaterializedView, Strategy, Table}; | ||
use scylla::transport::ClusterData; | ||
|
||
// ============= ClusterData ============= // | ||
#[napi] | ||
pub struct ScyllaClusterData { | ||
inner: Arc<ClusterData>, | ||
} | ||
|
||
impl From<Arc<ClusterData>> for ScyllaClusterData { | ||
fn from(cluster_data: Arc<ClusterData>) -> Self { | ||
ScyllaClusterData { | ||
inner: cluster_data, | ||
} | ||
} | ||
} | ||
|
||
#[napi] | ||
impl ScyllaClusterData { | ||
#[napi] | ||
/// Access keyspaces details collected by the driver Driver collects various schema details like | ||
/// tables, partitioners, columns, types. They can be read using this method | ||
pub fn get_keyspace_info(&self) -> Option<HashMap<String, ScyllaKeyspace>> { | ||
let keyspaces_info = self.inner.get_keyspace_info(); | ||
|
||
if keyspaces_info.is_empty() { | ||
None | ||
} else { | ||
Some( | ||
keyspaces_info | ||
.iter() | ||
.map(|(k, v)| (k.clone(), ScyllaKeyspace::from((*v).clone()))) | ||
.collect(), | ||
) | ||
} | ||
} | ||
} | ||
// ======================================= // | ||
|
||
// ============= Keyspace ============= // | ||
#[napi(object)] | ||
#[derive(Clone)] | ||
pub struct ScyllaKeyspace { | ||
pub strategy: ScyllaStrategy, | ||
pub tables: HashMap<String, ScyllaTable>, | ||
pub views: HashMap<String, ScyllaMaterializedView>, | ||
// pub user_defined_types: HashMap<String, ScyllaUserDefinedType>, | ||
} | ||
|
||
impl From<Keyspace> for ScyllaKeyspace { | ||
fn from(keyspace: Keyspace) -> Self { | ||
ScyllaKeyspace { | ||
tables: keyspace | ||
.tables | ||
.into_iter() | ||
.map(|(k, v)| (k, ScyllaTable::from(v))) | ||
.collect(), | ||
views: keyspace | ||
.views | ||
.into_iter() | ||
.map(|(k, v)| (k, ScyllaMaterializedView::from(v))) | ||
.collect(), | ||
strategy: keyspace.strategy.into(), | ||
// TODO: Implement ScyllaUserDefinedType | ||
// user_defined_types: keyspace.user_defined_types.into_iter().map(|(k, v)| (k, ScyllaUserDefinedType::from(v))).collect(), | ||
} | ||
} | ||
} | ||
// ======================================= // | ||
|
||
// ============= Strategy ============= // | ||
#[napi(object)] | ||
#[derive(Clone)] | ||
pub struct ScyllaStrategy { | ||
pub kind: String, | ||
pub data: Option<Either3<SimpleStrategy, NetworkTopologyStrategy, Other>>, | ||
} | ||
|
||
#[napi(object)] | ||
#[derive(Clone)] | ||
pub struct SimpleStrategy { | ||
pub replication_factor: u32, | ||
} | ||
|
||
#[napi(object)] | ||
#[derive(Clone)] | ||
pub struct NetworkTopologyStrategy { | ||
pub datacenter_repfactors: HashMap<String, i32>, | ||
} | ||
|
||
#[napi(object)] | ||
#[derive(Clone)] | ||
pub struct Other { | ||
pub name: String, | ||
pub data: HashMap<String, String>, | ||
} | ||
|
||
impl From<Strategy> for ScyllaStrategy { | ||
fn from(strategy: Strategy) -> Self { | ||
match strategy { | ||
Strategy::SimpleStrategy { replication_factor } => ScyllaStrategy { | ||
kind: "SimpleStrategy".to_string(), | ||
data: Some(Either3::A(SimpleStrategy { | ||
replication_factor: replication_factor as u32, | ||
})), | ||
}, | ||
Strategy::NetworkTopologyStrategy { | ||
datacenter_repfactors, | ||
} => ScyllaStrategy { | ||
kind: "NetworkTopologyStrategy".to_string(), | ||
data: Some(Either3::B(NetworkTopologyStrategy { | ||
datacenter_repfactors: datacenter_repfactors | ||
.into_iter() | ||
.map(|(k, v)| (k, v as i32)) | ||
.collect(), | ||
})), | ||
}, | ||
Strategy::Other { name, data } => ScyllaStrategy { | ||
kind: name.clone(), | ||
data: Some(Either3::C(Other { | ||
name: name.clone(), | ||
data, | ||
})), | ||
}, | ||
Strategy::LocalStrategy => ScyllaStrategy { | ||
kind: "LocalStrategy".to_string(), | ||
data: None, | ||
}, | ||
} | ||
} | ||
} | ||
// ======================================= // | ||
|
||
// ============= Table ============= // | ||
#[napi(object)] | ||
#[derive(Clone)] | ||
pub struct ScyllaTable { | ||
pub columns: Vec<String>, | ||
pub partition_key: Vec<String>, | ||
pub clustering_key: Vec<String>, | ||
pub partitioner: Option<String>, | ||
} | ||
|
||
impl From<Table> for ScyllaTable { | ||
fn from(table: Table) -> Self { | ||
ScyllaTable { | ||
columns: table.columns.clone().into_keys().collect::<Vec<String>>(), | ||
partition_key: table.partition_key.clone(), | ||
clustering_key: table.clustering_key.clone(), | ||
partitioner: table.partitioner.clone(), | ||
} | ||
} | ||
} | ||
// ======================================= // | ||
|
||
// ============= MaterializedView ============= // | ||
#[napi(object)] | ||
#[derive(Clone)] | ||
pub struct ScyllaMaterializedView { | ||
pub view_metadata: ScyllaTable, | ||
pub base_table_name: String, | ||
} | ||
|
||
impl From<MaterializedView> for ScyllaMaterializedView { | ||
fn from(view: MaterializedView) -> Self { | ||
ScyllaMaterializedView { | ||
view_metadata: ScyllaTable::from(view.view_metadata), | ||
base_table_name: view.base_table_name, | ||
} | ||
} | ||
} | ||
// ======================================= // |