tikv_client/raw/
mod.rs

1// Copyright 2018 TiKV Project Authors. Licensed under Apache-2.0.
2
3//! Raw related functionality.
4//!
5//! Using the [`raw::Client`](client::Client) you can utilize TiKV's raw interface.
6//!
7//! This interface offers optimal performance as it does not require coordination with a timestamp
8//! oracle, while the transactional interface does.
9//!
10//! **Warning:** It is not advisable to use both raw and transactional functionality in the same keyspace.
11
12use std::convert::TryFrom;
13use std::fmt;
14
15pub use self::client::Client;
16use crate::Error;
17
18mod client;
19pub mod lowering;
20mod requests;
21
22/// A [`ColumnFamily`](ColumnFamily) is an optional parameter for [`raw::Client`](Client) requests.
23///
24/// TiKV uses RocksDB's `ColumnFamily` support. You can learn more about RocksDB's `ColumnFamily`s [on their wiki](https://github.com/facebook/rocksdb/wiki/Column-Families).
25///
26/// By default in TiKV data is stored in three different `ColumnFamily` values, configurable in the TiKV server's configuration:
27///
28/// * Default: Where real user data is stored. Set by `[rocksdb.defaultcf]`.
29/// * Write: Where MVCC and index related data are stored. Set by `[rocksdb.writecf]`.
30/// * Lock: Where lock information is stored. Set by `[rocksdb.lockcf]`.
31///
32/// Not providing a call a `ColumnFamily` means it will use the default value of `default`.
33///
34/// The best (and only) way to create a [`ColumnFamily`](ColumnFamily) is via the `From` implementation:
35///
36/// # Examples
37/// ```rust
38/// # use tikv_client::ColumnFamily;
39/// # use std::convert::TryFrom;
40///
41/// let cf = ColumnFamily::try_from("write").unwrap();
42/// let cf = ColumnFamily::try_from(String::from("write")).unwrap();
43/// ```
44///
45/// **But, you should not need to worry about all this:** Many functions which accept a
46/// `ColumnFamily` accept an `Into<ColumnFamily>`, which means all of the above types can be passed
47/// directly to those functions.
48#[derive(Clone, Eq, PartialEq, Hash, Debug)]
49pub enum ColumnFamily {
50    Default,
51    Lock,
52    Write,
53}
54
55impl TryFrom<&str> for ColumnFamily {
56    type Error = Error;
57
58    fn try_from(value: &str) -> Result<Self, Self::Error> {
59        match value {
60            "default" => Ok(ColumnFamily::Default),
61            "lock" => Ok(ColumnFamily::Lock),
62            "write" => Ok(ColumnFamily::Write),
63            s => Err(Error::ColumnFamilyError(s.to_owned())),
64        }
65    }
66}
67
68impl TryFrom<String> for ColumnFamily {
69    type Error = Error;
70
71    fn try_from(value: String) -> Result<Self, Self::Error> {
72        TryFrom::try_from(&*value)
73    }
74}
75
76impl fmt::Display for ColumnFamily {
77    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
78        match self {
79            ColumnFamily::Default => f.write_str("default"),
80            ColumnFamily::Lock => f.write_str("lock"),
81            ColumnFamily::Write => f.write_str("write"),
82        }
83    }
84}
85
86trait RawRpcRequest: Default {
87    fn set_cf(&mut self, cf: String);
88
89    fn maybe_set_cf(&mut self, cf: Option<ColumnFamily>) {
90        if let Some(cf) = cf {
91            self.set_cf(cf.to_string());
92        }
93    }
94}