tikv_client/request/
codec.rs

1// Copyright 2023 TiKV Project Authors. Licensed under Apache-2.0.
2
3use crate::proto::kvrpcpb;
4use crate::request::KvRequest;
5
6pub trait Codec: Clone + Sync + Send + 'static {
7    fn encode_request<R: KvRequest>(&self, _req: &mut R) {}
8    // TODO: fn decode_response()
9}
10
11#[derive(Clone, Default)]
12pub struct ApiV1TxnCodec {}
13
14impl Codec for ApiV1TxnCodec {}
15
16#[derive(Clone, Default)]
17pub struct ApiV1RawCodec {}
18
19impl Codec for ApiV1RawCodec {}
20
21#[derive(Clone)]
22pub struct ApiV2TxnCodec {
23    _keyspace_id: u32,
24}
25
26impl ApiV2TxnCodec {
27    pub fn new(keyspace_id: u32) -> Self {
28        Self {
29            _keyspace_id: keyspace_id,
30        }
31    }
32}
33
34impl Codec for ApiV2TxnCodec {
35    fn encode_request<R: KvRequest>(&self, req: &mut R) {
36        req.set_api_version(kvrpcpb::ApiVersion::V2);
37        // TODO: req.encode_request(self);
38    }
39}
40
41// TODO: pub struct ApiV2RawCodec
42
43// EncodeRequest is just a type wrapper to avoid passing not encoded request to `PlanBuilder` by mistake.
44#[derive(Clone)]
45pub struct EncodedRequest<Req: KvRequest> {
46    pub inner: Req,
47}
48
49impl<Req: KvRequest> EncodedRequest<Req> {
50    pub fn new<C: Codec>(mut req: Req, codec: &C) -> Self {
51        codec.encode_request(&mut req);
52        Self { inner: req }
53    }
54}