fix: make sure tables are initialized

This commit is contained in:
mokurin000
2025-08-03 19:30:03 +08:00
parent a1b3a8ef0e
commit ca81c6495a
6 changed files with 57 additions and 34 deletions

View File

@@ -39,5 +39,8 @@ palc = { version = "0.0.1", features = ["derive"] }
futures-util = { version = "0.3.31", optional = true }
ctrlc = { version = "3.4.7", features = ["termination"] }
# magic macro
crabtime = { workspace = true }
[build-dependencies]
version_check = "0.9.5"

View File

@@ -11,11 +11,6 @@ static DATABASE: LazyLock<redb::Database> = LazyLock::new(|| {
db
});
pub const PLAYERS: TableDefinition<'_, u32, Vec<u8>> = redb::TableDefinition::new("players");
pub const PLAYER_B50: TableDefinition<'_, u32, Vec<u8>> = redb::TableDefinition::new("b50");
pub const PLAYER_RECORD: TableDefinition<'_, u32, Vec<u8>> = redb::TableDefinition::new("records");
pub const PLAYER_REGION: TableDefinition<'_, u32, Vec<u8>> = redb::TableDefinition::new("regions");
pub fn write_txn() -> Result<WriteTransaction, redb::Error> {
Ok(DATABASE.begin_write()?)
}
@@ -38,11 +33,33 @@ pub fn open_table_ro(
Ok(read.open_table(definition)?)
}
pub fn init_db() -> Result<(), redb::Error> {
let write_txn = DATABASE.begin_write()?;
write_txn.open_table(PLAYERS)?;
write_txn.open_table(PLAYER_B50)?;
write_txn.open_table(PLAYER_RECORD)?;
write_txn.commit()?;
Ok(())
#[crabtime::function]
fn table_definitions_impl(tables: Vec<String>) {
let mut defs: Vec<String> = Vec::new();
for table in tables {
let definition = table.to_uppercase();
let table_name = format!("\"{table}\"");
crabtime::output!(
pub const {{definition}}: TableDefinition<'_, u32, Vec<u8>> = redb::TableDefinition::new({{table_name}});
);
defs.push(format!("write_txn.open_table({definition})?;"));
}
let init_statements = defs.join("\n");
crabtime::output!(
pub fn init_db() -> Result<(), redb::Error> {
let write_txn = DATABASE.begin_write()?;
{
{ init_statements }
}
write_txn.commit()?;
Ok(())
}
);
}
table_definitions_impl!(["players", "b50", "records", "regions"]);

View File

@@ -244,7 +244,7 @@ async fn main() -> Result<(), Box<dyn snafu::Error>> {
max_rating,
} => {
use crate::{
cache::{PLAYER_RECORD, PLAYERS},
cache::{PLAYERS, RECORDS},
utils::helpers::{cached_concurrent_fetch_userfn, read_cache},
};
@@ -255,7 +255,7 @@ async fn main() -> Result<(), Box<dyn snafu::Error>> {
players.iter().map(|p| p.user_id).collect::<Vec<u32>>(),
&client,
concurrency,
PLAYER_RECORD,
RECORDS,
get_user_all_music,
)
.await?;
@@ -269,7 +269,7 @@ async fn main() -> Result<(), Box<dyn snafu::Error>> {
use sdgb_api::title::methods::GetUserRatingApiExt;
use crate::{
cache::{PLAYER_B50, PLAYERS},
cache::{B50, PLAYERS},
utils::helpers::{cached_concurrent_fetch, read_cache},
};
@@ -280,7 +280,7 @@ async fn main() -> Result<(), Box<dyn snafu::Error>> {
players.iter().map(|p| p.user_id).collect::<Vec<u32>>(),
&client,
concurrency,
PLAYER_B50,
B50,
)
.await?;
}
@@ -293,7 +293,7 @@ async fn main() -> Result<(), Box<dyn snafu::Error>> {
use sdgb_api::title::methods::GetUserRegionApiExt;
use crate::{
cache::{PLAYER_REGION, PLAYERS},
cache::{PLAYERS, REGIONS},
utils::helpers::{cached_concurrent_fetch, read_cache},
};
@@ -304,7 +304,7 @@ async fn main() -> Result<(), Box<dyn snafu::Error>> {
players.iter().map(|p| p.user_id).collect::<Vec<u32>>(),
&client,
concurrency,
PLAYER_REGION,
REGIONS,
)
.await?;
}
@@ -317,9 +317,9 @@ async fn main() -> Result<(), Box<dyn snafu::Error>> {
}
#[cfg(feature = "fetchall")]
Commands::ScrapeAllB50Dump {} => {
use crate::{cache::PLAYER_B50, utils::helpers::dump_cache};
use crate::{cache::B50, utils::helpers::dump_cache};
dump_cache::<GetUserRatingApiResp>("b50.json", PLAYER_B50)?;
dump_cache::<GetUserRatingApiResp>("b50.json", B50)?;
}
Commands::Userdata { user_id } => {