Commit 57bdde7a authored by Allan Chappell's avatar Allan Chappell
Browse files

Fixing fatals and timeouts on loads of content.

parent 9e3e0f49
Loading
Loading
Loading
Loading
+22 −12
Original line number Diff line number Diff line
@@ -124,13 +124,17 @@ function views_natural_sort_rebuild_index_batch_set(array $entry_types = array()
    $entry_types = module_invoke_all('views_natural_sort_get_entry_types');
  }

  $operations = array();
  foreach ($entry_types as $entry_type) {
    // Queue up all the data that needs to be rebuilt.
    module_invoke_all('views_natural_sort_queue_rebuild_data', $entry_type);
    // Build the batch Operation list.
    $operations[] = array(
      'views_natural_sort_rebuild_index',
      array($entry_type),
    );
  }
  // Run the queue.
  $batch = array(
    'operations' => array(array('views_natural_sort_rebuild_index', array())),
    'operations' => $operations,
    'title' => t('Rebuilding Views Natural Sort Indexing Entries'),
    'finished' => 'views_natural_sort_rebuild_index_finished',
    'file' => drupal_get_path('module', 'views_natural_sort') . '/views_natural_sort.admin.inc',
@@ -142,23 +146,29 @@ function views_natural_sort_rebuild_index_batch_set(array $entry_types = array()
/**
 * Batch API callback for rebuild_index.
 */
function views_natural_sort_rebuild_index(&$context) {
  $queue = views_natural_sort_get_queue();
function views_natural_sort_rebuild_index($entry_type, &$context) {
  // Alias sandbox for easier referencing.
  $sandbox = &$context['sandbox'];
  // Alias results for easier referencing.
  $results = &$context['results'];
  if (empty($sandbox)) {
    $sandbox['current'] = 0;
    $sandbox['max'] = $queue->numberOfItems();
    // Getting just the count is ok, because theoretically, any items added in
    // the process of things getting reindexed would already have entries.
    $sandbox['max'] = module_invoke($entry_type['module'], 'views_natural_sort_queue_rebuild_data_count', $entry_type);
    $sandbox['items_per_batch'] = variable_get('views_natural_sort_rebuild_items_per_batch', '500');
  }
  for ($i = 0; $i < $sandbox['items_per_batch'] && $sandbox['current'] < $sandbox['max']; $i++) {
    $item = $queue->claimItem(10);
    if ($item) {
      views_natural_sort_process_index_queue($item->data);
      $queue->deleteItem($item);
    }
  $items = module_invoke($entry_type['module'], 'views_natural_sort_queue_rebuild_data', $entry_type, $sandbox['current'], $sandbox['items_per_batch']);
  for ($i = 0; $i < count($items) && $sandbox['current'] < $sandbox['max']; $i++) {
    views_natural_sort_store($items[$i]);
    $context['message'] = t(
      'Now processing @count of @max @entry_type items.',
      array(
        '@max' => $sandbox['max'],
        '@count' => $sandbox['current'],
        '@entry_type' => $entry_type['entity_type'] . ':' . $entry_type['field']
      )
    );
    $sandbox['current']++;
  }
  $results['entries'] = $sandbox['current'];
+45 −10
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@
 *       array(
 *         'entity_type' - string Ex. node
 *         'field ' - string Field name. Lines up with property or field.
 *         'module' - string naming the module that manages indexing.
 *       ),
 *     )
 */
@@ -22,6 +23,7 @@ function hook_views_natural_sort_get_entry_types() {
    array(
      'entity_type' => 'user',
      'field' => 'book_favorites',
      'module' => 'book_favorites_module',
    ),
  );
}
@@ -35,6 +37,7 @@ function hook_views_natural_sort_get_entry_types() {
 *       array(
 *         'entity_type' - string Ex. node
 *         'field ' - string Field name. Lines up with property or field.
 *         'module' - string naming the module that manages indexing.
 *       ),
 *     )
 */
@@ -54,17 +57,25 @@ function hook_views_natural_sort_get_entry_types_alter(array &$entry_types) {
 *
 * @param array $entry_type
 *   Array representing an entry type with an entity_type field pair.
 *     $entity_type - The type of the entity we are getting
 *     'entry_type' - The type of the entity we are getting
 *                    data that needs to be re-indexed from
 *     $field - The field that needs to be re-indexed.
 *     'field' - The field that needs to be re-indexed.
 *     'module' - string naming the module that manages indexing.
 * @param int $offset
 *   Integer representing the start of the subset we want to grab.
 * @param int $limit
 *   Integer representing the number of items in the subset we want to grab.
 */
function hook_views_natural_sort_queue_rebuild_data(array $entry_type) {
function hook_views_natural_sort_queue_rebuild_data(array $entry_type, $offset = 0, $limit = NULL) {
  if ($entry_type['entity_type'] != 'user' || $entry_type['field'] != 'book_favorites') {
    return array();
  }
  $result = db_select('user', 'u')
  $query = db_select('user', 'u')
    ->fields('u', array('uid', 'book_favorites'))
    ->execute();
  if ($limit) {
    $query->range($offset, $limit);
  }
  $result = $query->execute();
  $queue = views_natural_sort_get_queue();
  foreach ($result as $row) {
    // Grab the data returned and queue it up for transformation.
@@ -78,6 +89,30 @@ function hook_views_natural_sort_queue_rebuild_data(array $entry_type) {
  }
}

/**
 * Used for a custom module to return a count for the data being re-indexed.
 *
 * @param array $entry_type
 *   Array representing an entry type with an entity_type field pair.
 *     'entry_type' - The type of the entity we are getting
 *                    data that needs to be re-indexed from
 *     'field' - The field that needs to be re-indexed.
 *     'module' - string naming the module that manages indexing.
 *
 * @return int
 *   Integer representing the total number of items we are re-indexing.
 */
function hook_views_natural_sort_queue_rebuild_data_count(array $entry_type) {
  if ($entry_type['entity_type'] != 'user' || $entry_type['field'] != 'book_favorites') {
    return 0;
  }
  $result = db_select('user', 'u')
    ->fields('u', array('uid', 'book_favorites'))
    ->execute()
    ->rowCount();
  return $result;
}

/**
 * Used to define custom transformations or reorder transformations.
 *
@@ -86,11 +121,11 @@ function hook_views_natural_sort_queue_rebuild_data(array $entry_type) {
 * @param array $index_entry
 *   A representation of the original entry that is would have been put in the
 *   database before the transformation
 *     $eid - Entity Id of the item referenced
 *     $entity_type - The Entity Type. Ex. node
 *     $field - reference to the property or field name
 *     $delta - the item number in that field or property
 *     $content - The original string before
 *     'eid' - Entity Id of the item referenced
 *     'entity_type' - The Entity Type. Ex. node
 *     'field' - reference to the property or field name
 *     'delta' - the item number in that field or property
 *     'content' - The original string before
 *                transformations.
 */
function hook_views_natural_sort_transformations_alter(array &$transformations, array $index_entry) {
+9 −1
Original line number Diff line number Diff line
@@ -175,5 +175,13 @@ function views_natural_sort_update_7201() {
    }
  }
  cache_clear_all();
  return t('If you implement your views using Features, be sure to update the Featues that contain View Natural Sort Views immediatly after this upgrade and before the next revert.');
  return t('If you implement your views using Features, be sure to update the Features that contain View Natural Sort Views immediately after this upgrade and before the next revert.');
}

/**
 * Removing the queue because queuing didn't work out.
 */

function views_natural_sort_update_7202() {
  DrupalQueue::get('views_natural_sort_index_queue')->deleteQueue();
}
+23 −35
Original line number Diff line number Diff line
@@ -99,6 +99,7 @@ function views_natural_sort_views_natural_sort_get_entry_types() {
      $entry_types[] = array(
        'entity_type' => $entity_type,
        'field' => $property,
        'module' => 'views_natural_sort',
      );
    }
  }
@@ -108,14 +109,16 @@ function views_natural_sort_views_natural_sort_get_entry_types() {
/**
 * Implements hook_views_natural_sort_queue_rebuild_data().
 */
function views_natural_sort_views_natural_sort_queue_rebuild_data($entry_type) {
function views_natural_sort_views_natural_sort_queue_rebuild_data($entry_type, $offset = 0, $limit = NULL) {
  $supported_entity_types = views_natural_sort_get_entry_types();
  if (empty($supported_entity_properties[$entry_type['entity_type'] . '|' . $entry_type['field']])) {
    return array();
  }
  $queue = views_natural_sort_get_queue();

  $query = new EntityFieldQuery();
  if ($limit) {
    $query->range($offset, $limit);
  }
  $result = $query->entityCondition('entity_type', $entry_type['entity_type'])
    ->execute();
  $entity_ids = array();
@@ -124,18 +127,33 @@ function views_natural_sort_views_natural_sort_queue_rebuild_data($entry_type) {
    $entity_ids = array_keys($result[$entry_type['entity_type']]);
  }

  $data = array();
  foreach ($entity_ids as $entity_id) {
    $results = entity_load($entry_type['entity_type'], array($entity_id));
    $entity = reset($results);
    $field_name = $entry_type['field'];
    $queue->createItem(array(
    $data[] = array(
      'eid' => $entity_id,
      'entity_type' => $entry_type['entity_type'],
      'field' => $field_name,
      'delta' => 0,
      'content' => $entity->$field_name,
    ));
    );
  }
}

/**
 * Implements hook_views_natural_sort_queue_rebuild_data_count().
 */
function views_natural_sort_views_natural_sort_queue_rebuild_data_count($entry_type) {
  $supported_entity_types = views_natural_sort_get_entry_types();
  if (empty($supported_entity_properties[$entry_type['entity_type'] . '|' . $entry_type['field']])) {
    return 0;
  }
  $result = $query->entityCondition('entity_type', $entry_type['entity_type'])
    ->count()
    ->execute();
  return $result;
}

/**
@@ -146,7 +164,7 @@ function views_natural_sort_views_natural_sort_queue_rebuild_data($entry_type) {
function views_natural_sort_entity_insert($entity, $type) {
  $supported_entity_types = views_natural_sort_get_entry_types();
  foreach ($supported_entity_types as $key => $entry_type) {
    if ($entry_type['entity_type'] == $type && isset($entity->{$entry_type['field']})) {
    if ($entry_type['entity_type'] == $type && $entry_type['module'] == 'views_natural_sort' && isset($entity->{$entry_type['field']})) {
      views_natural_sort_store(views_natural_sort_entity_to_vns($entity, $type, $entry_type['field']));
    }
  }
@@ -175,29 +193,6 @@ function views_natural_sort_entity_delete($entity, $type) {
  ));
}

/**
 * Implements hook_cron_queue_info().
 */
function views_natural_sort_cron_queue_info() {
  return array(
    'views_natural_sort_index_queue' => array(
      'worker callback' => 'views_natural_sort_process_index_queue',
      'skip on cron' => TRUE,
    ),
  );
}

/**
 * Queue worker callback for views_natural_sort_index_queue.
 *
 * @see callback_queue_worker
 */
function views_natural_sort_process_index_queue($data) {
  if (is_array($data)) {
    views_natural_sort_store($data);
  }
}

/**
 * Store Multiple views_natural_sort entries.
 *
@@ -481,10 +476,3 @@ function views_natural_sort_entity_to_vns($entity, $entity_type, $field) {
    'content' => $entity->$field,
  );
}

/**
 * Get the queue used to store natural sort index jobs.
 */
function views_natural_sort_get_queue() {
  return DrupalQueue::get('views_natural_sort_index_queue');
}
+38 −8
Original line number Diff line number Diff line
@@ -86,6 +86,7 @@ function views_natural_sort_text_field_get_entry_types_from_field($field) {
    $entry_types[] = array(
      'entity_type' => $entity_type,
      'field' => $field['field_name'],
      'module' => 'views_natural_sort_text_field',
    );
  }
  return $entry_types;
@@ -94,11 +95,10 @@ function views_natural_sort_text_field_get_entry_types_from_field($field) {
/**
 * Implements hook_views_natural_sort_queue_rebuild_data().
 */
function views_natural_sort_text_field_views_natural_sort_queue_rebuild_data($entry_type) {
function views_natural_sort_text_field_views_natural_sort_queue_rebuild_data($entry_type, $offset = 0, $limit = NULL) {
  if (!in_array($entry_type, views_natural_sort_text_field_views_natural_sort_get_entry_types())) {
    return array();
  }
  $queue = views_natural_sort_get_queue();
  $field = field_info_field($entry_type['field']);
  $data = array();
  $bundles = array();
@@ -109,22 +109,52 @@ function views_natural_sort_text_field_views_natural_sort_queue_rebuild_data($en
    }
  }
  $entity_info = entity_get_info($entry_type['entity_type']);
  $entity_ids = db_select($entity_info['base table'], 'e')
  // Get rows for each field value so we can accurately use count functionality
  // on this query. Not putting a distinct here because it would throw off the
  // calculation. Ordering by entity id because newer ones should have entries
  // built as they are entered.
  $query = db_select($entity_info['base table'], 'e')
    ->fields('e', array($entity_info['entity keys']['id']))
    ->condition('e.' . $entity_info['bundle keys']['bundle'], $bundles, 'IN')
    ->execute()
    ->orderBy('e.' . $entity_info['entity keys']['id']);
  $query->join('field_data_' . $entry_type['field'], 'f', 'f.entity_type = :entity_type AND f.deleted = 0 AND f.entity_id = e.' . $entity_info['entity keys']['id'], array('entity_type' => $entry_type['entity_type']));
  if ($limit) {
    $query->range($offset, $limit);
  }
  $entity_ids = $query->execute()
    ->fetchCol();

  array_unique($entity_ids);
  if (!empty($entity_ids)) {
    foreach ($entity_ids as $entity_id) {
      $results = entity_load($entry_type['entity_type'], array($entity_id), array(), TRUE);
      $entity = reset($results);
      $entries = _views_natural_sort_text_field_to_vns($entry_type['entity_type'], $entity, $field);
      foreach ($entries as $entry) {
        $queue->createItem($entry);
      $data = array_merge($data, $entries);
    }
  }
  return $data;
}

function views_natural_sort_text_field_views_natural_sort_queue_rebuild_data_count($entry_type) {
  if (!in_array($entry_type, views_natural_sort_text_field_views_natural_sort_get_entry_types())) {
    return array();
  }
  $field = field_info_field($entry_type['field']);
  $data = array();
  $bundles = array();

  if (isset($field['bundles'][$entry_type['entity_type']])) {
    foreach ($field['bundles'][$entry_type['entity_type']] as $key => $bundle) {
      $bundles[] = $bundle;
    }
  }
  $entity_info = entity_get_info($entry_type['entity_type']);
  $query = db_select($entity_info['base table'], 'e')
    ->fields('e', array($entity_info['entity keys']['id']))
    ->condition('e.' . $entity_info['bundle keys']['bundle'], $bundles, 'IN');
  $query->join('field_data_' . $entry_type['field'], 'f', 'f.entity_type = :entity_type AND f.deleted = 0 AND f.entity_id = e.' . $entity_info['entity keys']['id'], array('entity_type' => $entry_type['entity_type']));
  return $query->execute()
    ->rowCount();
}

/**