Skip to content

Commit da26bb4

Browse files
authored
Merge pull request #269 from nrc/docs
Improve documentation
2 parents 20318ab + 8a128ac commit da26bb4

File tree

13 files changed

+406
-300
lines changed

13 files changed

+406
-300
lines changed

README.md

Lines changed: 81 additions & 70 deletions
Large diffs are not rendered by default.

src/config.rs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,11 @@
33
use serde_derive::{Deserialize, Serialize};
44
use std::{path::PathBuf, time::Duration};
55

6-
/// The configuration for either a `raw::Client` or a `transaction::Client`.
6+
/// The configuration for either a [`RawClient`](crate::RawClient) or a
7+
/// [`TransactionClient`](crate::TransactionClient).
78
///
8-
/// Because TiKV is managed by a [PD](https://github.com/pingcap/pd/) cluster, the endpoints for PD
9-
/// must be provided, **not** the TiKV nodes.
10-
///
11-
/// It's important to **include more than one PD endpoint** (include all, if possible!)
12-
/// This helps avoid having a *single point of failure*.
13-
///
14-
/// By default, this client will use an insecure connection over instead of one protected by
15-
/// Transport Layer Security (TLS). Your deployment may have chosen to rely on security measures
16-
/// such as a private network, or a VPN layer to provide secure transmission.
17-
///
18-
/// To use a TLS secured connection, use the `with_security` function to set the required
19-
/// parameters.
20-
///
21-
/// TiKV does not currently offer encrypted storage (or encryption-at-rest).
9+
/// See also [`TransactionOptions`](crate::TransactionOptions) which provides more ways to configure
10+
/// requests.
2211
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
2312
#[serde(default)]
2413
#[serde(rename_all = "kebab-case")]
@@ -43,11 +32,16 @@ impl Default for Config {
4332
}
4433

