lynxdb 2023.12.17-alpha

A rust client of LynxDB.
Documentation
/*
 * Copyright 2023 Baili Zhang.
 *
 * 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.
 */

use std::net::{SocketAddrV4, TcpStream};

pub struct Connection {
    tcp_stream: Option<TcpStream>,
}

impl Connection {
    fn new(tcp_stream: TcpStream) -> Connection {
        Connection {
            tcp_stream: Some(tcp_stream)
        }
    }

    fn fail() -> Connection {
        Connection {
            tcp_stream: None
        }
    }

    pub fn is_not_connected(&self) -> bool {
        self.tcp_stream.is_none()
    }

    pub fn find() {
        println!("Find")
    }

    pub fn insert(&self, key: String, column_family: String, column: String, value: String) {
        if self.is_not_connected() {}
        println!("Insert")
    }

    pub fn delete() {
        println!("Delete")
    }
}

pub fn create_connection(db_addr: SocketAddrV4) -> Connection {
    let result = TcpStream::connect(db_addr);
    if result.is_err() {
        println!("Connect to LynxDB server {} failed !", db_addr);
        return Connection::fail();
    }
    let tcp_stream = result.unwrap();
    Connection::new(tcp_stream)
}

#[cfg(test)]
mod tests {
    use std::net::{Ipv4Addr, SocketAddrV4};

    use super::*;

    fn init_connection() -> Connection {
        let host = Ipv4Addr::new(127, 0, 0, 1);
        let db_addr = SocketAddrV4::new(host, 7820);
        create_connection(db_addr)
    }

    #[test]
    fn test_001() {
        let connection = init_connection();
        assert!(connection.is_not_connected())
    }

    #[test]
    fn test_002() {
        let connection = init_connection();
        connection.insert("key", "column_family", "column", "value");
    }
}