You are here

function rec_example_load_file_high_performance in Recommender API 7.3

Same name and namespace in other branches
  1. 7.5 rec_example/rec_example.module \rec_example_load_file_high_performance()

File

rec_example/rec_example.module, line 133

Code

function rec_example_load_file_high_performance($file_name = '/tmp/100k/u1.base') {
  db_delete('recommender_preference_staging')
    ->execute();
  $ratings_str = file_get_contents($file_name, 'r');
  if ($ratings_str) {
    $ratings = explode("\n", $ratings_str);
    $query = db_insert('recommender_preference_staging')
      ->fields(array(
      'source_eid',
      'target_eid',
      'score',
      'updated',
    ));
    foreach ($ratings as $line) {
      $fields = explode("\t", $line);
      assert(count($fields) == 4);
      $query
        ->values(array(
        'source_eid' => $fields[0],
        'target_eid' => $fields[1],
        'score' => $fields[2],
        'updated' => $fields[3],
      ));
    }
    $query
      ->execute();
  }
  else {
    drupal_set_message('Cannot load file: ' . $file_name);
  }
}