You are here

public function SearchApiDenormalizedEntityDataSourceController::createPermutationItems in Search API Grouping 7.2

Generates the permutation items for the given index.

This part split's the given entity_ids into the related denormalized ids and adds them to the tracking table. It also marks deprecated denormalized ids as obsolete in the tracking table. These entries will be removed from index during the next indexing.

Parameters

SearchApiIndex $index: The index to deal with.

string $entity_type: The entity type to handle.

array $entities: Array with the entities to process. Keyed by the entity id.

1 call to SearchApiDenormalizedEntityDataSourceController::createPermutationItems()
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 327
Contains the SearchApiDenormalizedEntityDataSourceController class.

Class

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

Code

public function createPermutationItems(SearchApiIndex $index, $entity_type, array $entities) {
  $denormalization_fields = DenormalizedEntityIndexHijack::getDenormalizeProcessorFields($index);
  $denomalized_item_ids = array();
  $item_ids_entity_id_mapping = array();
  foreach ($entities as $entity_id => $entity) {
    $ids = search_api_grouping_generate_pseudo_keys($entity, $entity_type, $denormalization_fields);

    // Avoid errors if there aren't any id's ready yet.
    if (!empty($ids)) {
      $item_ids_entity_id_mapping += array_combine($ids, array_fill(0, count($ids), $entity_id));
      $denomalized_item_ids = array_merge($denomalized_item_ids, $ids);
    }
  }
  if (!empty($denomalized_item_ids)) {

    // Mark obsolete permutations.
    $obsolete = db_update($this->table)
      ->fields(array(
      $this->changedColumn => -2,
    ))
      ->condition($this->indexIdColumn, $index->id)
      ->condition($this->itemIdColumn, $denomalized_item_ids, 'NOT IN')
      ->condition('entity_type', $entity_type)
      ->condition('etid', array_keys($entities), 'IN')
      ->execute();

    // Ensure new permutations are inserted.
    $existing_keys = db_select($this->table)
      ->fields($this->table, array(
      $this->itemIdColumn,
    ))
      ->condition($this->indexIdColumn, $index->id)
      ->condition('entity_type', $entity_type)
      ->condition($this->itemIdColumn, $denomalized_item_ids, 'IN')
      ->execute()
      ->fetchAll(PDO::FETCH_COLUMN, 0);
    $missing_keys = $denomalized_item_ids;
    if ($existing_keys) {
      $missing_keys = array_diff($denomalized_item_ids, $existing_keys);
    }
    if (!empty($missing_keys)) {
      $insert = db_insert($this->table)
        ->fields(array(
        $this->indexIdColumn,
        $this->itemIdColumn,
        'entity_type',
        'etid',
        $this->changedColumn,
      ));
      $num_records = 0;
      foreach ($missing_keys as $missing_key) {
        $insert
          ->values(array(
          $index->id,
          $missing_key,
          $entity_type,
          $item_ids_entity_id_mapping[$missing_key],
          REQUEST_TIME,
        ));

        // 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();
    }
  }
  if (!empty($entities)) {

    // Mark all processed items as processed.
    db_update('search_api_denormalized_entity')
      ->fields(array(
      'queued' => 0,
      'needs_processing' => 0,
    ))
      ->condition($this->indexIdColumn, $index->id)
      ->condition('etid', array_keys($entities))
      ->condition('entity_type', $entity_type)
      ->condition(db_or()
      ->condition('queued', 0, '>')
      ->condition('needs_processing', 0, '>'))
      ->execute();
  }
}