1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use std::error;
use std::fmt;
use std::result;
use std::time::Duration;

use async_trait::async_trait;
use nebula_fbthrift_graph::{
    errors::graph_service::ExecuteError,
    types::{ErrorCode, ExecutionResponse},
};

cfg_if::cfg_if! {
    if #[cfg(feature = "serde_feature")] {
        use serde::de::DeserializeOwned;
        use serde_nebula_fbthrift_graph::de::{deserialize_execution_response, data::DataDeserializeError};

        #[async_trait]
        pub trait Query
        {
            async fn query_as<D: DeserializeOwned>(&mut self, stmt: &str) -> result::Result<QueryOutput<D>, QueryError>;

            async fn query(&mut self, stmt: &str) -> result::Result<QueryOutput<()>, QueryError> {
                self.query_as(stmt).await
            }

            async fn show_hosts(&mut self) -> result::Result<QueryOutput<Host>, QueryError> {
                self.query_as(STMT_SHOW_HOSTS).await
            }
            async fn show_spaces(&mut self) -> result::Result<QueryOutput<Space>, QueryError> {
                self.query_as(STMT_SHOW_SPACES).await
            }
        }

        #[derive(Debug)]
        pub struct QueryOutput<D> where D: DeserializeOwned {
            pub latency: Duration,
            pub space_name: Option<String>,
            pub data_set: Vec<D>,
        }

        impl<D> QueryOutput<D> where D: DeserializeOwned {
            pub fn new(res: ExecutionResponse) -> result::Result<Self, QueryError> {
                let latency = Duration::from_micros(res.latency_in_us as u64);
                let space_name = res.space_name.clone();
                let data_set = deserialize_execution_response::<D>(&res).map_err(|err| QueryError::DataDeserializeError(err))?;

                Ok(Self {
                    latency,
                    space_name,
                    data_set,
                })
            }
        }
    } else {
        #[async_trait]
        pub trait Query {
            async fn query(&mut self, stmt: &str) -> result::Result<QueryOutput, QueryError>;
        }

        #[derive(Debug)]
        pub struct QueryOutput {
            pub latency: Duration,
            pub space_name: Option<String>,
            pub data_set: (),
        }

        impl QueryOutput {
            pub fn new(res: ExecutionResponse) -> Self {
                let latency = Duration::from_micros(res.latency_in_us as u64);
                let space_name = res.space_name;
                let data_set = ();

                Self {
                    latency,
                    space_name,
                    data_set,
                }
            }
        }
    }
}

#[derive(Debug)]
pub enum QueryError {
    ExecuteError(ExecuteError),
    ResponseError(ErrorCode, Option<String>),
    #[cfg(feature = "serde_feature")]
    DataDeserializeError(DataDeserializeError),
}

impl fmt::Display for QueryError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::ExecuteError(err) => write!(f, "ExecuteError {}", err),
            Self::ResponseError(err_code, err_msg) => write!(
                f,
                "ResponseError err_code:{} err_msg:{:?}",
                err_code, err_msg
            ),
            #[cfg(feature = "serde_feature")]
            Self::DataDeserializeError(err) => write!(f, "DataDeserializeError {}", err),
        }
    }
}

impl error::Error for QueryError {
    fn description(&self) -> &str {
        match self {
            Self::ExecuteError(_) => "ExecuteError",
            Self::ResponseError(_, _) => "ResponseError",
            #[cfg(feature = "serde_feature")]
            Self::DataDeserializeError(_) => "DataDeserializeError",
        }
    }
}

//
//
//
cfg_if::cfg_if! {
    if #[cfg(feature = "serde_feature")] {
        use serde::Deserialize;

        const STMT_SHOW_HOSTS: &str = "SHOW HOSTS;";
        #[derive(Deserialize, Debug)]
        pub struct Host {
            #[serde(rename(deserialize = "Ip"))]
            pub ip: String,
            #[serde(rename(deserialize = "Port"))]
            pub port: String,
            #[serde(rename(deserialize = "Status"))]
            pub status: String,
            #[serde(rename(deserialize = "Leader count"))]
            pub leader_count: u64,
            #[serde(rename(deserialize = "Leader distribution"))]
            pub leader_distribution: String,
            #[serde(rename(deserialize = "Partition distribution"))]
            pub partition_distribution: String,
        }

        const STMT_SHOW_SPACES: &str = "SHOW SPACES;";
        #[derive(Deserialize, Debug)]
        pub struct Space {
            #[serde(rename(deserialize = "Name"))]
            pub name: String,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use std::io;

    #[test]
    fn impl_std_fmt_display() -> io::Result<()> {
        let err = QueryError::ResponseError(ErrorCode::E_DISCONNECTED, None);
        println!("{}", err.to_string());

        Ok(())
    }

    #[test]
    fn impl_std_error_error() -> io::Result<()> {
        let err = io::Error::new(
            io::ErrorKind::Other,
            QueryError::ResponseError(ErrorCode::E_DISCONNECTED, None),
        );
        println!("{}", err.to_string());

        Ok(())
    }
}