mirror of
https://github.com/EasyTier/EasyTier.git
synced 2026-05-15 10:25:40 +00:00
513695297c
* [OHOS.with ai] 将配置管理/配置分享/路由聚合/实例状态解析下沉至 Rust 内核,收敛职责并提升性能 (#2209) * feat: add ohrs config store and startup error logging * feat: full ability core for ohos * feat: full ability core for ohos * feat: clean code --------- Co-authored-by: FrankHan <frankhan@FrankHans-Mac-mini.local> * fix: 添加缺失文件 * fix: 修复更新路由启动两次TUN问题,并调整日志 * fix: rustfmt * fix: 适配Cidr忽略/32格式路由 * fix: 修复Option适配错误 * fix: rustfmt * fix: rustfmt --------- Co-authored-by: FrankHan <frankhan@FrankHans-Mac-mini.local>
68 lines
1.9 KiB
Rust
68 lines
1.9 KiB
Rust
use crate::config::storage::config_meta::{now_ts_string, open_db};
|
|
use ohos_hilog_binding::hilog_error;
|
|
use rusqlite::{Connection, params};
|
|
use serde_json::{Map, Value};
|
|
|
|
pub(super) fn load_config_map_from_db(config_id: &str) -> Option<Map<String, Value>> {
|
|
let conn = open_db()?;
|
|
let mut stmt = conn
|
|
.prepare(
|
|
"SELECT field_name, field_json
|
|
FROM stored_config_fields
|
|
WHERE config_id = ?1",
|
|
)
|
|
.ok()?;
|
|
let rows = stmt
|
|
.query_map(params![config_id], |row| {
|
|
let field_name: String = row.get(0)?;
|
|
let field_json: String = row.get(1)?;
|
|
Ok((field_name, field_json))
|
|
})
|
|
.ok()?;
|
|
|
|
let mut object = Map::new();
|
|
for row in rows {
|
|
let (field_name, field_json) = row.ok()?;
|
|
let value = serde_json::from_str::<Value>(&field_json).ok()?;
|
|
object.insert(field_name, value);
|
|
}
|
|
|
|
if object.is_empty() {
|
|
None
|
|
} else {
|
|
Some(object)
|
|
}
|
|
}
|
|
|
|
pub(super) fn replace_config_fields(
|
|
tx: &Connection,
|
|
config_id: &str,
|
|
fields: Map<String, Value>,
|
|
) -> Option<()> {
|
|
if let Err(e) = tx.execute(
|
|
"DELETE FROM stored_config_fields WHERE config_id = ?1",
|
|
params![config_id],
|
|
) {
|
|
hilog_error!(
|
|
"[Rust] failed to clear existing config fields {}: {}",
|
|
config_id,
|
|
e
|
|
);
|
|
return None;
|
|
}
|
|
|
|
for (field_name, value) in fields {
|
|
let field_json = serde_json::to_string(&value).ok()?;
|
|
if let Err(e) = tx.execute(
|
|
"INSERT INTO stored_config_fields (config_id, field_name, field_json, updated_at)
|
|
VALUES (?1, ?2, ?3, ?4)",
|
|
params![config_id, field_name, field_json, now_ts_string()],
|
|
) {
|
|
hilog_error!("[Rust] failed to persist config field {}: {}", config_id, e);
|
|
return None;
|
|
}
|
|
}
|
|
|
|
Some(())
|
|
}
|