You are here

function rec_example_load_file_high_performance in Recommender API 7.5

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

File

rec_example/rec_example.module, line 134

Code

function rec_example_load_file_high_performance($file_name = '/tmp/data') {

  // default file should be grouplens/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_id',
      'target_id',
      'score',
      'updated',
    ));
    foreach ($ratings as $line) {
      $fields = explode("\t", $line);
      assert(count($fields) == 4);
      $query
        ->values(array(
        'source_id' => $fields[0],
        'target_id' => $fields[1],
        'score' => $fields[2],
        'updated' => $fields[3],
      ));
    }
    $query
      ->execute();
  }
  else {
    drupal_set_message('Cannot load file: ' . $file_name);
  }
}