You are here

public function SearchApiDenormalizedEntityDataSourceController::queuePermutationGeneration in Search API Grouping 7.2

Queues the generation of the permutations.

File

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

Class

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

Code

public function queuePermutationGeneration() {

  // Remove queued timestamp after 6 hours assuming the update has failed.
  // So the queue item will created again.
  db_update($this->table)
    ->fields(array(
    'queued' => 0,
  ))
    ->condition('queued', REQUEST_TIME - 3600 * 6, '<')
    ->condition('entity_type', $this
    ->getEntityType())
    ->execute();

  // Now fetch all items marked with needs processing grouped by entity.
  $result = db_select($this->table)
    ->fields($this->table, array())
    ->condition('needs_processing', 1)
    ->condition('queued', 0)
    ->condition('entity_type', $this
    ->getEntityType())
    ->groupBy('etid')
    ->execute();
  $queue = DrupalQueue::get('search_api_grouping_generate_permuatations');
  $queue_item = array();
  foreach ($result as $item) {
    $queue_item[$item->etid] = $item;

    // Store a bunch of items per queue item.
    if (count($queue_item) == 15) {
      if ($queue
        ->createItem($queue_item)) {

        // Add timestamp to avoid queueing item more than once.
        db_update($this->table)
          ->fields(array(
          'queued' => REQUEST_TIME,
        ))
          ->condition('etid', array_keys($queue_item))
          ->condition('entity_type', $this
          ->getEntityType())
          ->execute();
        $queue_item = array();
      }
    }
  }

  // Store the last collection.
  if (!empty($queue_item)) {
    if ($queue
      ->createItem($queue_item)) {

      // Add timestamp to avoid queueing item more than once.
      db_update($this->table)
        ->fields(array(
        'queued' => REQUEST_TIME,
      ))
        ->condition('etid', array_keys($queue_item))
        ->condition('entity_type', $this
        ->getEntityType())
        ->execute();
    }
  }
}