Skip to content

Commit

Permalink
Update keyspace and table names in examples
Browse files Browse the repository at this point in the history
ks is changed from ks to examples_ks
table names are changed from t to the example file name

Examples share an explicitly named keyspace

Examples now have unique table names preventing errors that occur from two examples sharing the same table
  • Loading branch information
nsipplswezey committed Oct 19, 2023
1 parent 2641d1b commit c8abb78
Show file tree
Hide file tree
Showing 20 changed files with 201 additions and 97 deletions.
8 changes: 4 additions & 4 deletions examples/allocations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ async fn main() -> Result<()> {
let session: Session = SessionBuilder::new().known_node(args.node).build().await?;
let session = Arc::new(session);

session.query("CREATE KEYSPACE IF NOT EXISTS ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await?;
session.query("CREATE KEYSPACE IF NOT EXISTS examples_ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await?;
session.await_schema_agreement().await.unwrap();

session
.query(
"CREATE TABLE IF NOT EXISTS ks.alloc_test (a int, b int, c text, primary key (a, b))",
"CREATE TABLE IF NOT EXISTS examples_ks.allocations (a int, b int, c text, primary key (a, b))",
&[],
)
.await?;
Expand All @@ -145,13 +145,13 @@ async fn main() -> Result<()> {

let prepared_inserts = Arc::new(
session
.prepare("INSERT INTO ks.alloc_test (a, b, c) VALUES (?, ?, 'abc')")
.prepare("INSERT INTO examples_ks.allocations (a, b, c) VALUES (?, ?, 'abc')")
.await?,
);

let prepared_selects = Arc::new(
session
.prepare("SELECT * FROM ks.alloc_test WHERE a = ? and b = ?")
.prepare("SELECT * FROM examples_ks.allocations WHERE a = ? and b = ?")
.await?,
);

Expand Down
4 changes: 2 additions & 2 deletions examples/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ async fn main() -> Result<()> {
.await
.unwrap();

session.query("CREATE KEYSPACE IF NOT EXISTS ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await.unwrap();
session.query("CREATE KEYSPACE IF NOT EXISTS examples_ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await.unwrap();
session
.query("DROP TABLE IF EXISTS ks.t;", &[])
.query("DROP TABLE IF EXISTS examples_ks.auth;", &[])
.await
.unwrap();

Expand Down
34 changes: 26 additions & 8 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,31 @@ async fn main() -> Result<()> {

let session: Session = SessionBuilder::new().known_node(uri).build().await?;

session.query("CREATE KEYSPACE IF NOT EXISTS ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await?;
session.query("CREATE KEYSPACE IF NOT EXISTS examples_ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await?;

session
.query(
"CREATE TABLE IF NOT EXISTS ks.t (a int, b int, c text, primary key (a, b))",
"CREATE TABLE IF NOT EXISTS examples_ks.basic (a int, b int, c text, primary key (a, b))",
&[],
)
.await?;

session
.query("INSERT INTO ks.t (a, b, c) VALUES (?, ?, ?)", (3, 4, "def"))
.query(
"INSERT INTO examples_ks.basic (a, b, c) VALUES (?, ?, ?)",
(3, 4, "def"),
)
.await?;

session
.query("INSERT INTO ks.t (a, b, c) VALUES (1, 2, 'abc')", &[])
.query(
"INSERT INTO examples_ks.basic (a, b, c) VALUES (1, 2, 'abc')",
&[],
)
.await?;

let prepared = session
.prepare("INSERT INTO ks.t (a, b, c) VALUES (?, 7, ?)")
.prepare("INSERT INTO examples_ks.basic (a, b, c) VALUES (?, 7, ?)")
.await?;
session
.execute(&prepared, (42_i32, "I'm prepared!"))
Expand All @@ -43,7 +49,11 @@ async fn main() -> Result<()> {
.await?;

// Rows can be parsed as tuples
if let Some(rows) = session.query("SELECT a, b, c FROM ks.t", &[]).await?.rows {
if let Some(rows) = session
.query("SELECT a, b, c FROM examples_ks.basic", &[])
.await?
.rows
{
for row in rows.into_typed::<(i32, i32, String)>() {
let (a, b, c) = row?;
println!("a, b, c: {}, {}, {}", a, b, c);
Expand All @@ -58,15 +68,23 @@ async fn main() -> Result<()> {
_c: String,
}

if let Some(rows) = session.query("SELECT a, b, c FROM ks.t", &[]).await?.rows {
if let Some(rows) = session
.query("SELECT a, b, c FROM examples_ks.basic", &[])
.await?
.rows
{
for row_data in rows.into_typed::<RowData>() {
let row_data = row_data?;
println!("row_data: {:?}", row_data);
}
}

// Or simply as untyped rows
if let Some(rows) = session.query("SELECT a, b, c FROM ks.t", &[]).await?.rows {
if let Some(rows) = session
.query("SELECT a, b, c FROM examples_ks.basic", &[])
.await?
.rows
{
for row in rows {
let a = row.columns[0].as_ref().unwrap().as_int().unwrap();
let b = row.columns[1].as_ref().unwrap().as_int().unwrap();
Expand Down
4 changes: 2 additions & 2 deletions examples/cloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ async fn main() -> Result<()> {
.await
.unwrap();

session.query("CREATE KEYSPACE IF NOT EXISTS ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}",
session.query("CREATE KEYSPACE IF NOT EXISTS examples_ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}",
&[]).await.unwrap();
session
.query("DROP TABLE IF EXISTS ks.t;", &[])
.query("DROP TABLE IF EXISTS examples_ks.cloud;", &[])
.await
.unwrap();

Expand Down
23 changes: 17 additions & 6 deletions examples/compare-tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,25 @@ async fn main() -> Result<()> {

let session: Session = SessionBuilder::new().known_node(uri).build().await?;

session.query("CREATE KEYSPACE IF NOT EXISTS ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await?;
session.query("CREATE KEYSPACE IF NOT EXISTS examples_ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await?;

session
.query(
"CREATE TABLE IF NOT EXISTS ks.t (pk bigint primary key)",
"CREATE TABLE IF NOT EXISTS examples_ks.compare_tokens (pk bigint primary key)",
&[],
)
.await?;

let prepared = session.prepare("INSERT INTO ks.t (pk) VALUES (?)").await?;
let prepared = session
.prepare("INSERT INTO examples_ks.compare_tokens (pk) VALUES (?)")
.await?;

for pk in (0..100_i64).chain(99840..99936_i64) {
session
.query("INSERT INTO ks.t (pk) VALUES (?)", (pk,))
.query(
"INSERT INTO examples_ks.compare_tokens (pk) VALUES (?)",
(pk,),
)
.await?;

let serialized_pk = (pk,).serialized()?.into_owned();
Expand All @@ -36,14 +41,20 @@ async fn main() -> Result<()> {
"Token endpoints for query: {:?}",
session
.get_cluster_data()
.get_token_endpoints("ks", Token { value: t })
.get_token_endpoints("examples_ks", Token { value: t })
.iter()
.map(|n| n.address)
.collect::<Vec<NodeAddr>>()
);

let qt = session
.query(format!("SELECT token(pk) FROM ks.t where pk = {}", pk), &[])
.query(
format!(
"SELECT token(pk) FROM examples_ks.compare_tokens where pk = {}",
pk
),
&[],
)
.await?
.rows
.unwrap()
Expand Down
45 changes: 33 additions & 12 deletions examples/cql-time-types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ async fn main() -> Result<()> {

let session: Session = SessionBuilder::new().known_node(uri).build().await?;

session.query("CREATE KEYSPACE IF NOT EXISTS ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await?;
session.query("CREATE KEYSPACE IF NOT EXISTS examples_ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await?;

// Date
// Date is a year, month and day in the range -5877641-06-23 to -5877641-06-23

session
.query(
"CREATE TABLE IF NOT EXISTS ks.dates (d date primary key)",
"CREATE TABLE IF NOT EXISTS examples_ks.dates (d date primary key)",
&[],
)
.await?;
Expand All @@ -33,10 +33,17 @@ async fn main() -> Result<()> {
let example_date: NaiveDate = NaiveDate::from_ymd_opt(2020, 2, 20).unwrap();

session
.query("INSERT INTO ks.dates (d) VALUES (?)", (example_date,))
.query(
"INSERT INTO examples_ks.dates (d) VALUES (?)",
(example_date,),
)
.await?;

if let Some(rows) = session.query("SELECT d from ks.dates", &[]).await?.rows {
if let Some(rows) = session
.query("SELECT d from examples_ks.dates", &[])
.await?
.rows
{
for row in rows.into_typed::<(NaiveDate,)>() {
let (read_date,): (NaiveDate,) = match row {
Ok(read_date) => read_date,
Expand All @@ -50,10 +57,17 @@ async fn main() -> Result<()> {
// Dates outside this range must be represented in the raw form - an u32 describing days since -5877641-06-23
let example_big_date: Date = Date(u32::MAX);
session
.query("INSERT INTO ks.dates (d) VALUES (?)", (example_big_date,))
.query(
"INSERT INTO examples_ks.dates (d) VALUES (?)",
(example_big_date,),
)
.await?;

if let Some(rows) = session.query("SELECT d from ks.dates", &[]).await?.rows {
if let Some(rows) = session
.query("SELECT d from examples_ks.dates", &[])
.await?
.rows
{
for row in rows {
let read_days: u32 = match row.columns[0] {
Some(CqlValue::Date(days)) => days,
Expand All @@ -69,17 +83,24 @@ async fn main() -> Result<()> {

session
.query(
"CREATE TABLE IF NOT EXISTS ks.times (t time primary key)",
"CREATE TABLE IF NOT EXISTS examples_ks.times (t time primary key)",
&[],
)
.await?;

// Time as bound value must be wrapped in value::Time to differentiate from Timestamp
session
.query("INSERT INTO ks.times (t) VALUES (?)", (Time(example_time),))
.query(
"INSERT INTO examples_ks.times (t) VALUES (?)",
(Time(example_time),),
)
.await?;

if let Some(rows) = session.query("SELECT t from ks.times", &[]).await?.rows {
if let Some(rows) = session
.query("SELECT t from examples_ks.times", &[])
.await?
.rows
{
for row in rows.into_typed::<(Duration,)>() {
let (read_time,): (Duration,) = row?;

Expand All @@ -92,21 +113,21 @@ async fn main() -> Result<()> {

session
.query(
"CREATE TABLE IF NOT EXISTS ks.timestamps (t timestamp primary key)",
"CREATE TABLE IF NOT EXISTS examples_ks.timestamps (t timestamp primary key)",
&[],
)
.await?;

// Timestamp as bound value must be wrapped in value::Timestamp to differentiate from Time
session
.query(
"INSERT INTO ks.timestamps (t) VALUES (?)",
"INSERT INTO examples_ks.timestamps (t) VALUES (?)",
(Timestamp(example_timestamp),),
)
.await?;

if let Some(rows) = session
.query("SELECT t from ks.timestamps", &[])
.query("SELECT t from examples_ks.timestamps", &[])
.await?
.rows
{
Expand Down
19 changes: 14 additions & 5 deletions examples/custom_deserialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@ async fn main() -> Result<()> {

let session: Session = SessionBuilder::new().known_node(uri).build().await?;

session.query("CREATE KEYSPACE IF NOT EXISTS ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await?;
session.query("CREATE KEYSPACE IF NOT EXISTS examples_ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await?;
session
.query(
"CREATE TABLE IF NOT EXISTS ks.t (pk int PRIMARY KEY, v text)",
"CREATE TABLE IF NOT EXISTS examples_ks.custom_deserialization (pk int primary key, v text)",
&[],
)
.await?;

session
.query("INSERT INTO ks.t (pk, v) VALUES (1, 'asdf')", ())
.query(
"INSERT INTO examples_ks.custom_deserialization (pk, v) VALUES (1, 'asdf')",
(),
)
.await?;

// You can implement FromCqlVal for your own types
Expand All @@ -38,7 +41,10 @@ async fn main() -> Result<()> {
}

let (v,) = session
.query("SELECT v FROM ks.t WHERE pk = 1", ())
.query(
"SELECT v FROM examples_ks.custom_deserialization WHERE pk = 1",
(),
)
.await?
.single_row_typed::<(MyType,)>()?;
assert_eq!(v, MyType("asdf".to_owned()));
Expand All @@ -62,7 +68,10 @@ async fn main() -> Result<()> {
impl_from_cql_value_from_method!(MyOtherType, into_my_other_type);

let (v,) = session
.query("SELECT v FROM ks.t WHERE pk = 1", ())
.query(
"SELECT v FROM examples_ks.custom_deserialization WHERE pk = 1",
(),
)
.await?
.single_row_typed::<(MyOtherType,)>()?;
assert_eq!(v, MyOtherType("asdf".to_owned()));
Expand Down
19 changes: 13 additions & 6 deletions examples/execution_profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,17 @@ async fn main() -> Result<()> {
session_3_config.add_known_node(uri);
let session3: Session = Session::connect(session_3_config).await?;

session1.query("CREATE KEYSPACE IF NOT EXISTS ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await?;
session1.query("CREATE KEYSPACE IF NOT EXISTS examples_ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await?;

session2
.query(
"CREATE TABLE IF NOT EXISTS ks.t (a int, b int, c text, primary key (a, b))",
"CREATE TABLE IF NOT EXISTS examples_ks.execution_profile (a int, b int, c text, primary key (a, b))",
&[],
)
.await?;

let mut query_insert: Query = "INSERT INTO ks.t (a, b, c) VALUES (?, ?, ?)".into();
let mut query_insert: Query =
"INSERT INTO examples_ks.execution_profile (a, b, c) VALUES (?, ?, ?)".into();

// As `query_insert` is set another handle than session1, the execution profile pointed by query's handle
// will be preferred, so the query below will be executed with `profile2`, even though `session1` is set `profile1`.
Expand All @@ -79,12 +80,18 @@ async fn main() -> Result<()> {
handle2.map_to_another_profile(profile1);
// And now the following queries are executed with profile1:
session1.query(query_insert.clone(), (3, 4, "def")).await?;
session2.query("SELECT * FROM ks.t", ()).await?;
session2
.query("SELECT * FROM examples_ks.execution_profile", ())
.await?;

// One can unset a profile handle from a statement and, since then, execute it with session's default profile.
query_insert.set_execution_profile_handle(None);
session3.query("SELECT * FROM ks.t", ()).await?; // This executes with default session profile.
session2.query("SELECT * FROM ks.t", ()).await?; // This executes with profile1.
session3
.query("SELECT * FROM examples_ks.execution_profile", ())
.await?; // This executes with default session profile.
session2
.query("SELECT * FROM examples_ks.execution_profile", ())
.await?; // This executes with profile1.

Ok(())
}
Loading

0 comments on commit c8abb78

Please sign in to comment.