#database-driver #amazon #async #pure #ledger #implemented #pool

qldb

Driver for Amazon's QLDB Database implemented in pure rust

29 releases (18 stable)

3.2.8 Oct 1, 2023
3.2.7 Jul 17, 2022
3.1.3 Jun 26, 2022
3.0.1 Dec 14, 2021
0.4.1 Nov 18, 2020

#308 in Database interfaces

Download history 5189/week @ 2023-11-21 5328/week @ 2023-11-28 6237/week @ 2023-12-05 6170/week @ 2023-12-12 6121/week @ 2023-12-19 6056/week @ 2023-12-26 6066/week @ 2024-01-02 5996/week @ 2024-01-09 5939/week @ 2024-01-16 5756/week @ 2024-01-23 5464/week @ 2024-01-30 5480/week @ 2024-02-06 5590/week @ 2024-02-13 5718/week @ 2024-02-20 5601/week @ 2024-02-27 4702/week @ 2024-03-05

22,386 downloads per month

Apache-2.0/MIT

68KB
1K SLoC

Amazon's QLDB Driver

Driver for Amazon's QLDB Database implemented in pure rust.

Documentation Crates.io Rust

The driver is fairly tested and should be ready to test in real projects.

Example

use qldb::QldbClient;
use std::collections::HashMap;

let client = QldbClient::default("rust-crate-test", 200).await?;

let mut value_to_insert = HashMap::new();
// This will insert a documents with a key "test_column"
// with the value "IonValue::String(test_value)"
value_to_insert.insert("test_column", "test_value");

client
    .transaction_within(|client| async move {   
        client
            .query("INSERT INTO TestTable VALUE ?")
            .param(value_to_insert)
            .execute()
            .await?;
        Ok(())
    })
    .await?;

Session Pool

The driver has a session pool. The second parameter in the QldbClient::default is the maximum size of the connection pool.

The pool will be auto-populated as parallel transaction are being requested until it reaches the provided maximum.

The pool uses one independent thread with a single-threaded executor (async-executor) in order to be able to spawn tasks after the session has been returned.

Alternative session Pool

There is an alternative session pool that will require an spawner function to be provided. It allows to have the pool running by using the spawn function of the executor you use. We tested async-std and tokio, but others should work as well.

This pool will spawn two internal tasks handling the pool.

Use this if you want for this driver to not create a new thread.

Example with async-std:

    let client = QldbClient::default_with_spawner(
        "rust-crate-test", 
        200, 
        Arc::new(move |fut| {async_std::task::spawn(Box::pin(fut));})
    )
    .await?

Or, with tokio:

    let client = QldbClient::default_with_spawner(
        "rust-crate-test", 
        200, 
        Arc::new(move |fut| {tokio::spawn(Box::pin(fut));})
    )
    .await?

Select the pool you want to use

By default, both pools are available by using the methods QldbClient::default and QldbClient::default_with_spawner. If you don't want the pool to be available in runtime, you can disable by removing the default features. Still, you will need to add at least one feature to enable one pool.

This will only enable the default pool, the one that uses one thread.

qldb = { version = "3", default_features = false, features = ["internal_pool_with_thread"]}

This will only enable the alternative pool, the one that requires an spawner

qldb = { version = "3", default_features = false, features = ["internal_pool_with_spawner"]}

Underlying Ion Format Implementation

The library uses ion-binary-rs, which is our own, pure rust, implementation of the format. It is very well tested and ready to use in production too.

Test

For tests you will need to have some AWS credentials in your PC (as env variables or in ~/.aws/credentials). There needs to be a QLDB database with the name "rust-crate-test" in the aws account. The tests need to be run sequentially, so in order to run the tests please run the following command:

RUST_TEST_THREADS=1 cargo test

Dependencies

~21–36MB
~649K SLoC