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
1717let to_insert : CqlTimeuuid = CqlTimeuuid :: from_str (" 8e14e760-7fa8-11eb-bc66-000000000001" )? ;
18+
1819session
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) ) .
0 commit comments