c9bc4c9af795467ce0ade63bb9cb8bfa2a8fe5bb
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * The InnoDB indexer is responsible for creating and destroying
30 * full-text indices on InnoDB classes.
32 class CRM_Core_InnoDBIndexer
{
33 const IDX_PREFIX
= "civicrm_fts_";
36 * @var CRM_Core_InnoDBIndexer
38 private static $singleton = NULL;
40 public static function singleton($fresh = FALSE) {
41 if ($fresh || self
::$singleton === NULL) {
43 'civicrm_address' => array(
44 array('street_address', 'city', 'postal_code')
46 'civicrm_contact' => array(
47 array('sort_name', 'nick_name', 'display_name'),
49 'civicrm_email' => array(
52 'civicrm_note' => array(
53 array('subject', 'note'),
55 'civicrm_phone' => array(
58 'civicrm_tag' => array(
62 $active = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'enable_innodb_fts', NULL, FALSE);
63 self
::$singleton = new self($active, $indices);
65 return self
::$singleton;
70 * Respond to changes in the "enable_innodb_fts" setting
72 * @param bool $oldValue
73 * @param bool $newValue
74 * @param array $metadata Specification of the setting (per *.settings.php)
76 public static function onToggleFts($oldValue, $newValue, $metadata) {
77 $indexer = CRM_Core_InnoDBIndexer
::singleton();
78 $indexer->setActive($newValue);
79 $indexer->fixSchemaDifferences();
83 * @var array (string $table => array $indices)
85 * ex: $indices['civicrm_contact'][0] = array('first_name', 'last_name');
94 public function __construct($isActive, $indices) {
95 $this->isActive
= $isActive;
96 $this->indices
= $this->normalizeIndices($indices);
99 public function fixSchemaDifferences() {
100 // Limitation: This won't pick up stale indices on tables which are not
101 // declared in $this->indices. That's not much of an issue for now b/c
102 // we have a static list of tables.
103 foreach ($this->indices
as $tableName => $ign) {
104 $todoSqls = $this->reconcileIndexSqls($tableName);
105 foreach ($todoSqls as $todoSql) {
106 CRM_Core_DAO
::executeQuery($todoSql);
112 * Determine if an index is expected to exist
114 * @param string $table
115 * @param array $fields list of field names that must be in the index
118 public function hasDeclaredIndex($table, $fields) {
119 if (!$this->isActive
) {
123 if (isset($this->indices
[$table])) {
124 foreach ($this->indices
[$table] as $idxFields) {
125 // TODO determine if $idxFields must be exact match or merely a subset
126 // if (sort($fields) == sort($idxFields)) {
127 if (array_diff($fields, $idxFields) == array()) {
137 * Get a list of FTS index names that are currently defined in the database.
139 * @param string $table
140 * @return array (string $indexName => string $indexName)
142 public function findActualFtsIndexNames($table) {
143 // Note: this only works in MySQL 5.6, but this whole system is intended to only work in MySQL 5.6
145 SELECT i.name as index_name
146 FROM information_schema.innodb_sys_tables t
147 JOIN information_schema.innodb_sys_indexes i USING (table_id)
148 WHERE t.name = concat(database(),'/$table')
149 AND i.name like '" . self
::IDX_PREFIX
. "%'
151 $dao = CRM_Core_DAO
::executeQuery($sql);
152 $indexNames = array();
153 while ($dao->fetch()) {
154 $indexNames[$dao->index_name
] = $dao->index_name
;
160 * Generate a "CREATE INDEX" statement for each desired
164 * @return array (string $indexName => string $sql)
166 public function buildIndexSql($table) {
167 $sqls = array(); // array (string $idxName => string $sql)
168 if ($this->isActive
&& isset($this->indices
[$table])) {
169 foreach ($this->indices
[$table] as $fields) {
170 $name = self
::IDX_PREFIX
. md5($table . '::' . implode(',', $fields));
171 $sqls[$name] = sprintf("CREATE FULLTEXT INDEX %s ON %s (%s)", $name, $table, implode(',', $fields));
178 * Generate a "DROP INDEX" statement for each existing FTS index
180 * @param string $table
181 * @return array (string $idxName => string $sql)
183 public function dropIndexSql($table) {
185 $names = $this->findActualFtsIndexNames($table);
186 foreach ($names as $name) {
187 $sqls[$name] = sprintf("DROP INDEX %s ON %s", $name, $table);
193 * Construct a set of SQL statements which will create (or preserve)
194 * required indices and destroy unneeded indices.
199 public function reconcileIndexSqls($table) {
200 $buildIndexSqls = $this->buildIndexSql($table);
201 $dropIndexSqls = $this->dropIndexSql($table);
203 $allIndexNames = array_unique(array_merge(
204 array_keys($dropIndexSqls),
205 array_keys($buildIndexSqls)
209 foreach ($allIndexNames as $indexName) {
210 if (isset($buildIndexSqls[$indexName]) && isset($dropIndexSqls[$indexName])) {
213 elseif (isset($buildIndexSqls[$indexName])) {
214 $todoSqls[] = $buildIndexSqls[$indexName];
217 $todoSqls[] = $dropIndexSqls[$indexName];
224 * Put the indices into a normalized format
229 public function normalizeIndices($indices) {
231 foreach ($indices as $table => $indicesByTable) {
232 foreach ($indicesByTable as $k => $fields) {
234 $result[$table][] = $fields;
241 * @param boolean $isActive
243 public function setActive($isActive) {
244 $this->isActive
= $isActive;
250 public function getActive() {
251 return $this->isActive
;