Skip to content

Commit c1e8293

Browse files
committed
docs: add an entry for LWT queries
The entry explains that lightweight transaction queries are almost like regular queries, except they have a serial consistency level in addition to the regular consistency level.
1 parent dcd9c05 commit c1e8293

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

docs/source/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- [Prepared query](queries/prepared.md)
2020
- [Batch statement](queries/batch.md)
2121
- [Paged query](queries/paged.md)
22+
- [Lightweight transaction query (LWT)](queries/lwt.md)
2223
- [USE keyspace](queries/usekeyspace.md)
2324
- [Schema agreement](queries/schema_agreement.md)
2425

docs/source/queries/lwt.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Lightweight transaction (LWT) query
2+
3+
A lightweight transaction query can be expressed just like any other query, via `Session`, with the notable difference of having an additional consistency level parameter - the `serial_consistency_level`.
4+
5+
6+
### Format of the query
7+
A lightweight transaction query is not a separate type - it can be expressed just like any other queries: via `SimpleQuery`, `PreparedStatement`, batches, and so on. The difference lays in the query string itself - when it contains a condition (e.g. `IF NOT EXISTS`), it becomes a lightweight transaction. It's important to remember that CQL specification requires a separate, additional consistency level to be defined for LWT queries - `serial_consistency_level`. The serial consistency level can only be set to two values: `SerialConsistency::Serial` or `SerialConsistency::LocalSerial`. The "local" variant makes the transaction consistent only within the same datacenter. For convenience, Scylla Rust Driver sets the default consistency level to `LocalSerial`, as it's more commonly used. For cross-datacenter consistency, please remember to always override the default with `SerialConsistency::Serial`.
8+
```rust
9+
# extern crate scylla;
10+
# use scylla::Session;
11+
# use std::error::Error;
12+
# async fn check_only_compiles(session: &Session) -> Result<(), Box<dyn Error>> {
13+
use scylla::query::Query;
14+
use scylla::statement::{Consistency, SerialConsistency};
15+
16+
// Create a Query manually to change the Consistency to ONE
17+
let mut my_query: Query = Query::new("INSERT INTO ks.tab (a) VALUES(?) IF NOT EXISTS".to_string());
18+
my_query.set_consistency(Consistency::One);
19+
// Use cross-datacenter serial consistency
20+
my_query.set_serial_consistency(Some(SerialConsistency::Serial));
21+
22+
// Insert a value into the table
23+
let to_insert: i32 = 12345;
24+
session.query(my_query, (to_insert,)).await?;
25+
# Ok(())
26+
# }
27+
```
28+
29+
The rest of the API remains identical for LWT and non-LWT queries.
30+
31+
See [Query API documentation](https://docs.rs/scylla/0.2.0/scylla/statement/query/struct.Query.html) for more options
32+

0 commit comments

Comments
 (0)