From: Eloy Lafuente Date: Tue, 1 Dec 2009 18:10:51 +0000 (+0000) Subject: MDL-21015 - add optional xmldb_structure cache in the install_one_table_from_xmldb_file() X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=a97c0dd5980bf2dc336aa0373b6199e0ec32fded;p=moodle.git MDL-21015 - add optional xmldb_structure cache in the install_one_table_from_xmldb_file() method. 100x speedup for UnitTestCaseUsingDatabase tests. Defaults to false (old behaviour) --- diff --git a/lib/ddl/database_manager.php b/lib/ddl/database_manager.php index bbf30554a7..196c2a07ac 100644 --- a/lib/ddl/database_manager.php +++ b/lib/ddl/database_manager.php @@ -411,10 +411,21 @@ class database_manager { * * @param $file full path to the XML file to be used * @param $tablename the name of the table. + * @param bool $cachestructures boolean to decide if loaded xmldb structures can be safely cached + * useful for testunits loading the enormous main xml file hundred of times (100x) */ - public function install_one_table_from_xmldb_file($file, $tablename) { - $xmldb_file = $this->load_xmldb_file($file); - $xmldb_structure = $xmldb_file->getStructure(); + public function install_one_table_from_xmldb_file($file, $tablename, $cachestructures = false) { + + static $xmldbstructurecache = array(); // To store cached structures + if (!empty($xmldbstructurecache) && array_key_exists($file, $xmldbstructurecache)) { + $xmldb_structure = $xmldbstructurecache[$file]; + } else { + $xmldb_file = $this->load_xmldb_file($file); + $xmldb_structure = $xmldb_file->getStructure(); + if ($cachestructures) { + $xmldbstructurecache[$file] = $xmldb_structure; + } + } $targettable = $xmldb_structure->getTable($tablename); if (is_null($targettable)) {