Skip to content

Commit 79d0d56

Browse files
committed
Documentation in lib.rs
Signed-off-by: Nick Cameron <nrc@ncameron.org>
1 parent a89599e commit 79d0d56

File tree

2 files changed

+63
-11
lines changed

2 files changed

+63
-11
lines changed

src/lib.rs

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,28 @@
66
//! This crate lets you connect to a TiKV cluster and use either a transactional or raw (simple
77
//! get/put style without transactional consistency guarantees) API to access and update your data.
88
//!
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.
26+
//!
927
//! ## Choosing an API
1028
//!
11-
//! This crate offers both [**raw**](raw/index.html) and
12-
//! [**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.
1331
//!
1432
//! The consequence of supporting transactions is increased overhead of coordination with the
1533
//! placement driver and TiKV, and additional code complexity.
@@ -18,7 +36,7 @@
1836
//!
1937
//! ### Transactional
2038
//!
21-
//! The [transactional](transaction/index.html) API supports **transactions** via multi-version
39+
//! The [transactional](Transaction) API supports **transactions** via multi-version
2240
//! concurrency control (MVCC).
2341
//!
2442
//! Best when you mostly do complex sets of actions, actions which may require a rollback,
@@ -27,24 +45,56 @@
2745
//!
2846
//! ### Raw
2947
//!
30-
//! 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
3149
//! transactional abilities.
3250
//!
3351
//! Best when you mostly do single value changes, and have very limited cross-value
3452
//! requirements. You will not be able to use transactions with this API.
53+
//!
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).
60+
//!
61+
//! ### Examples
62+
//!
63+
//! Raw mode:
64+
//!
65+
//! ```rust
66+
//! use tikv_client::RawClient;
67+
//!
68+
//! let client = RawClient::new(vec!["127.0.0.1:2379"]).await?;
69+
//! client.put("key".to_owned(), "value".to_owned()).await?;
70+
//! let value = client.get("key".to_owned()).await?;
71+
//! ```
72+
//!
73+
//! Transactional mode:
74+
//!
75+
//! ```rust
76+
//! use tikv_client::TransactionClient;
77+
//!
78+
//! let txn_client = TransactionClient::new(vec!["127.0.0.1:2379"]).await?;
79+
//! let mut txn = txn_client.begin_optimistic().await?;
80+
//! txn.put("key".to_owned(), "value".to_owned()).await?;
81+
//! let value = txn.get("key".to_owned()).await?;
82+
//! txn.commit().await?;
83+
//! ```
3584
3685
#[macro_use]
37-
mod request;
38-
86+
pub mod request;
3987
#[macro_use]
40-
mod transaction;
88+
#[doc(hidden)]
89+
pub mod transaction;
4190

4291
mod backoff;
4392
mod compat;
4493
mod config;
4594
mod kv;
4695
mod pd;
47-
mod raw;
96+
#[doc(hidden)]
97+
pub mod raw;
4898
mod region;
4999
mod stats;
50100
mod store;
@@ -64,14 +114,15 @@ pub use crate::backoff::Backoff;
64114
#[doc(inline)]
65115
pub use crate::kv::{BoundRange, IntoOwnedRange, Key, KvPair, Value};
66116
#[doc(inline)]
67-
pub use crate::raw::{lowering::*, Client as RawClient, ColumnFamily};
117+
pub use crate::raw::{lowering as raw_lowering, Client as RawClient, ColumnFamily};
68118
#[doc(inline)]
69119
pub use crate::request::RetryOptions;
70120
#[doc(inline)]
71121
pub use crate::timestamp::{Timestamp, TimestampExt};
72122
#[doc(inline)]
73123
pub use crate::transaction::{
74-
lowering::*, CheckLevel, Client as TransactionClient, Snapshot, Transaction, TransactionOptions,
124+
lowering as transaction_lowering, CheckLevel, Client as TransactionClient, Snapshot,
125+
Transaction, TransactionOptions,
75126
};
76127
#[doc(inline)]
77128
pub use config::Config;

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;

0 commit comments

Comments
 (0)