Sfoglia il codice sorgente

Fix to speed URL setting

spficklin 11 anni fa
parent
commit
4f7df45342

+ 51 - 10
tripal_feature/includes/tripal_feature.sync_features.inc

@@ -118,22 +118,63 @@ function tripal_feature_sync_form_submit($form, &$form_state) {
     'tripal_feature_sync_features', $job_args, $user->uid);
 }
 /**
- *
+ *  
+ * @param $na 
+ *   Tripal expects all jobs to have at least one argument. For this function
+ *   we don't need any, so we have this dummy argument as a filler
+ * @param $job_id
  */
-function tripal_feature_set_urls($job_id = NULL) {
-  // first get the list of features that have been synced
+function tripal_feature_set_urls($na = NULL, $job_id = NULL) {
+  
+  // get the number of records we need to set URLs for
+  $csql = "SELECT count(*) FROM {chado_feature}";
+  $num_nodes = db_result(db_query($csql));
+  
+  print "$job_id\n";
+  
+  // calculate the interval at which we will print an update on the screen
+  $num_set = 0;
+  $num_per_interval = 10000;                      
+  
+  // prepate the statements which will quickly add url alias
+  $psql = "
+    PREPARE del_url_alias_by_src (text) AS
+    DELETE FROM {url_alias} WHERE src = \$1
+  ";
+  db_query($psql);
+  $psql = "
+    PREPARE ins_url_alias_nisrds (text, text) AS
+    INSERT INTO url_alias (src, dst) VALUES (\$1, \$2)
+  ";
+  db_query($psql);
+  
+  
+  // get the list of features that have been synced
   $sql = "SELECT * FROM {chado_feature}";
-  $nodes = db_query($sql);
+  $nodes = db_query($sql);  
   while ($node = db_fetch_object($nodes)) {
    
     // remove any previous alias
-    db_query("DELETE FROM {url_alias} WHERE src = '%s'", "node/$node->nid");
-    
-    // add the new alias
-    $url_alias = tripal_feature_get_feature_url($node);
-    print "Setting URL alias for feature $feature->name: node/$node->nid => $url_alias\n";
-    path_set_alias("node/$node->nid", $url_alias);
+    $src = "node/$node->nid";
+    $dst = tripal_feature_get_feature_url($node);
+    $success = db_query("EXECUTE del_url_alias_by_src('%s')", $src);    
+    $success = db_query("EXECUTE ins_url_alias_nisrds('%s', '%s')", $src, $dst);
+
+    // update the job status every 1% features
+    if ($job and $num_nodes % $num_per_interval == 0) {
+      $percent = ($num_set / $num_nodes) * 100;
+      tripal_job_set_progress($job, intval($percent));
+      $percent = sprintf("%.2f", $percent);
+      print "Setting URLs (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes.\r";
+      
+    }
+    $num_set++;
   }
+  $percent = ($num_set / $num_nodes) * 100;
+  tripal_job_set_progress($job, intval($percent));
+  $percent = sprintf("%.2f", $percent);
+  print "Setting URLs (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes.\r";
+  print "\nDone. Set " . number_format($num_set) . " URLs\n";
 }
 /**
  *

+ 45 - 9
tripal_stock/includes/tripal_stock.sync_stocks.inc

@@ -119,19 +119,55 @@ function tripal_stock_sync_form_submit($form, &$form_state) {
  */
 function tripal_stock_set_urls($job_id = NULL) {
   
-  // first get the list of stocks that have been synced
+  // get the number of records we need to set URLs for
+  $csql = "SELECT count(*) FROM {chado_stock}";
+  $num_nodes = db_result(db_query($csql));
+  
+  print "$job_id\n";
+  
+  // calculate the interval at which we will print an update on the screen
+  $num_set = 0;
+  $num_per_interval = 10000;                      
+  
+  // prepate the statements which will quickly add url alias
+  $psql = "
+    PREPARE del_url_alias_by_src (text) AS
+    DELETE FROM {url_alias} WHERE src = \$1
+  ";
+  db_query($psql);
+  $psql = "
+    PREPARE ins_url_alias_nisrds (text, text) AS
+    INSERT INTO url_alias (src, dst) VALUES (\$1, \$2)
+  ";
+  db_query($psql);
+  
+  
+  // get the list of stocks that have been synced
   $sql = "SELECT * FROM {chado_stock}";
-  $nodes = db_query($sql);
+  $nodes = db_query($sql);  
   while ($node = db_fetch_object($nodes)) {
-    
+   
     // remove any previous alias
-    db_query("DELETE FROM {url_alias} WHERE src = '%s'", "node/$node->nid");
-    
-    // add the new alias
-    $url_alias = tripal_stock_get_stock_url($node);
-    print "Setting URL alias for stock $stock->name: node/$node->nid => $url_alias\n";
-    path_set_alias("node/$node->nid", $url_alias);
+    $src = "node/$node->nid";
+    $dst = tripal_stock_get_stock_url($node);
+    $success = db_query("EXECUTE del_url_alias_by_src('%s')", $src);    
+    $success = db_query("EXECUTE ins_url_alias_nisrds('%s', '%s')", $src, $dst);
+
+    // update the job status every 1% stocks
+    if ($job and $num_nodes % $num_per_interval == 0) {
+      $percent = ($num_set / $num_nodes) * 100;
+      tripal_job_set_progress($job, intval($percent));
+      $percent = sprintf("%.2f", $percent);
+      print "Setting URLs (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes.\r";
+      
+    }
+    $num_set++;
   }
+  $percent = ($num_set / $num_nodes) * 100;
+  tripal_job_set_progress($job, intval($percent));
+  $percent = sprintf("%.2f", $percent);
+  print "Setting URLs (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes.\r";
+  print "\nDone. Set " . number_format($num_set) . " URLs\n";
 }
 /**
  *