You are here

public function SearchApiAbstractDataSourceController::trackItemInsert in Search API 7

Starts tracking the index status for the given items on the given indexes.

Parameters

array $item_ids: The IDs of new items to track.

SearchApiIndex[] $indexes: The indexes for which items should be tracked.

Return value

SearchApiIndex[]|null All indexes for which any items were added; or NULL if items were added for all of them.

Throws

SearchApiDataSourceException If any error state was encountered.

Overrides SearchApiDataSourceControllerInterface::trackItemInsert

3 calls to SearchApiAbstractDataSourceController::trackItemInsert()
SearchApiAbstractDataSourceController::startTracking in includes/datasource.inc
Initializes tracking of the index status of items for the given indexes.
SearchApiCombinedEntityDataSourceController::trackItemInsert in includes/datasource_multiple.inc
Starts tracking the index status for the given items on the given indexes.
SearchApiEntityDataSourceController::trackItemInsert in includes/datasource_entity.inc
Starts tracking the index status for the given items on the given indexes.
3 methods override SearchApiAbstractDataSourceController::trackItemInsert()
SearchApiCombinedEntityDataSourceController::trackItemInsert in includes/datasource_multiple.inc
Starts tracking the index status for the given items on the given indexes.
SearchApiEntityDataSourceController::trackItemInsert in includes/datasource_entity.inc
Starts tracking the index status for the given items on the given indexes.
SearchApiExternalDataSourceController::trackItemInsert in includes/datasource_external.inc
Start tracking the index status for the given items on the given indexes.

File

includes/datasource.inc, line 594
Contains the SearchApiDataSourceControllerInterface as well as a default base class.

Class

SearchApiAbstractDataSourceController
Provides a default base class for datasource controllers.

Code

public function trackItemInsert(array $item_ids, array $indexes) {
  if (!$this->table || $item_ids === array()) {
    return;
  }
  foreach ($indexes as $index) {
    $this
      ->checkIndex($index);
  }

  // Since large amounts of items can overstrain the database, only add items
  // in chunks.
  foreach (array_chunk($item_ids, 1000) as $chunk) {
    $insert = db_insert($this->table)
      ->fields(array(
      $this->itemIdColumn,
      $this->indexIdColumn,
      $this->changedColumn,
    ));
    foreach ($indexes as $index) {

      // We have to make sure we don't try to insert duplicate items.
      $select = db_select($this->table, 't');
      $select
        ->addField('t', $this->itemIdColumn);
      $select
        ->condition($this->indexIdColumn, $index->id);
      $select
        ->condition($this->itemIdColumn, $chunk, 'IN');
      $existing = $select
        ->execute()
        ->fetchCol();
      $existing = array_flip($existing);
      foreach ($chunk as $item_id) {
        if (isset($existing[$item_id])) {
          continue;
        }
        $insert
          ->values(array(
          $this->itemIdColumn => $item_id,
          $this->indexIdColumn => $index->id,
          $this->changedColumn => 1,
        ));
      }
    }
    try {
      $insert
        ->execute();
    } catch (Exception $e) {
      $tmp = array_slice($item_ids, 0, 10);
      $item_ids_string = '"' . implode('", "', $tmp) . '"';
      $index_names = array();
      foreach ($indexes as $index) {
        $index_names[] = $index->name;
      }
      $vars = array(
        '%indexes' => implode(', ', $index_names),
        '@item_ids' => $item_ids_string,
      );
      watchdog_exception('search_api', $e, '%type while tracking item inserts (IDs: @item_ids) on index(es) %indexes: !message in %function (line %line of %file).', $vars);
    }
  }
}