-
Notifications
You must be signed in to change notification settings - Fork 5
/
sync.rs
226 lines (219 loc) · 7.95 KB
/
sync.rs
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* Copyright 2023, Sayan Nandan <nandansayan@outlook.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//! # Synchronous database I/O
//!
//! This module provides the necessary items to establish a synchronous connection to the database server. If you need
//! to use connection pooling, consider checking the [`pool`](crate::pool) module.
//!
//! See the [`crate`] root documentation for help on establishing and using database connections.
//!
use {
crate::{
config::Config,
error::{ClientResult, ConnectionSetupError, Error},
protocol::{
handshake::{ClientHandshake, ServerHandshake},
state_init::{DecodeState, MRespState, PipelineResult, RState},
Decoder,
},
query::Pipeline,
response::{FromResponse, Response},
Query,
},
native_tls::{Certificate, TlsConnector, TlsStream},
std::{
io::{Read, Write},
net::TcpStream,
ops::{Deref, DerefMut},
},
};
/// A `skyhash/TCP` connection
///
/// **Specification**
/// - Protocol version: `Skyhash/2.0`
/// - Query mode: `QTDEX-1A/BQL-S1`
/// - Authentication plugin: `pwd`
#[derive(Debug)]
pub struct Connection(TcpConnection<TcpStream>);
/// A `skyhash/TLS` connection
///
/// **Specification**
/// - Protocol version: `Skyhash/2.0`
/// - Query mode: `QTDEX-1A/BQL-S1`
/// - Authentication plugin: `pwd`
#[derive(Debug)]
pub struct ConnectionTls(TcpConnection<TlsStream<TcpStream>>);
impl Deref for Connection {
type Target = TcpConnection<TcpStream>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for Connection {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl Deref for ConnectionTls {
type Target = TcpConnection<TlsStream<TcpStream>>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for ConnectionTls {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl Config {
/// Establish a connection to the database using the current configuration
pub fn connect(&self) -> ClientResult<Connection> {
let mut tcpstream = TcpStream::connect((self.host(), self.port()))?;
let handshake = ClientHandshake::new(self);
tcpstream.write_all(handshake.inner())?;
let mut resp = [0u8; 4];
tcpstream.read_exact(&mut resp)?;
match ServerHandshake::parse(resp)? {
ServerHandshake::Error(e) => return Err(ConnectionSetupError::HandshakeError(e).into()),
ServerHandshake::Okay(_suggestion) => {
return Ok(Connection(TcpConnection::new(tcpstream)))
}
}
}
/// Establish a TLS connection to the database using the current configuration.
/// Pass the certificate in PEM format.
pub fn connect_tls(&self, cert: &str) -> ClientResult<ConnectionTls> {
let stream = TcpStream::connect((self.host(), self.port()))?;
let mut stream = TlsConnector::builder()
.add_root_certificate(Certificate::from_pem(cert.as_bytes()).map_err(|e| {
ConnectionSetupError::Other(format!("failed to parse certificate: {e}"))
})?)
.danger_accept_invalid_hostnames(true)
.build()
.map_err(|e| {
ConnectionSetupError::Other(format!("failed to set up TLS acceptor: {e}"))
})?
.connect(self.host(), stream)
.map_err(|e| ConnectionSetupError::Other(format!("TLS handshake failed: {e}")))?;
let handshake = ClientHandshake::new(self);
stream.write_all(handshake.inner())?;
let mut resp = [0u8; 4];
stream.read_exact(&mut resp)?;
match ServerHandshake::parse(resp)? {
ServerHandshake::Error(e) => return Err(ConnectionSetupError::HandshakeError(e).into()),
ServerHandshake::Okay(_suggestion) => {
return Ok(ConnectionTls(TcpConnection::new(stream)))
}
}
}
}
#[derive(Debug)]
/// The underlying connection type
///
/// This can't be constructed directly!
pub struct TcpConnection<C: Write + Read> {
con: C,
buf: Vec<u8>,
}
impl<C: Write + Read> TcpConnection<C> {
fn new(con: C) -> Self {
Self {
con,
buf: Vec::with_capacity(crate::BUFSIZE),
}
}
/// Execute a pipeline. The server returns the queries in the order they were sent (unless otherwise set).
pub fn execute_pipeline(&mut self, pipeline: &Pipeline) -> ClientResult<Vec<Response>> {
self.buf.clear();
self.buf.push(b'P');
// query count
self.buf.extend(
itoa::Buffer::new()
.format(pipeline.query_count())
.as_bytes(),
);
self.buf.push(b'\n');
// packet size
self.buf
.extend(itoa::Buffer::new().format(pipeline.buf().len()).as_bytes());
self.buf.push(b'\n');
// write
self.con.write_all(&self.buf)?;
self.con.write_all(pipeline.buf())?;
self.buf.clear();
// read
let mut expected = Decoder::MIN_READBACK;
let mut cursor = 0;
let mut state = MRespState::default();
loop {
let mut buf = [0u8; crate::BUFSIZE];
let n = self.con.read(&mut buf)?;
if n == 0 {
return Err(Error::IoError(std::io::ErrorKind::ConnectionReset.into()));
}
if n < expected {
continue;
}
self.buf.extend_from_slice(&buf[..n]);
let mut decoder = Decoder::new(&self.buf, cursor);
match decoder.validate_pipe(cursor == 0, state) {
PipelineResult::Completed(r) => return Ok(r),
PipelineResult::Pending(_state) => {
expected = 1;
cursor = decoder.position();
state = _state;
}
PipelineResult::Error(e) => return Err(e.into()),
}
}
}
/// Run a query and return a raw [`Response`]
pub fn query(&mut self, q: &Query) -> ClientResult<Response> {
self.buf.clear();
q.write_packet(&mut self.buf).unwrap();
self.con.write_all(&self.buf)?;
self.buf.clear();
let mut state = RState::default();
let mut cursor = 0;
loop {
let mut buf = [0u8; crate::BUFSIZE];
let n = self.con.read(&mut buf)?;
if n == 0 {
return Err(Error::IoError(std::io::ErrorKind::ConnectionReset.into()));
}
self.buf.extend_from_slice(&buf[..n]);
let mut decoder = Decoder::new(&self.buf, cursor);
match decoder.validate_response(state) {
DecodeState::ChangeState(new_state) => {
state = new_state;
cursor = decoder.position();
continue;
}
DecodeState::Completed(resp) => return Ok(resp),
DecodeState::Error(e) => return Err(e.into()),
}
}
}
/// Run and parse a query into the indicated type. The type must implement [`FromResponse`]
pub fn query_parse<T: FromResponse>(&mut self, q: &Query) -> ClientResult<T> {
self.query(q).and_then(FromResponse::from_response)
}
/// Call this if the internally allocated buffer is growing too large and impacting your performance. However, normally
/// you will not need to call this
pub fn reset_buffer(&mut self) {
self.buf.shrink_to_fit()
}
}