This commit is contained in:
shenjack 2024-07-22 21:06:37 +08:00
parent 6d6ec7b05c
commit af355c6b72
Signed by: shenjack
GPG Key ID: 7B1134A979775551
7 changed files with 64 additions and 18 deletions

View File

@ -1,11 +1,14 @@
pub use sea_orm_migration::prelude::*;
mod m20240719_00001_create_main_data_table;
mod m20240719_00002_create_long_data_table;
mod m20240721_221623_create_indexs;
pub mod m20240719_00001_create_main_data_table;
pub mod m20240719_00002_create_long_data_table;
pub mod m20240721_221623_create_indexs;
pub use m20240721_221623_create_indexs::FULL_DATA_VIEW;
pub const TEXT_DATA_MAX_LEN: usize = 1024;
pub type SaveId = u32;
pub struct Migrator;
#[async_trait::async_trait]

View File

@ -2,6 +2,8 @@ use sea_orm::{EnumIter, Iterable};
use sea_orm_migration::prelude::extension::postgres::Type;
use sea_orm_migration::prelude::*;
use crate::TEXT_DATA_MAX_LEN;
#[derive(DeriveMigrationName)]
pub struct Migration;
@ -56,7 +58,7 @@ impl MigrationTrait for Migration {
// len = 64
.col(ColumnDef::new(MainData::BlakeHash).char_len(64).not_null())
.col(ColumnDef::new(MainData::Len).big_integer().not_null())
.col(ColumnDef::new(MainData::ShortData).string_len(1024))
.col(ColumnDef::new(MainData::ShortData).string_len(TEXT_DATA_MAX_LEN as u32))
.to_owned(),
)
.await?;

View File

@ -3,6 +3,10 @@ name = "sr_download"
version = "0.1.0"
edition = "2021"
[[bin]]
name = "get-save"
path = "./src/get-save.rs"
[dependencies]
reqwest = "0.12.5"
tokio = { version = "1.38.1", features = ["full"] }

View File

@ -2,7 +2,7 @@ use std::path::Path;
use serde::{Deserialize, Serialize};
use crate::SaveId;
use migration::SaveId;
#[derive(Serialize, Deserialize)]
pub struct ConfigFile {

View File

@ -6,10 +6,10 @@ use sea_orm::{
};
use tracing::{event, Level};
use crate::config::ConfigFile;
use crate::model;
use crate::model::sea_orm_active_enums::SaveType;
use crate::{config::ConfigFile, SaveId};
use crate::{model, TEXT_DATA_MAX_LEN};
use migration::{Migrator, MigratorTrait, FULL_DATA_VIEW};
use migration::{Migrator, MigratorTrait, SaveId, FULL_DATA_VIEW, TEXT_DATA_MAX_LEN};
#[derive(Debug, Clone)]
pub struct DbData {
@ -83,12 +83,16 @@ pub async fn connect(conf: &ConfigFile) -> anyhow::Result<DatabaseConnection> {
event!(Level::INFO, "Connecting to database");
let db: DatabaseConnection = Database::connect(opt).await?;
db.ping().await?;
event!(Level::INFO, "Connected to database, starting migration");
Migrator::up(&db, None).await?;
event!(Level::INFO, "Migration finished");
Ok(db)
}
pub async fn migrate(db: &DatabaseConnection) -> anyhow::Result<()> {
event!(Level::INFO, "Starting migration");
Migrator::up(db, None).await?;
event!(Level::INFO, "Migration finished");
Ok(())
}
pub async fn find_max_id(db: &DatabaseConnection) -> SaveId {
// SELECT save_id from main_data ORDER BY save_id DESC LIMIT 1
// 我丢你老母, 有这时间写这个, 我都写完 sql 语句了
@ -112,11 +116,22 @@ pub async fn find_max_id(db: &DatabaseConnection) -> SaveId {
}
}
#[allow(non_snake_case)]
pub fn SaveType_from_str(str: String) -> Option<SaveType> {
match str.to_lowercase().as_str() {
"save" => Some(SaveType::Save),
"ship" => Some(SaveType::Ship),
"none" => Some(SaveType::None),
"unknown" => Some(SaveType::Unknown),
_ => None
}
}
/// 直接从数据库中查询数据, 这里数据库已经准备好了根据长度区分过的数据
/// 可以从 full view 里直接选数据
pub async fn get_raw_data(save_id: SaveId, db: &DatabaseConnection) -> Option<DbData> {
let sql = format!(
"SELECT data, save_type, blake_hash FROM {} WHERE save_id = {}",
"SELECT data, save_type::text FROM {} WHERE save_id = {}",
FULL_DATA_VIEW, save_id
);
let datas = db
@ -126,9 +141,8 @@ pub async fn get_raw_data(save_id: SaveId, db: &DatabaseConnection) -> Option<Db
))
.await
.ok()??;
let text: String = datas.try_get_by_index(0).ok()?;
let save_type: SaveType = datas.try_get_by_index(1).ok()?;
let blake_hash: String = datas.try_get_by_index(2).ok()?;
let text: String = datas.try_get("", "data").ok()?;
let save_type: SaveType = SaveType_from_str(datas.try_get("", "save_type").ok()?)?;
Some(DbData::new(save_id, text, save_type))
}

View File

@ -0,0 +1,24 @@
use std::path::Path;
#[allow(unused)]
mod config;
#[allow(unused)]
mod db_part;
#[allow(unused)]
mod model;
use migration::SaveId;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let config = config::ConfigFile::read_from_file(Path::new("config.toml")).unwrap();
let db = db_part::connect(&config).await.unwrap();
let want_get_id = std::env::args().nth(1).ok_or(anyhow::anyhow!("No input"))?.parse::<SaveId>()?;
let data = db_part::get_raw_data(want_get_id, &db).await.ok_or(anyhow::anyhow!("No data"))?;
println!("{}", data.text.ok_or(anyhow::anyhow!("No text"))?);
Ok(())
}

View File

@ -10,12 +10,10 @@ mod db_part;
mod model;
mod net;
pub type SaveId = u32;
pub const TEXT_DATA_MAX_LEN: usize = 1024;
use model::sea_orm_active_enums::SaveType;
use crate::db_part::CoverStrategy;
use migration::SaveId;
async fn big_worker(
db: sea_orm::DatabaseConnection,
@ -89,6 +87,7 @@ async fn main_works(mut stop_receiver: Receiver<()>) -> anyhow::Result<()> {
};
let db_connect = db_part::connect(&conf).await?;
db_part::migrate(&db_connect).await?;
let db_max_id = db_part::find_max_id(&db_connect).await;
event!(