4534
impl Config {
46-
/// Set the certificate authority, certificate, and key locations for the
47-
/// [`Config`](Config).
35+
/// Set the certificate authority, certificate, and key locations for clients.
4836
///
49-
/// By default, TiKV connections do not utilize transport layer security. Enable it by setting
50-
/// these values.
37+
/// By default, this client will use an insecure connection over instead of one protected by
38+
/// Transport Layer Security (TLS). Your deployment may have chosen to rely on security measures
39+
/// such as a private network, or a VPN layer to provide secure transmission.
40+
///
41+
/// To use a TLS secured connection, use the `with_security` function to set the required
42+
/// parameters.
43+
///
44+
/// TiKV does not currently offer encrypted storage (or encryption-at-rest).
5145
///
5246
/// # Examples
5347
/// ```rust
@@ -66,16 +60,21 @@ impl Config {
6660
self
6761
}
6862

69-
/// Set the timeout for the [`Config`](Config).
63+
/// Set the timeout for clients.
64+
///
65+
/// The timeout is used for all requests when using or connecting to a TiKV cluster (including
66+
/// PD nodes). If the request does not complete within timeout, the request is cancelled and
67+
/// an error returned to the user.
7068
///
69+
/// The default timeout is two seconds.
7170
///
7271
/// # Examples
7372
/// ```rust
7473
/// # use tikv_client::Config;
7574
/// # use std::time::Duration;
76-
/// let config = Config::default().timeout(Duration::from_secs(10));
75+
/// let config = Config::default().with_timeout(Duration::from_secs(10));
7776
/// ```
78-
pub fn timeout(mut self, timeout: Duration) -> Self {
77+
pub fn with_timeout(mut self, timeout: Duration) -> Self {
7978
self.timeout = timeout;
8079
self
8180
}

src/lib.rs

Lines changed: 72 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,108 @@
11
// Copyright 2018 TiKV Project Authors. Licensed under Apache-2.0.
22

3-
// Long and nested future chains can quickly result in large generic types.
4-
#![type_length_limit = "16777216"]
5-
#![allow(clippy::redundant_closure)]
6-
#![allow(clippy::type_complexity)]
7-
#![allow(incomplete_features)]
8-
9-
//! This crate provides a clean, ready to use client for [TiKV](https://github.com/tikv/tikv), a
10-
//! distributed transactional Key-Value database written in Rust.
11-
//!
12-
//! With this crate you can easily connect to any TiKV deployment, interact with it, and mutate the
13-
//! data it contains.
3+
//! This crate provides an easy-to-use client for [TiKV](https://github.com/tikv/tikv), a
4+
//! distributed, transactional key-value database written in Rust.
5+
//!
6+
//! This crate lets you connect to a TiKV cluster and use either a transactional or raw (simple
7+
//! get/put style without transactional consistency guarantees) API to access and update your data.
8+
//!
9+
//! The TiKV Rust client supports several levels of abstraction. The most convenient way to use the
10+
//! client is via [`RawClient`] and [`TransactionClient`]. This gives a very high-level API which
11+
//! mostly abstracts over the distributed nature of the store and has sensible defaults for all
12+
//! protocols. This interface can be configured, primarily when creating the client or transaction
13+
//! objects via the [`Config`] and [`TransactionOptions`] structs. Using some options, you can take
14+
//! over parts of the protocols (such as retrying failed messages) yourself.
15+
//!
16+
//! The lowest level of abstraction is to create and send gRPC messages directly to TiKV (and PD)
17+
//! nodes. The `tikv-client-store` and `tikv-client-pd` crates make this easier than using the
18+
//! protobuf definitions and a gRPC library directly, but give you the same level of control.
19+
//!
20+
//! In between these levels of abstraction, you can send and receive individual messages to the TiKV
21+
//! cluster, but take advantage of library code for common operations such as resolving data to
22+
//! regions and thus nodes in the cluster, or retrying failed messages. This can be useful for
23+
//! testing a TiKV cluster or for some advanced use cases. See the [`request`] module for
24+
//! this API, and [`raw::lowering`] and [`transaction::lowering`] for
25+
//! convenience methods for creating request objects.
1426
//!
1527
//! ## Choosing an API
1628
//!
17-
//! This crate offers both [**raw**](raw/index.html) and
18-
//! [**transactional**](transaction/index.html) APIs. You should choose just one for your system.
29+
//! This crate offers both [raw](RawClient) and
30+
//! [transactional](Transaction) APIs. You should choose just one for your system.
1931
//!
20-
//! The *consequence* of supporting transactions is increased overhead of coordination with the
21-
//! placement driver for timestamp acquisition. This is approximately 1 RTT.
32+
//! The consequence of supporting transactions is increased overhead of coordination with the
33+
//! placement driver and TiKV, and additional code complexity.
2234
//!
2335
//! *While it is possible to use both APIs at the same time, doing so is unsafe and unsupported.*
2436
//!
25-
//! Choose the one that suites your needs as described below, then add the import statement to your
26-
//! file where you need to use the library.
27-
//!
2837
//! ### Transactional
2938
//!
30-
//! The [transactional](transaction/index.html) API supports **transactions** via Multi-Version
31-
//! Concurrency Control (MVCC).
39+
//! The [transactional](Transaction) API supports **transactions** via multi-version
40+
//! concurrency control (MVCC).
3241
//!
33-
//! **Best when you mostly do** complex sets of actions, actions which may require a rollback,
34-
//! operations affecting multiple keys or values, or operations that depend on strong ordering.
42+
//! Best when you mostly do complex sets of actions, actions which may require a rollback,
43+
//! operations affecting multiple keys or values, or operations that depend on strong consistency.
3544
//!
36-
//! ```rust
37-
//! use tikv_client::*;
38-
//! ```
3945
//!
4046
//! ### Raw
4147
//!
42-
//! The [raw](raw/index.html) API has **reduced coordination overhead**, but lacks any
48+
//! The [raw](RawClient) API has reduced coordination overhead, but lacks any
4349
//! transactional abilities.
4450
//!
45-
//! **Best when you mostly do** single row changes, and have very limited cross-row (eg. foreign
46-
//! key) requirements. You will not be able to use transactions with this API.
51+
//! Best when you mostly do single value changes, and have very limited cross-value
52+
//! requirements. You will not be able to use transactions with this API.
4753
//!
48-
//! ```rust
49-
//! use tikv_client::*;
50-
//! ```
54+
//! ## Usage
55+
//!
56+
//! The general flow of using the client crate is to create either a raw or transaction client
57+
//! object (which can be configured) then send commands using the client object, or use it to create
58+
//! transactions objects. In the latter case, the transaction is built up using various commands and
59+
//! then committed (or rolled back).
5160
//!
52-
//! ## Connect
61+
//! ### Examples
5362
//!
54-
//! Regardless of which API you choose, you'll need to connect your client
55-
//! ([raw](raw::Client), [transactional](transaction::Client)).
63+
//! Raw mode:
5664
//!
5765
//! ```rust,no_run
58-
//! # use tikv_client::*;
66+
//! # use tikv_client::{RawClient, Result};
5967
//! # use futures::prelude::*;
60-
//!
68+
//! # fn main() -> Result<()> {
6169
//! # futures::executor::block_on(async {
62-
//! // Configure endpoints and optional TLS.
63-
//! let config = Config::default().with_security("root.ca", "internal.cert", "internal.key");
64-
//!
65-
//! // Get a transactional client.
66-
//! let client = TransactionClient::new_with_config(
67-
//! vec![
68-
//! // A list of PD endpoints.
69-
//! "192.168.0.100:2379",
70-
//! "192.168.0.101:2379",
71-
//! ], config).await.unwrap();
72-
//! # });
70+
//! let client = RawClient::new(vec!["127.0.0.1:2379"]).await?;
71+
//! client.put("key".to_owned(), "value".to_owned()).await?;
72+
//! let value = client.get("key".to_owned()).await?;
73+
//! # Ok(())
74+
//! # })}
7375
//! ```
7476
//!
75-
//! At this point, you should seek the documentation in the related API modules.
77+
//! Transactional mode:
78+
//!
79+
//! ```rust,no_run
80+
//! # use tikv_client::{TransactionClient, Result};
81+
//! # use futures::prelude::*;
82+
//! # fn main() -> Result<()> {
83+
//! # futures::executor::block_on(async {
84+
//! let txn_client = TransactionClient::new(vec!["127.0.0.1:2379"]).await?;
85+
//! let mut txn = txn_client.begin_optimistic().await?;
86+
//! txn.put("key".to_owned(), "value".to_owned()).await?;
87+
//! let value = txn.get("key".to_owned()).await?;
88+
//! txn.commit().await?;
89+
//! # Ok(())
90+
//! # })}
91+
//! ```
7692
7793
#[macro_use]
78-
mod request;
79-
94+
pub mod request;
8095
#[macro_use]
81-
mod transaction;
96+
#[doc(hidden)]
97+
pub mod transaction;
8298

8399
mod backoff;
84100
mod compat;
85101
mod config;
86102
mod kv;
87103
mod pd;
88-
mod raw;
104+
#[doc(hidden)]
105+
pub mod raw;
89106
mod region;
90107
mod stats;
91108
mod store;
@@ -105,18 +122,17 @@ pub use crate::backoff::Backoff;
105122
#[doc(inline)]
106123
pub use crate::kv::{BoundRange, IntoOwnedRange, Key, KvPair, Value};
107124
#[doc(inline)]
108-
pub use crate::raw::{lowering::*, Client as RawClient, ColumnFamily};
125+
pub use crate::raw::{lowering as raw_lowering, Client as RawClient, ColumnFamily};
109126
#[doc(inline)]
110127
pub use crate::request::RetryOptions;
111128
#[doc(inline)]
112129
pub use crate::timestamp::{Timestamp, TimestampExt};
113130
#[doc(inline)]
114131
pub use crate::transaction::{
115-
lowering::*, CheckLevel, Client as TransactionClient, Snapshot, Transaction, TransactionOptions,
132+
lowering as transaction_lowering, CheckLevel, Client as TransactionClient, Snapshot,
133+
Transaction, TransactionOptions,
116134
};
117135
#[doc(inline)]
118136
pub use config::Config;
119137
#[doc(inline)]
120-
pub use region::{Region, RegionId, RegionVerId, StoreId};
121-
#[doc(inline)]
122138
pub use tikv_client_common::{security::SecurityManager, Error, Result};

src/mock.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
88
use crate::{
99
pd::{PdClient, PdRpcClient, RetryClient},
10+
region::{Region, RegionId},
1011
store::Store,
11-
Config, Error, Key, Region, RegionId, Result, Timestamp,
12+
Config, Error, Key, Result, Timestamp,
1213
};
1314
use async_trait::async_trait;
1415
use derive_new::new;

src/pd/client.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
// Copyright 2018 TiKV Project Authors. Licensed under Apache-2.0.
22

33
use crate::{
4-
compat::stream_fn, kv::codec, pd::RetryClient, store::Store, BoundRange, Config, Key, Region,
5-
RegionId, Result, SecurityManager, Timestamp,
4+
compat::stream_fn,
5+
kv::codec,
6+
pd::RetryClient,
7+
region::{Region, RegionId},
8+
store::Store,
9+
BoundRange, Config, Key, Result, SecurityManager, Timestamp,
610
};
711
use async_trait::async_trait;
812
use futures::{prelude::*, stream::BoxStream};

src/pd/retry.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
//! A utility module for managing and retrying PD requests.
44
5-
use crate::{stats::pd_stats, Error, Region, RegionId, Result, SecurityManager, StoreId};
5+
use crate::{
6+
region::{Region, RegionId, StoreId},
7+
stats::pd_stats,
8+
Error, Result, SecurityManager,
9+
};
610
use async_trait::async_trait;
711
use futures_timer::Delay;
812
use grpcio::Environment;

0 commit comments

Comments
 (0)