iotdb/
lib.rs

1pub mod pretty;
2pub mod rpc;
3pub mod session;
4pub mod tablet;
5
6pub mod client {
7    use log::{debug, trace};
8
9    type ClientType = TSIServiceSyncClient<Box<dyn TInputProtocol>, Box<dyn TOutputProtocol>>;
10
11    use crate::rpc::TSIServiceSyncClient;
12    use thrift::protocol::{
13        TBinaryInputProtocol, TBinaryOutputProtocol, TCompactInputProtocol, TCompactOutputProtocol,
14        TInputProtocol, TOutputProtocol,
15    };
16    use thrift::transport::{TFramedReadTransport, TFramedWriteTransport, TIoChannel, TTcpChannel};
17
18    pub struct Client {
19        host: String,
20        port: String,
21        rpc_compaction: bool,
22    }
23
24    impl Default for Client {
25        fn default() -> Self {
26            Self {
27                host: "localhost".to_string(),
28                port: "6667".to_string(),
29                rpc_compaction: false,
30            }
31        }
32    }
33
34    impl Client {
35        pub fn new(host: &str, port: &str) -> Client {
36            Self {
37                host: host.to_string(),
38                port: port.to_string(),
39                rpc_compaction: Client::default().rpc_compaction,
40            }
41        }
42
43        pub fn enable_rpc_compaction(&mut self) -> &mut Client {
44            self.rpc_compaction = false;
45            self
46        }
47
48        pub fn create(&mut self) -> thrift::Result<ClientType> {
49            trace!("Create a IotDB client");
50
51            let mut channel = TTcpChannel::new();
52            channel.open(format!("{}:{}", self.host, self.port).as_str())?;
53            let (i_chan, o_chan) = channel.split()?;
54
55            let i_tran = TFramedReadTransport::new(i_chan);
56            let o_tran = TFramedWriteTransport::new(o_chan);
57
58            let (i_prot, o_prot): (Box<dyn TInputProtocol>, Box<dyn TOutputProtocol>);
59            if self.rpc_compaction {
60                i_prot = Box::new(TCompactInputProtocol::new(i_tran));
61                o_prot = Box::new(TCompactOutputProtocol::new(o_tran));
62                debug!("Create a compaction client");
63            } else {
64                i_prot = Box::new(TBinaryInputProtocol::new(i_tran, true));
65                o_prot = Box::new(TBinaryOutputProtocol::new(o_tran, true));
66                debug!("Create a binary client");
67            }
68            Ok(TSIServiceSyncClient::new(i_prot, o_prot))
69        }
70    }
71}