3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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;
42 * @return CRM_Core_InnoDBIndexer
44 public static function singleton($fresh = FALSE) {
45 if ($fresh || self
::$singleton === NULL) {
47 'civicrm_address' => array(
48 array('street_address', 'city', 'postal_code'),
50 'civicrm_activity' => array(
51 array('subject', 'details'),
53 'civicrm_contact' => array(
54 array('sort_name', 'nick_name', 'display_name'),
56 'civicrm_contribution' => array(
57 array('source', 'amount_level', 'trxn_Id', 'invoice_id'),
59 'civicrm_email' => array(
62 'civicrm_membership' => array(
65 'civicrm_note' => array(
66 array('subject', 'note'),
68 'civicrm_participant' => array(
69 array('source', 'fee_level'),
71 'civicrm_phone' => array(
74 'civicrm_tag' => array(
78 $active = Civi
::settings()->get('enable_innodb_fts');
79 self
::$singleton = new self($active, $indices);
81 return self
::$singleton;
86 * Respond to changes in the "enable_innodb_fts" setting
88 * @param bool $oldValue
89 * @param bool $newValue
90 * @param array $metadata
91 * Specification of the setting (per *.settings.php).
93 public static function onToggleFts($oldValue, $newValue, $metadata) {
94 if (empty($oldValue) && empty($newValue)) {
98 $indexer = CRM_Core_InnoDBIndexer
::singleton();
99 $indexer->setActive($newValue);
100 $indexer->fixSchemaDifferences();
104 * @var array (string $table => array $indices)
106 * ex: $indices['civicrm_contact'][0] = array('first_name', 'last_name');
121 public function __construct($isActive, $indices) {
122 $this->isActive
= $isActive;
123 $this->indices
= $this->normalizeIndices($indices);
127 * Fix schema differences.
129 * Limitation: This won't pick up stale indices on tables which are not
130 * declared in $this->indices. That's not much of an issue for now b/c
131 * we have a static list of tables.
133 public function fixSchemaDifferences() {
134 foreach ($this->indices
as $tableName => $ign) {
135 $todoSqls = $this->reconcileIndexSqls($tableName);
136 foreach ($todoSqls as $todoSql) {
137 CRM_Core_DAO
::executeQuery($todoSql);
143 * Determine if an index is expected to exist.
145 * @param string $table
146 * @param array $fields
147 * List of field names that must be in the index.
150 public function hasDeclaredIndex($table, $fields) {
151 if (!$this->isActive
) {
155 if (isset($this->indices
[$table])) {
156 foreach ($this->indices
[$table] as $idxFields) {
157 // TODO determine if $idxFields must be exact match or merely a subset
158 // if (sort($fields) == sort($idxFields)) {
159 if (array_diff($fields, $idxFields) == array()) {
169 * Get a list of FTS index names that are currently defined in the database.
171 * @param string $table
173 * (string $indexName => string $indexName)
175 public function findActualFtsIndexNames($table) {
176 $mysqlVersion = CRM_Core_DAO
::singleValueQuery('SELECT VERSION()');
177 if (version_compare($mysqlVersion, '5.6', '<')) {
178 // If we're not on 5.6+, then there cannot be any InnoDB FTS indices!
179 // Also: information_schema.innodb_sys_indexes is only available on 5.6+.
183 // Note: this only works in MySQL 5.6, but this whole system is intended to only work in MySQL 5.6
185 SELECT i.name as index_name
186 FROM information_schema.innodb_sys_tables t
187 JOIN information_schema.innodb_sys_indexes i USING (table_id)
188 WHERE t.name = concat(database(),'/$table')
189 AND i.name like '" . self
::IDX_PREFIX
. "%'
191 $dao = CRM_Core_DAO
::executeQuery($sql);
192 $indexNames = array();
193 while ($dao->fetch()) {
194 $indexNames[$dao->index_name
] = $dao->index_name
;
200 * Generate a "CREATE INDEX" statement for each desired
206 * (string $indexName => string $sql)
208 public function buildIndexSql($table) {
209 $sqls = array(); // array (string $idxName => string $sql)
210 if ($this->isActive
&& isset($this->indices
[$table])) {
211 foreach ($this->indices
[$table] as $fields) {
212 $name = self
::IDX_PREFIX
. md5($table . '::' . implode(',', $fields));
213 $sqls[$name] = sprintf("CREATE FULLTEXT INDEX %s ON %s (%s)", $name, $table, implode(',', $fields));
220 * Generate a "DROP INDEX" statement for each existing FTS index.
222 * @param string $table
225 * (string $idxName => string $sql)
227 public function dropIndexSql($table) {
229 $names = $this->findActualFtsIndexNames($table);
230 foreach ($names as $name) {
231 $sqls[$name] = sprintf("DROP INDEX %s ON %s", $name, $table);
237 * Construct a set of SQL statements which will create (or preserve)
238 * required indices and destroy unneeded indices.
240 * @param string $table
244 public function reconcileIndexSqls($table) {
245 $buildIndexSqls = $this->buildIndexSql($table);
246 $dropIndexSqls = $this->dropIndexSql($table);
248 $allIndexNames = array_unique(array_merge(
249 array_keys($dropIndexSqls),
250 array_keys($buildIndexSqls)
254 foreach ($allIndexNames as $indexName) {
255 if (isset($buildIndexSqls[$indexName]) && isset($dropIndexSqls[$indexName])) {
258 elseif (isset($buildIndexSqls[$indexName])) {
259 $todoSqls[] = $buildIndexSqls[$indexName];
262 $todoSqls[] = $dropIndexSqls[$indexName];
269 * Put the indices into a normalized format.
274 public function normalizeIndices($indices) {
276 foreach ($indices as $table => $indicesByTable) {
277 foreach ($indicesByTable as $k => $fields) {
279 $result[$table][] = $fields;
286 * Setter for isActive.
288 * @param bool $isActive
290 public function setActive($isActive) {
291 $this->isActive
= $isActive;
295 * Getter for isActive.
299 public function getActive() {
300 return $this->isActive
;