You are here

public function SearchApiDenormalizedEntityDataSourceController::startTracking in Search API Grouping 7.2

Initialize tracking of the index status of items for the given indexes.

All currently known items of this data source's type should be inserted into the tracking table for the given indexes, with status "changed". If items were already present, these should also be set to "changed" and not be inserted again.

Parameters

array $indexes: The SearchApiIndex objects for which item tracking should be initialized.

Throws

SearchApiDataSourceException If any of the indexes doesn't use the same item type as this controller.

Overrides SearchApiEntityDataSourceController::startTracking

1 call to SearchApiDenormalizedEntityDataSourceController::startTracking()
SearchApiDenormalizedEntityDataSourceController::trackItemChange in includes/datasource_denormalized_entity.inc
Set the tracking status of the given items to "changed"/"dirty".

File

includes/datasource_denormalized_entity.inc, line 159
Contains the SearchApiDenormalizedEntityDataSourceController class.

Class

SearchApiDenormalizedEntityDataSourceController
Data source for all entities known to the Entity API.

Code

public function startTracking(array $indexes) {
  if (!$this->table) {
    return;
  }

  // We first clear the tracking table for all indexes, so we can just insert
  // all items again without any key conflicts.
  $this
    ->stopTracking($indexes);
  $entity_type = $this
    ->getEntityType();

  // Just insert the entities without the permutations and create the batch
  // to expand every entity.
  $query = new EntityFieldQuery();
  $result = $query
    ->entityCondition('entity_type', $entity_type)
    ->execute();
  if (!empty($result[$entity_type])) {
    $insert = db_insert($this->table)
      ->fields(array(
      'index_id',
      $this->itemIdColumn,
      'entity_type',
      'etid',
      'changed',
      'needs_processing',
    ));
    $num_records = 0;
    foreach ($result[$entity_type] as $entity_id => $data) {
      foreach ($indexes as $index) {
        $denormalization_fields = DenormalizedEntityIndexHijack::getDenormalizeProcessorFields($index);
        $insert
          ->values(array(
          'index_id' => $index->id,
          $this->itemIdColumn => $entity_type . '-' . $entity_id . str_repeat('-0', count($denormalization_fields)),
          'entity_type' => $entity_type,
          'etid' => $entity_id,
          'changed' => REQUEST_TIME,
          'needs_processing' => 1,
        ));

        // Execute in batches to avoid the memory overhead of all of those
        // records in the query object.
        if (++$num_records == 20) {
          $insert
            ->execute();
          $num_records = 0;
        }
      }
    }
    $insert
      ->execute();
  }

  // Create a new queue to generate permutations.
  $queue = DrupalQueue::get('search_api_grouping_generate_permuatations');
  $queue
    ->deleteQueue();
  $queue
    ->createQueue('search_api_grouping_generate_permuatations');

  // Is there a better way then re-queueing all indexes??
  db_update($this->table)
    ->fields(array(
    'queued' => 0,
  ))
    ->condition('queued', 0, '>')
    ->execute();

  // Reuse cron function to re-create the queue.
  search_api_grouping_cron();
}