// Big optimization of install process (execution time /2 during database population process)

This commit is contained in:
rMalie
2011-12-27 17:14:02 +00:00
parent 34e3600da2
commit f92839d45c
11 changed files with 113 additions and 32 deletions
+86 -16
View File
@@ -54,6 +54,10 @@ class InstallXmlLoader
protected $ids = array(); protected $ids = array();
protected $primaries = array();
protected $delayed_inserts = array();
public function __construct() public function __construct()
{ {
$this->language = InstallLanguages::getInstance(); $this->language = InstallLanguages::getInstance();
@@ -290,6 +294,8 @@ class InstallXmlLoader
$this->copyImages($entity, $identifier, (string)$xml->fields['image'], $data); $this->copyImages($entity, $identifier, (string)$xml->fields['image'], $data);
} }
} }
$this->flushDelayedInserts();
} }
/** /**
@@ -373,6 +379,20 @@ class InstallXmlLoader
return $data; return $data;
} }
public function flushDelayedInserts()
{
foreach ($this->delayed_inserts as $entity => $queries)
{
$type = 'INSERT IGNORE';
if ($entity == 'access')
$type = 'REPLACE';
if (!Db::getInstance()->autoExecute(_DB_PREFIX_.$entity, $queries, $type))
$this->setError($this->language->l('An SQL error occured for entity <i>%1$s</i>: <i>%2$s</i>', $entity, Db::getInstance()->getMsgError()));
unset($this->delayed_inserts[$entity]);
}
}
/** /**
* Create a simple entity with all its data and lang data * Create a simple entity with all its data and lang data
* If a methode createEntity$entity exists, use it. Else if $classname is given, use it. Else do a simple insert in database. * If a methode createEntity$entity exists, use it. Else if $classname is given, use it. Else do a simple insert in database.
@@ -385,16 +405,15 @@ class InstallXmlLoader
*/ */
public function createEntity($entity, $identifier, $classname, array $data, array $data_lang = array()) public function createEntity($entity, $identifier, $classname, array $data, array $data_lang = array())
{ {
$xml = $this->loadEntity($entity);
if (method_exists($this, 'createEntity'.Tools::toCamelCase($entity))) if (method_exists($this, 'createEntity'.Tools::toCamelCase($entity)))
{ {
// Create entity with custom method in current class // Create entity with custom method in current class
$method = 'createEntity'.Tools::toCamelCase($entity); $method = 'createEntity'.Tools::toCamelCase($entity);
$entity_id = $this->$method($data, $data_lang); $entity_id = $this->$method($identifier, $data, $data_lang);
} }
else if ($classname) else if ($classname)
{ {
$xml = $this->loadEntity($entity);
// Create entity with ObjectModel class // Create entity with ObjectModel class
$object = new $classname(); $object = new $classname();
$object->hydrate($data); $object->hydrate($data);
@@ -405,15 +424,22 @@ class InstallXmlLoader
} }
else else
{ {
// Create entity in database); // Generate primary key manually
$execute_type = 'INSERT IGNORE'; $primary = '';
if ($entity == 'access') $entity_id = 0;
$execute_type = 'REPLACE'; if (!$xml->fields['primary'])
$primary = 'id_'.$entity;
else if (strpos((string)$xml->fields['primary'], ',') === false)
$primary = (string)$xml->fields['primary'];
if (!Db::getInstance()->autoExecute(_DB_PREFIX_.$entity, array_map('pSQL', $data), $execute_type)) if ($primary)
$this->setError($this->language->l('An SQL error occured for entity <i>%1$s</i>: <i>%2$s</i>', $entity, Db::getInstance()->getMsgError())); {
$entity_id = Db::getInstance()->Insert_ID(); $entity_id = $this->generatePrimary($entity, $primary);
$data[$primary] = $entity_id;
}
// Store INSERT queries in order to optimize install with grouped inserts
$this->delayed_inserts[$entity][] = array_map('pSQL', $data);
if ($data_lang) if ($data_lang)
{ {
$real_data_lang = array(); $real_data_lang = array();
@@ -425,8 +451,7 @@ class InstallXmlLoader
{ {
$insert_data_lang['id_'.$entity] = $entity_id; $insert_data_lang['id_'.$entity] = $entity_id;
$insert_data_lang['id_lang'] = $id_lang; $insert_data_lang['id_lang'] = $id_lang;
if (!Db::getInstance()->autoExecute(_DB_PREFIX_.$entity.'_lang', array_map('pSQL', $insert_data_lang), 'INSERT IGNORE')) $this->delayed_inserts[$entity.'_lang'][] = array_map('pSQL', $insert_data_lang);
$this->setError($this->language->l('An SQL error occured for entity <i>%1$s</i>: <i>%2$s</i>', $entity, Db::getInstance()->getMsgError()));
} }
} }
} }
@@ -434,11 +459,56 @@ class InstallXmlLoader
$this->storeId($entity, $identifier, $entity_id); $this->storeId($entity, $identifier, $entity_id);
} }
public function createEntityConfiguration(array $data, array $data_lang = array()) public function createEntityTab($identifier, array $data, array $data_lang)
{ {
if (!Configuration::get($data['name'])) static $position = array();
Configuration::updateGlobalValue($data['name'], ($data_lang) ? $data_lang['value'] : $data['value']);
return Configuration::getIdByName($data['name']); $entity = 'tab';
$xml = $this->loadEntity($entity);
if (!isset($position[$data['id_parent']]))
$position[$data['id_parent']] = 0;
$data['position'] = $position[$data['id_parent']]++;
// Generate primary key manually
$primary = '';
$entity_id = 0;
if (!$xml->fields['primary'])
$primary = 'id_'.$entity;
else if (strpos((string)$xml->fields['primary'], ',') === false)
$primary = (string)$xml->fields['primary'];
if ($primary)
{
$entity_id = $this->generatePrimary($entity, $primary);
$data[$primary] = $entity_id;
}
// Store INSERT queries in order to optimize install with grouped inserts
$this->delayed_inserts[$entity][] = array_map('pSQL', $data);
if ($data_lang)
{
$real_data_lang = array();
foreach ($data_lang as $field => $list)
foreach ($list as $id_lang => $value)
$real_data_lang[$id_lang][$field] = $value;
foreach ($real_data_lang as $id_lang => $insert_data_lang)
{
$insert_data_lang['id_'.$entity] = $entity_id;
$insert_data_lang['id_lang'] = $id_lang;
$this->delayed_inserts[$entity.'_lang'][] = array_map('pSQL', $insert_data_lang);
}
}
$this->storeId($entity, $identifier, $entity_id);
}
public function generatePrimary($entity, $primary)
{
if (!isset($this->primaries[$entity]))
$this->primaries[$entity] = (int)Db::getInstance()->getValue('SELECT '.$primary.' FROM '._DB_PREFIX_.$entity.' ORDER BY '.$primary.' DESC');
return ++$this->primaries[$entity];
} }
public function copyImages($entity, $identifier, $path, array $data, $extension = 'jpg') public function copyImages($entity, $identifier, $path, array $data, $extension = 'jpg')
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<entity_address_format> <entity_address_format>
<fields primary="id_country" class="AddressFormat"> <fields primary="id_country">
<field name="id_country" relation="country"/> <field name="id_country" relation="country"/>
<field name="format"/> <field name="format"/>
</fields> </fields>
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<entity_configuration> <entity_configuration>
<fields id="name" class="Configuration" null="1"> <fields id="name" null="1">
<field name="name"/> <field name="name"/>
<field name="value"/> <field name="value"/>
</fields> </fields>
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<entity_country> <entity_country>
<fields id="iso_code" class="Country"> <fields id="iso_code">
<field name="id_zone" relation="zone"/> <field name="id_zone" relation="zone"/>
<field name="iso_code"/> <field name="iso_code"/>
<field name="call_prefix"/> <field name="call_prefix"/>
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<entity_hook> <entity_hook>
<fields id="name" class="Hook"> <fields id="name">
<field name="name"/> <field name="name"/>
<field name="title"/> <field name="title"/>
<field name="description"/> <field name="description"/>
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<entity_meta> <entity_meta>
<fields id="page" class="Meta"> <fields id="page">
<field name="page"/> <field name="page"/>
</fields> </fields>
<entities> <entities>
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<entity_state> <entity_state>
<fields id="iso_code" class="State"> <fields id="iso_code">
<field name="id_country" relation="country"/> <field name="id_country" relation="country"/>
<field name="id_zone" relation="zone"/> <field name="id_zone" relation="zone"/>
<field name="name"/> <field name="name"/>
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<entity_tab> <entity_tab>
<fields id="name" class="Tab" ordersql="a.id_parent, a.position" image="t"> <fields id="name" ordersql="a.id_parent, a.position" image="t">
<field name="id_parent" relation="tab"/> <field name="id_parent" relation="tab"/>
<field name="class_name"/> <field name="class_name"/>
<field name="active"/> <field name="active"/>
+1 -1
View File
@@ -35,4 +35,4 @@ try
catch (PrestashopInstallerException $e) catch (PrestashopInstallerException $e)
{ {
$e->displayMessage(); $e->displayMessage();
} }
+18 -7
View File
@@ -53,6 +53,8 @@ require_once _PS_INSTALL_PATH_.'classes/sqlLoader.php';
require_once _PS_INSTALL_PATH_.'classes/xmlLoader.php'; require_once _PS_INSTALL_PATH_.'classes/xmlLoader.php';
require_once _PS_INSTALL_PATH_.'classes/simplexml.php'; require_once _PS_INSTALL_PATH_.'classes/simplexml.php';
@set_time_limit(300);
class InstallLog class InstallLog
{ {
/** /**
@@ -68,8 +70,9 @@ class InstallLog
} }
protected $fd; protected $fd;
protected $start_time = array(); protected $data = array();
protected $last_time; protected $last_time;
protected $depth = 0;
public function __construct() public function __construct()
{ {
@@ -79,20 +82,28 @@ class InstallLog
public function write($id) public function write($id)
{ {
$str = "[$id]"; $str = str_pad("[$id]", 35, ' ');
$str .= ' - [Time: '.round(microtime(true) - $this->last_time, 4).']'; if (isset($this->data[$id]['start']))
if (isset($this->start_time[$id])) $str .= str_pad(round(microtime(true) - $this->data[$id]['start'], 4).'ms', 10, ' ');
$str .= ' - [Length: '.round(microtime(true) - $this->start_time[$id], 4).']'; $str .= str_pad(round(microtime(true) - $this->last_time, 4).'ms', 10, ' ');
fwrite($this->fd, "$str\n"); $this->data[$id]['str'] = str_repeat("\t", $this->depth - 1)."$str\n";
$this->depth--;
} }
public function start($id) public function start($id)
{ {
$this->start_time[$id] = microtime(true); $this->data[$id] = array('start' => microtime(true));
$this->depth++;
} }
public function __destruct() public function __destruct()
{ {
foreach ($this->data as $k => $info)
if (!isset($info['str']))
$this->write($k);
foreach ($this->data as $info)
fwrite($this->fd, $info['str']);
fclose($this->fd); fclose($this->fd);
} }
} }
+1 -1
View File
@@ -196,7 +196,7 @@ class InstallModelInstall extends InstallAbstractModel
} }
} }
// Copy language default images (we do this action after database in populated because we need image types informations) // Copy language default images (we do this action after database in populated because we need image types information)
foreach ($languages as $iso) foreach ($languages as $iso)
$this->copyLanguageImages($iso); $this->copyLanguageImages($iso);