Skip to content

Commit 19c3123

Browse files
committed
git fire!: wip impl RequestCodec for requests
Signed-off-by: iosmanthus <[email protected]>
1 parent 0a72d31 commit 19c3123

File tree

3 files changed

+36
-63
lines changed

3 files changed

+36
-63
lines changed

src/raw/requests.rs

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ use super::RawRpcRequest;
1212
use crate::{
1313
collect_first,
1414
pd::PdClient,
15-
plain_request, plain_response,
1615
request::{
17-
plan::ResponseWithShard, codec::RequestCodec, Collect, CollectSingle,
18-
DefaultProcessor, KvRequest, Merge, Process, Shardable, SingleKey,
16+
codec::RequestCodec, plan::ResponseWithShard, Collect, CollectSingle, DefaultProcessor,
17+
KvRequest, Merge, Process, Shardable, SingleKey,
1918
},
2019
store::{store_stream_for_keys, store_stream_for_ranges, RegionStore},
2120
transaction::HasLocks,
@@ -31,20 +30,7 @@ pub fn new_raw_get_request(key: Vec<u8>, cf: Option<ColumnFamily>) -> kvrpcpb::R
3130
req
3231
}
3332

34-
impl<C: RequestCodec> KvRequest<C> for kvrpcpb::RawGetRequest {
35-
type Response = kvrpcpb::RawGetResponse;
36-
37-
fn encode_request(&self, codec: &C) -> Cow<Self> {
38-
plain_request!(self, codec);
39-
todo!()
40-
}
41-
42-
fn decode_response(&self, codec: &C, resp: Self::Response) -> Result<Self::Response> {
43-
plain_response!(resp, codec);
44-
todo!()
45-
}
46-
}
47-
33+
kv_request_with_key!(kvrpcpb::RawGetRequest, kvrpcpb::RawGetResponse);
4834
shardable_key!(kvrpcpb::RawGetRequest);
4935
collect_first!(kvrpcpb::RawGetResponse);
5036

@@ -78,20 +64,7 @@ pub fn new_raw_batch_get_request(
7864
req
7965
}
8066

81-
impl<C: RequestCodec> KvRequest<C> for kvrpcpb::RawBatchGetRequest {
82-
type Response = kvrpcpb::RawBatchGetResponse;
83-
84-
fn encode_request(&self, codec: &C) -> Cow<Self> {
85-
plain_request!(self, codec);
86-
todo!()
87-
}
88-
89-
fn decode_response(&self, codec: &C, resp: Self::Response) -> Result<Self::Response> {
90-
plain_response!(resp, codec);
91-
todo!()
92-
}
93-
}
94-
67+
kv_request_with_key!(kvrpcpb::RawBatchGetRequest, kvrpcpb::RawBatchGetResponse);
9568
shardable_keys!(kvrpcpb::RawBatchGetRequest);
9669

9770
impl Merge<kvrpcpb::RawBatchGetResponse> for Collect {
@@ -120,22 +93,10 @@ pub fn new_raw_put_request(
12093
req
12194
}
12295

123-
impl<C: RequestCodec> KvRequest<C> for kvrpcpb::RawPutRequest {
124-
type Response = kvrpcpb::RawPutResponse;
125-
126-
fn encode_request(&self, codec: &C) -> Cow<Self> {
127-
plain_request!(self, codec);
128-
todo!()
129-
}
130-
131-
fn decode_response(&self, codec: &C, resp: Self::Response) -> Result<Self::Response> {
132-
plain_response!(resp, codec);
133-
todo!()
134-
}
135-
}
136-
96+
kv_request_with_key!(kvrpcpb::RawPutRequest, kvrpcpb::RawPutResponse);
13797
shardable_key!(kvrpcpb::RawPutRequest);
13898
collect_first!(kvrpcpb::RawPutResponse);
99+
139100
impl SingleKey for kvrpcpb::RawPutRequest {
140101
fn key(&self) -> &Vec<u8> {
141102
&self.key

src/request/codec.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,6 @@ const RAW_MODE_MAX_KEY: Prefix = [RAW_MODE_PREFIX + 1, 0, 0, 0];
1414
const TXN_MODE_MIN_KEY: Prefix = [TXN_MODE_PREFIX, 0, 0, 0];
1515
const TXN_MODE_MAX_KEY: Prefix = [TXN_MODE_PREFIX + 1, 0, 0, 0];
1616

17-
#[macro_export]
18-
macro_rules! plain_request {
19-
($req: ident, $codec: ident) => {
20-
if $codec.is_plain() {
21-
return ::std::borrow::Cow::Borrowed($req);
22-
}
23-
};
24-
}
25-
26-
#[macro_export]
27-
macro_rules! plain_response {
28-
($resp: ident, $codec: ident) => {
29-
if $codec.is_plain() {
30-
return Ok($resp);
31-
}
32-
};
33-
}
34-
3517
pub trait RequestCodec: Sized + Clone + Sync + Send + 'static {
3618
fn encode_key(&self, key: Key) -> Key {
3719
key

src/request/mod.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,36 @@ pub trait KvRequest<C>: Request + Sized + Clone + Sync + Send + 'static {
4141
}
4242
}
4343

44+
#[macro_export]
45+
macro_rules! kv_request_with_key {
46+
($req: ty, $resp: ty) => {
47+
impl<C> KvRequest<C> for $req
48+
where
49+
C: RequestCodec,
50+
{
51+
type Response = $resp;
52+
53+
fn encode_request(&self, codec: &C) -> Cow<Self> {
54+
if codec.is_plain() {
55+
return Cow::Borrowed(self);
56+
}
57+
unimplemented!()
58+
}
59+
60+
fn decode_response(
61+
&self,
62+
codec: &C,
63+
resp: Self::Response,
64+
) -> crate::Result<Self::Response> {
65+
if codec.is_plain() {
66+
return Ok(resp);
67+
}
68+
unimplemented!()
69+
}
70+
}
71+
};
72+
}
73+
4474
#[derive(Clone, Debug, new, Eq, PartialEq)]
4575
pub struct RetryOptions {
4676
/// How to retry when there is a region error and we need to resolve regions with PD.

0 commit comments

Comments
 (0)