Skip to content

Commit 9a092f9

Browse files
piodulwprzytula
andcommitted
examples: adjust to use the new interface
This commit goes over all unadjusted examples and changes them to use the new deserialization framework. Again, it contains a lot of changes, but they are quite simple. Co-authored-by: Wojciech Przytuła <[email protected]>
1 parent 5919cf9 commit 9a092f9

24 files changed

+213
-192
lines changed

examples/allocations.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use anyhow::Result;
2-
use scylla::{statement::prepared_statement::PreparedStatement, LegacySession, SessionBuilder};
2+
use scylla::transport::session::Session;
3+
use scylla::{statement::prepared_statement::PreparedStatement, SessionBuilder};
34
use std::io::Write;
45
use std::sync::atomic::{AtomicUsize, Ordering};
56
use std::sync::Arc;
@@ -65,7 +66,7 @@ fn print_stats(stats: &stats_alloc::Stats, reqs: f64) {
6566
}
6667

6768
async fn measure(
68-
session: Arc<LegacySession>,
69+
session: Arc<Session>,
6970
prepared: Arc<PreparedStatement>,
7071
reqs: usize,
7172
parallelism: usize,
@@ -128,10 +129,7 @@ async fn main() -> Result<()> {
128129

129130
println!("Connecting to {} ...", args.node);
130131

131-
let session: LegacySession = SessionBuilder::new()
132-
.known_node(args.node)
133-
.build_legacy()
134-
.await?;
132+
let session: Session = SessionBuilder::new().known_node(args.node).build().await?;
135133
let session = Arc::new(session);
136134

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

examples/auth.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ async fn main() -> Result<()> {
1010
let session = SessionBuilder::new()
1111
.known_node(uri)
1212
.user("cassandra", "cassandra")
13-
.build_legacy()
13+
.build()
1414
.await
1515
.unwrap();
1616

examples/basic.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use anyhow::Result;
2-
use futures::TryStreamExt;
3-
use scylla::macros::FromRow;
4-
use scylla::transport::session::LegacySession;
2+
use futures::StreamExt as _;
3+
use futures::TryStreamExt as _;
4+
use scylla::frame::response::result::Row;
5+
use scylla::transport::session::Session;
6+
use scylla::DeserializeRow;
57
use scylla::SessionBuilder;
68
use std::env;
79

@@ -11,7 +13,7 @@ async fn main() -> Result<()> {
1113

1214
println!("Connecting to {} ...", uri);
1315

14-
let session: LegacySession = SessionBuilder::new().known_node(uri).build_legacy().await?;
16+
let session: Session = SessionBuilder::new().known_node(uri).build().await?;
1517

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

@@ -53,39 +55,38 @@ async fn main() -> Result<()> {
5355
let mut iter = session
5456
.query_iter("SELECT a, b, c FROM examples_ks.basic", &[])
5557
.await?
56-
.into_typed::<(i32, i32, String)>();
58+
.rows_stream::<(i32, i32, String)>()?;
5759
while let Some((a, b, c)) = iter.try_next().await? {
5860
println!("a, b, c: {}, {}, {}", a, b, c);
5961
}
6062

61-
// Or as custom structs that derive FromRow
62-
#[derive(Debug, FromRow)]
63+
// Or as custom structs that derive DeserializeRow
64+
#[allow(unused)]
65+
#[derive(Debug, DeserializeRow)]
6366
struct RowData {
64-
_a: i32,
65-
_b: Option<i32>,
66-
_c: String,
67+
a: i32,
68+
b: Option<i32>,
69+
c: String,
6770
}
6871

6972
let mut iter = session
7073
.query_iter("SELECT a, b, c FROM examples_ks.basic", &[])
7174
.await?
72-
.into_typed::<RowData>();
75+
.rows_stream::<RowData>()?;
7376
while let Some(row_data) = iter.try_next().await? {
7477
println!("row_data: {:?}", row_data);
7578
}
7679

7780
// Or simply as untyped rows
7881
let mut iter = session
7982
.query_iter("SELECT a, b, c FROM examples_ks.basic", &[])
80-
.await?;
81-
while let Some(row) = iter.try_next().await? {
83+
.await?
84+
.rows_stream::<Row>()?;
85+
while let Some(row) = iter.next().await.transpose()? {
8286
let a = row.columns[0].as_ref().unwrap().as_int().unwrap();
8387
let b = row.columns[1].as_ref().unwrap().as_int().unwrap();
8488
let c = row.columns[2].as_ref().unwrap().as_text().unwrap();
8589
println!("a, b, c: {}, {}, {}", a, b, c);
86-
87-
// Alternatively each row can be parsed individually
88-
// let (a2, b2, c2) = row.into_typed::<(i32, i32, String)>() ?;
8990
}
9091

9192
let metrics = session.get_metrics();

examples/cloud.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ async fn main() -> Result<()> {
1212
.unwrap_or("examples/config_data.yaml".to_owned());
1313
let session = CloudSessionBuilder::new(Path::new(&config_path))
1414
.unwrap()
15-
.build_legacy()
15+
.build()
1616
.await
1717
.unwrap();
1818

examples/compare-tokens.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use anyhow::Result;
22
use scylla::routing::Token;
33
use scylla::transport::NodeAddr;
4-
use scylla::{LegacySession, SessionBuilder};
4+
use scylla::{Session, SessionBuilder};
55
use std::env;
66

77
#[tokio::main]
@@ -10,7 +10,7 @@ async fn main() -> Result<()> {
1010

1111
println!("Connecting to {} ...", uri);
1212

13-
let session: LegacySession = SessionBuilder::new().known_node(uri).build_legacy().await?;
13+
let session: Session = SessionBuilder::new().known_node(uri).build().await?;
1414

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

@@ -51,7 +51,9 @@ async fn main() -> Result<()> {
5151
(pk,),
5252
)
5353
.await?
54-
.single_row_typed::<(i64,)>()?;
54+
.into_rows_result()?
55+
.expect("Got not Rows result")
56+
.single_row()?;
5557
assert_eq!(t, qt);
5658
println!("token for {}: {}", pk, t);
5759
}

examples/cql-time-types.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
use anyhow::Result;
55
use chrono::{DateTime, NaiveDate, NaiveTime, Utc};
6-
use futures::{StreamExt, TryStreamExt};
6+
use futures::{StreamExt as _, TryStreamExt as _};
77
use scylla::frame::response::result::CqlValue;
88
use scylla::frame::value::{CqlDate, CqlTime, CqlTimestamp};
9-
use scylla::transport::session::LegacySession;
9+
use scylla::transport::session::Session;
1010
use scylla::SessionBuilder;
1111
use std::env;
1212

@@ -16,7 +16,7 @@ async fn main() -> Result<()> {
1616

1717
println!("Connecting to {} ...", uri);
1818

19-
let session: LegacySession = SessionBuilder::new().known_node(uri).build_legacy().await?;
19+
let session: Session = SessionBuilder::new().known_node(uri).build().await?;
2020

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

@@ -44,7 +44,7 @@ async fn main() -> Result<()> {
4444
let mut iter = session
4545
.query_iter("SELECT d from examples_ks.dates", &[])
4646
.await?
47-
.into_typed::<(NaiveDate,)>();
47+
.rows_stream::<(NaiveDate,)>()?;
4848
while let Some(row_result) = iter.next().await {
4949
let (read_date,): (NaiveDate,) = match row_result {
5050
Ok(read_date) => read_date,
@@ -66,7 +66,7 @@ async fn main() -> Result<()> {
6666
let mut iter = session
6767
.query_iter("SELECT d from examples_ks.dates", &[])
6868
.await?
69-
.into_typed::<(time::Date,)>();
69+
.rows_stream::<(time::Date,)>()?;
7070
while let Some(row_result) = iter.next().await {
7171
let (read_date,): (time::Date,) = match row_result {
7272
Ok(read_date) => read_date,
@@ -88,7 +88,7 @@ async fn main() -> Result<()> {
8888
let mut iter = session
8989
.query_iter("SELECT d from examples_ks.dates", &[])
9090
.await?
91-
.into_typed::<(CqlValue,)>();
91+
.rows_stream::<(CqlValue,)>()?;
9292
while let Some(row_result) = iter.next().await {
9393
let read_days: u32 = match row_result {
9494
Ok((CqlValue::Date(CqlDate(days)),)) => days,
@@ -124,7 +124,7 @@ async fn main() -> Result<()> {
124124
let mut iter = session
125125
.query_iter("SELECT d from examples_ks.times", &[])
126126
.await?
127-
.into_typed::<(NaiveTime,)>();
127+
.rows_stream::<(NaiveTime,)>()?;
128128
while let Some((read_time,)) = iter.try_next().await? {
129129
println!("Parsed a time into chrono::NaiveTime: {:?}", read_time);
130130
}
@@ -139,7 +139,7 @@ async fn main() -> Result<()> {
139139
let mut iter = session
140140
.query_iter("SELECT d from examples_ks.times", &[])
141141
.await?
142-
.into_typed::<(time::Time,)>();
142+
.rows_stream::<(time::Time,)>()?;
143143
while let Some((read_time,)) = iter.try_next().await? {
144144
println!("Parsed a time into time::Time: {:?}", read_time);
145145
}
@@ -154,7 +154,7 @@ async fn main() -> Result<()> {
154154
let mut iter = session
155155
.query_iter("SELECT d from examples_ks.times", &[])
156156
.await?
157-
.into_typed::<(CqlTime,)>();
157+
.rows_stream::<(CqlTime,)>()?;
158158
while let Some((read_time,)) = iter.try_next().await? {
159159
println!("Read a time as raw nanos: {:?}", read_time);
160160
}
@@ -185,7 +185,7 @@ async fn main() -> Result<()> {
185185
let mut iter = session
186186
.query_iter("SELECT d from examples_ks.timestamps", &[])
187187
.await?
188-
.into_typed::<(DateTime<Utc>,)>();
188+
.rows_stream::<(DateTime<Utc>,)>()?;
189189
while let Some((read_time,)) = iter.try_next().await? {
190190
println!(
191191
"Parsed a timestamp into chrono::DateTime<chrono::Utc>: {:?}",
@@ -206,7 +206,7 @@ async fn main() -> Result<()> {
206206
let mut iter = session
207207
.query_iter("SELECT d from examples_ks.timestamps", &[])
208208
.await?
209-
.into_typed::<(time::OffsetDateTime,)>();
209+
.rows_stream::<(time::OffsetDateTime,)>()?;
210210
while let Some((read_time,)) = iter.try_next().await? {
211211
println!(
212212
"Parsed a timestamp into time::OffsetDateTime: {:?}",
@@ -227,7 +227,7 @@ async fn main() -> Result<()> {
227227
let mut iter = session
228228
.query_iter("SELECT d from examples_ks.timestamps", &[])
229229
.await?
230-
.into_typed::<(CqlTimestamp,)>();
230+
.rows_stream::<(CqlTimestamp,)>()?;
231231
while let Some((read_time,)) = iter.try_next().await? {
232232
println!("Read a timestamp as raw millis: {:?}", read_time);
233233
}

examples/cqlsh-rs.rs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ use rustyline::completion::{Completer, Pair};
33
use rustyline::error::ReadlineError;
44
use rustyline::{CompletionType, Config, Context, Editor};
55
use rustyline_derive::{Helper, Highlighter, Hinter, Validator};
6+
use scylla::frame::response::result::Row;
7+
use scylla::transport::session::Session;
68
use scylla::transport::Compression;
7-
use scylla::{LegacyQueryResult, LegacySession, SessionBuilder};
9+
use scylla::QueryRowsResult;
10+
use scylla::SessionBuilder;
811
use std::env;
912

1013
#[derive(Helper, Highlighter, Validator, Hinter)]
@@ -173,23 +176,24 @@ impl Completer for CqlHelper {
173176
}
174177
}
175178

176-
fn print_result(result: &LegacyQueryResult) {
177-
if result.rows.is_none() {
178-
println!("OK");
179-
return;
180-
}
181-
for row in result.rows.as_ref().unwrap() {
182-
for column in &row.columns {
183-
print!("|");
184-
print!(
185-
" {:16}",
186-
match column {
187-
None => "null".to_owned(),
188-
Some(value) => format!("{:?}", value),
189-
}
190-
);
179+
fn print_result(result: Option<&QueryRowsResult>) {
180+
if let Some(rows_result) = result {
181+
for row in rows_result.rows::<Row>().unwrap() {
182+
let row = row.unwrap();
183+
for column in &row.columns {
184+
print!("|");
185+
print!(
186+
" {:16}",
187+
match column {
188+
None => "null".to_owned(),
189+
Some(value) => format!("{:?}", value),
190+
}
191+
);
192+
}
193+
println!("|")
191194
}
192-
println!("|")
195+
} else {
196+
println!("OK");
193197
}
194198
}
195199

@@ -199,10 +203,10 @@ async fn main() -> Result<()> {
199203

200204
println!("Connecting to {} ...", uri);
201205

202-
let session: LegacySession = SessionBuilder::new()
206+
let session: Session = SessionBuilder::new()
203207
.known_node(uri)
204208
.compression(Some(Compression::Lz4))
205-
.build_legacy()
209+
.build()
206210
.await?;
207211

208212
let config = Config::builder()
@@ -222,7 +226,10 @@ async fn main() -> Result<()> {
222226
let maybe_res = session.query_unpaged(line, &[]).await;
223227
match maybe_res {
224228
Err(err) => println!("Error: {}", err),
225-
Ok(res) => print_result(&res),
229+
Ok(res) => {
230+
let rows_res = res.into_rows_result()?;
231+
print_result(rows_res.as_ref())
232+
}
226233
}
227234
}
228235
Err(ReadlineError::Interrupted) => continue,

0 commit comments

Comments
 (0)