cdrs 0.6.0

Cassandra DB driver written in Rust
Documentation

CDRS Build Status

CDRS is a native Cassandra driver written in Rust. The motivation to write it in Rust is a lack of native one. Existing ones are bindings to C clients.

Documentation

CDRS is under active development at the moment, so API may not be stable.

As well CDRS provides tools for mapping results into Rust structures It supports 4-th version of Cassandra protocol.

Supported features

  • lz4 decompression
  • snappy decompression
  • password authorization
  • tracing information
  • warning information

Frames

Request

  • STARTUP
  • AUTH_RESPONSE
  • OPTIONS
  • QUERY
  • PREPARE
  • EXECUTE
  • BATCH
  • REGISTER

Response

  • ERROR
  • READY
  • AUTHENTICATE
  • SUPPORTED
  • RESULT (Void)
  • RESULT (Rows)
  • RESULT (Set_keyspace)
  • RESULT (Prepared)
  • RESULT (Schema_change)
    • Target KEYSPACE
    • Target TABLE
    • Target TYPE
    • Target FUNCTION
    • Target AGGREGATE
  • EVENT
  • AUTH_CHALLENGE
  • AUTH_SUCCESS

Examples

Creating new connection and authorization

To use password authenticator, just include the one implemented in cdrs::authenticators.

use cdrs::client::CDRS;
use cdrs::authenticators::PasswordAuthenticator;

After that you can create a new instace of CDRS and establish new connection:

let authenticator = PasswordAuthenticator::new("user", "pass");
let addr = "127.0.0.1:9042";

// pass authenticator into CDRS' constructor
let client = CDRS::new(addr, authenticator).unwrap();
use cdrs::compression;
// start session without compression
let mut session = try!(client.start(compression::None));

If Server does not require authorization authenticator won't be used, but is still required for the constructor (most probably it will be refactored in future).

Getting supported options

Before session established an application may want to know which options are supported by a server (for instance to figure out which compression to use). That's why CDRS instance has a method get_options which could be called before session get started. Options are presented as HashMap<String, Vec<String>>.

let options = try!(client.get_options());

This should be called before session started to let you know which compression to choose and because session object borrows CDRS instance.

Using compression

Two types of compression are supported - snappy and lz4. To use compression just start connection with desired type:

// session without compression
let mut session_res = client.start(compression::None);
// session  lz4 compression
let mut session_res = client.start(compression::Lz4);
// v with snappy compression
let mut session_res = client.start(compression::Snappy);

Query execution

Query execution is provided in scope of Session. So to start executing queries you need to start Session first.

Use Query:

let use_query_string = String::from("USE my_namespace;");
let with_tracing = false;
let with_warnings = false;

match session.prepare(use_query_string, with_tracing, with_warnings) {
    Ok(set_keyspace) => {
        // use_keyspace is a result frame of type SetKeyspace
    },
    Err(err) => log!(err)
}
Create Query:

Creating new table could be performed via session.query. In case of success method return Schema Change frame that contains Change Type, Target and options that contain namespace and a name of created table.

use std::default::Default;
use cdrs::client::{Query, QueryBuilder};
use cdrs::consistency::Consistency;

let mut select_query: Query = QueryBuilder::new("CREATE TABLE keyspace.emp (
    empID int,
    deptID int,
    first_name varchar,
    last_name varchar,
    PRIMARY KEY (empID, deptID)
    );")
    .consistency(Consistency::One)
    .finalize();
let with_tracing = false;
let with_warnings = false;

let table_created = session.query(select_query, with_tracing, with_warnings).is_ok();

Select Query:

As a response to select query CDRS returns a result frame of type Rows with data items (columns) encoded in Cassandra's way.

use std::default::Default;
use cdrs::client::Query;
use cdrs::consistency::Consistency;

let mut select_query: Query = QueryBuilder::new(use_query.clone()).finalize();
let with_tracing = false;
let with_warnings = false;

match session.query(select_query, with_tracing, with_warnings) {
    Ok(res) => println!("Result frame: {:?},\nparsed body: {:?}", res, res.get_body());,
    Err(err) => log!(err)
}
Select Query (mapping results):

Once CDRS got response to SELECT query you can map rows encapsulated within Result frame into Rust values or into List, Map or UDT helper structures which provide a way to convert wrapped values into plain ones.

As an example let's consider a case when application gets a collection of messages of following format:


struct Message {
    pub author: String,
    pub text: String
}

To get a collection of messages Vec<Message> let's convert a result of query into collection of rows Vec<cdrs::types::row::Row> and then convert each column into appropriate Rust type:

use cdrs::error::{Result as CResult};

let res_body = parsed.get_body();
let rows = res_body.into_rows().unwrap();
let messages: Vec<CResult<Message>> = rows
    .iter()
    .map(|row| Message {
        author: row.get_by_name("author").unwrap(),
        text: row.get_by_name("text").unwrap()
    })
    .collect();

There is no difference between Cassandra's List and Sets in terms of Rust. They could be represented as Vec<T>. To convert a frame into a structure that contains a collection of elements do as follows:


struct Author {
    pub name: String,
    pub messages: Vec<String>
}

//...
use cdrs::error::{Result as CResult};
let res_body = parsed.get_body();
let rows = res_body.into_rows().unwrap();
let messages: Vec<CAuthor> = rows
    .iter()
    .map(|row| {
        let name: String = row.get_by_name("name").unwrap();
        let messages: Vec<String> = row
            // unwrap Option<CResult<T>>, where T implements AsRust
            .get_by_name("messages").unwrap().unwrap()
            .as_rust().unwrap();
        return Author {
            author: name,
            text: messages
        };
    })
    .collect();

License

The MIT License (MIT)

Copyright (c) 2016 Alex Pikalov

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.