Skip to content

Commit 02377b7

Browse files
committed
git fire!: wip api v2
Signed-off-by: iosmanthus <[email protected]>
1 parent 7c3de1a commit 02377b7

File tree

4 files changed

+124
-9
lines changed

4 files changed

+124
-9
lines changed

src/kv/key.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use proptest::{arbitrary::any_with, collection::size_range};
88
#[cfg(test)]
99
use proptest_derive::Arbitrary;
1010
use std::{fmt, ops::Bound, u8};
11+
use std::ops::{Deref, DerefMut};
1112
use tikv_client_proto::kvrpcpb;
1213

1314
const _PROPTEST_KEY_MAX: usize = 1024 * 2; // 2 KB
@@ -179,3 +180,17 @@ impl fmt::Debug for Key {
179180
write!(f, "Key({})", HexRepr(&self.0))
180181
}
181182
}
183+
184+
impl Deref for Key {
185+
type Target = Vec<u8>;
186+
187+
fn deref(&self) -> &Self::Target {
188+
&self.0
189+
}
190+
}
191+
192+
impl DerefMut for Key {
193+
fn deref_mut(&mut self) -> &mut Self::Target {
194+
&mut self.0
195+
}
196+
}

src/raw/client.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::{
1919
},
2020
Backoff, BoundRange, ColumnFamily, Key, KvPair, Result, Value,
2121
};
22+
use crate::request::request_codec::RawApiV1;
2223

2324
const MAX_RAW_KV_SCAN_LIMIT: u32 = 10240;
2425

@@ -50,6 +51,16 @@ impl<C: RequestCodec> Clone for Client<C> {
5051
}
5152
}
5253

