26 lines
747 B
Rust
26 lines
747 B
Rust
use std::sync::LazyLock;
|
|
|
|
use redb::{Table, TableDefinition, WriteTransaction};
|
|
|
|
static DATABASE: LazyLock<redb::Database> = LazyLock::new(|| {
|
|
redb::Database::builder()
|
|
.create("players.redb")
|
|
.expect("failed to open database")
|
|
});
|
|
const DIFINITION: TableDefinition<'_, u32, Vec<u8>> = redb::TableDefinition::new("players");
|
|
|
|
pub fn write_txn() -> Result<WriteTransaction, redb::Error> {
|
|
Ok(DATABASE.begin_write()?)
|
|
}
|
|
|
|
pub fn open_table(write: &WriteTransaction) -> Result<Table<'_, u32, Vec<u8>>, redb::Error> {
|
|
Ok(write.open_table(DIFINITION)?)
|
|
}
|
|
|
|
pub fn init_db() -> Result<(), redb::Error> {
|
|
let write_txn = DATABASE.begin_write()?;
|
|
write_txn.open_table(DIFINITION)?;
|
|
write_txn.commit()?;
|
|
Ok(())
|
|
}
|