commit 54db5da1a1ba67462d6e2c6eff8fd7f60748265d Author: Damien Tournoud Date: Thu Aug 4 12:30:23 2011 +0200 1064884--follow-up.patch diff --git a/includes/datasource.inc b/includes/datasource.inc index 3b8bcf7..f514739 100644 --- a/includes/datasource.inc +++ b/includes/datasource.inc @@ -413,8 +413,8 @@ abstract class SearchApiAbstractDataSourceController implements SearchApiDataSou * If any of the indexes doesn't use the same item type as this controller. */ public function startTracking(array $indexes) { - // Types that set "track index status" to FALSE should override this method - // in their data source controller and provide their own logic, if possible. + // If no table is set, the data source controller should implement another + // way of tracking item status, if possible. if (!$this->table) { return; } diff --git a/includes/index_entity.inc b/includes/index_entity.inc index ae37424..c975a03 100644 --- a/includes/index_entity.inc +++ b/includes/index_entity.inc @@ -160,11 +160,12 @@ class SearchApiIndex extends Entity { } /** - * Execute necessary tasks for a newly created index (either created in the - * database, or for the first time loaded from code). + * Execute necessary tasks for a newly created index. */ public function postCreate() { - $this->queueItems(); + if ($this->enabled) { + $this->queueItems(); + } $server = $this->server(); if ($server) { // Tell the server about the new index. diff --git a/search_api.install b/search_api.install index d849b51..d65228b 100644 --- a/search_api.install +++ b/search_api.install @@ -425,64 +425,19 @@ function search_api_install() { /** * Implements hook_enable(). + * + * Mark all items as "dirty", since we can't know whether they are. */ function search_api_enable() { - // Mark all items as "dirty", since we can't know whether they are. - db_delete('search_api_item') - ->execute(); $types = array(); foreach (search_api_index_load_multiple(FALSE) as $index) { - $types[$index->entity_type][] = $index->id; - } - if (!$types) { - return; - } - $insert = db_insert('search_api_item') - ->fields(array('item_id', 'index_id', 'changed')); - foreach ($types as $type => $indexes) { - // Partly copied from SearchApiIndex::queueItems(). - - $entity_info = entity_get_info($type); - - // If the entity info specifies a base table, use that to add all items - // directly in an INSERT ... SELECT. Otherwise, use entity_load() and add - // the values to the outer $insert query. - if (!empty($entity_info['base table'])) { - // Use a subselect, which will probably be much faster than entity_load(). - - // Assumes that all entities use the "base table" property and the - // "entity keys[id]" in the same way as the default controller. - $id_field = $entity_info['entity keys']['id']; - $table = $entity_info['base table']; - - // Select all entity ids and all relevant indexes. - $query = db_select($table, 't'); - $query->join('search_api_index', 'i', 'i.entity_type = :type AND i.read_only = :read_only AND i.enabled = :enabled', - array(':type' => $type, ':read_only' => 0, ':enabled' => 1)); - $query->addField('t', $id_field, 'item_id'); - $query->addField('i', 'id', 'index_id'); - $query->addExpression('1', 'changed'); - - // INSERT ... SELECT ... - db_insert('search_api_item') - ->from($query) - ->execute(); - } - else { - foreach (entity_load($type) as $id => $entity) { - foreach ($indexes as $index_id) { - $insert->values(array( - 'item_id' => $id, - 'index_id' => $index_id, - 'changed' => 1, - )); - } - } + if ($index->enabled) { + $types[$index->item_type][] = $index; } } - // Only execute this query if we added any values to it. - if (isset($index_id)) { - $insert->execute(); + foreach ($types as $type => $indexes) { + $controller = search_api_get_datasource_controller($type); + $controller->startTracking($indexes); } } @@ -490,8 +445,14 @@ function search_api_enable() { * Implements hook_disable(). */ function search_api_disable() { - db_delete('search_api_item') - ->execute(); + $types = array(); + foreach (search_api_index_load_multiple(FALSE) as $index) { + $types[$index->item_type][] = $index; + } + foreach ($types as $type => $indexes) { + $controller = search_api_get_datasource_controller($type); + $controller->stopTracking($indexes); + } } /** diff --git a/search_api.module b/search_api.module index 4ff3f84..4c1d1f8 100644 --- a/search_api.module +++ b/search_api.module @@ -505,7 +505,8 @@ function search_api_search_api_server_delete(SearchApiServer $server) { /** * Implements hook_search_api_index_insert(). * - * Populates {search_api_item} for new indexes. + * Adds the index to its server (if any) and starts tracking indexed items (if + * the index is enabled). */ function search_api_search_api_index_insert(SearchApiIndex $index) { $index->postCreate(); @@ -604,7 +605,7 @@ function search_api_search_api_index_delete(SearchApiIndex $index) { */ function search_api_entity_insert($entity, $type) { // When inserting a new search index, the new index was already inserted into - // search_api_item. This would lead to a duplicate-key issue, if we would + // the tracking table. This would lead to a duplicate-key issue, if we would // continue. if ($type == 'search_api_index') { return;