An embedded, in-memory, zero-copy, atomicity, consistency, MVCC, almost lock-free and serializable snapshot isolation database engine.
[
][Github-url]
[
][CI-url]
[
][codecov-url]
[
](https://gist.github.com/al8n/327b2a8aef9003246e45c6e47fe63937#file-skipdb)
[
][doc-url]
[
][crates-url]
[
][crates-url]
English | [简体中文][zh-cn-url]
## Introduction
An embedded, in-memory, zero-copy, MVCC, almost lock-free and serializable snapshot isolation database engine.
`async-skipdb`'s SSI (Serializable Snapshot Isolation) transaction model is referenced to [`foundationdb`'s paper](https://www.foundationdb.org/files/fdb-paper.pdf) and [`badger`](https://github.com/dgraph-io/badger).
For sync version, please see [`skipdb`](https://crates.io/crates/skipdb).
This crate contains two kinds of in-memory key-value database:
1. `SerializableDb`
Supports both concurrent execution of full serializable snapshot isolation transactions and optimistic concurrency control transactions.
Transactions are created by `SerializableDb::serializable_write` can handle all kinds of write skew correctly.
Transactions are created by `SerializableDb::optimistic_write` can handle all kinds of direct dependent write skew, but cannot handle all kinds of indirect dependent write skew e.g. https://wiki.postgresql.org/wiki/SSI#Intersecting_Data.
2. `OptimisticDb`
Only support oncurrent execution of optimistic concurrency control, which means the write transaction cannot detect all kinds of write skew.
All kinds of direct dependent write skew can be handled correctly, but cannot handle all kinds of indirect dependent write skew e.g. https://wiki.postgresql.org/wiki/SSI#Intersecting_Data.
## Features
- Atomicity, Consistency, Isolation, MVCC, concurrent safe and almost lock-free.
- No extra allocation and copy, there is no `Arc` wrapper for both key and value stored in the database, which means that users provide `K` and `V`, and database store `K` and `V` directly.
- Zero-copy and in-place compaction, which means there is no copy, no extra allocation when compacting.
- Concurrent execution of transactions, providing serializable snapshot isolation, avoiding write skews.
- Both read transaction and write transaction are `Send + Sync + 'static`, which means you do not need to handle annoying lifetime problem anymore.
- Lock-free and concurrent safe read transaction: the read transaction is totally concurrent safe and can be shared in multiple threads, there is no lock in read transaction.
- `BTreeMap` like user friendly API and all iterators implement `Iterator` trait, which means users use Rust powerful conbinators when iterating over the database.
- Runtime agnostic, `tokio`, `async-std`, `smol`, `wasm-bindgen-futures` and any other async runtime.
- 100% safe, sets `[forbid(unsafe_code)]`.
## Installation
- `tokio`
```toml
[dependencies]
async-skipdb = { version = "0.2", features = ["tokio"] }
```
- `async-std`
```toml
[dependencies]
async-skipdb = { version = "0.2", features = ["async-std"] }
```
- `smol`
```toml
[dependencies]
async-skipdb = { version = "0.2", features = ["smol"] }
```
- `wasm-bindgen-futures`
```toml
[dependencies]
async-skipdb = { version = "0.2", features = ["wasm"] }
```
## Example
```rust
use async_skipdb::serializable::TokioSerializableDb;
#[derive(Debug)]
struct Person {
name: String,
hobby: String,
age: u8,
}
#[tokio::main]
async fn main() {
let db: TokioSerializableDb