Skip to content

sonicjhon1/lupabase

lupabase

Docs Crates.io Downloads License

Lupabase is a blazingly fast (work-in-progress) database, written entirely in Rust. It focuses on simplicity, portability with flexible storage backends.

Features

  • Multiple Database engines built-in:

    • MemoryDB: In-memory, non-persistent storage. Extremely fast, backed by RwLock and HashMap
    • JsonDB: Persists records to disk using the JSON format
    • CborDB: Persists records to disk using the CBOR format
  • Flexible database record:

    • DatabaseRecord: A minimal, general-purpose record type for database operations that support custom paths. Suitable record that needs to be stored at custom file paths.
    • DatabaseRecordPartitioned: A partitioned (named) record type that enables full feature support across all database operations. This includes everything supported by DatabaseRecord. Recommended ✅
  • DatabaseRecordsUtils for database records utilities:

    • Filter records by unique key
    • Detect intersecting and non-intersecting records
    • Easily extract unique identifiers
  • DatabaseTransaction for ACID-like transactions:

    • Start transactions
    • Commit stored transations
    • Rollback transactions to a previous snapshot
  • Raw database I/O with serde through DatabaseIO:

    • Store any type that implements Serialize
    • Retrieve any type that implements Deserialize

This is not

  • A standalone database server
  • A relational database

Example/Usage

use lupabase::prelude::*;
use serde::{Serialize, Deserialize};

// Setup Record
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
struct User {
    id: u32,
    name: String,
}

impl DatabaseRecordPartitioned for User {
    const PARTITION: &str = "users";
}

impl DatabaseRecord for User {
    type Unique = u32;

    fn unique_value(&self) -> Self::Unique {
        self.id
    }
}

// Create a new Database (This example uses in-memory database that is wiped on exit)
let db = memorydb::MemoryDB::new("Test");

let users = vec![
    User { id: 0, name: "Alice".into() },
    User { id: 1, name: "Bob".into() },
];

// Initialize the Database
db.try_initialize_storage::<User, _>(users.clone()).unwrap();

// Query
let users_db = db.get_all::<User>().unwrap();
assert_eq!(users_db, users);

// Initialize the Database with a custom storage path
// The ::<Vec<User>> parameter is optional if the default_record is inferable / not empty
db.try_initialize_storage_with_path::<Vec<User>>(vec![], "custom_path").unwrap();

// Query from custom storage path
db.insert_with_path(User { id: 2, name: "Lupa".into() }, "custom_path").unwrap();

let users_custom_db = db.get_all_with_path::<User>("custom_path").unwrap();
assert_eq!(users_custom_db.len(), 1);
assert_eq!(users_custom_db[0].unique_value(), 2);

// Additional helpers
use lupabase::record::utils::*;

let uniques = users_db.as_uniques();
println!("Unique IDs: {:?}", uniques);

if let Some(user) = users_db.find_by_unique(&1) {
    println!("Found user: {}", user.name);
}

Roadmap

  • Variadic / multiple path (partition) function call
  • Concurrency-safe multi-threaded transactions
  • Improved tests and docs
  • Examples

Status

This project is under active development and not yet production-ready. Although it is in active use for my own projects, you should expect breaking changes if you use this library in the current status.

License

All code in this repository is dual-licensed under either:

at your option.

Your contributions

Any contribution submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

About

Blazingly fast (work-in-progress) database written entirely in Rust and focuses on simplicity, portability with flexible storage backends

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages