@@ -7,7 +7,7 @@ use crate::lib::environment::Environment;
7
7
use crate :: lib:: error:: { BuildError , DfxError , DfxResult } ;
8
8
use crate :: lib:: metadata:: dfx:: DfxMetadata ;
9
9
use crate :: lib:: metadata:: names:: { CANDID_ARGS , CANDID_SERVICE , DFX } ;
10
- use crate :: lib:: operations :: canister ;
10
+ use crate :: lib:: graph :: traverse_filtered :: { self , DfsFiltered } ;
11
11
use crate :: lib:: wasm:: file:: { compress_bytes, read_wasm_module} ;
12
12
use crate :: util:: assets;
13
13
use anyhow:: { anyhow, bail, Context } ;
@@ -578,39 +578,58 @@ impl CanisterPool {
578
578
Some ( canisters_to_build) => canisters_to_build,
579
579
None => self . canisters . iter ( ) . map ( |canister| canister. get_name ( ) . to_string ( ) ) . collect ( ) ,
580
580
} ;
581
- // Transform the graph of file dependencies to graph of canister dependencies.
582
- // For this do DFS for each of `real_canisters_to_build`.
583
- // TODO: Somebody, adopt this code to `pethgraph`.
581
+ // // Transform the graph of file dependencies to graph of canister dependencies.
582
+ // // For this do DFS for each of `real_canisters_to_build`.
584
583
let source_graph = & self . imports . borrow ( ) . graph ;
585
584
let mut dest_graph: DiGraph < CanisterId , ( ) > = DiGraph :: new ( ) ;
586
585
let mut dest_id_set = HashMap :: new ( ) ;
587
- let mut name_to_dest = HashMap :: new ( ) ;
588
- for start_name in real_canisters_to_build. iter ( ) {
589
- let dest_start = self . get_first_canister_with_name ( & start_name) . unwrap ( ) . canister_id ( ) ;
590
- let dest_start = * dest_id_set. entry ( dest_start. clone ( ) ) . or_insert_with ( || dest_graph. add_node ( dest_start. clone ( ) ) ) ; // TODO: always inserts
591
- name_to_dest. insert ( start_name, dest_start) ;
592
- let mut iter = Dfs :: new ( & source_graph, dest_start) ;
593
- iter. next ( & source_graph) ;
594
- while let Some ( cur_source_id) = iter. next ( & source_graph) {
595
- let cur_source_node = source_graph. node_weight ( cur_source_id) . unwrap ( ) ;
596
- if let MotokoImport :: Canister ( name) = cur_source_node {
597
- let parent_in_source_id = * iter. stack . iter ( ) . rev ( ) . find (
598
- |& entry|
599
- if let Some ( MotokoImport :: Canister ( _parent_name) ) = source_graph. node_weight ( * entry) {
600
- true
601
- } else {
602
- false
603
- }
604
- ) . unwrap ( ) ;
605
- // Both parent and current ancestor are `Canister` dependencies.
606
- let parent_in_dest_id =
607
- name_to_dest. entry ( parent_in_source_id) . or_insert_with ( || dest_graph. add_node ( cur_canister_id) ) ;
608
- dest_graph. add_edge ( parent_in_dest_id, b, ( ) )
609
- // let parent_in_source = source_graph.node_weight(*parent_in_source).unwrap();
586
+ let dfs = Dfs :: from_parts ( real_canisters_to_build, HashMap :: new ( ) ) ; // TODO: Use `FixedBitSet` instead of `HashMap`?
587
+ let filtered_dfs = DfsFiltered :: new ( dfs) ;
588
+ filtered_dfs. traverse (
589
+ source_graph,
590
+ |s| {
591
+ if let Some ( MotokoImport :: Canister ( _parent_name) ) = source_graph. node_weight ( * entry) {
592
+ true
593
+ } else {
594
+ false
610
595
}
611
- // let cur_node_id = id_set.entry(cur_source_id).or_insert_with(|| id_set.insert(cur_source_id));
596
+ } ,
597
+ |parent, child| {
598
+ let parent_id = * dest_id_set. entry ( parent) . or_insert_with ( || dest_graph. add_node ( parent) ) ;
599
+ let child_id = * dest_id_set. entry ( child) . or_insert_with ( || dest_graph. add_node ( child) ) ;
600
+ dest_graph. add_edge ( parent_id, child_id, ( ) ) ;
612
601
}
613
- }
602
+ ) ;
603
+ // let source_graph = &self.imports.borrow().graph;
604
+ // let mut dest_graph: DiGraph<CanisterId, ()> = DiGraph::new();
605
+ // let mut dest_id_set = HashMap::new();
606
+ // let mut name_to_dest = HashMap::new();
607
+ // for start_name in real_canisters_to_build.iter() {
608
+ // let dest_start = self.get_first_canister_with_name(&start_name).unwrap().canister_id();
609
+ // let dest_start = *dest_id_set.entry(dest_start.clone()).or_insert_with(|| dest_graph.add_node(dest_start.clone())); // TODO: always inserts
610
+ // name_to_dest.insert(start_name, dest_start);
611
+ // let mut iter = Dfs::new(&source_graph, dest_start);
612
+ // iter.next(&source_graph);
613
+ // while let Some(cur_source_id) = iter.next(&source_graph) {
614
+ // let cur_source_node = source_graph.node_weight(cur_source_id).unwrap();
615
+ // if let MotokoImport::Canister(name) = cur_source_node {
616
+ // let parent_in_source_id = *iter.stack.iter().rev().find(
617
+ // |&entry|
618
+ // if let Some(MotokoImport::Canister(_parent_name)) = source_graph.node_weight(*entry) {
619
+ // true
620
+ // } else {
621
+ // false
622
+ // }
623
+ // ).unwrap();
624
+ // // Both parent and current ancestor are `Canister` dependencies.
625
+ // let parent_in_dest_id =
626
+ // name_to_dest.entry(parent_in_source_id).or_insert_with(|| dest_graph.add_node(cur_canister_id));
627
+ // dest_graph.add_edge(parent_in_dest_id, b, ())
628
+ // // let parent_in_source = source_graph.node_weight(*parent_in_source).unwrap();
629
+ // }
630
+ // // let cur_node_id = id_set.entry(cur_source_id).or_insert_with(|| id_set.insert(cur_source_id));
631
+ // }
632
+ // }
614
633
615
634
Ok ( dest_graph)
616
635
// FIXME: Wrong behavior on indirect dependencies.
0 commit comments