You are here

class FeedImportSQLHashes in Feed Import 7.3

This class implements SQL hash storage

Hierarchy

Expanded class hierarchy of FeedImportSQLHashes

2 string references to 'FeedImportSQLHashes'
FeedImport::getEmptyFeed in feed_import_base/inc/feed_import.inc
Gets a new empty feed configuration.
feed_import_feed_import_hash_manager_info in ./feed_import.module
Implements hook_feed_import_hash_manager_info().

File

feed_import_base/inc/feed_import.inc, line 1761
This file contains Feed Import helpers.

View source
class FeedImportSQLHashes extends FeedImportHashManager {

  // Feed machine name.
  protected $feedName;

  // Entity name.
  protected $entity;

  // Insert format.
  protected $insertFormat;

  // Update format.
  protected $updateFormat;

  // Reuseable db objects.
  protected $select;
  protected $insert;
  protected $update;

  // Options
  protected $updateChunkSize = 500;
  protected $insertChunkSize = 500;
  protected $group = '';

  /**
   * {@inheritdoc}
   */
  public function __construct($entity_name, $feed_machine_name) {
    $this->entity = $entity_name;
    $this->feedName = $feed_machine_name;
  }

  /**
   * {@inheritdoc}
   */
  public function setOptions(array $options, $overwrite = FALSE) {
    if (isset($options['ttl']) && $options['ttl'] >= 0) {
      $this->ttl = (int) $options['ttl'];
    }
    if (isset($options['update_chunk']) && $options['update_chunk'] > 0) {
      $this->updateChunkSize = (int) $options['update_chunk'];
    }
    if (isset($options['insert_chunk']) && $options['insert_chunk'] > 0) {
      $this->insertChunkSize = (int) $options['insert_chunk'];
    }
    if (isset($options['group'])) {
      $this->group = $options['group'];
    }

    // Create insert format.
    $this->insertFormat = array(
      $this->feedName,
      $this->group,
      $this->entity,
      0,
      // holds id
      NULL,
      // holds hash
      0,
    );

    // Create select object.
    $this->select = db_select('feed_import_hashes', 'f')
      ->fields('f', array(
      'hash',
      'id',
      'entity_id',
      'expire',
    ))
      ->condition('feed_group', $this->group)
      ->condition('entity', $this->entity)
      ->condition('hash', array(), 'IN');

    // Create update object.
    $this->update = db_update('feed_import_hashes')
      ->condition('feed_group', $this->group)
      ->condition('entity', $this->entity);

    // Create update format.
    $this->updateFormat = array(
      'expire' => 0,
    );

    // Create insert object.
    $this->insert = db_insert('feed_import_hashes')
      ->fields(array(
      'feed_machine_name',
      'feed_group',
      'entity',
      'entity_id',
      'hash',
      'expire',
    ));
  }

  /**
   * {@inheritdoc}
   */
  public function hash(&$uniq) {
    return $this->generatedHashes[] = md5($uniq);
  }

  /**
   * {@inheritdoc}
   */
  public function get() {
    if (!$this->generatedHashes) {
      return array();
    }

    // Get select conditions (reuse same select object).
    $cond =& $this->select
      ->conditions();

    // Remove last condition.
    array_pop($cond);

    // Return hashes.
    $hashes = $this->select
      ->condition('hash', $this->generatedHashes, 'IN')
      ->execute()
      ->fetchAllAssoc('hash');
    $this->generatedHashes = array();
    return $hashes;
  }

  // Items left to insert.
  protected $toInsert = 0;

  /**
   * {@inheritdoc}
   */
  public function insert($id, $hash) {
    $this->insertFormat[3] = $id;
    $this->insertFormat[4] = $hash;
    $this->insertFormat[5] = $this->ttl ? $this->ttl + time() : 0;
    $this->insert
      ->values($this->insertFormat);

    // Commit insert.
    if (++$this->toInsert == $this->insertChunkSize) {
      $this
        ->insertCommit();
    }
  }

  /**
   * {@inheritdoc}
   */
  public function insertCommit() {
    if ($this->toInsert) {
      $this->insert
        ->execute();
      $this->toInsert = 0;
    }
  }

  // Ids that need to be updated.
  protected $updateIds = array();
  protected $toUpdate = 0;

  /**
   * {@inheritdoc}
   */
  public function update($id) {
    $this->updateIds[] = $id;
    if (++$this->toUpdate == $this->updateChunkSize) {
      $this
        ->_update($this->updateIds, $this->ttl ? $this->ttl + time() : 0);
      $this->toUpdate = 0;
    }
  }

  // Ids that need to be protected.
  protected $protectIds = array();
  protected $toProtect = 0;

