Example #1
0
 public function testAddNodeDoesNotAddAnyRepeatedNode()
 {
     $undirectedGraph = new Graph(self::UNDIRECTED_GRAPH, self::NON_WEIGHTED_GRAPH);
     $undirectedGraph->addNode(self::NODE);
     $nodeSet = array();
     $nodeSet[] = self::NODE;
     $nodeSet[] = self::NODE;
     $undirectedGraph->addNodeSet($nodeSet);
     $allNodes = $undirectedGraph->getAllNodes();
     $this->assertEquals(self::NODE, current($allNodes));
     $this->assertEquals(1, count($allNodes));
 }
Example #2
0
 /**
  * Generates directed graph that represents indexing dependencies between tables in the database
  * and then derives a list of indexed tables that might contain rows needing to be reindexed because
  * they use the subject table as part of their indexing.
  */
 private function __getDependencies($ps_subject_table)
 {
     $o_graph = new Graph();
     $va_indexed_tables = $this->getIndexedTables();
     $va_indexed_table_name_list = array();
     foreach ($va_indexed_tables as $vn_table_num => $va_table_info) {
         $va_indexed_table_name_list[] = $vs_indexed_table = $va_table_info['name'];
         if ($vs_indexed_table == $ps_subject_table) {
             continue;
         }
         // the subject can't be dependent upon itself
         // get list related tables used to index the subject table
         $va_related_tables = $this->getRelatedIndexingTables($vs_indexed_table);
         foreach ($va_related_tables as $vs_related_table) {
             // get list of tables in indexing relationship
             // eg. if the subject is 'objects', and the related table is 'entities' then
             // the table list would be ['objects', 'objects_x_entities', 'entities']
             $va_info = $this->getTableIndexingInfo($vs_indexed_table, $vs_related_table);
             $va_table_list_list = $va_info['tables'];
             if (!is_array($va_table_list_list) || !sizeof($va_table_list_list)) {
                 if ($vs_table_key = $va_info['key']) {
                     // Push direct relationship through one-to-many key onto table list
                     $va_table_list_list = array($vs_related_table => array());
                 } else {
                     $va_table_list_list = array();
                 }
             }
             foreach ($va_table_list_list as $vs_list_name => $va_table_list) {
                 array_unshift($va_table_list, $vs_indexed_table);
                 array_push($va_table_list, $vs_related_table);
                 if (in_array($ps_subject_table, $va_table_list)) {
                     // we only care about indexing relationships that include the subject table
                     // for each each related table record the intervening tables in the graph
                     $vs_last_table = '';
                     foreach ($va_table_list as $vs_tablename) {
                         $o_graph->addNode($vs_tablename);
                         if ($vs_last_table) {
                             if ($va_rel = $this->opo_datamodel->getOneToManyRelations($vs_tablename, $vs_last_table)) {
                                 // determining direction of relationship (directionality is from the "many" table to the "one" table
                                 $o_graph->addRelationship($vs_tablename, $vs_last_table, 10, true);
                             } else {
                                 $o_graph->addRelationship($vs_last_table, $vs_tablename, 10, true);
                             }
                         }
                         $vs_last_table = $vs_tablename;
                     }
                 }
             }
         }
     }
     $va_topo_list = $o_graph->getTopologicalSort();
     $va_deps = array();
     foreach ($va_topo_list as $vs_tablename) {
         if ($vs_tablename == $ps_subject_table) {
             continue;
         }
         if (!in_array($vs_tablename, $va_indexed_table_name_list)) {
             continue;
         }
         $va_deps[] = $vs_tablename;
     }
     return $va_deps;
 }