diff --git a/Cargo.lock b/Cargo.lock index d945420..e75e5f0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -173,9 +173,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.2.30" +version = "1.2.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "deec109607ca693028562ed836a5f1c4b8bd77755c4e132fc5ce11b0b6211ae7" +checksum = "c3a42d84bb6b69d3a8b3eaacf0d88f179e1929695e1ad012b6cf64d9caaa5fd2" dependencies = [ "shlex", ] @@ -261,9 +261,9 @@ dependencies = [ [[package]] name = "compio-driver" -version = "0.8.1" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "737212fe00b4af769f7e8f156c25ffafd5888d4d21834e100ea068dea1086ef8" +checksum = "6ea757a5a1be2c2613f298a4ca703958251ff34bfdb803d78d55d01f48fae249" dependencies = [ "aligned-array", "cfg-if", @@ -1312,9 +1312,9 @@ dependencies = [ [[package]] name = "redb" -version = "2.6.1" +version = "2.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fef838cd981b5c46e9e91e20e4623e43b29b5c251eb245b34da0cbd2da09ab27" +checksum = "59b38b05028f398f08bea4691640503ec25fcb60b82fb61ce1f8fd1f4fccd3f7" dependencies = [ "libc", ] @@ -1507,6 +1507,7 @@ name = "sdgb-cli" version = "0.1.0" dependencies = [ "compio", + "crabtime", "ctrlc", "futures-util", "nyquest-preset", @@ -1550,9 +1551,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.141" +version = "1.0.142" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30b9eff21ebe718216c6ec64e1d9ac57087aad11efc64e32002bce4a0d4c03d3" +checksum = "030fedb782600dcbd6f02d479bf0d817ac3bb40d644745b769d6a96bc3afc5a7" dependencies = [ "itoa", "memchr", @@ -1797,9 +1798,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.47.0" +version = "1.47.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43864ed400b6043a4757a25c7a64a8efde741aed79a056a2fb348a406701bb35" +checksum = "89e49afdadebb872d3145a5638b59eb0691ea23e46ca484037cfab3b76b95038" dependencies = [ "backtrace", "io-uring", diff --git a/Cargo.toml b/Cargo.toml index 6d7b00a..e3df531 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,9 +16,10 @@ snafu = { version = "0.8.6", features = ["backtrace", "rust_1_81"] } serde = { version = "1.0.219", features = ["derive"] } serde_json = "1.0.141" strum = { version = "0.27.2", features = ["derive"] } -tokio = { version = "1", features = ["rt-multi-thread"] } +tokio = { version = "1.47.1", features = ["rt-multi-thread"] } compio = { version = "0.15.0", features = ["runtime"] } -redb = "2.6.1" +redb = "2.6.2" +crabtime = { git = "https://github.com/wdanilo/crabtime.git", rev = "2ed856f5" } [profile.release] lto = true diff --git a/sdgb-api/Cargo.toml b/sdgb-api/Cargo.toml index cea2631..3ff1724 100644 --- a/sdgb-api/Cargo.toml +++ b/sdgb-api/Cargo.toml @@ -23,6 +23,9 @@ music-db = { workspace = true } # (de)serialization serde = { workspace = true } +# magic macro +crabtime = { workspace = true } + # hashing digest = "0.10.7" hmac-sha256 = { version = "1.1.12", features = ["digest010", "traits010"] } @@ -42,5 +45,3 @@ aes = "0.8.4" cipher = { version = "0.4.4", features = ["block-padding"] } bincode = { version = "2.0.1", optional = true } -# magic macro -crabtime = { git = "https://github.com/wdanilo/crabtime.git", rev = "2ed856f5" } diff --git a/sdgb-cli/Cargo.toml b/sdgb-cli/Cargo.toml index 662717d..518110b 100644 --- a/sdgb-cli/Cargo.toml +++ b/sdgb-cli/Cargo.toml @@ -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" diff --git a/sdgb-cli/src/cache/mod.rs b/sdgb-cli/src/cache/mod.rs index 313db70..0e0e47e 100644 --- a/sdgb-cli/src/cache/mod.rs +++ b/sdgb-cli/src/cache/mod.rs @@ -11,11 +11,6 @@ static DATABASE: LazyLock = LazyLock::new(|| { db }); -pub const PLAYERS: TableDefinition<'_, u32, Vec> = redb::TableDefinition::new("players"); -pub const PLAYER_B50: TableDefinition<'_, u32, Vec> = redb::TableDefinition::new("b50"); -pub const PLAYER_RECORD: TableDefinition<'_, u32, Vec> = redb::TableDefinition::new("records"); -pub const PLAYER_REGION: TableDefinition<'_, u32, Vec> = redb::TableDefinition::new("regions"); - pub fn write_txn() -> Result { 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) { + let mut defs: Vec = Vec::new(); + + for table in tables { + let definition = table.to_uppercase(); + let table_name = format!("\"{table}\""); + + crabtime::output!( + pub const {{definition}}: TableDefinition<'_, u32, Vec> = 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"]); diff --git a/sdgb-cli/src/main.rs b/sdgb-cli/src/main.rs index 314f824..9eda92c 100644 --- a/sdgb-cli/src/main.rs +++ b/sdgb-cli/src/main.rs @@ -244,7 +244,7 @@ async fn main() -> Result<(), Box> { 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> { players.iter().map(|p| p.user_id).collect::>(), &client, concurrency, - PLAYER_RECORD, + RECORDS, get_user_all_music, ) .await?; @@ -269,7 +269,7 @@ async fn main() -> Result<(), Box> { 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> { players.iter().map(|p| p.user_id).collect::>(), &client, concurrency, - PLAYER_B50, + B50, ) .await?; } @@ -293,7 +293,7 @@ async fn main() -> Result<(), Box> { 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> { players.iter().map(|p| p.user_id).collect::>(), &client, concurrency, - PLAYER_REGION, + REGIONS, ) .await?; } @@ -317,9 +317,9 @@ async fn main() -> Result<(), Box> { } #[cfg(feature = "fetchall")] Commands::ScrapeAllB50Dump {} => { - use crate::{cache::PLAYER_B50, utils::helpers::dump_cache}; + use crate::{cache::B50, utils::helpers::dump_cache}; - dump_cache::("b50.json", PLAYER_B50)?; + dump_cache::("b50.json", B50)?; } Commands::Userdata { user_id } => {