Skip to content

Commit 0fa3118

Browse files
committed
docs: update for new paging API
1 parent fd204b6 commit 0fa3118

File tree

4 files changed

+70
-23
lines changed

4 files changed

+70
-23
lines changed

docs/source/queries/paged.md

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# Paged query
2-
Sometimes query results might not fit in a single page. Paged queries
3-
allow to receive the whole result page by page.
2+
Sometimes query results might be so big that one prefers not to fetch them all at once,
3+
e.g. to reduce latency and/or memory footprint.
4+
Paged queries allow to receive the whole result page by page, with a configurable page size.
45

5-
`Session::query_iter` and `Session::execute_iter` take a [simple query](simple.md) or a [prepared query](prepared.md)
6-
and return an `async` iterator over result `Rows`.
6+
`Session::query_iter` and `Session::execute_iter` take a [simple query](simple.md)
7+
or a [prepared query](prepared.md) and return an `async` iterator over result `Rows`.
78

89
> ***Warning***\
910
> In case of unprepared variant (`Session::query_iter`) if the values are not empty
@@ -79,7 +80,7 @@ On a `Query`:
7980
use scylla::query::Query;
8081

8182
let mut query: Query = Query::new("SELECT a, b FROM ks.t");
82-
query.set_page_size(16);
83+
query.set_page_size(16.try_into().unwrap());
8384

8485
let _ = session.query_iter(query, &[]).await?; // ...
8586
# Ok(())
@@ -98,7 +99,7 @@ let mut prepared: PreparedStatement = session
9899
.prepare("SELECT a, b FROM ks.t")
99100
.await?;
100101

101-
prepared.set_page_size(16);
102+
prepared.set_page_size(16.try_into().unwrap());
102103

103104
let _ = session.execute_iter(prepared, &[]).await?; // ...
104105
# Ok(())
@@ -117,12 +118,33 @@ On a `Query`:
117118
# use std::error::Error;
118119
# async fn check_only_compiles(session: &Session) -> Result<(), Box<dyn Error>> {
119120
use scylla::query::Query;
121+
use scylla::statement::{PagingState, PagingStateResponse};
122+
use std::ops::ControlFlow;
123+
124+
let paged_query = Query::new("SELECT a, b, c FROM ks.t").with_page_size(6.try_into().unwrap());
125+
126+
let mut paging_state = PagingState::start();
127+
loop {
128+
let (res, paging_state_response) = session
129+
.query_single_page(paged_query.clone(), &[], paging_state)
130+
.await?;
131+
132+
// Do something with `res`.
133+
// ...
134+
135+
match paging_state_response.into_paging_control_flow() {
136+
ControlFlow::Break(()) => {
137+
// No more pages to be fetched.
138+
break;
139+
}
140+
ControlFlow::Continue(new_paging_state) => {
141+
// Update paging state from the response, so that query
142+
// will be resumed from where it ended the last time.
143+
paging_state = new_paging_state
144+
}
145+
}
146+
}
120147

121-
let paged_query = Query::new("SELECT a, b, c FROM ks.t").with_page_size(6);
122-
let res1 = session.query(paged_query.clone(), &[]).await?;
123-
let res2 = session
124-
.query_single_page(paged_query.clone(), &[], res1.paging_state)
125-
.await?;
126148
# Ok(())
127149
# }
128150
```
@@ -139,14 +161,37 @@ On a `PreparedStatement`:
139161
# use std::error::Error;
140162
# async fn check_only_compiles(session: &Session) -> Result<(), Box<dyn Error>> {
141163
use scylla::query::Query;
164+
use scylla::statement::{PagingState, PagingStateResponse};
165+
use std::ops::ControlFlow;
142166

143167
let paged_prepared = session
144-
.prepare(Query::new("SELECT a, b, c FROM ks.t").with_page_size(7))
145-
.await?;
146-
let res1 = session.execute(&paged_prepared, &[]).await?;
147-
let res2 = session
148-
.execute_single_page(&paged_prepared, &[], res1.paging_state)
168+
.prepare(Query::new("SELECT a, b, c FROM ks.t").with_page_size(7.try_into().unwrap()))
149169
.await?;
170+
171+
let mut paging_state = PagingState::start();
172+
loop {
173+
let (res, paging_state_response) = session
174+
.execute_single_page(&paged_prepared, &[], paging_state)
175+
.await?;
176+
177+
println!(
178+
"Paging state response from the prepared statement execution: {:#?} ({} rows)",
179+
paging_state_response,
180+
res.rows_num()?,
181+
);
182+
183+
match paging_state_response.into_paging_control_flow() {
184+
ControlFlow::Break(()) => {
185+
// No more pages to be fetched.
186+
break;
187+
}
188+
ControlFlow::Continue(new_paging_state) => {
189+
// Update paging state from the response, so that query
190+
// will be resumed from where it ended the last time.
191+
paging_state = new_paging_state
192+
}
193+
}
194+
}
150195
# Ok(())
151196
# }
152197
```

