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};
24
25#[derive(Debug, Clone)]
26pub struct ResponseHeader(PbResponseHeader);
27
28impl ResponseHeader {
29    #[inline]
30    pub fn protocol_version(&self) -> u64 {
31        self.0.protocol_version
32    }
33
34    #[inline]
35    pub fn error_code(&self) -> i32 {
36        match self.0.error.as_ref() {
37            Some(err) => err.code,
38            None => 0,
39        }
40    }
41
42    #[inline]
43    pub fn error_msg(&self) -> String {
44        match self.0.error.as_ref() {
45            Some(err) => err.err_msg.clone(),
46            None => "None".to_string(),
47        }
48    }
49}
50
51#[derive(Debug, Clone, PartialEq)]
52pub struct KeyValue {
53    pub key: Vec<u8>,
54    pub value: Vec<u8>,
55}
56
57impl From<KeyValue> for PbKeyValue {
58    fn from(kv: KeyValue) -> Self {
59        Self {
60            key: kv.key,
61            value: kv.value,
62        }
63    }
64}
65
66impl From<etcd_client::KeyValue> for KeyValue {
67    fn from(kv: etcd_client::KeyValue) -> Self {
68        Self {
69            key: kv.key().to_vec(),
70            value: kv.value().to_vec(),
71        }
72    }
73}
74
75impl From<KeyValue> for (Vec<u8>, Vec<u8>) {
76    fn from(kv: KeyValue) -> Self {
77        (kv.key, kv.value)
78    }
79}
80
81impl From<(Vec<u8>, Vec<u8>)> for KeyValue {
82    fn from(kv: (Vec<u8>, Vec<u8>)) -> Self {
83        Self {
84            key: kv.0,
85            value: kv.1,
86        }
87    }
88}
89
90impl Display for KeyValue {
91    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
92        write!(
93            f,
94            "({}, {})",
95            String::from_utf8_lossy(&self.key),
96            String::from_utf8_lossy(&self.value)
97        )
98    }
99}
100
101impl KeyValue {
102    #[inline]
103    pub fn new(kv: PbKeyValue) -> Self {
104        Self {
105            key: kv.key,
106            value: kv.value,
107        }
108    }
109
110    #[inline]
111    pub fn key(&self) -> &[u8] {
112        &self.key
113    }
114
115    #[inline]
116    pub fn take_key(&mut self) -> Vec<u8> {
117        std::mem::take(&mut self.key)
118    }
119
120    #[inline]
121    pub fn value(&self) -> &[u8] {
122        &self.value
123    }
124
125    #[inline]
126    pub fn take_value(&mut self) -> Vec<u8> {
127        std::mem::take(&mut self.value)
128    }
129}
130
131#[cfg(test)]
132mod tests {
133    use api::v1::meta::{Error, ResponseHeader as PbResponseHeader};
134
135    use super::*;
136
137    #[test]
138    fn test_response_header_trans() {
139        let pb_header = PbResponseHeader {
140            protocol_version: 101,
141            error: Some(Error {
142                code: 100,
143                err_msg: "test".to_string(),
144            }),
145        };
146
147        let header = ResponseHeader(pb_header);
148        assert_eq!(101, header.protocol_version());
149        assert_eq!(100, header.error_code());
150        assert_eq!("test".to_string(), header.error_msg());
151    }
152}