3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * The InnoDB indexer is responsible for creating and destroying
14 * full-text indices on InnoDB classes.
16 class CRM_Core_InnoDBIndexer
{
17 const IDX_PREFIX
= 'civicrm_fts_';
20 * @var CRM_Core_InnoDBIndexer
22 private static $singleton = NULL;
26 * @return CRM_Core_InnoDBIndexer
28 public static function singleton($fresh = FALSE) {
29 if ($fresh || self
::$singleton === NULL) {
31 'civicrm_address' => [
32 ['street_address', 'city', 'postal_code'],
34 'civicrm_activity' => [
35 ['subject', 'details'],
37 'civicrm_contact' => [
38 ['sort_name', 'nick_name', 'display_name'],
40 'civicrm_contribution' => [
41 ['source', 'amount_level', 'trxn_Id', 'invoice_id'],
46 'civicrm_membership' => [
52 'civicrm_participant' => [
53 ['source', 'fee_level'],
62 $active = Civi
::settings()->get('enable_innodb_fts');
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
75 public static function onToggleFts($oldValue, $newValue): void
{
76 if (empty($oldValue) && empty($newValue)) {
80 $indexer = CRM_Core_InnoDBIndexer
::singleton();
81 $indexer->setActive($newValue);
82 $indexer->fixSchemaDifferences();
88 * (string $table => array $indices)
90 * ex: $indices['civicrm_contact'][0] = array('first_name', 'last_name');
104 * @param bool $isActive
105 * @param array $indices
107 public function __construct($isActive, $indices) {
108 $this->isActive
= $isActive;
109 $this->indices
= $this->normalizeIndices($indices);
113 * Fix schema differences.
115 * Limitation: This won't pick up stale indices on tables which are not
116 * declared in $this->indices. That's not much of an issue for now b/c
117 * we have a static list of tables.
119 public function fixSchemaDifferences() {
120 foreach ($this->indices
as $tableName => $ign) {
121 $todoSqls = $this->reconcileIndexSqls($tableName);
122 foreach ($todoSqls as $todoSql) {
123 CRM_Core_DAO
::executeQuery($todoSql);
129 * Determine if an index is expected to exist.
131 * @param string $table
132 * @param array $fields
133 * List of field names that must be in the index.
136 public function hasDeclaredIndex($table, $fields) {
137 if (!$this->isActive
) {
141 if (isset($this->indices
[$table])) {
142 foreach ($this->indices
[$table] as $idxFields) {
143 // TODO determine if $idxFields must be exact match or merely a subset
144 // if (sort($fields) == sort($idxFields)) {
145 if (array_diff($fields, $idxFields) == []) {
155 * Get a list of FTS index names that are currently defined in the database.
157 * @param string $table
160 * (string $indexName => string $indexName)
162 public function findActualFtsIndexNames(string $table): array {
163 $dao = CRM_Core_DAO
::executeQuery("
164 SELECT index_name as index_name
165 FROM information_Schema.STATISTICS
166 WHERE table_schema = '" . CRM_Core_DAO
::getDatabaseName() . "'
167 AND table_name = '$table'
168 AND index_type = 'FULLTEXT'
173 while ($dao->fetch()) {
174 $indexNames[$dao->index_name
] = $dao->index_name
;
180 * Generate a "CREATE INDEX" statement for each desired
186 * (string $indexName => string $sql)
188 public function buildIndexSql($table): array {
189 // array (string $idxName => string $sql)
191 if ($this->isActive
&& isset($this->indices
[$table])) {
192 foreach ($this->indices
[$table] as $fields) {
193 $name = self
::IDX_PREFIX
. md5($table . '::' . implode(',', $fields));
194 $sqls[$name] = sprintf('CREATE FULLTEXT INDEX %s ON %s (%s)', $name, $table, implode(',', $fields));
201 * Generate a "DROP INDEX" statement for each existing FTS index.
203 * @param string $table
206 * (string $idxName => string $sql)
208 public function dropIndexSql($table) {
210 $names = $this->findActualFtsIndexNames($table);
211 foreach ($names as $name) {
212 $sqls[$name] = sprintf("DROP INDEX %s ON %s", $name, $table);
218 * Construct a set of SQL statements which will create (or preserve)
219 * required indices and destroy unneeded indices.
221 * @param string $table
225 public function reconcileIndexSqls($table) {
226 $buildIndexSqls = $this->buildIndexSql($table);
227 $dropIndexSqls = $this->dropIndexSql($table);
229 $allIndexNames = array_unique(array_merge(
230 array_keys($dropIndexSqls),
231 array_keys($buildIndexSqls)
235 foreach ($allIndexNames as $indexName) {
236 if (isset($buildIndexSqls[$indexName]) && isset($dropIndexSqls[$indexName])) {
239 elseif (isset($buildIndexSqls[$indexName])) {
240 $todoSqls[] = $buildIndexSqls[$indexName];
243 $todoSqls[] = $dropIndexSqls[$indexName];
250 * Put the indices into a normalized format.
255 public function normalizeIndices($indices) {
257 foreach ($indices as $table => $indicesByTable) {
258 foreach ($indicesByTable as $k => $fields) {
260 $result[$table][] = $fields;
267 * Setter for isActive.
269 * @param bool $isActive
271 public function setActive($isActive) {
272 $this->isActive
= $isActive;
276 * Getter for isActive.
280 public function getActive() {
281 return $this->isActive
;