Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: adding context to timeuuid using v1 feature #980

Merged
merged 3 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock.msrv

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 47 additions & 4 deletions docs/source/data-types/timeuuid.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Timeuuid

`Timeuuid` is represented as `value::CqlTimeuuid`.
`value::CqlTimeuuid` is a wrapper for `uuid::Uuid` with custom ordering logic
which follows Scylla/Cassandra semantics.
The `Timeuuid` type is represented as `value::CqlTimeuuid`.

Also, `value::CqlTimeuuid` is a wrapper for `uuid::Uuid` with custom ordering logic which follows Scylla/Cassandra semantics.
danielhe4rt marked this conversation as resolved.
Show resolved Hide resolved

```rust
# extern crate scylla;
Expand All @@ -27,4 +27,47 @@ if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.ro
}
# Ok(())
# }
```
```

## Creating your own Timeuuid

To create your own `Timeuuid` objects from timestamp-based `uuid` v1, you need to enable the feature `v1` of `uuid` crate using:

```shell
cargo add uuid -F v1
```

and now you're gonna be able to use the `uuid::v1` features:

```rust
danielhe4rt marked this conversation as resolved.
Show resolved Hide resolved
# extern crate uuid;
# extern crate scylla;
# use scylla::Session;
# use std::error::Error;
# use std::str::FromStr;
use scylla::IntoTypedRows;
use scylla::frame::value::CqlTimeuuid;
use uuid::Uuid;
# async fn check_only_compiles(session: &Session) -> Result<(), Box<dyn Error>> {

// Tip: you can use random stable numbers or your MAC Address
danielhe4rt marked this conversation as resolved.
Show resolved Hide resolved
let node_id = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC];

// Build your Timeuuid with the current timestamp
let to_insert = CqlTimeuuid::from(Uuid::now_v1(&node_id));

session
.query("INSERT INTO keyspace.table (a) VALUES(?)", (to_insert,))
.await?;

danielhe4rt marked this conversation as resolved.
Show resolved Hide resolved
// Read timeuuid from the table
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
for row in rows.into_typed::<(CqlTimeuuid,)>() {
let (timeuuid_value,): (CqlTimeuuid,) = row?;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use raw rows field on QueryResult; prefer rows_typed::<(CqlTimeuuid,)>() method:

Suggested change
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
for row in rows.into_typed::<(CqlTimeuuid,)>() {
let (timeuuid_value,): (CqlTimeuuid,) = row?;
}
}
let mut iter = session.query("SELECT a FROM keyspace.table", &[]).await?.rows_typed::<(CqlTimeuuid,)>();
while let Some((timeuuid_value,)) = iter.next().transpose()? {
// do something with timeuuid_value
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll update, but can you explain in details why I shouldn't use it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed! Ready to merge.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rows make you interact with an untyped, materialised vec of columns. At the present state of the driver, the main drawback is reduced type safety; as you call into_typed anyway, this negative aspect is mitigated.
Another reason for avoiding using rows ATM, which is relevant here, is the ongoing deserialization refactor. We ditch the materialized vec of CqlValue at all to reduce unnecessary allocations. The new deserialization framework is lazy, which means that the values are going to be deserialized on-the-fly straight to the end type, based on the type provided in the iterator (rows_typed::<T>).
With the new framework having landed in the driver, rows will be removed at all (although their capabilities can be emulated for the users that really need them).

See #955

}
# Ok(())
# }
```

Learn more about UUID::v1 [here](https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_1_(date-time_and_MAC_address)).
2 changes: 1 addition & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ tracing = "0.1.25"
tracing-subscriber = { version = "0.3.14", features = ["env-filter"] }
chrono = { version = "0.4", default-features = false }
time = { version = "0.3.22" }
uuid = "1.0"
uuid = { version = "1.0", features = ["v1"]}
tower = "0.4"
stats_alloc = "0.1"
clap = { version = "3.2.4", features = ["derive"] }
Expand Down
Loading