common_meta/
rpc.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15pub mod ddl;
16pub mod lock;
17pub mod procedure;
18pub mod router;
19pub mod store;
20
21use std::fmt::{Display, Formatter};
22
23use api::v1::meta::{KeyValue as PbKeyValue, ResponseHeader as PbResponseHeader};
24use serde::{Deserialize, Serialize};
25
26#[derive(Debug, Clone)]
27pub struct ResponseHeader(PbResponseHeader);
28
29impl ResponseHeader {
30    #[inline]
31    pub fn protocol_version(&self) -> u64 {
32        self.0.protocol_version
33    }
34
35    #[inline]
36    pub fn error_code(&self) -> i32 {
37        match self.0.error.as_ref() {
38            Some(err) => err.code,
39            None => 0,
40        }
41    }
42
43    #[inline]
44    pub fn error_msg(&self) -> String {
45        match self.0.error.as_ref() {
46            Some(err) => err.err_msg.clone(),
47            None => "None".to_string(),
48        }
49    }
50}
51
52#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
53pub struct KeyValue {
54    pub key: Vec<u8>,
55    pub value: Vec<u8>,
56}
57
58impl From<KeyValue> for PbKeyValue {
59    fn from(kv: KeyValue) -> Self {
60        Self {
61            key: kv.key,
62            value: kv.value,
63        }
64    }
65}
66
67impl From<etcd_client::KeyValue> for KeyValue {
68    fn from(kv: etcd_client::KeyValue) -> Self {
69        Self {
70            key: kv.key().to_vec(),
71            value: kv.value().to_vec(),
72        }
73    }
74}
75
76impl From<KeyValue> for (Vec<u8>, Vec<u8>) {
77    fn from(kv: KeyValue) -> Self {
78        (kv.key, kv.value)
79    }
80}
81
82impl From<(Vec<u8>, Vec<u8>)> for KeyValue {
83    fn from(kv: (Vec<u8>, Vec<u8>)) -> Self {
84        Self {
85            key: kv.0,
86            value: kv.1,
87        }
88    }
89}
90
91impl Display for KeyValue {
92    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
93        write!(
94            f,
95            "({}, {})",
96            String::from_utf8_lossy(&self.key),
97            String::from_utf8_lossy(&self.value)
98        )
99    }
100}
101
102impl KeyValue {
103    #[inline]
104    pub fn new(kv: PbKeyValue) -> Self {
105        Self {
106            key: kv.key,
107            value: kv.value,
108        }
109    }
110
111    #[inline]
112    pub fn key(&self) -> &[u8] {
113        &self.key
114    }
115
116    #[inline]
117    pub fn take_key(&mut self) -> Vec<u8> {
118        std::mem::take(&mut self.key)
119    }
120
121    #[inline]
122    pub fn value(&self) -> &[u8] {
123        &self.value
124    }
125
126    #[inline]
127    pub fn take_value(&mut self) -> Vec<u8> {
128        std::mem::take(&mut self.value)
129    }
130}
131
132#[cfg(test)]
133mod tests {
134    use api::v1::meta::{Error, ResponseHeader as PbResponseHeader};
135
136    use super::*;
137
138    #[test]
139    fn test_response_header_trans() {
140        let pb_header = PbResponseHeader {
141            protocol_version: 101,
142            error: Some(Error {
143                code: 100,
144                err_msg: "test".to_string(),
145            }),
146        };
147
148        let header = ResponseHeader(pb_header);
149        assert_eq!(101, header.protocol_version());
150        assert_eq!(100, header.error_code());
151        assert_eq!("test".to_string(), header.error_msg());
152    }
153}