Skip to content

Commit 43044bd

Browse files
authored
docs: add context to timeuuid using v1 feature (#980)
* docs: adding context to timeuuid using v1 feature * docs: adding context to timeuuid creation * docs: adding context to timeuuid using v1 feature
1 parent e68adf5 commit 43044bd

File tree

3 files changed

+65
-10
lines changed

3 files changed

+65
-10
lines changed

Cargo.lock.msrv

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/source/data-types/timeuuid.md

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Timeuuid
22

3-
`Timeuuid` is represented as `value::CqlTimeuuid`.
4-
`value::CqlTimeuuid` is a wrapper for `uuid::Uuid` with custom ordering logic
5-
which follows Scylla/Cassandra semantics.
3+
The `Timeuuid` type is represented as `value::CqlTimeuuid`.
4+
5+
Also, `value::CqlTimeuuid` is a wrapper for `uuid::Uuid` with custom ordering logic which follows Scylla/Cassandra semantics.
66

77
```rust
88
# extern crate scylla;
@@ -15,16 +15,64 @@ use scylla::frame::value::CqlTimeuuid;
1515

1616
// Insert some timeuuid into the table
1717
let to_insert: CqlTimeuuid = CqlTimeuuid::from_str("8e14e760-7fa8-11eb-bc66-000000000001")?;
18+
1819
session
1920
.query("INSERT INTO keyspace.table (a) VALUES(?)", (to_insert,))
2021
.await?;
2122

22-
// Read timeuuid from the table
23-
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
24-
for row in rows.into_typed::<(CqlTimeuuid,)>() {
25-
let (timeuuid_value,): (CqlTimeuuid,) = row?;
26-
}
23+
// Read Timeuuid from the table
24+
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
25+
26+
let mut iter = result.rows_typed::<(CqlTimeuuid, )>()?;
27+
28+
while let Some((timeuuid,)) = iter.next().transpose()? {
29+
println!("Read a value from row: {}", timeuuid);
2730
}
2831
# Ok(())
2932
# }
30-
```
33+
```
34+
35+
## Creating your own Timeuuid
36+
37+
To create your own `Timeuuid` objects from timestamp-based `uuid` v1, you need to enable the feature `v1` of `uuid` crate using:
38+
39+
```shell
40+
cargo add uuid -F v1
41+
```
42+
43+
and now you're gonna be able to use the `uuid::v1` features:
44+
45+
```rust
46+
# extern crate uuid;
47+
# extern crate scylla;
48+
# use scylla::Session;
49+
# use std::error::Error;
50+
# use std::str::FromStr;
51+
use scylla::IntoTypedRows;
52+
use scylla::frame::value::CqlTimeuuid;
53+
use uuid::Uuid;
54+
# async fn check_only_compiles(session: &Session) -> Result<(), Box<dyn Error>> {
55+
56+
// Tip: you can use random stable numbers or your MAC Address
57+
let node_id = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC];
58+
59+
// Build your Timeuuid with the current timestamp
60+
let to_insert = CqlTimeuuid::from(Uuid::now_v1(&node_id));
61+
62+
session
63+
.query("INSERT INTO keyspace.table (a) VALUES(?)", (to_insert,))
64+
.await?;
65+
66+
// Read Timeuuid from the table
67+
let result = session.query("SELECT a FROM keyspace.table", &[]).await?;
68+
69+
let mut iter = result.rows_typed::<(CqlTimeuuid, )>()?;
70+
71+
while let Some((timeuuid,)) = iter.next().transpose()? {
72+
println!("Read a value from row: {}", timeuuid);
73+
}
74+
# Ok(())
75+
# }
76+
```
77+
78+
Learn more about UUID::v1 [here](https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_1_(date-time_and_MAC_address)).

examples/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ tracing = "0.1.25"
1616
tracing-subscriber = { version = "0.3.14", features = ["env-filter"] }
1717
chrono = { version = "0.4", default-features = false }
1818
time = { version = "0.3.22" }
19-
uuid = "1.0"
19+
uuid = { version = "1.0", features = ["v1"]}
2020
tower = "0.4"
2121
stats_alloc = "0.1"
2222
clap = { version = "3.2.4", features = ["derive"] }

0 commit comments

Comments
 (0)