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
/*
 * Created on Wed May 12 2021
 *
 * Copyright (c) 2021 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.
 *
*/

//! # Database connections
//!
//! This crate provides a [`Connection`] object that can be used to connect to a Skytable database instance
//! and write/read queries/responses to/from it

use crate::deserializer::{ParseError, Parser, RawResponse};
use crate::{Query, Response};
pub use std::io::Result as IoResult;
use std::io::{Error, ErrorKind, Read, Write};
use std::net::TcpStream;

/// 4 KB Read Buffer
const BUF_CAP: usize = 4096;

#[derive(Debug)]
#[cfg_attr(docsrs, doc(cfg(feature = "sync")))]
/// A `Connection` is a wrapper around a`TcpStream` and a read buffer
pub struct Connection {
    stream: TcpStream,
    buffer: Vec<u8>,
}

impl Connection {
    /// Create a new connection to a Skytable instance hosted on `host` and running on `port`
    pub fn new(host: &str, port: u16) -> IoResult<Self> {
        let stream = TcpStream::connect((host, port))?;
        Ok(Connection {
            stream: stream,
            buffer: Vec::with_capacity(BUF_CAP),
        })
    }
    /// This function will write a [`Query`] to the stream and read the response from the
    /// server. It will then determine if the returned response is complete or incomplete
    /// or invalid and return an appropriate variant of [`Response`] wrapped in [`IoResult`]
    /// for any I/O errors that may occur
    ///
    /// ## Panics
    /// This method will panic if the [`Query`] supplied is empty (i.e has no arguments)
    pub fn run_simple_query(&mut self, query: &Query) -> IoResult<Response> {
        assert!(query.__len() != 0, "A `Query` cannot be of zero length!");
        query.write_query_to_sync(&mut self.stream)?;
        self.stream.flush()?;
        loop {
            let mut buffer = [0u8; 1024];
            match self.stream.read(&mut buffer) {
                Ok(0) => return Err(Error::from(ErrorKind::ConnectionReset)),
                Ok(read) => {
                    self.buffer.extend(&buffer[..read]);
                }
                Err(e) => return Err(e),
            }
            match self.try_response() {
                Ok((query, forward_by)) => {
                    self.buffer.drain(..forward_by);
                    match query {
                        RawResponse::SimpleQuery(s) => return Ok(Response::Item(s)),
                        RawResponse::PipelinedQuery(_) => {
                            unimplemented!("Pipelined queries aren't implemented yet")
                        }
                    }
                }
                Err(e) => match e {
                    ParseError::NotEnough => (),
                    ParseError::BadPacket | ParseError::UnexpectedByte => {
                        self.buffer.clear();
                        return Ok(Response::InvalidResponse);
                    }
                    ParseError::DataTypeParseError => return Ok(Response::ParseError),
                    ParseError::Empty => return Err(Error::from(ErrorKind::ConnectionReset)),
                    ParseError::UnknownDatatype => return Ok(Response::UnsupportedDataType),
                },
            }
        }
    }
    /// This function is a subroutine of `run_query` used to parse the response packet
    fn try_response(&mut self) -> Result<(RawResponse, usize), ParseError> {
        if self.buffer.is_empty() {
            // The connection was possibly reset
            return Err(ParseError::Empty);
        }
        Parser::new(&self.buffer).parse()
    }
}

impl crate::actions::SyncSocket for crate::sync::Connection {
    fn run(&mut self, q: Query) -> std::result::Result<Response, std::io::Error> {
        self.run_simple_query(&q)
    }
}