54+
impl Client<RawApiV1> {
55+
pub async fn new<S: Into<String>>(
56+
pd_endpoints: Vec<S>,
57+
logger: Option<Logger>,
58+
) -> Result<Self> {
59+
Self::new_with_config(pd_endpoints, Config::default(), RawApiV1, logger).await
60+
}
61+
}
62+
63+
5364
impl<C: RawCodec> Client<C> {
5465
/// Create a raw [`Client`] and connect to the TiKV cluster.
5566
///
@@ -69,7 +80,7 @@ impl<C: RawCodec> Client<C> {
6980
/// .unwrap();
7081
/// # });
7182
/// ```
72-
pub async fn new<S: Into<String>>(
83+
pub async fn new_with_codec<S: Into<String>>(
7384
pd_endpoints: Vec<S>,
7485
codec: C,
7586
logger: Option<Logger>,

src/request/request_codec.rs

Lines changed: 91 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1+
use tikv_client_common::Error;
12
use tikv_client_proto::metapb::Region;
3+
use tikv_client_store::Request;
24

3-
use crate::{kv::codec::decode_bytes_in_place, Key, Result};
5+
use crate::{Key, kv::codec::decode_bytes_in_place, Result};
6+
7+
const KEYSPACE_PREFIX_LEN: usize = 4;
8+
const RAW_MODE_PREFIX: u8 = b'r';
9+
const TXN_MODE_PREFIX: u8 = b'x';
10+
const RAW_MODE_MAX_KEY: Prefix = [RAW_MODE_PREFIX + 1, 0, 0, 0];
11+
const TXN_MODE_MAX_KEY: Prefix = [TXN_MODE_PREFIX + 1, 0, 0, 0];
12+
const MAX_KEYSPACE_ID: KeySpaceId = [0xff, 0xff, 0xff];
413

514
#[macro_export]
615
macro_rules! plain_request {
@@ -41,14 +50,90 @@ pub trait RequestCodec: Sized + Clone + Sync + Send + 'static {
4150
}
4251
}
4352

44-
#[derive(Clone)]
45-
pub struct RawApiV1;
53+
#[derive(Copy, Clone)]
54+
enum KeyMode {
55+
Raw,
56+
Txn,
57+
}
4658

47-
impl RequestCodec for RawApiV1 {}
59+
impl From<KeyMode> for u8 {
60+
fn from(mode: KeyMode) -> u8 {
61+
match mode {
62+
KeyMode::Raw => b'r',
63+
KeyMode::Txn => b'x',
64+
}
65+
}
66+
}
67+
68+
type Prefix = [u8; KEYSPACE_PREFIX_LEN];
69+
70+
type KeySpaceId = [u8; 3];
71+
72+
#[derive(Copy, Clone)]
73+
struct KeySpaceCodec {
74+
mode: KeyMode,
75+
id: KeySpaceId,
76+
}
77+
78+
impl From<KeySpaceCodec> for Prefix {
79+
fn from(codec: KeySpaceCodec) -> Self {
80+
[codec.mode.into(), codec.id[0], codec.id[1], codec.id[2]]
81+
}
82+
}
83+
84+
impl RequestCodec for KeySpaceCodec {
85+
fn encode_key(&self, mut key: Key) -> Key {
86+
let this = self.clone();
87+
let mut encoded = Vec::with_capacity(key.len() + KEYSPACE_PREFIX_LEN);
88+
let prefix: Prefix = this.into();
89+
encoded.extend_from_slice(&prefix);
90+
encoded.append(&mut key);
91+
encoded.into()
92+
}
93+
94+
fn decode_key(&self, mut key: Key) -> Result<Key> {
95+
let prefix: Prefix = self.clone().into();
96+
97+
if !key.starts_with(&prefix) {
98+
return Err(Error::CorruptedKeyspace {
99+
expected: prefix.to_vec(),
100+
actual: key[..KEYSPACE_PREFIX_LEN].to_vec(),
101+
key: key.into(),
102+
});
103+
}
104+
105+
Ok(key.split_off(KEYSPACE_PREFIX_LEN).into())
106+
}
107+
108+
fn encode_range(&self, start: Key, end: Key) -> (Key, Key) {
109+
(self.encode_key(start), self.encode_key(end))
110+
}
111+
112+
fn encode_pd_query(&self, key: Key) -> Key {
113+
todo!()
114+
}
115+
116+
fn decode_region(&self, region: Region) -> Result<Region> {
117+
todo!()
118+
}
119+
120+
fn is_plain(&self) -> bool {
121+
todo!()
122+
}
123+
}
48124

49125
#[derive(Clone)]
50126
pub struct TxnApiV1;
51127

128+
#[derive(Clone)]
129+
pub struct RawApiV1;
130+
131+
pub trait RawCodec: RequestCodec {}
132+
133+
pub trait TxnCodec: RequestCodec {}
134+
135+
impl RequestCodec for RawApiV1 {}
136+
52137
impl RequestCodec for TxnApiV1 {
53138
fn encode_pd_query(&self, key: Key) -> Key {
54139
key.to_encoded()
@@ -62,8 +147,6 @@ impl RequestCodec for TxnApiV1 {
62147
}
63148
}
64149

65-
pub trait RawCodec: RequestCodec {}
66-
pub trait TxnCodec: RequestCodec {}
67-
68150
impl RawCodec for RawApiV1 {}
69-
impl TxnCodec for TxnApiV1 {}
151+
152+
impl TxnCodec for TxnApiV1 {}

tikv-client-common/src/errors.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ pub enum Error {
9393
inner: Box<Error>,
9494
success_keys: Vec<Vec<u8>>,
9595
},
96+
#[error("Corrupted keyspace: expected: {:?}, actual: {:?}, key: {:?}" , expected, actual, key)]
97+
CorruptedKeyspace {
98+
expected: Vec<u8>,
99+
actual: Vec<u8>,
100+
key: Vec<u8>
101+
},
96102
}
97103

98104
impl From<tikv_client_proto::errorpb::Error> for Error {

0 commit comments

Comments
 (0)