|
| 1 | +# Timestamp generators |
| 2 | + |
| 3 | +If you want to generate timestamps on the client side you can provide |
| 4 | +a TimestampGenerator to a SessionBuilder when creating a Session. Then |
| 5 | +every executed statement will have attached a new timestamp generated |
| 6 | +by the provided TimestampGenerator. |
| 7 | +Timestamps are set according to precendence: |
| 8 | + |
| 9 | +1. ```USING TIMESTAMP``` in the query itself |
| 10 | +2. Manually using ```set_timestamp``` on the query |
| 11 | +3. Timestamp generated by the generator |
| 12 | + |
| 13 | +## Simple Timestamp Generator |
| 14 | + |
| 15 | +Most basic client-side timestamp generator. Generates timestamp |
| 16 | +based on system clock. Provides no guarantees and panics when the system clock |
| 17 | +provides timestamp before the unix epoch. |
| 18 | + |
| 19 | +## Monotonic Timestamp Generator |
| 20 | + |
| 21 | +Client-side timestamp generator. Guarantees monotonic timestamps |
| 22 | +based on the system clock, with automatic timestamp incrementation |
| 23 | +if the system clock timestamp would not be monotonic. If the clock skew |
| 24 | +exceeds `warning_threshold` of the generator (can be changed with `with_warning_times`, 1s by default) |
| 25 | +user will be warned with timestamp generation with `warning_interval` cooldown period |
| 26 | +(can be changed with `with_warning_times`, 1s by default) to not spam the user. If user does not want to |
| 27 | +be warned about the clock skew, the warnings can be turned off with `without_warnings` function. |
| 28 | + |
| 29 | +``` rust |
| 30 | +# extern crate scylla; |
| 31 | +# use std::error::Error; |
| 32 | +# async fn check_only_compiles() -> Result<(), Box<dyn std::error::Error>> { |
| 33 | +use scylla::client::session::Session; |
| 34 | +use scylla::client::session_builder::SessionBuilder; |
| 35 | +use scylla::policies::timestamp_generator::MonotonicTimestampGenerator; |
| 36 | +use scylla::query::Query; |
| 37 | +use std::sync::Arc; |
| 38 | + |
| 39 | +let session: Session = SessionBuilder::new() |
| 40 | + .known_node("127.0.0.1:9042") |
| 41 | + .timestamp_generator(Arc::new(MonotonicTimestampGenerator::new())) |
| 42 | + .build() |
| 43 | + .await?; |
| 44 | + |
| 45 | +// This query will have a timestamp generated |
| 46 | +// by the monotonic timestamp generator |
| 47 | +let my_query: Query = Query::new("INSERT INTO ks.tab (a) VALUES(?)"); |
| 48 | +let to_insert: i32 = 12345; |
| 49 | +session.query_unpaged(my_query, (to_insert,)).await?; |
| 50 | +# Ok(()) |
| 51 | +# } |
| 52 | +``` |
| 53 | + |
| 54 | + |
0 commit comments