TiKV Rust Client is still in the proof-of-concept stage and under development. You can track the development at tikv/client-rust repository.

Before TiKV Rust Client is officially released, it is not recommended to be used in a production environment.

This guide introduces how to interact with TiKV using Rust Client.

The minimum supported version of Rust is 1.40. The minimum supported version of TiKV is 5.0.0.

Basic data types

Both RawKV API and TxnKV API use the following basic data types:

  • Key: Refers to a key in the store. String and Vec<u8> implement Into<Key>, so you can pass them directly into client functions.
  • Value: Refers to a value in the store, which is an alias of Vec<u8>.
  • KvPair: Refers to a key-value pair. It provides convenient methods for conversion to and from other types.
  • BoundRange: Used for range related requests like scan. It implements From for Rust ranges so you can pass a Rust range of keys to the request. For example: client.delete_range(vec![]..).

Add dependencies

Before you start, you need to add the tikv-client as a dependency in the Cargo.toml file of your project.

  1. [dependencies]
  2. tikv-client = "0.1.0"

Raw key-value API

With a connected tikv_client::RawClient, you can perform actions such as put, get, delete, and scan:

  1. let client = RawClient::new(vec!["127.0.0.1:2379"]).await?;
  2. let key = "Hello".to_owned();
  3. let value = "RawKV".to_owned();
  4. // put
  5. let result = client.put(key.to_owned(), value.to_owned()).await?;
  6. assert_eq!(result, ());
  7. // get
  8. let result = client.get(key.to_owned()).await?;
  9. assert_eq!(result.unwrap(), value.as_bytes());
  10. // delete
  11. let result = client.delete(key.to_owned()).await?;
  12. assert_eq!(result, ());
  13. // get
  14. let result = client.get(key.to_owned()).await?;
  15. assert_eq!(result, None);
  16. // scan
  17. let limit = 1000;
  18. client.put("k1".to_owned(), "v1".to_owned()).await?;
  19. client.put("k2".to_owned(), "v2".to_owned()).await?;
  20. client.put("k3".to_owned(), "v3".to_owned()).await?;
  21. client.put("k4".to_owned(), "v4".to_owned()).await?;
  22. let result = client.scan("k1".to_owned().."k5".to_owned(), limit).await?;
  23. println!("{:?}", result);

These functions also have batch variants (batch_put, batch_get, batch_delete and batch_scan), which help to considerably reduce network overhead and greatly improve performance under certain workloads.

You can find all the functions that RawClient supports in the Raw requests table.

Transactional key-value API

With a connected tikv_client::TransactionClient, you can begin a transaction:

  1. use tikv_client::TransactionClient;
  2. let txn_client = TransactionClient::new(vec!["127.0.0.1:2379"]).await?;
  3. let mut txn = txn_client.begin_optimistic().await?;

Then you can send commands such as get, set, delete, and scan:

  1. let key = "Hello".to_owned();
  2. let value = "TxnKV".to_owned();
  3. // put
  4. let mut txn = txn_client.begin_optimistic().await?;
  5. txn.put(key.to_owned(), value.to_owned()).await?;
  6. txn.commit().await?;
  7. // get
  8. let mut txn = txn_client.begin_optimistic().await?;
  9. let result = txn.get(key.to_owned()).await?;
  10. txn.commit().await?;
  11. assert_eq!(result.unwrap(), value.as_bytes());
  12. // delete
  13. let mut txn = txn_client.begin_optimistic().await?;
  14. txn.delete(key.to_owned()).await?;
  15. txn.commit().await?;
  16. // get
  17. let mut txn = txn_client.begin_optimistic().await?;
  18. let result = txn.get(key.to_owned()).await?;
  19. txn.commit().await?;
  20. assert_eq!(result, None);
  21. // scan
  22. let mut txn = txn_client.begin_optimistic().await?;
  23. txn.put("k1".to_owned(), "v1".to_owned()).await?;
  24. txn.put("k2".to_owned(), "v2".to_owned()).await?;
  25. txn.put("k3".to_owned(), "v3".to_owned()).await?;
  26. txn.put("k4".to_owned(), "v4".to_owned()).await?;
  27. txn.commit().await?;
  28. let limit = 1000;
  29. let mut txn2 = txn_client.begin_optimistic().await?;
  30. let result = txn2.scan("k1".to_owned().."k5".to_owned(), limit).await?;
  31. result.for_each(|pair| println!("{:?}", pair));
  32. txn2.commit().await?;

You can commit these changes when you are ready, or roll back if you prefer to abort the operation:

  1. if all_is_good {
  2. txn.commit().await?;
  3. } else {
  4. txn.rollback().await?;
  5. }

These functions also have batch variants (batch_put, batch_get, batch_delete and batch_scan), which help to considerably reduce network overhead and greatly improve performance under certain workloads.

You can find all the functions that TransactionClient supports in the Transactional requests table.