Skip to content

Commit

Permalink
feat: create records for route and route usage of each parsed module
Browse files Browse the repository at this point in the history
  • Loading branch information
wtlin1228 committed Oct 10, 2024
1 parent c393e3d commit e44c781
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 24 deletions.
51 changes: 44 additions & 7 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use dt_core::{
},
path_resolver::{PathResolver, ToCanonicalString},
portable::Portable,
route::SymbolToRoutes,
route::{Route, SymbolToRoutes},
scheduler::ParserCandidateScheduler,
};
use std::{
Expand Down Expand Up @@ -397,6 +397,33 @@ impl Project {
}
Ok(())
}

pub fn add_route_usage(
&self,
module: &models::Module,
route_usage: &Vec<Route>,
) -> anyhow::Result<()> {
for Route { path, depend_on } in route_usage.iter() {
let route = self
.project
.add_route(&self.db.conn, path)
.context(format!("create route {} for project", path))?;
for symbol_name in depend_on.iter() {
let symbol = module
.get_symbol(
&self.db.conn,
models::SymbolVariant::LocalVariable,
&symbol_name,
)
.context(format!(
"try to add route for symbol {}, but symbol doesn't exist",
symbol_name,
))?;
models::RouteUsage::create(&self.db.conn, &route, &symbol)?;
}
}
Ok(())
}
}

fn main() -> anyhow::Result<()> {
Expand All @@ -423,17 +450,27 @@ fn main() -> anyhow::Result<()> {
let module_ast = Input::Path(module_src).get_module_ast()?;
let symbol_dependency = collect_symbol_dependency(&module_ast, module_src)?;
let i18n_usage = i18n_to_symbol.collect_i18n_usage(module_src, &module_ast)?;
symbol_to_route.collect_route_dependency(&module_ast, &symbol_dependency)?;
let route_usage =
symbol_to_route.collect_route_dependency(&module_ast, &symbol_dependency)?;

let module = project.add_module(&symbol_dependency).context(format!(
"add module {} to project",
symbol_dependency.canonical_path
))?;
let module = project
.add_module(&symbol_dependency)
.context(format!(
"add module {} to project",
symbol_dependency.canonical_path
))
.context(format!("add module {} to project", module_src))?;
project
.add_i18n_usage(&module, &i18n_usage)
.context(format!(
"add i18n usage of module {} to project",
symbol_dependency.canonical_path
module_src
))?;
project
.add_route_usage(&module, &route_usage)
.context(format!(
"add route usage of module {} to project",
module_src
))?;

depend_on_graph.add_symbol_dependency(symbol_dependency)?;
Expand Down
47 changes: 41 additions & 6 deletions crates/dt_database/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ impl Project {
pub fn get_translation(&self, conn: &Connection, key: &str) -> anyhow::Result<Translation> {
Translation::retrieve(conn, self, key)
}

pub fn add_route(&self, conn: &Connection, path: &str) -> anyhow::Result<Route> {
Route::create(conn, self, path)
}
}

#[derive(Debug)]
Expand Down Expand Up @@ -423,15 +427,17 @@ impl TranslationUsage {
#[derive(Debug)]
pub struct Route {
pub id: usize,
pub project_id: usize,
pub path: String,
}

impl Model for Route {
fn table() -> String {
"
route (
id INTEGER PRIMARY KEY AUTOINCREMENT,
path TEXT NOT NULL
id INTEGER PRIMARY KEY AUTOINCREMENT,
project_id INTEGER REFERENCES project(id) ON DELETE CASCADE,
path TEXT NOT NULL
)
"
.to_string()
Expand All @@ -442,9 +448,24 @@ impl Route {
pub fn from_row(row: &Row) -> rusqlite::Result<Self> {
Ok(Self {
id: row.get(0)?,
path: row.get(1)?,
project_id: row.get(1)?,
path: row.get(2)?,
})
}

/// single thread only: last_insert_rowid()
pub fn create(conn: &Connection, project: &Project, path: &str) -> anyhow::Result<Self> {
conn.execute(
"INSERT INTO route (project_id, path) VALUES (?1, ?2)",
params![project.id, path],
)?;
let route = conn.query_row(
"SELECT * FROM route WHERE id=last_insert_rowid()",
(),
Self::from_row,
)?;
Ok(route)
}
}

// Join Table
Expand All @@ -459,9 +480,9 @@ impl Model for RouteUsage {
fn table() -> String {
"
route_usage (
id INTEGER PRIMARY KEY AUTOINCREMENT,
translation_id INTEGER REFERENCES route(id) ON DELETE CASCADE,
symbol_id INTEGER REFERENCES symbol(id) ON DELETE CASCADE
id INTEGER PRIMARY KEY AUTOINCREMENT,
route_id INTEGER REFERENCES route(id) ON DELETE CASCADE,
symbol_id INTEGER REFERENCES symbol(id) ON DELETE CASCADE
)
"
.to_string()
Expand All @@ -476,4 +497,18 @@ impl RouteUsage {
symbol_id: row.get(2)?,
})
}

/// single thread only: last_insert_rowid()
pub fn create(conn: &Connection, route: &Route, symbol: &Symbol) -> anyhow::Result<Self> {
conn.execute(
"INSERT INTO route_usage (route_id, symbol_id) VALUES (?1, ?2)",
params![route.id, symbol.id],
)?;
let route = conn.query_row(
"SELECT * FROM route_usage WHERE id=last_insert_rowid()",
(),
Self::from_row,
)?;
Ok(route)
}
}
23 changes: 12 additions & 11 deletions crates/dt_route/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ impl SymbolToRoutes {
&mut self,
module_ast: &Module,
symbol_dependency: &SymbolDependency,
) -> anyhow::Result<()> {
) -> anyhow::Result<Vec<Route>> {
if Self::should_collect(symbol_dependency) {
let routes = Self::collect(module_ast, symbol_dependency)?;
self.aggregate(symbol_dependency.canonical_path.as_str(), routes);
self.aggregate(symbol_dependency.canonical_path.as_str(), &routes);
return Ok(routes);
}
Ok(())
Ok(vec![])
}

fn should_collect(symbol_dependency: &SymbolDependency) -> bool {
Expand Down Expand Up @@ -71,14 +72,14 @@ impl SymbolToRoutes {
Ok(route_visitor.routes)
}

fn aggregate(&mut self, module_path: &str, routes: Vec<Route>) {
fn aggregate(&mut self, module_path: &str, routes: &Vec<Route>) {
let mut map = HashMap::new();
for route in routes {
for symbol in route.depend_on {
if !map.contains_key(&symbol) {
map.insert(symbol, vec![route.path.to_owned()]);
for symbol in route.depend_on.iter() {
if !map.contains_key(symbol) {
map.insert(symbol.to_string(), vec![route.path.to_owned()]);
} else {
map.get_mut(&symbol).unwrap().push(route.path.to_owned());
map.get_mut(symbol).unwrap().push(route.path.to_owned());
}
}
}
Expand All @@ -87,9 +88,9 @@ impl SymbolToRoutes {
}

#[derive(Debug)]
struct Route {
path: String,
depend_on: HashSet<String>,
pub struct Route {
pub path: String,
pub depend_on: HashSet<String>,
}

#[derive(Debug)]
Expand Down

0 comments on commit e44c781

Please sign in to comment.