  /**
   * {@inheritdoc}
   */
  public function protect($id) {
    $this->protectIds[] = $id;
    if (++$this->toProtect == $this->updateChunkSize) {
      $this
        ->_update($this->protectIds, static::MARK_PROTECTED);
      $this->toProtect = 0;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function updateCommit() {
    if ($this->toUpdate) {
      $this
        ->_update($this->updateIds, $this->ttl ? $this->ttl + time() : 0);
      $this->toUpdate = 0;
    }
    if ($this->toProtect) {
      $this
        ->_update($this->protectIds, static::MARK_PROTECTED);
      $this->toProtect = 0;
    }
  }

  /**
   * Makes an update.
   */
  protected function _update(array &$ids, $expire) {
    $this->updateFormat['expire'] = $expire;
    $this->update
      ->fields($this->updateFormat);

    // Get update conditions.
    $conditions =& $this->update
      ->conditions();
    if (count($ids) <= $this->updateChunkSize) {

      // Update hashes.
      $this->update
        ->condition('id', $ids, 'IN')
        ->execute();

      // Remove last IN condition.
      array_pop($conditions);

      // Clear array.
      $ids = array();
    }
    else {

      // Split ids in chunks.
      $ids = array_chunk($ids, $this->updateChunkSize);
      for ($i = 0, $m = count($ids); $i < $m; $i++) {

        // Update hashes.
        $this->update
          ->condition('id', $ids[$i], 'IN')
          ->execute();
        unset($ids[$i]);

        // Remove last IN condition.
        array_pop($conditions);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function delete(array $ids) {
    db_delete('feed_import_hashes')
      ->condition('id', $ids)
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public static function deleteByFeed($name) {
    db_delete('feed_import_hashes')
      ->condition('feed_machine_name', $name)
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public static function deleteByGroup($group) {
    db_delete('feed_import_hashes')
      ->condition('feed_group', $group)
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public static function deleteEntities($ids, $entity_type) {
    db_delete('feed_import_hashes')
      ->condition('entity', $entity_type)
      ->condition('entity_id', $ids)
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public static function getExpired($name, $max = 0) {
    $q = db_select('feed_import_hashes', 'f')
      ->fields('f', array(
      'entity',
      'id',
      'entity_id',
    ))
      ->condition('feed_machine_name', $name)
      ->condition('expire', array(
      static::MARK_PROTECTED + 1,
      time(),
    ), 'BETWEEN');
    if ($max) {
      $q
        ->range(0, $max);
    }
    $q = $q
      ->execute();
    $ret = array();
    while ($r = $q
      ->fetch(PDO::FETCH_ASSOC)) {
      $ret[$r['entity']][$r['id']] = $r['entity_id'];
    }
    return $ret;
  }

  /**
   * {@inheritdoc}
   */
  public static function rescheduleAll($name, $ttl) {
    if ($ttl) {
      $ttl += time();
    }
    db_update('feed_import_hashes')
      ->condition('feed_machine_name', $name)
      ->condition('expire', static::MARK_PROTECTED, '>')
      ->fields(array(
      'expire' => $ttl,
    ))
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public static function totalHashes($name = NULL) {
    $q = db_select('feed_import_hashes', 'f')
      ->fields('f', array(
      'feed_machine_name',
    ));
    if ($name) {
      $q
        ->condition('feed_machine_name', $name);
    }
    $q
      ->addExpression('COUNT(*)', 'cnt');
    $q = $q
      ->groupBy('feed_machine_name')
      ->execute()
      ->fetchAllKeyed();
    if ($name && is_scalar($name)) {
      return isset($q[$name]) ? $q[$name] : 0;
    }
    return $q;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FeedImportConfigurable::$options protected property
FeedImportConfigurable::cleanLines public static function Helper function to get lines of a string
FeedImportHashManager::$generatedHashes protected property
FeedImportHashManager::$ttl protected property
FeedImportHashManager::MARK_PROTECTED constant
FeedImportSQLHashes::$entity protected property
FeedImportSQLHashes::$feedName protected property
FeedImportSQLHashes::$group protected property
FeedImportSQLHashes::$insert protected property
FeedImportSQLHashes::$insertChunkSize protected property
FeedImportSQLHashes::$insertFormat protected property
FeedImportSQLHashes::$protectIds protected property
FeedImportSQLHashes::$select protected property
FeedImportSQLHashes::$toInsert protected property
FeedImportSQLHashes::$toProtect protected property
FeedImportSQLHashes::$toUpdate protected property
FeedImportSQLHashes::$update protected property
FeedImportSQLHashes::$updateChunkSize protected property
FeedImportSQLHashes::$updateFormat protected property
FeedImportSQLHashes::$updateIds protected property
FeedImportSQLHashes::delete public static function Deletes hashes from database. Overrides FeedImportStaticHashManager::delete
FeedImportSQLHashes::deleteByFeed public static function Deletes all hashes for specified feed name Overrides FeedImportStaticHashManager::deleteByFeed
FeedImportSQLHashes::deleteByGroup public static function Deletes all hashes of specified group Overrides FeedImportStaticHashManager::deleteByGroup
FeedImportSQLHashes::deleteEntities public static function Deletes entities by type and id Overrides FeedImportStaticHashManager::deleteEntities
FeedImportSQLHashes::get public function Returns an array of entity keyed by hash, using generated hashes. Overrides FeedImportHashManager::get
FeedImportSQLHashes::getExpired public static function Returns an array of expired entity ids keyed by entity name Overrides FeedImportStaticHashManager::getExpired
FeedImportSQLHashes::hash public function Creates a hash, based on uniq Also adds it on generated hashes Overrides FeedImportHashManager::hash 1
FeedImportSQLHashes::insert public function Inserts a new hash. Overrides FeedImportHashManager::insert
FeedImportSQLHashes::insertCommit public function Commits the insert to storage. Overrides FeedImportHashManager::insertCommit
FeedImportSQLHashes::protect public function Protects a hash for updates Overrides FeedImportHashManager::protect
FeedImportSQLHashes::rescheduleAll public static function Reschedules all entities Overrides FeedImportStaticHashManager::rescheduleAll
FeedImportSQLHashes::setOptions public function Sets options for this instance Overrides FeedImportConfigurable::setOptions 1
FeedImportSQLHashes::totalHashes public static function Returns total number of hashes for desired feeds. Overrides FeedImportStaticHashManager::totalHashes
FeedImportSQLHashes::update public function Updates hashes. Overrides FeedImportHashManager::update
FeedImportSQLHashes::updateCommit public function Commits the update to storage. Overrides FeedImportHashManager::updateCommit
FeedImportSQLHashes::_update protected function Makes an update.
FeedImportSQLHashes::__construct public function Constructor. Overrides FeedImportHashManager::__construct