refactor: dump database to json

This commit is contained in:
mokurin000
2025-07-31 11:25:04 +08:00
parent 3ab53b426d
commit b72addd661
7 changed files with 84 additions and 52 deletions

View File

@@ -1,6 +1,6 @@
use std::sync::LazyLock;
use redb::{Table, TableDefinition, WriteTransaction};
use redb::{ReadTransaction, Table, TableDefinition, WriteTransaction};
static DATABASE: LazyLock<redb::Database> = LazyLock::new(|| {
redb::Database::builder()
@@ -17,6 +17,16 @@ pub fn open_table(write: &WriteTransaction) -> Result<Table<'_, u32, Vec<u8>>, r
Ok(write.open_table(DIFINITION)?)
}
pub fn read_txn() -> Result<ReadTransaction, redb::Error> {
Ok(DATABASE.begin_read()?)
}
pub fn open_table_read(
read: &ReadTransaction,
) -> Result<redb::ReadOnlyTable<u32, Vec<u8>>, redb::Error> {
Ok(read.open_table(DIFINITION)?)
}
pub fn init_db() -> Result<(), redb::Error> {
let write_txn = DATABASE.begin_write()?;
write_txn.open_table(DIFINITION)?;