docs/source/queries/result.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Query result
22

3-
`Session::query` and `Session::execute` return a `QueryResult` with rows represented as `Option<Vec<Row>>`.
3+
`Session::query_unpaged`, `Session::query_single_page`, `Session::execute_unpaged` and `Session::execute_single_page`
4+
return a `QueryResult` with rows represented as `Option<Vec<Row>>`.
45

56
### Basic representation
67
`Row` is a basic representation of a received row. It can be used by itself, but it's a bit awkward to use:

docs/source/queries/simple.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@ session
2020
> By default the query is unpaged and might cause heavy load on the cluster.\
2121
> In such cases set a page size and use [paged query](paged.md) instead.\
2222
>
23-
> When page size is set, `query` will return only the first page of results.
23+
> `query_unpaged` will return all results in one, possibly giant, piece
24+
> (unless a timeout occurs due to high load incurred by the cluster).
2425
2526
> ***Warning***\
2627
> If the values are not empty, driver first needs to send a `PREPARE` request
2728
> in order to fetch information required to serialize values. This will affect
2829
> performance because 2 round trips will be required instead of 1.
2930
3031
### First argument - the query
31-
As the first argument `Session::query` takes anything implementing `Into<Query>`.\
32+
As the first argument `Session::query_unpaged` takes anything implementing `Into<Query>`.\
3233
You can create a query manually to set custom options. For example to change query consistency:
3334
```rust
3435
# extern crate scylla;
@@ -74,7 +75,7 @@ Here the first `?` will be filled with `2` and the second with `"Some text"`.
7475
See [Query values](values.md) for more information about sending values in queries
7576

7677
### Query result
77-
`Session::query` returns `QueryResult` with rows represented as `Option<Vec<Row>>`.\
78+
`Session::query_unpaged` returns `QueryResult` with rows represented as `Option<Vec<Row>>`.\
7879
Each row can be parsed as a tuple of rust types using `rows_typed`:
7980
```rust
8081
# extern crate scylla;
@@ -92,8 +93,6 @@ while let Some(read_row) = iter.next().transpose()? {
9293
# Ok(())
9394
# }
9495
```
95-
> In cases where page size is set, simple query returns only a single page of results.\
96-
> To receive all pages use a [paged query](paged.md) instead.\
9796

9897
See [Query result](result.md) for more information about handling query results
9998

docs/source/queries/usekeyspace.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ session
4949
The first argument is the keyspace name.\
5050
The second argument states whether this name is case sensitive.
5151

52-
It is also possible to send raw use keyspace query using `Session::query` instead of `Session::use_keyspace` such as:
52+
It is also possible to send raw use keyspace query using `Session::query_*` instead of `Session::use_keyspace` such as:
53+
5354
```rust
5455
# extern crate scylla;
5556
# use scylla::Session;
@@ -59,6 +60,7 @@ session.query_unpaged("USE my_keyspace", &[]).await?;
5960
# Ok(())
6061
# }
6162
```
63+
6264
This method has a slightly worse latency than `Session::use_keyspace` - there are two roundtrips needed instead of one.
6365
Therefore, `Session::use_keyspace` is the preferred method for setting keyspaces.
6466

0 commit comments

Comments
 (0)